forked from clbustos/statsample
/
test_csv.rb
80 lines (79 loc) · 2.79 KB
/
test_csv.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require(File.expand_path(File.dirname(__FILE__)+'/helpers_tests.rb'))
class StatsampleCSVTestCase < MiniTest::Unit::TestCase
def setup
@ds=Statsample::CSV.read(File.dirname(__FILE__)+"/fixtures/test_csv.csv")
end
def test_read
assert_equal(6,@ds.cases)
assert_equal(%w{id name age city a1}, @ds.fields)
id=[1,2,3,4,5,6].to_vector(:scale)
name=["Alex","Claude","Peter","Franz","George","Fernand"].to_vector(:nominal)
age=[20,23,25,27,5.5,nil].to_vector(:scale)
city=["New York","London","London","Paris","Tome",nil].to_vector(:nominal)
a1=["a,b","b,c","a",nil,"a,b,c",nil].to_vector(:nominal)
ds_exp=Statsample::Dataset.new({'id'=>id,'name'=>name,'age'=>age,'city'=>city,'a1'=>a1}, %w{id name age city a1})
ds_exp.fields.each{|f|
assert_equal(ds_exp[f],@ds[f])
}
assert_equal(ds_exp,@ds)
end
def test_nil
assert_equal(nil,@ds['age'][5])
end
def test_repeated
ds=Statsample::CSV.read(File.dirname(__FILE__)+"/fixtures/repeated_fields.csv")
assert_equal(%w{id name_1 age_1 city a1 name_2 age_2},ds.fields)
age=[3,4,5,6,nil,8].to_vector(:scale)
assert_equal(age,ds['age_2'])
end
def test_write
filename=Tempfile.new("afile")
# filename=Dir::tmpdir+"/test_write.csv"
Statsample::CSV.write(@ds, filename.path)
ds2=Statsample::CSV.read(filename.path)
i=0
ds2.each_array{|row|
assert_equal(@ds.case_as_array(i),row)
i+=1
}
end
end
class StatsampleCSVTestCase2 < MiniTest::Unit::TestCase
def setup
@ds=Statsample::CSV.read19(File.dirname(__FILE__)+"/fixtures/test_csv.csv")
end
def test_read
assert_equal(6,@ds.cases)
assert_equal(%w{id name age city a1}, @ds.fields)
id=[1,2,3,4,5,6].to_vector(:scale)
name=["Alex","Claude","Peter","Franz","George","Fernand"].to_vector(:nominal)
age=[20,23,25,27,5.5,nil].to_vector(:scale)
city=["New York","London","London","Paris","Tome",nil].to_vector(:nominal)
a1=["a,b","b,c","a",nil,"a,b,c",nil].to_vector(:nominal)
ds_exp=Statsample::Dataset.new({'id'=>id,'name'=>name,'age'=>age,'city'=>city,'a1'=>a1}, %w{id name age city a1})
ds_exp.fields.each{|f|
assert_equal(ds_exp[f],@ds[f])
}
assert_equal(ds_exp,@ds)
end
def test_nil
assert_equal(nil,@ds['age'][5])
end
def test_repeated
ds=Statsample::CSV.read19(File.dirname(__FILE__)+"/fixtures/repeated_fields.csv")
assert_equal(%w{id name_1 age_1 city a1 name_2 age_2},ds.fields)
age=[3,4,5,6,nil,8].to_vector(:scale)
assert_equal(age,ds['age_2'])
end
def test_write
filename=Tempfile.new("afile")
# filename=Dir::tmpdir+"/test_write.csv"
Statsample::CSV.write(@ds, filename.path)
ds2=Statsample::CSV.read19(filename.path)
i=0
ds2.each_array{|row|
assert_equal(@ds.case_as_array(i),row)
i+=1
}
end
end