Skip to content
Snippets Groups Projects
Commit 78dc0973 authored by Daniil Medvedev's avatar Daniil Medvedev
Browse files

csv added lua tests

parent ec193ca6
No related branches found
No related tags found
No related merge requests found
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
#!/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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment