joinfix 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +21 -0
- data/README +86 -0
- data/TEST_README +44 -0
- data/lib/joinfix/fixture.rb +29 -0
- data/lib/joinfix/fixtures.rb +271 -0
- data/lib/joinfix/fixtures_class.rb +102 -0
- data/lib/joinfix.rb +208 -0
- data/rails/README +182 -0
- data/rails/Rakefile +10 -0
- data/rails/app/controllers/application.rb +7 -0
- data/rails/app/helpers/application_helper.rb +3 -0
- data/rails/app/models/group.rb +4 -0
- data/rails/app/models/inner_child.rb +4 -0
- data/rails/app/models/nested_child.rb +4 -0
- data/rails/app/models/nested_parent.rb +4 -0
- data/rails/app/models/user.rb +4 -0
- data/rails/app/models/user_group.rb +5 -0
- data/rails/config/boot.rb +45 -0
- data/rails/config/database.yml +36 -0
- data/rails/config/environment.rb +60 -0
- data/rails/config/environments/development.rb +21 -0
- data/rails/config/environments/production.rb +18 -0
- data/rails/config/environments/test.rb +19 -0
- data/rails/config/routes.rb +23 -0
- data/rails/db/migrate/001_create_nested_parents.rb +12 -0
- data/rails/db/migrate/002_create_nested_children.rb +12 -0
- data/rails/db/migrate/003_create_inner_children.rb +12 -0
- data/rails/db/migrate/004_create_users.rb +11 -0
- data/rails/db/migrate/005_create_groups.rb +11 -0
- data/rails/db/migrate/006_create_user_groups.rb +12 -0
- data/rails/db/schema.rb +35 -0
- data/rails/doc/README_FOR_APP +2 -0
- data/rails/public/404.html +30 -0
- data/rails/public/500.html +30 -0
- data/rails/public/dispatch.cgi +10 -0
- data/rails/public/dispatch.fcgi +24 -0
- data/rails/public/dispatch.rb +10 -0
- data/rails/public/favicon.ico +0 -0
- data/rails/public/images/rails.png +0 -0
- data/rails/public/index.html +277 -0
- data/rails/public/javascripts/application.js +2 -0
- data/rails/public/javascripts/controls.js +833 -0
- data/rails/public/javascripts/dragdrop.js +942 -0
- data/rails/public/javascripts/effects.js +1088 -0
- data/rails/public/javascripts/prototype.js +2515 -0
- data/rails/public/robots.txt +1 -0
- data/rails/script/about +3 -0
- data/rails/script/breakpointer +3 -0
- data/rails/script/console +3 -0
- data/rails/script/destroy +3 -0
- data/rails/script/generate +3 -0
- data/rails/script/performance/benchmarker +3 -0
- data/rails/script/performance/profiler +3 -0
- data/rails/script/plugin +3 -0
- data/rails/script/process/inspector +3 -0
- data/rails/script/process/reaper +3 -0
- data/rails/script/process/spawner +3 -0
- data/rails/script/runner +3 -0
- data/rails/script/server +3 -0
- data/rails/test/fixtures/groups.yml +3 -0
- data/rails/test/fixtures/inner_children.yml +2 -0
- data/rails/test/fixtures/nested_children.yml +2 -0
- data/rails/test/fixtures/nested_parents.yml +33 -0
- data/rails/test/fixtures/user_groups.yml +0 -0
- data/rails/test/fixtures/users.yml +20 -0
- data/rails/test/test_helper.rb +29 -0
- data/rails/test/unit/group_test.rb +10 -0
- data/rails/test/unit/inner_child_test.rb +10 -0
- data/rails/test/unit/nested_child_test.rb +10 -0
- data/rails/test/unit/nested_parent_test.rb +24 -0
- data/rails/test/unit/user_group_test.rb +10 -0
- data/rails/test/unit/user_test.rb +12 -0
- data/test/belongs_to_test.rb +77 -0
- data/test/config.yml +5 -0
- data/test/fixtures/as_children.yml +0 -0
- data/test/fixtures/bt_children.yml +0 -0
- data/test/fixtures/bt_parents.yml +30 -0
- data/test/fixtures/habtm_children.yml +0 -0
- data/test/fixtures/habtm_children_habtm_parents.yml +0 -0
- data/test/fixtures/habtm_joins.yml +0 -0
- data/test/fixtures/habtm_parents.yml +18 -0
- data/test/fixtures/hm_children.yml +0 -0
- data/test/fixtures/hm_joins.yml +0 -0
- data/test/fixtures/hm_parents.yml +34 -0
- data/test/fixtures/ho_children.yml +0 -0
- data/test/fixtures/ho_parents.yml +14 -0
- data/test/fixtures/inner_children.yml +2 -0
- data/test/fixtures/nested_children.yml +0 -0
- data/test/fixtures/nested_parents.yml +33 -0
- data/test/fixtures/no_join_fixes.yml +4 -0
- data/test/fixtures/omap_no_join_fixes.yml +7 -0
- data/test/fixtures/polymorphic_children.yml +0 -0
- data/test/has_and_belongs_to_many_test.rb +72 -0
- data/test/has_many_test.rb +97 -0
- data/test/has_one_test.rb +56 -0
- data/test/joinfix_test.rb +287 -0
- data/test/joinfix_test_helper.rb +54 -0
- data/test/joinfix_test_suite.rb +10 -0
- data/test/nested_test.rb +70 -0
- metadata +189 -0
@@ -0,0 +1,97 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/joinfix_test_helper'
|
2
|
+
|
3
|
+
class HmMigration < ActiveRecord::Migration
|
4
|
+
def self.up
|
5
|
+
create_table :hm_parents do |t|
|
6
|
+
t.column :field, :string
|
7
|
+
end
|
8
|
+
create_table :hm_children do |t|
|
9
|
+
t.column :field, :string
|
10
|
+
t.column :hm_parent_id, :integer
|
11
|
+
t.column :alt_id, :integer
|
12
|
+
end
|
13
|
+
create_table :hm_joins do |t|
|
14
|
+
t.column :hm_parent_id, :integer
|
15
|
+
t.column :hm_child_id, :integer
|
16
|
+
end
|
17
|
+
create_table :as_children do |t|
|
18
|
+
t.column :polymorphic_parent_id, :integer
|
19
|
+
t.column :polymorphic_parent_type, :string
|
20
|
+
t.column :field, :string
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.down
|
25
|
+
drop_table :hm_parents
|
26
|
+
drop_table :hm_children
|
27
|
+
drop_table :hm_joins
|
28
|
+
drop_table :as_children
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class HmParent < ActiveRecord::Base
|
33
|
+
has_many :hm_children
|
34
|
+
has_many :children,
|
35
|
+
:foreign_key => "alt_id",
|
36
|
+
:class_name => "HmChild"
|
37
|
+
|
38
|
+
has_many :hm_joins
|
39
|
+
has_many :join_children,
|
40
|
+
:through => :hm_joins,
|
41
|
+
:source => :hm_child
|
42
|
+
|
43
|
+
has_many :through_children,
|
44
|
+
:through => :hm_joins,
|
45
|
+
:source => :hm_child
|
46
|
+
|
47
|
+
has_many :as_children,
|
48
|
+
:as => :polymorphic_parent
|
49
|
+
end
|
50
|
+
|
51
|
+
class HmChild < ActiveRecord::Base
|
52
|
+
end
|
53
|
+
|
54
|
+
class HmJoin < ActiveRecord::Base
|
55
|
+
belongs_to :hm_child
|
56
|
+
end
|
57
|
+
|
58
|
+
class AsChild < ActiveRecord::Base
|
59
|
+
belongs_to :polymorphic_parent, :polymorphic => true
|
60
|
+
end
|
61
|
+
|
62
|
+
class HasManyTest < Test::Unit::TestCase
|
63
|
+
fixtures :hm_parents, :hm_children, :hm_joins, :as_children
|
64
|
+
|
65
|
+
def setup
|
66
|
+
end
|
67
|
+
|
68
|
+
def teardown
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_has_many
|
72
|
+
database_test(HmMigration) do
|
73
|
+
entry = hm_parents(:without_children)
|
74
|
+
assert_equal "without_children", entry.field
|
75
|
+
assert entry.hm_children.empty?
|
76
|
+
assert entry.children.empty?
|
77
|
+
assert entry.join_children.empty?
|
78
|
+
assert entry.as_children.empty?
|
79
|
+
|
80
|
+
entry = hm_parents(:with_hm_children)
|
81
|
+
assert_equal "with_hm_children", entry.field
|
82
|
+
assert_equal ["1","2"], entry.hm_children.collect {|e| e.field}.sort
|
83
|
+
|
84
|
+
entry = hm_parents(:with_children)
|
85
|
+
assert_equal "with_children", entry.field
|
86
|
+
assert_equal ["3","4"], entry.children.collect {|e| e.field}.sort
|
87
|
+
|
88
|
+
entry = hm_parents(:with_through_children)
|
89
|
+
assert_equal "with_through_children", entry.field
|
90
|
+
assert_equal ["5","6"], entry.join_children.collect {|e| e.field}.sort
|
91
|
+
|
92
|
+
entry = hm_parents(:with_as_children)
|
93
|
+
assert_equal "with_as_children", entry.field
|
94
|
+
assert_equal ["7","8"], entry.as_children.collect {|e| e.field}.sort
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/joinfix_test_helper'
|
2
|
+
|
3
|
+
class HoMigration < ActiveRecord::Migration
|
4
|
+
def self.up
|
5
|
+
create_table :ho_parents do |t|
|
6
|
+
t.column :field, :string
|
7
|
+
end
|
8
|
+
create_table :ho_children do |t|
|
9
|
+
t.column :field, :string
|
10
|
+
t.column :ho_parent_id, :integer
|
11
|
+
t.column :alt_id, :integer
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.down
|
16
|
+
drop_table :ho_parents
|
17
|
+
drop_table :ho_children
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class HoParent < ActiveRecord::Base
|
22
|
+
has_one :ho_child
|
23
|
+
has_one :child,
|
24
|
+
:foreign_key => "alt_id",
|
25
|
+
:class_name => "HoChild"
|
26
|
+
end
|
27
|
+
|
28
|
+
class HoChild < ActiveRecord::Base
|
29
|
+
end
|
30
|
+
|
31
|
+
class HasOneTest < Test::Unit::TestCase
|
32
|
+
fixtures :ho_parents, :ho_children
|
33
|
+
|
34
|
+
def setup
|
35
|
+
end
|
36
|
+
|
37
|
+
def teardown
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_has_one
|
41
|
+
database_test(HoMigration) do
|
42
|
+
entry = ho_parents(:without_child)
|
43
|
+
assert_equal "without_child", entry.field
|
44
|
+
assert_nil entry.ho_child
|
45
|
+
assert_nil entry.child
|
46
|
+
|
47
|
+
entry = ho_parents(:with_ho_child)
|
48
|
+
assert_equal "with_ho_child", entry.field
|
49
|
+
assert_equal "1", entry.ho_child.field
|
50
|
+
|
51
|
+
entry = ho_parents(:with_child)
|
52
|
+
assert_equal "with_child", entry.field
|
53
|
+
assert_equal "2", entry.child.field
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,287 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/joinfix_test_helper'
|
2
|
+
require 'joinfix'
|
3
|
+
|
4
|
+
class NoJoinFixMigration < ActiveRecord::Migration
|
5
|
+
def self.up
|
6
|
+
create_table :no_join_fixes do |t|
|
7
|
+
t.column :field, :string
|
8
|
+
end
|
9
|
+
create_table :omap_no_join_fixes do |t|
|
10
|
+
t.column :field, :string
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.down
|
15
|
+
drop_table :no_join_fixes
|
16
|
+
drop_table :omap_no_join_fixes
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class NoJoinFix < ActiveRecord::Base
|
21
|
+
end
|
22
|
+
|
23
|
+
class OmapNoJoinFix < ActiveRecord::Base
|
24
|
+
end
|
25
|
+
|
26
|
+
class JoinFixTest < Test::Unit::TestCase
|
27
|
+
fixtures :no_join_fixes, :omap_no_join_fixes
|
28
|
+
|
29
|
+
attr_reader :joinfix, :parent, :child
|
30
|
+
def setup
|
31
|
+
@joinfix = JoinFix.new("joinfix")
|
32
|
+
@parent = JoinFix.new("parent_entry", :name => "parent_name")
|
33
|
+
@child = JoinFix.new("child_entry", :name => "child_name")
|
34
|
+
end
|
35
|
+
|
36
|
+
def teardown
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# compatability tests
|
41
|
+
#
|
42
|
+
|
43
|
+
def test_fixtures_without_templates_are_unaffected
|
44
|
+
database_test(NoJoinFixMigration) do |connection|
|
45
|
+
assert_equal "1", no_join_fixes(:first).field
|
46
|
+
assert_equal "2", no_join_fixes(:second).field
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_omap_fixtures_without_templates_are_unaffected
|
51
|
+
database_test(NoJoinFixMigration) do |connection|
|
52
|
+
assert_equal 1, omap_no_join_fixes(:first).id
|
53
|
+
assert_equal 2, omap_no_join_fixes(:second).id
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
#
|
58
|
+
# preserved keys test
|
59
|
+
#
|
60
|
+
|
61
|
+
def test_id_keys_are_preserved
|
62
|
+
[:id, :preserved_id].each do |key|
|
63
|
+
assert JoinFix.preserves?(key)
|
64
|
+
assert JoinFix.preserves?(key.to_s)
|
65
|
+
end
|
66
|
+
|
67
|
+
[:not_preserved, :pid, "_id"].each do |key|
|
68
|
+
assert !JoinFix.preserves?(key)
|
69
|
+
assert !JoinFix.preserves?(key.to_s)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
#
|
74
|
+
# join tests
|
75
|
+
#
|
76
|
+
|
77
|
+
# Note that for all of these tests, the envisioned fixture looks like:
|
78
|
+
# parent_entry:
|
79
|
+
# name: parent_name
|
80
|
+
# association:
|
81
|
+
# child_entry:
|
82
|
+
# name: child_name
|
83
|
+
#
|
84
|
+
# This template produces the parent and child entries. The configurations may be derived from
|
85
|
+
# reflection on the given association for the parent table ActiveRecord::Base class.
|
86
|
+
#
|
87
|
+
|
88
|
+
# join_belongs_to tests
|
89
|
+
def test_join_belongs_to
|
90
|
+
assert_nil parent.join_belongs_to(child, :foreign_key => "foreign_key", :child_table => "child_table")
|
91
|
+
|
92
|
+
assert_equal({:name => "parent_name", ["foreign_key", "child_table"] => "child_entry"}, parent)
|
93
|
+
assert_equal({:name => "child_name"}, child)
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_join_belongs_to_when_polymorphic
|
97
|
+
assert_nil parent.join_belongs_to(child,
|
98
|
+
:polymorphic => true,
|
99
|
+
:associable => "associable",
|
100
|
+
:foreign_key => "associable",
|
101
|
+
:child_table => "child_table",
|
102
|
+
:child_class => "ChildTable" )
|
103
|
+
|
104
|
+
assert_equal({
|
105
|
+
:name => "parent_name",
|
106
|
+
["associable", "child_table"] => "child_entry",
|
107
|
+
"associable_type" => "ChildTable"}, parent)
|
108
|
+
assert_equal({:name => "child_name"}, child)
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_join_belongs_to_validates_inclusion_of_required_configurations
|
112
|
+
assert_raise(ArgumentError) { joinfix.join_belongs_to(nil, {}) }
|
113
|
+
assert_raise(ArgumentError) { joinfix.join_belongs_to(nil, :polymorphic => true) }
|
114
|
+
assert_raise(ArgumentError) { joinfix.join_belongs_to(nil, :child_table => "child_table") }
|
115
|
+
end
|
116
|
+
|
117
|
+
# join_has_one tests
|
118
|
+
def test_join_has_one
|
119
|
+
assert_nil parent.join_has_one(child,
|
120
|
+
:foreign_key => "foreign_key",
|
121
|
+
:parent_table => "parent_table")
|
122
|
+
|
123
|
+
assert_equal({:name => "parent_name"}, parent)
|
124
|
+
assert_equal({:name => "child_name", ["foreign_key", "parent_table"] => "parent_entry"}, child)
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_join_has_one_validates_inclusion_of_required_configurations
|
128
|
+
assert_raise(ArgumentError) { joinfix.join_has_one(nil, {}) }
|
129
|
+
assert_raise(ArgumentError) { joinfix.join_has_one(nil, :foreign_key => "foreign_key") }
|
130
|
+
assert_raise(ArgumentError) { joinfix.join_has_one(nil, :parent_table => "parent_table") }
|
131
|
+
end
|
132
|
+
|
133
|
+
# join_has_and_belongs_to_many tests
|
134
|
+
def test_join_has_and_belongs_to_many
|
135
|
+
join = parent.join_has_and_belongs_to_many(child,
|
136
|
+
:foreign_key => "foreign_key",
|
137
|
+
:parent_table => "parent_table",
|
138
|
+
:association_foreign_key => "association_foreign_key",
|
139
|
+
:child_table => "child_table")
|
140
|
+
|
141
|
+
assert_equal({:name => "parent_name"}, parent)
|
142
|
+
assert_equal({:name => "child_name"}, child)
|
143
|
+
assert_equal({["foreign_key", "parent_table"] => "parent_entry", ["association_foreign_key", "child_table"] => "child_entry"}, join)
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_join_has_and_belongs_to_many_with_attributes
|
147
|
+
attributes = {:some_attribute => "attribute"}
|
148
|
+
|
149
|
+
join = parent.join_has_and_belongs_to_many(child,
|
150
|
+
:foreign_key => "foreign_key",
|
151
|
+
:parent_table => "parent_table",
|
152
|
+
:association_foreign_key => "association_foreign_key",
|
153
|
+
:child_table => "child_table",
|
154
|
+
:attributes => attributes)
|
155
|
+
|
156
|
+
assert_equal({:name => "parent_name"}, parent)
|
157
|
+
assert_equal({:name => "child_name"}, child)
|
158
|
+
assert_equal(attributes.merge( ["foreign_key", "parent_table"] => "parent_entry", ["association_foreign_key", "child_table"] => "child_entry" ), join)
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_join_has_and_belongs_to_many_validates_inclusion_of_required_configurations
|
162
|
+
assert_raise(ArgumentError) { joinfix.join_has_and_belongs_to_many(nil, {}) }
|
163
|
+
assert_raise(ArgumentError) { joinfix.join_has_and_belongs_to_many(nil, :foreign_key => "foreign_key") }
|
164
|
+
assert_raise(ArgumentError) { joinfix.join_has_and_belongs_to_many(nil, :association_foreign_key => "parent_table") }
|
165
|
+
end
|
166
|
+
|
167
|
+
# join_has_many test
|
168
|
+
def test_join_has_many
|
169
|
+
assert_nil parent.join_has_many(child,
|
170
|
+
:foreign_key => "foreign_key",
|
171
|
+
:parent_table => "parent_table")
|
172
|
+
|
173
|
+
assert_equal({:name => "parent_name"}, parent)
|
174
|
+
assert_equal({:name => "child_name", ["foreign_key", "parent_table"] => "parent_entry"}, child)
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_join_has_many_as
|
178
|
+
assert_nil parent.join_has_many(child,
|
179
|
+
:as => "associable",
|
180
|
+
:foreign_key => "associable",
|
181
|
+
:parent_table => "parent_table",
|
182
|
+
:parent_class => "ParentTable")
|
183
|
+
|
184
|
+
assert_equal({:name => "parent_name"}, parent)
|
185
|
+
assert_equal({
|
186
|
+
:name => "child_name",
|
187
|
+
["associable", "parent_table"] => "parent_entry",
|
188
|
+
"associable_type" => "ParentTable"}, child)
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_join_has_many_through
|
192
|
+
join = parent.join_has_many(child,
|
193
|
+
:through => "some_join_table",
|
194
|
+
:foreign_key => "foreign_key",
|
195
|
+
:parent_table => "parent_table",
|
196
|
+
:association_foreign_key => "association_foreign_key",
|
197
|
+
:child_table => "child_table")
|
198
|
+
|
199
|
+
assert_equal({:name => "parent_name"}, parent)
|
200
|
+
assert_equal({:name => "child_name"}, child)
|
201
|
+
assert_equal({["foreign_key", "parent_table"] => "parent_entry", ["association_foreign_key", "child_table"] => "child_entry"}, join)
|
202
|
+
end
|
203
|
+
|
204
|
+
def test_join_has_many_through_with_attributes
|
205
|
+
attributes = {:some_attribute => "attribute"}
|
206
|
+
|
207
|
+
join = parent.join_has_many(child,
|
208
|
+
:through => "some_join_table",
|
209
|
+
:foreign_key => "foreign_key",
|
210
|
+
:parent_table => "parent_table",
|
211
|
+
:association_foreign_key => "association_foreign_key",
|
212
|
+
:child_table => "child_table",
|
213
|
+
:attributes => attributes)
|
214
|
+
|
215
|
+
assert_equal({:name => "parent_name"}, parent)
|
216
|
+
assert_equal({:name => "child_name"}, child)
|
217
|
+
assert_equal(attributes.merge(["foreign_key", "parent_table"] => "parent_entry", ["association_foreign_key", "child_table"] => "child_entry"), join)
|
218
|
+
end
|
219
|
+
|
220
|
+
#
|
221
|
+
# test extract_if
|
222
|
+
#
|
223
|
+
|
224
|
+
def test_extract_if_with_no_inputs
|
225
|
+
joinfix = JoinFix.new("extraction", :do_not_extract=> 'value')
|
226
|
+
|
227
|
+
extracted = joinfix.extract_if
|
228
|
+
|
229
|
+
# in this case there are no keys, so no pairs are extracted
|
230
|
+
assert_equal({}, extracted)
|
231
|
+
assert_equal({:do_not_extract => 'value'}, joinfix)
|
232
|
+
end
|
233
|
+
|
234
|
+
def test_extract_if_using_allowed_keys
|
235
|
+
joinfix = JoinFix.new("extraction", :do_not_extract => 'value', :extract => 'value')
|
236
|
+
|
237
|
+
extracted = joinfix.extract_if :extract
|
238
|
+
|
239
|
+
assert_equal({:extract=> 'value'}, extracted)
|
240
|
+
assert_equal({:do_not_extract => 'value'}, joinfix)
|
241
|
+
end
|
242
|
+
|
243
|
+
def test_extract_if_using_block
|
244
|
+
joinfix = JoinFix.new("extraction", :do_not_extract => 'value', :extract => 'value')
|
245
|
+
|
246
|
+
extracted = joinfix.extract_if do |key, value|
|
247
|
+
key == :extract
|
248
|
+
end
|
249
|
+
|
250
|
+
assert_equal({:extract => 'value'}, extracted)
|
251
|
+
assert_equal({:do_not_extract => 'value'}, joinfix)
|
252
|
+
end
|
253
|
+
|
254
|
+
#
|
255
|
+
# test extract_unless
|
256
|
+
#
|
257
|
+
|
258
|
+
def test_extract_unless_with_no_inputs
|
259
|
+
joinfix = JoinFix.new("extraction", :extract=> 'value')
|
260
|
+
|
261
|
+
extracted = joinfix.extract_unless
|
262
|
+
|
263
|
+
# in this case there are no allowed keys, so all pairs are extracted
|
264
|
+
assert_equal({:extract => 'value'}, extracted)
|
265
|
+
assert_equal({}, joinfix)
|
266
|
+
end
|
267
|
+
|
268
|
+
def test_extract_unless_using_allowed_keys
|
269
|
+
joinfix = JoinFix.new("extraction", :do_not_extract => 'value', :extract => 'value')
|
270
|
+
|
271
|
+
extracted = joinfix.extract_unless :do_not_extract
|
272
|
+
|
273
|
+
assert_equal({:extract=> 'value'}, extracted)
|
274
|
+
assert_equal({:do_not_extract => 'value'}, joinfix)
|
275
|
+
end
|
276
|
+
|
277
|
+
def test_extract_unless_using_block
|
278
|
+
joinfix = JoinFix.new("extraction", :do_not_extract => 'value', :extract => 'value')
|
279
|
+
|
280
|
+
extracted = joinfix.extract_unless do |key, value|
|
281
|
+
key == :do_not_extract
|
282
|
+
end
|
283
|
+
|
284
|
+
assert_equal({:extract => 'value'}, extracted)
|
285
|
+
assert_equal({:do_not_extract => 'value'}, joinfix)
|
286
|
+
end
|
287
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'gemdev'
|
2
|
+
require 'test/unit/subsets'
|
3
|
+
require 'active_record'
|
4
|
+
require 'active_record/fixtures'
|
5
|
+
require 'joinfix'
|
6
|
+
require 'tempfile'
|
7
|
+
|
8
|
+
# Try to extablish a connection to the configured database
|
9
|
+
# if this doesn't work, then none of the database_tests will run
|
10
|
+
config = YAML.load_file(File.dirname(__FILE__) + '/config.yml').symbolize_keys
|
11
|
+
ActiveRecord::Base.establish_connection(config)
|
12
|
+
begin
|
13
|
+
ActiveRecord::Base.connection
|
14
|
+
|
15
|
+
# Not sure what these configurations should point to, but
|
16
|
+
# setup_with_fixtures requires that configurations is not empty
|
17
|
+
# in order to run, so...
|
18
|
+
ActiveRecord::Base.configurations["joinfix"] = config
|
19
|
+
rescue
|
20
|
+
puts "ActiveRecord connection could not be established using:"
|
21
|
+
pp config
|
22
|
+
end
|
23
|
+
|
24
|
+
class Test::Unit::TestCase
|
25
|
+
# With the setup in fixtures, I had to override setup and teardown so
|
26
|
+
# that by default, the setup_with_fixtures method is not called
|
27
|
+
#
|
28
|
+
# I do want to call setup_with_fixtures, but only in the context
|
29
|
+
# of database test
|
30
|
+
|
31
|
+
def setup
|
32
|
+
end
|
33
|
+
|
34
|
+
def teardown
|
35
|
+
end
|
36
|
+
|
37
|
+
def database_test(*migrations, &block)
|
38
|
+
switch_test( ActiveRecord::Base.connected? ) do
|
39
|
+
ActiveRecord::Migration.verbose = false
|
40
|
+
|
41
|
+
begin
|
42
|
+
migrations.each {|migration| migration.up}
|
43
|
+
setup_with_fixtures
|
44
|
+
yield(ActiveRecord::Base.connection)
|
45
|
+
ensure
|
46
|
+
teardown_with_fixtures
|
47
|
+
migrations.each {|migration| migration.down}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# set the fixture path
|
54
|
+
Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures"
|
@@ -0,0 +1,10 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), '../lib')
|
2
|
+
|
3
|
+
puts %{
|
4
|
+
NOTE! The joinfix tests are not run through this test suite
|
5
|
+
because they require a database to connect to. See TEST_README
|
6
|
+
for details and instructions on running the joinfix tests.
|
7
|
+
}
|
8
|
+
#
|
9
|
+
#ENV["ALL"] = 'true'
|
10
|
+
#Dir.glob("./**/*_test.rb").each {|test| require test}
|
data/test/nested_test.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/joinfix_test_helper'
|
2
|
+
|
3
|
+
class NestedMigration < ActiveRecord::Migration
|
4
|
+
def self.up
|
5
|
+
create_table :nested_parents do |t|
|
6
|
+
t.column :field, :string
|
7
|
+
t.column :inner_child_id, :integer
|
8
|
+
end
|
9
|
+
create_table :nested_children do |t|
|
10
|
+
t.column :field, :string
|
11
|
+
t.column :nested_parent_id, :integer
|
12
|
+
end
|
13
|
+
create_table :inner_children do |t|
|
14
|
+
t.column :field, :string
|
15
|
+
t.column :nested_child_id, :integer
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.down
|
20
|
+
drop_table :nested_parents
|
21
|
+
drop_table :nested_children
|
22
|
+
drop_table :inner_children
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class NestedParent < ActiveRecord::Base
|
27
|
+
belongs_to :inner_child
|
28
|
+
has_many :nested_children
|
29
|
+
end
|
30
|
+
|
31
|
+
class NestedChild < ActiveRecord::Base
|
32
|
+
belongs_to :nested_parent
|
33
|
+
has_many :inner_children
|
34
|
+
end
|
35
|
+
|
36
|
+
class InnerChild < ActiveRecord::Base
|
37
|
+
belongs_to :nested_child
|
38
|
+
has_many :nested_parents
|
39
|
+
end
|
40
|
+
|
41
|
+
class NestedTest < Test::Unit::TestCase
|
42
|
+
fixtures :nested_parents, :nested_children, :inner_children
|
43
|
+
|
44
|
+
def setup
|
45
|
+
end
|
46
|
+
|
47
|
+
def teardown
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_nested_fixtures
|
51
|
+
database_test(NestedMigration) do
|
52
|
+
parent_one= nested_parents(:parent_one)
|
53
|
+
parent_two = nested_parents(:parent_two)
|
54
|
+
parent_three = nested_parents(:parent_three)
|
55
|
+
parent_four = nested_parents(:parent_four)
|
56
|
+
|
57
|
+
query_result = NestedChild.find_by_field(1).nested_parent
|
58
|
+
assert_equal parent_one, query_result
|
59
|
+
|
60
|
+
query_result = NestedParent.find_by_field(1).nested_children.first.inner_children.first.nested_parents
|
61
|
+
assert_equal [parent_two, parent_three], query_result
|
62
|
+
|
63
|
+
query_result = NestedParent.find_by_field(1).inner_child.nested_child.nested_parent
|
64
|
+
assert_equal parent_two, query_result
|
65
|
+
|
66
|
+
query_result = InnerChild.find_by_field(3).nested_parents.first
|
67
|
+
assert_equal parent_four, query_result
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|