Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 241 lines (212 sloc) 6.314 kb
00a7f6d @jeremyevans Add specs
authored
1 require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper')
2
3 describe FixtureDependencies do
4 def load(*a) FixtureDependencies.load(*a) end
5 def verbose(i=3)
6 v = FixtureDependencies.verbose
7 FixtureDependencies.verbose = i
8 yield
9 ensure
10 FixtureDependencies.verbose = v
11 end
12
ac1b8a1 @jeremyevans Add composite key support when using Sequel
authored
13 def log
14 DB.loggers << Logger.new($stderr)
15 yield
16 ensure
17 DB.logger = nil
18 end
19
00a7f6d @jeremyevans Add specs
authored
20 after do
21 # Clear tables and fixture_dependencies caches
ac1b8a1 @jeremyevans Add composite key support when using Sequel
authored
22 [:com_self_refs, :com_albums_com_tags, :com_tags, :com_albums, :com_artists, :self_refs, :albums_tags, :tags, :albums, :artists].each{|x| DB[x].delete}
00a7f6d @jeremyevans Add specs
authored
23 FixtureDependencies.loaded.clear
24 FixtureDependencies.fixtures.clear
25 end
26
27 it "should load single records with underscore syntax" do
ac1b8a1 @jeremyevans Add composite key support when using Sequel
authored
28 ym = load(:artist__ym)
00a7f6d @jeremyevans Add specs
authored
29 ym.id.should == 1
30 ym.name.should == 'YM'
31 end
32
33 it "should load multiple records with underscore syntax and multiple arguments" do
34 rf, mo = load(:album__rf, :album__mo)
35 rf.id.should == 1
36 rf.name.should == 'RF'
37 mo.id.should == 2
38 mo.name.should == 'MO'
39 end
40
41 it "should load multiple records with hash syntax" do
42 rf, mo = load(:albums=>[:rf, :mo])
43 rf.id.should == 1
44 rf.name.should == 'RF'
45 mo.id.should == 2
46 mo.name.should == 'MO'
47 end
48
49 it "should load multiple records with a mix a hashes and symbols" do
50 rf, mo = load(:album__rf, :albums=>[:mo])
51 rf.id.should == 1
52 rf.name.should == 'RF'
53 mo.id.should == 2
54 mo.name.should == 'MO'
55 end
56
57 it "should load whole tables at once with single symbol" do
58 Artist.count.should == 0
59 load(:artists)
60 Artist.count.should == 2
61 end
62
63 it "should load whole tables at once with single symbol" do
64 Artist.count.should == 0
65 Album.count.should == 0
66 load(:artists, :albums)
67 Artist.count.should == 2
68 Album.count.should == 3
69 end
70
71 it "should load associated many_to_one records" do
72 rf = load(:album__rf)
73 rf.artist.id.should == 1
74 end
75
76 it "should load associated one_to_many records" do
77 nu = load(:artist__nu)
78 nu.albums.length.should == 1
79 nu.albums.first.id.should == 3
80 nu.albums.first.name.should == 'P'
81 end
82
83 it "should load associated many_to_many records and handle cycles (I->P->NU->P)" do
84 i = load(:tag__i)
b0fbefe @jeremyevans Actually test a to_many association with multiple associated objects
authored
85 i.albums.length.should == 2
00a7f6d @jeremyevans Add specs
authored
86 p = i.albums.first
87 p.id.should == 3
88 p.name.should == 'P'
b0fbefe @jeremyevans Actually test a to_many association with multiple associated objects
authored
89 mo = i.albums.last
90 mo.id.should == 2
91 mo.name.should == 'MO'
00a7f6d @jeremyevans Add specs
authored
92 nu = p.artist
93 nu.id.should == 2
94 nu.name.should == 'NU'
95 nu.albums.length.should == 1
96 nu.albums.first.should == p
97 end
98
99 it "should not load records that were not asked for" do
100 ym = load(:artist__ym)
101 Artist.count.should == 1
102 Artist.first.should == ym
103 load(:artist__nu)
104 Artist.count.should == 2
105 end
106
107 it "should handle self referential records" do
108 i = load(:self_ref__i)
109 i.id.should == 1
110 i.self_ref.id.should == 1
111 i.self_refs.length.should == 1
112 i.self_refs.first.id.should == 1
113 end
114
115 it "should handle association cycles" do
116 a = load(:self_ref__a)
117 a.id.should == 2
118 b = a.self_ref
119 b.id.should == 3
120 c = b.self_ref
121 c.id.should == 4
122 c.self_ref.should == a
123 end
124
125 it "should raise error nonexistent fixture files" do
126 class Foo < Sequel::Model; end
127 proc{load(:foos)}.should raise_error(ArgumentError)
128 end
129
130 it "should raise error for unsupported model classes" do
131 class Bar; def self.table_name() :bars end end
132 proc{load(:bars)}.should raise_error(TypeError)
133 end
ac1b8a1 @jeremyevans Add composite key support when using Sequel
authored
134
135 unless ENV['FD_AR']
136 it "should load single records with underscore syntax" do
137 ym = load(:com_artist__ym)
138 ym.id1.should == 1
139 ym.id2.should == 2
140 ym.name.should == 'YM'
141 end
142
143 it "should load multiple records with underscore syntax and multiple arguments" do
144 rf, mo = load(:com_album__rf, :com_album__mo)
145 rf.id1.should == 1
146 rf.id2.should == 2
147 rf.name.should == 'RF'
148 mo.id1.should == 3
149 mo.id2.should == 4
150 mo.name.should == 'MO'
151 end
152
153 it "should load multiple records with hash syntax" do
154 rf, mo = load(:com_albums=>[:rf, :mo])
155 rf.id1.should == 1
156 rf.id2.should == 2
157 rf.name.should == 'RF'
158 mo.id1.should == 3
159 mo.id2.should == 4
160 mo.name.should == 'MO'
161 end
162
163 it "should load multiple records with a mix a hashes and symbols" do
164 rf, mo = load(:com_album__rf, :com_albums=>[:mo])
165 rf.id1.should == 1
166 rf.id2.should == 2
167 rf.name.should == 'RF'
168 mo.id1.should == 3
169 mo.id2.should == 4
170 mo.name.should == 'MO'
171 end
172
173 it "should load whole tables at once with single symbol" do
174 ComArtist.count.should == 0
175 load(:com_artists)
176 ComArtist.count.should == 2
177 end
178
179 it "should load whole tables at once with single symbol" do
180 ComArtist.count.should == 0
181 ComAlbum.count.should == 0
182 load(:com_artists, :com_albums)
183 ComArtist.count.should == 2
184 ComAlbum.count.should == 3
185 end
186
187 it "should load associated many_to_one records" do
188 rf = load(:com_album__rf)
189 rf.artist.id1.should == 1
190 rf.artist.id2.should == 2
191 end
192
193 it "should load associated one_to_many records" do
194 nu = load(:com_artist__nu)
195 nu.albums.length.should == 1
196 nu.albums.first.id1.should == 5
197 nu.albums.first.id2.should == 6
198 nu.albums.first.name.should == 'P'
199 end
200
201 it "should load associated many_to_many records and handle cycles (I->P->NU->P)" do
202 i = load(:com_tag__i)
203 i.albums.length.should == 1
204 p = i.albums.first
205 p.id1.should == 5
206 p.id2.should == 6
207 p.name.should == 'P'
208 nu = p.artist
209 nu.id1.should == 3
210 nu.id2.should == 4
211 nu.name.should == 'NU'
212 nu.albums.length.should == 1
213 nu.albums.first.should == p
214 end
215
216 it "should handle self referential records" do
217 i = load(:com_self_ref__i)
218 i.id1.should == 1
219 i.id2.should == 2
220 i.self_ref.id1.should == 1
221 i.self_ref.id2.should == 2
222 i.self_refs.length.should == 1
223 i.self_refs.first.id1.should == 1
224 i.self_refs.first.id2.should == 2
225 end
226
227 it "should composite and associated primary keys" do
228 a = load(:com_self_ref__a)
229 a.id1.should == 3
230 a.id2.should == 4
231 b = a.self_ref
232 b.id1.should == 5
233 b.id2.should == 6
234 c = b.self_ref
235 c.id1.should == 7
236 c.id2.should == 8
237 c.self_ref.should == a
238 end
239 end
00a7f6d @jeremyevans Add specs
authored
240 end
Something went wrong with that request. Please try again.