sequel 1.5.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,113 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "spec_helper")
2
-
3
- describe Sequel::Model, "one_to_one" do
4
-
5
- before(:each) do
6
- MODEL_DB.reset
7
-
8
- @c1 = Class.new(Sequel::Model(:attributes)) do
9
- end
10
-
11
- @c2 = Class.new(Sequel::Model(:nodes)) do
12
- columns :id, :parent_id, :blah
13
- end
14
-
15
- @dataset = @c2.dataset
16
-
17
- @dataset.extend(Module.new {
18
- def fetch_rows(sql)
19
- @db << sql
20
- yield({:hey => 1})
21
- end
22
- })
23
- end
24
-
25
- it "should use implicit key if omitted" do
26
- @c2.one_to_one :parent, :from => @c2
27
-
28
- d = @c2.new(:id => 1, :parent_id => 234)
29
- p = d.parent
30
- p.class.should == @c2
31
- p.values.should == {:hey => 1}
32
-
33
- MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE (id = 234) LIMIT 1"]
34
- end
35
-
36
- it "should use explicit key if given" do
37
- @c2.one_to_one :parent, :from => @c2, :key => :blah
38
-
39
- d = @c2.new(:id => 1, :blah => 567)
40
- p = d.parent
41
- p.class.should == @c2
42
- p.values.should == {:hey => 1}
43
-
44
- MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE (id = 567) LIMIT 1"]
45
- end
46
-
47
- it "should return nil if key value is nil" do
48
- @c2.one_to_one :parent, :from => @c2
49
-
50
- d = @c2.new(:id => 1)
51
- d.parent.should == nil
52
- end
53
-
54
- it "should define a setter method" do
55
- @c2.one_to_one :parent, :from => @c2
56
-
57
- d = @c2.load(:id => 1)
58
- d.parent = @c2.new(:id => 4321)
59
- d.values.should == {:id => 1, :parent_id => 4321}
60
- d.save_changes
61
- MODEL_DB.sqls.last.should == "UPDATE nodes SET parent_id = 4321 WHERE (id = 1)"
62
-
63
- d.parent = nil
64
- d.values.should == {:id => 1, :parent_id => nil}
65
- d.save_changes
66
- MODEL_DB.sqls.last.should == "UPDATE nodes SET parent_id = NULL WHERE (id = 1)"
67
-
68
- e = @c2.new(:id => 6677)
69
- d.parent = e
70
- d.values.should == {:id => 1, :parent_id => 6677}
71
- d.save_changes
72
- MODEL_DB.sqls.last.should == "UPDATE nodes SET parent_id = 6677 WHERE (id = 1)"
73
- end
74
- end
75
-
76
- describe Sequel::Model, "one_to_many" do
77
-
78
- before(:each) do
79
- MODEL_DB.reset
80
-
81
- @c1 = Class.new(Sequel::Model(:attributes)) do
82
- end
83
-
84
- @c2 = Class.new(Sequel::Model(:nodes)) do
85
- columns :id
86
- end
87
- end
88
-
89
- it "should define a getter method" do
90
- @c2.one_to_many :attributes, :from => @c1, :key => :node_id
91
-
92
- n = @c2.new(:id => 1234)
93
- a = n.attributes_dataset
94
- a.should be_a_kind_of(Sequel::Dataset)
95
- a.sql.should == 'SELECT * FROM attributes WHERE (node_id = 1234)'
96
- end
97
-
98
- it "should support implicit key names" do
99
- $c1 = @c1
100
-
101
- module Music
102
- class BlueNote < Sequel::Model
103
- one_to_many :attributes, :from => $c1
104
- columns :id
105
- end
106
- end
107
-
108
- n = Music::BlueNote.new(:id => 1234)
109
- a = n.attributes_dataset
110
- a.should be_a_kind_of(Sequel::Dataset)
111
- a.sql.should == 'SELECT * FROM attributes WHERE (blue_note_id = 1234)'
112
- end
113
- end