1 require File.dirname(__FILE__) + '/../test_helper'
3 require 'doozer/alt_definition'
7 class AltDefinitionTest < Test::Unit::TestCase
13 should "generate fixture definition data structure" do # ---------------
25 expected_structure = {
31 :block_affects=>[{:child=>:belongs_to}]
35 :default_values=>{:title=>"My Title"},
36 :block_affects=>[{:child=>:belongs_to}],
37 :field_order=>[:title, :content, :author],
43 assert_equal expected_structure, Doozer.object_list
46 should "generate helper methods" do # ----------------------------------
50 assert_not_nil defined?(page)
51 assert_not_nil defined?(page_id)
52 assert_not_nil defined?(page_name)
53 assert_not_nil defined?(pages)
57 assert_not_nil defined?(person)
58 assert_not_nil defined?(person_id)
59 assert_not_nil defined?(person_name)
60 assert_not_nil defined?(people)
63 should "return next object id" do # ------------------------------------
67 assert_equal 1, Doozer.next_object_id( :user )
68 user :bob, :name=>'Bob'
69 assert_equal 2, Doozer.next_object_id( :user )
72 should "add objects to data pool from named arguments" do # ------------
76 user :bob, :name=>'Bob'
77 assert_equal 1, user_id(:bob)
78 assert_equal "Bob", users(:bob)[:name]
83 assert_equal 3, user_id(:user_3)
84 assert_equal 'Dan', users(:user_3)[:name]
87 should "add objects to data pool from positional arguments" do # -------
94 assert_equal 1, user_id(:bob)
95 assert_equal "Bob", users(:bob)[:name]
100 assert_equal 3, user_id(:user_3)
101 assert_equal 'Dan', users(:user_3)[:name]
104 should "add default values to data objects" do # -----------------------
105 created_on_time = Time.now
112 created_on created_on_time
119 page "Stuff", "Junk", "And more!"
121 assert_equal 3, Doozer.object_list[:page][:data_rows].keys.length
122 assert_equal created_on_time, pages(:page_1)[:created_on]
123 assert_equal "Slugline", pages(:page_1)[:slug]
124 assert_equal "A beginning", pages(:page_1)[:title]
127 should "allow multiple updates to a single named fixture" do # ---------
138 assert_equal 1, cars(:fast)[:id]
139 assert_equal 'CAR', cars(:fast)[:name]
140 assert_equal 'TYPE', cars(:fast)[:type]
141 assert_equal 'YEAR', cars(:fast)[:year]
143 car :fast, 'Corvette'
144 car :fast, :type=>'Sportscar'
145 car :fast, :year=>2002
147 assert_equal 'Corvette', cars(:fast)[:name]
148 assert_equal 'Sportscar', cars(:fast)[:type]
149 assert_equal 2002, cars(:fast)[:year]
152 # Last one in should win
154 assert_equal 'Tiburon', cars(:fast)[:name]
156 # Positional params 'beat' named params
157 car :fast, 'Viper', :name=>'Porche'
158 assert_equal 'Viper', cars(:fast)[:name]
160 # Shouldn't wind up with multiple :fast keys in object list
161 assert_equal [:fast], Doozer.object_list[:car][:row_names]
164 should "be able to override the target AR class" do # ------------------
167 target 'Comatose::Page'
170 assert_equal 'Comatose::Page', Doozer.object_list[:page][:target]
174 should "support block association definition" do # ---------------------
178 children :act_as_tree
181 assert_equal Doozer.object_list[:node][:block_affects], [{:child=>:parent_id}]
184 should "correctly nest fixtures" do # ----------------------------------
188 children :act_as_tree
192 node "First Child" do
193 assert_equal 2, Doozer.object_stack.length
200 assert_equal 1, nodes(:node_2)[:parent_id]
201 assert_equal 2, nodes(:node_3)[:parent_id]
202 assert_equal 2, nodes(:node_4)[:parent_id]
205 should "allow macro aliases" do # --------------------------------------
206 doozer :node, as=>:n do
208 children :act_as_tree
211 assert_not_nil defined?(n)
212 assert_not_nil defined?(n_id)
213 assert_not_nil defined?(ns)
223 assert_equal 1, ns(:node_2)[:parent_id]
224 assert_equal 2, ns(:node_3)[:parent_id]
225 assert_equal 2, ns(:node_4)[:parent_id]
228 should "allow alternate block syntax" do # -----------------------------
229 doozer(:node, as=>:n){
231 children :act_as_tree
242 assert_equal 1, ns(:node_2)[:parent_id]
243 assert_equal 2, ns(:node_3)[:parent_id]
244 assert_equal 2, ns(:node_4)[:parent_id]
246 expected_row_data = {
247 :node_1=>{:name=>"Root Node", :id=>1},
248 :node_2=>{:parent_id=>1, :name=>"First Child", :id=>2},
249 :node_3=>{:parent_id=>2, :name=>"Sub Child 1", :id=>3},
250 :node_4=>{:parent_id=>2, :name=>"Sub Child 2", :id=>4},
251 :node_5=>{:parent_id=>1, :name=>"Second Child", :id=>5}
254 assert_equal expected_row_data, Doozer.object_list[:node][:data_rows]
257 should "allow `belongs_to` relationships" do # --------------------------
264 fields { name; email }
269 # Use the default block_affects, which should be `block_affects :child=>:belongs_to`
274 page :welcome, 'Welcome' do
275 user :matt, 'Matt' do
276 comment :matt_first_comment, 'The beginning'
280 assert_equal user_id(:matt), pages(:welcome)[:user_id]
281 assert_equal user_id(:matt), comments(:matt_first_comment)[:user_id]
284 should "remove macro names on `reset!`" do # ---------------------------
287 assert_not_nil defined?(page)
288 assert_not_nil defined?(page_id)
289 assert_not_nil defined?(page_name)
290 assert_not_nil defined?(pages)
294 assert_nil defined?(page)
295 assert_nil defined?(page_id)
296 assert_nil defined?(page_name)
297 assert_nil defined?(pages)
300 should "allow access to the fixture name and data and from `ID`" do # --
305 page :one, "Page One"
306 page :two, "Page Two"
307 page :three, "Page Three"
309 assert_equal :one, page_name( page_id(:one) )
310 assert_equal :two, page_name( page_id(:two) )
312 assert_equal 'Page One', pages( page_id(:one) )[:title]
313 assert_equal 'Page Three', pages( page_id(:three) )[:title]