forked from amberframework/granite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_spec.cr
138 lines (113 loc) · 3.12 KB
/
save_spec.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require "../../spec_helper"
describe "#save" do
it "creates a new object" do
parent = Parent.new
parent.name = "Test Parent"
parent.save
parent.persisted?.should be_true
end
it "does not create an invalid object" do
parent = Parent.new
parent.name = ""
parent.save
parent.persisted?.should be_false
end
it "updates an existing object" do
Parent.clear
parent = Parent.new
parent.name = "Test Parent"
parent.save
parent.name = "Test Parent 2"
parent.save
parents = Parent.all
parents.size.should eq 1
found = Parent.first!
found.name.should eq parent.name
end
it "does not update an invalid object" do
parent = Parent.new
parent.name = "Test Parent"
parent.save
parent.name = ""
parent.save
parent = Parent.find! parent.id
parent.name.should eq "Test Parent"
end
describe "with a custom primary key" do
it "creates a new object" do
school = School.new
school.name = "Test School"
school.save
school.custom_id.should_not be_nil
end
it "updates an existing object" do
old_name = "Test School 1"
new_name = "Test School 2"
school = School.new
school.name = old_name
school.save
primary_key = school.custom_id
school.name = new_name
school.save
found_school = School.find! primary_key
found_school.custom_id.should eq primary_key
found_school.name.should eq new_name
end
it "updates states of new_record and persisted" do
parent = Parent.new
parent.new_record?.should be_true
parent.persisted?.should be_false
parent.name = "Test Parent"
parent.save
parent.new_record?.should be_false
parent.persisted?.should be_true
end
end
describe "with a modulized model" do
it "creates a new object" do
county = Nation::County.new
county.name = "Test School"
county.save
county.persisted?.should be_true
end
it "updates an existing object" do
old_name = "Test County 1"
new_name = "Test County 2"
county = Nation::County.new
county.name = old_name
county.save
primary_key = county.id
county.name = new_name
county.save
found_county = Nation::County.find! primary_key
found_county.name.should eq new_name
end
end
describe "using a reserved word as a column name" do
# `all` is a reserved word in almost RDB like MySQL, PostgreSQL
it "creates and updates" do
reserved_word = ReservedWord.new
reserved_word.all = "foo"
reserved_word.save
reserved_word.errors.empty?.should be_true
reserved_word.all = "bar"
reserved_word.save
reserved_word.errors.empty?.should be_true
reserved_word.all.should eq("bar")
end
end
end
describe "#save!" do
it "creates a new object" do
parent = Parent.new
parent.name = "Test Parent"
parent.save!
parent.persisted?.should be_true
end
it "does not create but raise an exception" do
parent = Parent.new
expect_raises(Granite::RecordNotSaved, "Parent") do
parent.save!
end
end
end