From 78dc097316b1dbfd7494abc1099e85406277e357 Mon Sep 17 00:00:00 2001
From: Daniil Medvedev <medvdanil@gmail.com>
Date: Fri, 17 Jul 2015 11:53:19 +0300
Subject: [PATCH] csv added lua tests

---
 test/app/csv.result   | 44 +++++++++++++++++++
 test/app/csv.test.lua | 99 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 143 insertions(+)
 create mode 100644 test/app/csv.result
 create mode 100755 test/app/csv.test.lua

diff --git a/test/app/csv.result b/test/app/csv.result
new file mode 100644
index 0000000000..0075bdb0d0
--- /dev/null
+++ b/test/app/csv.result
@@ -0,0 +1,44 @@
+obj test1:
+|a|	|b|	
+|1|	|ha
+"ha"
+ha|	
+|3|	|4|	
+
+obj test2:
+||	||	||	
+||	||	
+||	
+
+obj test3:
+||	||	
+|kp"v|	
+
+fio test1:
+|123|	|5|	|92|	|0|	|0|	
+|1|	|12  34|	|56|	|quote , |	|66|	
+|ok|	
+
+fio test2:
+fio test3:
+|1|	
+|23|	|456|	|abcac|	|'multiword field 4'|	
+|none|	|none|	|0|	
+||	||	||	
+|aba|	|adda|	|f3|	|0|	
+|local res = internal.pwrite(self.fh|	|data|	|len|	|offset)|	
+|iflag = bit.bor(iflag|	|fio.c.flag[ flag ])|	
+||	||	||	
+
+fio test4:
+|1|	
+|23|	|456|	|abcac|	|'multiword field 4'|	
+|none|	|none|	|0|	
+||	||	||	
+|aba|	|adda|	|f3|	|0|	
+|local res = internal.pwrite(self.fh|	|data|	|len|	|offset)|	
+|iflag = bit.bor(iflag|	|fio.c.flag[ flag ])|	
+||	||	||	
+
+test roundtrip: 
+true
diff --git a/test/app/csv.test.lua b/test/app/csv.test.lua
new file mode 100755
index 0000000000..ccfeabf425
--- /dev/null
+++ b/test/app/csv.test.lua
@@ -0,0 +1,99 @@
+#!/usr/bin/env tarantool
+
+local function table2str(t)
+    res = ""
+    for k, line in pairs(t) do
+        s = ""
+        for k2, field in pairs(line) do
+            s = s .. '|' .. field .. '|\t'
+        end
+        res = res .. s .. '\n'
+    end
+    return res
+end
+
+local function myread(self, bytes) 
+    self.i = self.i + bytes
+    return self.v:sub(self.i - bytes + 1, self.i) 
+end
+local csv = require('csv')
+local fio = require('fio')
+
+print("obj test1:")
+readable = {}
+readable.read = myread
+readable.v = "a,b\n1,\"ha\n\"\"ha\"\"\nha\"\n3,4\n"
+readable.i = 0
+print(table2str(csv.load(readable)))
+
+print("obj test2:")
+readable.v = ", ,\n , \n\n"
+readable.i = 0
+print(table2str(csv.load(readable, 1)))
+
+print("obj test3:")
+readable.v = ", \r\nkp\"\"v"
+readable.i = 0
+print(table2str(csv.load(readable, 3)))
+
+tmpdir = fio.tempdir()
+file1 = fio.pathjoin(tmpdir, 'file.1')
+file2 = fio.pathjoin(tmpdir, 'file.2')
+file3 = fio.pathjoin(tmpdir, 'file.3')
+
+print("fio test1:")
+local f = fio.open(file1, { 'O_WRONLY', 'O_TRUNC', 'O_CREAT' }, 0777)
+f:write("123 , 5  ,       92    , 0, 0\n" ..
+        "1, 12  34, 56, \"quote , \", 66\nok")
+f:close()
+f = fio.open(file1, {'O_RDONLY'}) 
+print(table2str(csv.load(f,10)))
+f:close()
+
+
+print("fio test2:")
+f = fio.open(file2, { 'O_WRONLY', 'O_TRUNC', 'O_CREAT' }, 0777)
+f:write("1\n23,456,abcac,\'multiword field 4\'\n" ..
+        "none,none,0\n" ..
+        ",,\n" ..
+        "aba,adda,f3,0\n" ..
+        "local res = internal.pwrite(self.fh, data, len, offset)\n" ..
+        "iflag = bit.bor(iflag, fio.c.flag[ flag ])\n" ..
+        ",,"
+)
+f:close()
+
+print("fio test3:")
+f = fio.open(file2, {'O_RDONLY'}) 
+print(table2str(csv.load(f, 1))) --symbol by symbol reading
+f:close()
+
+print("fio test4:")
+f = fio.open(file2, {'O_RDONLY'}) 
+print(table2str(csv.load(f, 7))) --7 symbols per chunk
+f:close()
+
+
+t = {
+    {'quote" d', ',and, comma', 'both " of " t,h,e,m'}, 
+    {'"""', ',","'}, 
+    {'mul\nti\nli\r\nne\n\n', 'field'},
+    {""},
+    {'"'},
+    {"\n"}
+}
+
+f = require("fio").open(file3, { "O_WRONLY", "O_TRUNC" , "O_CREAT"}, 0x1FF)
+csv.dump(f, t)
+f:close()
+f = fio.open(file3, {'O_RDONLY'}) 
+t2 = csv.load(f, 5)
+f:close()
+
+print("test roundtrip: ")
+print(table2str(t) == table2str(t2))
+
+fio.unlink(file1)
+fio.unlink(file2)
+fio.unlink(file3)
+fio.rmdir(tmpdir)
-- 
GitLab