linkage 0.1.0.pre → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +2 -0
  3. data/Guardfile +0 -1
  4. data/TODO +2 -0
  5. data/lib/linkage.rb +1 -0
  6. data/lib/linkage/comparator.rb +12 -2
  7. data/lib/linkage/comparators/strcompare.rb +68 -16
  8. data/lib/linkage/configuration.rb +112 -8
  9. data/lib/linkage/dataset.rb +124 -9
  10. data/lib/linkage/exceptions.rb +5 -0
  11. data/lib/linkage/field.rb +55 -18
  12. data/lib/linkage/field_set.rb +20 -0
  13. data/lib/linkage/helpers.rb +7 -0
  14. data/lib/linkage/helpers/csv.rb +28 -0
  15. data/lib/linkage/helpers/database.rb +47 -0
  16. data/lib/linkage/import_buffer.rb +3 -3
  17. data/lib/linkage/match_recorder.rb +4 -0
  18. data/lib/linkage/match_set.rb +51 -13
  19. data/lib/linkage/match_sets/csv.rb +36 -9
  20. data/lib/linkage/match_sets/database.rb +43 -2
  21. data/lib/linkage/matcher.rb +49 -3
  22. data/lib/linkage/result_set.rb +60 -22
  23. data/lib/linkage/result_sets/csv.rb +46 -28
  24. data/lib/linkage/result_sets/database.rb +44 -26
  25. data/lib/linkage/runner.rb +10 -0
  26. data/lib/linkage/score_recorder.rb +5 -0
  27. data/lib/linkage/score_set.rb +78 -20
  28. data/lib/linkage/score_sets/csv.rb +41 -15
  29. data/lib/linkage/score_sets/database.rb +43 -5
  30. data/lib/linkage/version.rb +1 -1
  31. data/linkage.gemspec +2 -0
  32. data/misc/uml/linkage.dia +0 -0
  33. data/misc/uml/linkage.png +0 -0
  34. data/misc/uml/linkage.svg +197 -0
  35. data/test/helper.rb +2 -11
  36. data/test/integration/test_database_result_set.rb +4 -2
  37. data/test/unit/comparators/test_strcompare.rb +29 -0
  38. data/test/unit/match_sets/test_csv.rb +44 -13
  39. data/test/unit/match_sets/test_database.rb +42 -1
  40. data/test/unit/result_sets/test_csv.rb +9 -69
  41. data/test/unit/result_sets/test_database.rb +20 -11
  42. data/test/unit/score_sets/test_csv.rb +68 -25
  43. data/test/unit/score_sets/test_database.rb +57 -1
  44. data/test/unit/test_comparator.rb +8 -0
  45. data/test/unit/test_configuration.rb +33 -6
  46. data/test/unit/test_dataset.rb +0 -7
  47. data/test/unit/test_matcher.rb +52 -3
  48. data/test/unit/test_result_set.rb +8 -14
  49. metadata +66 -32
@@ -2,50 +2,79 @@ require File.expand_path("../../test_match_sets", __FILE__)
2
2
 
3
3
  class UnitTests::TestMatchSets::TestCSV < Test::Unit::TestCase
4
4
  test "open_for_writing" do
5
- match_set = Linkage::MatchSets::CSV.new('foo.csv')
5
+ match_set = Linkage::MatchSets::CSV.new(:filename => 'foo.csv')
6
+ expected_filename = File.expand_path('foo.csv')
7
+ File.expects(:exist?).with(expected_filename).returns(false)
6
8
  csv = stub('csv')
7
- CSV.expects(:open).with('foo.csv', 'wb').returns(csv)
9
+ CSV.expects(:open).with(expected_filename, 'wb').returns(csv)
10
+ csv.expects(:<<).with(%w{id_1 id_2 score})
11
+ match_set.open_for_writing
12
+ end
13
+
14
+ test "open_for_writing with default options" do
15
+ match_set = Linkage::MatchSets::CSV.new
16
+ expected_filename = File.expand_path('matches.csv')
17
+ File.expects(:exist?).with(expected_filename).returns(false)
18
+ csv = stub('csv')
19
+ CSV.expects(:open).with(expected_filename, 'wb').returns(csv)
20
+ csv.expects(:<<).with(%w{id_1 id_2 score})
21
+ match_set.open_for_writing
22
+ end
23
+
24
+ test "open_for_writing with directory option" do
25
+ match_set = Linkage::MatchSets::CSV.new(:dir => 'foo')
26
+
27
+ expected_dir = File.expand_path('foo')
28
+ FileUtils.expects(:mkdir_p).with(expected_dir)
29
+ expected_filename = File.join(expected_dir, 'matches.csv')
30
+ File.expects(:exist?).with(expected_filename).returns(false)
31
+ csv = stub('csv')
32
+ CSV.expects(:open).with(expected_filename, 'wb').returns(csv)
8
33
  csv.expects(:<<).with(%w{id_1 id_2 score})
9
34
  match_set.open_for_writing
10
35
  end
11
36
 
12
37
  test "open_for_writing when already open" do
13
- match_set = Linkage::MatchSets::CSV.new('foo.csv')
38
+ match_set = Linkage::MatchSets::CSV.new(:filename => 'foo.csv')
39
+ expected_filename = File.expand_path('foo.csv')
40
+ File.expects(:exist?).once.with(expected_filename).returns(false)
14
41
  csv = stub('csv')
15
- CSV.expects(:open).once.with('foo.csv', 'wb').returns(csv)
42
+ CSV.expects(:open).once.with(expected_filename, 'wb').returns(csv)
16
43
  csv.expects(:<<).once.with(%w{id_1 id_2 score})
17
44
  match_set.open_for_writing
18
45
  match_set.open_for_writing
19
46
  end
20
47
 
21
48
  test "open_for_writing when file exists" do
22
- match_set = Linkage::MatchSets::CSV.new('foo.csv')
23
- File.expects(:exist?).with('foo.csv').returns(true)
49
+ match_set = Linkage::MatchSets::CSV.new(:filename => 'foo.csv')
50
+ expected_filename = File.expand_path('foo.csv')
51
+ File.expects(:exist?).with(expected_filename).returns(true)
24
52
  assert_raises(Linkage::ExistsError) do
25
53
  match_set.open_for_writing
26
54
  end
27
55
  end
28
56
 
29
57
  test "open_for_writing when file exists and forcing overwrite" do
30
- match_set = Linkage::MatchSets::CSV.new('foo.csv', :overwrite => true)
31
- File.stubs(:exist?).with('foo.csv').returns(true)
58
+ match_set = Linkage::MatchSets::CSV.new(:filename => 'foo.csv', :overwrite => true)
59
+ expected_filename = File.expand_path('foo.csv')
60
+ File.stubs(:exist?).with(expected_filename).returns(true)
32
61
  assert_nothing_raised do
33
62
  csv = stub('csv')
34
- CSV.expects(:open).with('foo.csv', 'wb').returns(csv)
63
+ CSV.expects(:open).with(expected_filename, 'wb').returns(csv)
35
64
  csv.expects(:<<).with(%w{id_1 id_2 score})
36
65
  match_set.open_for_writing
37
66
  end
38
67
  end
39
68
 
40
69
  test "add_match when unopened raises exception" do
41
- match_set = Linkage::MatchSets::CSV.new('foo.csv')
70
+ match_set = Linkage::MatchSets::CSV.new(:filename => 'foo.csv')
42
71
  assert_raises { match_set.add_match(1, 2, 1) }
43
72
  end
44
73
 
45
74
  test "add_match" do
46
75
  tempfile = Tempfile.new('linkage')
47
76
  tempfile.close
48
- match_set = Linkage::MatchSets::CSV.new(tempfile.path, :overwrite => true)
77
+ match_set = Linkage::MatchSets::CSV.new(:filename => tempfile.path, :overwrite => true)
49
78
  match_set.open_for_writing
50
79
  match_set.add_match(1, 2, 1)
51
80
  match_set.close
@@ -55,9 +84,11 @@ class UnitTests::TestMatchSets::TestCSV < Test::Unit::TestCase
55
84
  end
56
85
 
57
86
  test "add_match removes extra decimals" do
58
- match_set = Linkage::MatchSets::CSV.new('foo.csv')
87
+ match_set = Linkage::MatchSets::CSV.new(:filename => 'foo.csv')
88
+ expected_filename = File.expand_path('foo.csv')
89
+ File.expects(:exist?).with(expected_filename).returns(false)
59
90
  csv = stub('csv')
60
- CSV.stubs(:open).with('foo.csv', 'wb').returns(csv)
91
+ CSV.stubs(:open).with(expected_filename, 'wb').returns(csv)
61
92
  csv.stubs(:<<).with(%w{id_1 id_2 score})
62
93
  match_set.open_for_writing
63
94
 
@@ -4,6 +4,47 @@ class UnitTests::TestMatchSets::TestDatabase < Test::Unit::TestCase
4
4
  def setup
5
5
  @dataset = stub('dataset')
6
6
  @database = stub('database', :[] => @dataset)
7
+ Sequel::Database.stubs(:===).with(@database).returns(true)
8
+ end
9
+
10
+ test "open_for_writing with uri string" do
11
+ Sequel.expects(:connect).with('foo://bar').returns(@database)
12
+ match_set = Linkage::MatchSets::Database.new('foo://bar')
13
+ @database.stubs(:table_exists?).with(:matches).returns(false)
14
+ @database.expects(:create_table).with(:matches)
15
+ @database.expects(:[]).with(:matches).returns(@dataset)
16
+ match_set.open_for_writing
17
+ end
18
+
19
+ test "open_for_writing with filename option" do
20
+ Sequel.expects(:connect).with(:adapter => :sqlite, :database => 'foo.db').returns(@database)
21
+ match_set = Linkage::MatchSets::Database.new(:filename => 'foo.db')
22
+ @database.stubs(:table_exists?).with(:matches).returns(false)
23
+ @database.expects(:create_table).with(:matches)
24
+ @database.expects(:[]).with(:matches).returns(@dataset)
25
+ match_set.open_for_writing
26
+ end
27
+
28
+ test "open_for_writing with default options" do
29
+ Sequel.expects(:connect).with(:adapter => :sqlite, :database => 'matches.db').returns(@database)
30
+ match_set = Linkage::MatchSets::Database.new
31
+ @database.stubs(:table_exists?).with(:matches).returns(false)
32
+ @database.expects(:create_table).with(:matches)
33
+ @database.expects(:[]).with(:matches).returns(@dataset)
34
+ match_set.open_for_writing
35
+ end
36
+
37
+ test "open_for_writing with directory option" do
38
+ expected_directory = File.expand_path('foo')
39
+ FileUtils.expects(:mkdir_p).with(expected_directory)
40
+ expected_filename = File.join(expected_directory, 'matches.db')
41
+ Sequel.expects(:connect).with(:adapter => :sqlite, :database => expected_filename).returns(@database)
42
+
43
+ match_set = Linkage::MatchSets::Database.new(:dir => 'foo')
44
+ @database.stubs(:table_exists?).with(:matches).returns(false)
45
+ @database.expects(:create_table).with(:matches)
46
+ @database.expects(:[]).with(:matches).returns(@dataset)
47
+ match_set.open_for_writing
7
48
  end
8
49
 
9
50
  test "open_for_writing for database with no matches table" do
@@ -33,7 +74,7 @@ class UnitTests::TestMatchSets::TestDatabase < Test::Unit::TestCase
33
74
  end
34
75
 
35
76
  test "open_for_writing when matches table already exists and in overwrite mode" do
36
- match_set = Linkage::MatchSets::Database.new(@database, :overwrite => true)
77
+ match_set = Linkage::MatchSets::Database.new(@database, {:overwrite => true})
37
78
  @database.expects(:drop_table?).with(:matches)
38
79
  @database.expects(:create_table).with(:matches)
39
80
  @database.expects(:[]).with(:matches).returns(@dataset)
@@ -9,99 +9,39 @@ class UnitTests::TestResultSets::TestCSV < Test::Unit::TestCase
9
9
  FileUtils.remove_entry_secure(@tmpdir)
10
10
  end
11
11
 
12
- test "no directory option uses current directory" do
12
+ test "default options" do
13
13
  result_set = Linkage::ResultSets::CSV.new
14
14
 
15
- expected_score_file = './scores.csv'
16
15
  score_set = stub('score set')
17
- Linkage::ScoreSets::CSV.expects(:new).with(expected_score_file, {}).returns(score_set)
16
+ Linkage::ScoreSets::CSV.expects(:new).with({}).returns(score_set)
18
17
  assert_same score_set, result_set.score_set
19
18
 
20
- expected_match_file = './matches.csv'
21
19
  match_set = stub('match set')
22
- Linkage::MatchSets::CSV.expects(:new).with(expected_match_file, {}).returns(match_set)
20
+ Linkage::MatchSets::CSV.expects(:new).with({}).returns(match_set)
23
21
  assert_same match_set, result_set.match_set
24
22
  end
25
23
 
26
24
  test "directory option" do
27
- opts = {
28
- :dir => File.join(@tmpdir, 'foo')
29
- }
30
- result_set = Linkage::ResultSets::CSV.new(opts)
31
- assert Dir.exist?(opts[:dir])
25
+ result_set = Linkage::ResultSets::CSV.new(:dir => 'foo')
32
26
 
33
- expected_score_file = File.join(@tmpdir, 'foo', 'scores.csv')
34
27
  score_set = stub('score set')
35
- Linkage::ScoreSets::CSV.expects(:new).with(expected_score_file, {}).returns(score_set)
28
+ Linkage::ScoreSets::CSV.expects(:new).with({:dir => 'foo'}).returns(score_set)
36
29
  assert_same score_set, result_set.score_set
37
30
 
38
- expected_match_file = File.join(@tmpdir, 'foo', 'matches.csv')
39
31
  match_set = stub('match set')
40
- Linkage::MatchSets::CSV.expects(:new).with(expected_match_file, {}).returns(match_set)
32
+ Linkage::MatchSets::CSV.expects(:new).with({:dir => 'foo'}).returns(match_set)
41
33
  assert_same match_set, result_set.match_set
42
34
  end
43
35
 
44
36
  test "directory argument" do
45
- dir = File.join(@tmpdir, 'foo')
46
- result_set = Linkage::ResultSets::CSV.new(dir)
47
- assert Dir.exist?(dir)
37
+ result_set = Linkage::ResultSets::CSV.new('foo')
48
38
 
49
- expected_score_file = File.join(@tmpdir, 'foo', 'scores.csv')
50
39
  score_set = stub('score set')
51
- Linkage::ScoreSets::CSV.expects(:new).with(expected_score_file, {}).returns(score_set)
40
+ Linkage::ScoreSets::CSV.expects(:new).with({:dir => 'foo'}).returns(score_set)
52
41
  assert_same score_set, result_set.score_set
53
42
 
54
- expected_match_file = File.join(@tmpdir, 'foo', 'matches.csv')
55
43
  match_set = stub('match set')
56
- Linkage::MatchSets::CSV.expects(:new).with(expected_match_file, {}).returns(match_set)
57
- assert_same match_set, result_set.match_set
58
- end
59
-
60
- test "directory path is expanded" do
61
- opts = {
62
- :dir => "~/foo"
63
- }
64
- FileUtils.expects(:mkdir_p).with(File.expand_path("~/foo"))
65
- result_set = Linkage::ResultSets::CSV.new(opts)
66
- end
67
-
68
- test "custom filenames" do
69
- opts = {
70
- :dir => File.join(@tmpdir, 'foo'),
71
- :scores => {:filename => 'foo-scores.csv'},
72
- :matches => {:filename => 'foo-matches.csv'}
73
- }
74
- result_set = Linkage::ResultSets::CSV.new(opts)
75
- assert Dir.exist?(opts[:dir])
76
-
77
- expected_score_file = File.join(@tmpdir, 'foo', 'foo-scores.csv')
78
- score_set = stub('score set')
79
- Linkage::ScoreSets::CSV.expects(:new).with(expected_score_file, {}).returns(score_set)
80
- assert_same score_set, result_set.score_set
81
-
82
- expected_match_file = File.join(@tmpdir, 'foo', 'foo-matches.csv')
83
- match_set = stub('match set')
84
- Linkage::MatchSets::CSV.expects(:new).with(expected_match_file, {}).returns(match_set)
85
- assert_same match_set, result_set.match_set
86
- end
87
-
88
- test "miscellaneous options" do
89
- opts = {
90
- :dir => File.join(@tmpdir, 'foo'),
91
- :scores => {:foo => 'bar'},
92
- :matches => {:baz => 'qux'}
93
- }
94
- result_set = Linkage::ResultSets::CSV.new(opts)
95
- assert Dir.exist?(opts[:dir])
96
-
97
- expected_score_file = File.join(@tmpdir, 'foo', 'scores.csv')
98
- score_set = stub('score set')
99
- Linkage::ScoreSets::CSV.expects(:new).with(expected_score_file, :foo => 'bar').returns(score_set)
100
- assert_same score_set, result_set.score_set
101
-
102
- expected_match_file = File.join(@tmpdir, 'foo', 'matches.csv')
103
- match_set = stub('match set')
104
- Linkage::MatchSets::CSV.expects(:new).with(expected_match_file, :baz => 'qux').returns(match_set)
44
+ Linkage::MatchSets::CSV.expects(:new).with({:dir => 'foo'}).returns(match_set)
105
45
  assert_same match_set, result_set.match_set
106
46
  end
107
47
 
@@ -1,10 +1,24 @@
1
1
  require File.expand_path("../../test_result_sets", __FILE__)
2
2
 
3
3
  class UnitTests::TestResultSets::TestDatabase < Test::Unit::TestCase
4
+ test "with default options" do
5
+ database = stub('database')
6
+ Sequel.expects(:connect).with(:adapter => :sqlite, :database => 'results.db').returns(database)
7
+ result_set = Linkage::ResultSets::Database.new
8
+
9
+ score_set = stub('score set')
10
+ Linkage::ScoreSets::Database.expects(:new).with(database, {}).returns(score_set)
11
+ assert_same score_set, result_set.score_set
12
+
13
+ match_set = stub('match set')
14
+ Linkage::MatchSets::Database.expects(:new).with(database, {}).returns(match_set)
15
+ assert_same match_set, result_set.match_set
16
+ end
17
+
4
18
  test "with database object" do
5
19
  database = stub('database')
6
- database.stubs(:kind_of?).with(Sequel::Database).returns(true)
7
- result_set = Linkage::ResultSets::Database.new(database)
20
+ Sequel::Database.stubs(:===).with(database).returns(true)
21
+ result_set = Linkage::ResultSets::Database.new(database, {})
8
22
 
9
23
  score_set = stub('score set')
10
24
  Linkage::ScoreSets::Database.expects(:new).with(database, {}).returns(score_set)
@@ -43,22 +57,17 @@ class UnitTests::TestResultSets::TestDatabase < Test::Unit::TestCase
43
57
  assert_same match_set, result_set.match_set
44
58
  end
45
59
 
46
- test "with database options and scores/matches options" do
47
- opts = {
48
- :foo => 'bar',
49
- :scores => {:baz => 'qux'},
50
- :matches => {:corge => 'grault'}
51
- }
60
+ test "with overwrite option" do
52
61
  database = stub('database')
53
62
  Sequel.expects(:connect).with(:foo => 'bar').returns(database)
54
- result_set = Linkage::ResultSets::Database.new(opts)
63
+ result_set = Linkage::ResultSets::Database.new({:foo => 'bar'}, {:overwrite => true})
55
64
 
56
65
  score_set = stub('score set')
57
- Linkage::ScoreSets::Database.expects(:new).with(database, opts[:scores]).returns(score_set)
66
+ Linkage::ScoreSets::Database.expects(:new).with(database, {:overwrite => true}).returns(score_set)
58
67
  assert_same score_set, result_set.score_set
59
68
 
60
69
  match_set = stub('match set')
61
- Linkage::MatchSets::Database.expects(:new).with(database, opts[:matches]).returns(match_set)
70
+ Linkage::MatchSets::Database.expects(:new).with(database, {:overwrite => true}).returns(match_set)
62
71
  assert_same match_set, result_set.match_set
63
72
  end
64
73
 
@@ -2,69 +2,101 @@ require File.expand_path("../../test_score_sets", __FILE__)
2
2
 
3
3
  class UnitTests::TestScoreSets::TestCSV < Test::Unit::TestCase
4
4
  test "open_for_writing" do
5
- score_set = Linkage::ScoreSets::CSV.new('foo.csv')
5
+ score_set = Linkage::ScoreSets::CSV.new(:filename => 'foo.csv')
6
+ expected_filename = File.expand_path('foo.csv')
7
+ File.expects(:exist?).with(expected_filename).returns(false)
6
8
  csv = stub('csv')
7
- CSV.expects(:open).with('foo.csv', 'wb').returns(csv)
9
+ CSV.expects(:open).with(expected_filename, 'wb').returns(csv)
10
+ csv.expects(:<<).with(%w{comparator_id id_1 id_2 score})
11
+ score_set.open_for_writing
12
+ end
13
+
14
+ test "open_for_writing with default options" do
15
+ score_set = Linkage::ScoreSets::CSV.new
16
+ expected_filename = File.expand_path('scores.csv')
17
+ File.expects(:exist?).with(expected_filename).returns(false)
18
+ csv = stub('csv')
19
+ CSV.expects(:open).with(expected_filename, 'wb').returns(csv)
20
+ csv.expects(:<<).with(%w{comparator_id id_1 id_2 score})
21
+ score_set.open_for_writing
22
+ end
23
+
24
+ test "open_for_writing with directory option" do
25
+ score_set = Linkage::ScoreSets::CSV.new(:dir => 'foo')
26
+
27
+ expected_dir = File.expand_path('foo')
28
+ FileUtils.expects(:mkdir_p).with(expected_dir)
29
+ expected_filename = File.join(expected_dir, 'scores.csv')
30
+ csv = stub('csv')
31
+ CSV.expects(:open).with(expected_filename, 'wb').returns(csv)
8
32
  csv.expects(:<<).with(%w{comparator_id id_1 id_2 score})
9
33
  score_set.open_for_writing
10
34
  end
11
35
 
12
36
  test "open_for_writing when already open" do
13
- score_set = Linkage::ScoreSets::CSV.new('foo.csv')
37
+ score_set = Linkage::ScoreSets::CSV.new(:filename => 'foo.csv')
38
+ expected_filename = File.expand_path('foo.csv')
39
+ File.expects(:exist?).with(expected_filename).returns(false)
14
40
  csv = stub('csv')
15
- CSV.expects(:open).once.with('foo.csv', 'wb').returns(csv)
41
+ CSV.expects(:open).once.with(expected_filename, 'wb').returns(csv)
16
42
  csv.expects(:<<).once.with(%w{comparator_id id_1 id_2 score})
17
43
  score_set.open_for_writing
18
44
  score_set.open_for_writing
19
45
  end
20
46
 
21
47
  test "open_for_writing when file exists" do
22
- score_set = Linkage::ScoreSets::CSV.new('foo.csv')
23
- File.expects(:exist?).with('foo.csv').returns(true)
48
+ score_set = Linkage::ScoreSets::CSV.new(:filename => 'foo.csv')
49
+ expected_filename = File.expand_path('foo.csv')
50
+ File.expects(:exist?).with(expected_filename).returns(true)
24
51
  assert_raises(Linkage::ExistsError) do
25
52
  score_set.open_for_writing
26
53
  end
27
54
  end
28
55
 
29
56
  test "open_for_writing when file exists and forcing overwrite" do
30
- score_set = Linkage::ScoreSets::CSV.new('foo.csv', :overwrite => true)
31
- File.stubs(:exist?).with('foo.csv').returns(true)
57
+ score_set = Linkage::ScoreSets::CSV.new(:filename => 'foo.csv', :overwrite => true)
58
+ expected_filename = File.expand_path('foo.csv')
59
+ File.stubs(:exist?).with(expected_filename).returns(true)
32
60
  assert_nothing_raised do
33
61
  csv = stub('csv')
34
- CSV.expects(:open).with('foo.csv', 'wb').returns(csv)
62
+ CSV.expects(:open).with(expected_filename, 'wb').returns(csv)
35
63
  csv.expects(:<<).with(%w{comparator_id id_1 id_2 score})
36
64
  score_set.open_for_writing
37
65
  end
38
66
  end
39
67
 
40
68
  test "open_for_reading" do
41
- score_set = Linkage::ScoreSets::CSV.new('foo.csv')
42
- File.stubs(:exist?).with('foo.csv').returns(true)
69
+ score_set = Linkage::ScoreSets::CSV.new(:filename => 'foo.csv')
70
+ expected_filename = File.expand_path('foo.csv')
71
+ File.stubs(:exist?).with(expected_filename).returns(true)
43
72
  csv = stub('csv')
44
- CSV.expects(:open).with('foo.csv', 'rb', {:headers => true}).returns(csv)
73
+ CSV.expects(:open).with(expected_filename, 'rb', {:headers => true}).returns(csv)
45
74
  score_set.open_for_reading
46
75
  end
47
76
 
48
77
  test "open_for_reading when already open" do
49
- score_set = Linkage::ScoreSets::CSV.new('foo.csv')
50
- File.stubs(:exist?).with('foo.csv').returns(true)
78
+ score_set = Linkage::ScoreSets::CSV.new(:filename => 'foo.csv')
79
+ expected_filename = File.expand_path('foo.csv')
80
+ File.stubs(:exist?).with(expected_filename).returns(true)
51
81
  csv = stub('csv')
52
- CSV.expects(:open).once.with('foo.csv', 'rb', {:headers => true}).returns(csv)
82
+ CSV.expects(:open).once.with(expected_filename, 'rb', {:headers => true}).returns(csv)
53
83
  score_set.open_for_reading
54
84
  score_set.open_for_reading
55
85
  end
56
86
 
57
87
  test "open_for_reading when file doesn't exist" do
58
- score_set = Linkage::ScoreSets::CSV.new('foo.csv')
59
- File.expects(:exist?).with('foo.csv').returns(false)
88
+ score_set = Linkage::ScoreSets::CSV.new(:filename => 'foo.csv')
89
+ expected_filename = File.expand_path('foo.csv')
90
+ File.expects(:exist?).with(expected_filename).returns(false)
60
91
  assert_raises(Linkage::MissingError) do
61
92
  score_set.open_for_reading
62
93
  end
63
94
  end
64
95
 
65
96
  test "open_for_writing when in read mode raises exception" do
66
- score_set = Linkage::ScoreSets::CSV.new('foo.csv')
67
- File.stubs(:exist?).with('foo.csv').returns(true)
97
+ score_set = Linkage::ScoreSets::CSV.new(:filename => 'foo.csv')
98
+ expected_filename = File.expand_path('foo.csv')
99
+ File.stubs(:exist?).with(expected_filename).returns(true)
68
100
  csv = stub('csv')
69
101
  CSV.stubs(:open).returns(csv)
70
102
  score_set.open_for_reading
@@ -72,7 +104,7 @@ class UnitTests::TestScoreSets::TestCSV < Test::Unit::TestCase
72
104
  end
73
105
 
74
106
  test "open_for_reading when in write mode raises exception" do
75
- score_set = Linkage::ScoreSets::CSV.new('foo.csv')
107
+ score_set = Linkage::ScoreSets::CSV.new(:filename => 'foo.csv')
76
108
  csv = stub('csv', :<< => nil)
77
109
  CSV.stubs(:open).returns(csv)
78
110
  score_set.open_for_writing
@@ -80,13 +112,14 @@ class UnitTests::TestScoreSets::TestCSV < Test::Unit::TestCase
80
112
  end
81
113
 
82
114
  test "add_score when unopened raises exception" do
83
- score_set = Linkage::ScoreSets::CSV.new('foo.csv')
115
+ score_set = Linkage::ScoreSets::CSV.new(:filename => 'foo.csv')
84
116
  assert_raises { score_set.add_score(1, 1, 2, 1) }
85
117
  end
86
118
 
87
119
  test "add_score when in read mode raises exception" do
88
- score_set = Linkage::ScoreSets::CSV.new('foo.csv')
89
- File.stubs(:exist?).with('foo.csv').returns(true)
120
+ score_set = Linkage::ScoreSets::CSV.new(:filename => 'foo.csv')
121
+ expected_filename = File.expand_path('foo.csv')
122
+ File.stubs(:exist?).with(expected_filename).returns(true)
90
123
  csv = stub('csv')
91
124
  CSV.stubs(:open).returns(csv)
92
125
  score_set.open_for_reading
@@ -96,7 +129,7 @@ class UnitTests::TestScoreSets::TestCSV < Test::Unit::TestCase
96
129
  test "add_score" do
97
130
  tempfile = Tempfile.new('linkage')
98
131
  tempfile.close
99
- score_set = Linkage::ScoreSets::CSV.new(tempfile.path, :overwrite => true)
132
+ score_set = Linkage::ScoreSets::CSV.new(:filename => tempfile.path, :overwrite => true)
100
133
  score_set.open_for_writing
101
134
  score_set.add_score(1, 1, 2, 1)
102
135
  score_set.close
@@ -118,10 +151,13 @@ class UnitTests::TestScoreSets::TestCSV < Test::Unit::TestCase
118
151
  3,4,5,0
119
152
  EOF
120
153
  tempfile.close
121
- score_set = Linkage::ScoreSets::CSV.new(tempfile.path)
154
+ score_set = Linkage::ScoreSets::CSV.new(:filename => tempfile.path)
122
155
 
123
156
  pairs = []
157
+ score_set.open_for_reading
124
158
  score_set.each_pair { |*args| pairs << args }
159
+ score_set.close
160
+
125
161
  assert_equal 4, pairs.length
126
162
 
127
163
  pair_1 = pairs.detect { |pair| pair[0] == "1" && pair[1] == "2" }
@@ -145,6 +181,13 @@ class UnitTests::TestScoreSets::TestCSV < Test::Unit::TestCase
145
181
  assert_equal expected_4, pair_4[2]
146
182
  end
147
183
 
184
+ test "each_pair when not open for reading" do
185
+ score_set = Linkage::ScoreSets::CSV.new(:filename => "/foo/bar.csv")
186
+ assert_raise_message("not in read mode") do
187
+ score_set.each_pair { |*args| }
188
+ end
189
+ end
190
+
148
191
  test "registers itself" do
149
192
  assert_equal Linkage::ScoreSets::CSV, Linkage::ScoreSet['csv']
150
193
  end