-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase_spec.rb
More file actions
177 lines (143 loc) · 4.35 KB
/
base_spec.rb
File metadata and controls
177 lines (143 loc) · 4.35 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
require 'spec_helper'
describe RubyJsonApiClient::Base do
class Person < RubyJsonApiClient::Base
field :firstname, :string
validates :firstname, presence: true
end
describe :save do
it "should return false when errors" do
person = Person.new
end
end
describe :field do
it "should setup attributes for the model" do
person = Person.new
person.firstname = 'ryan'
expect(person.firstname).to eq 'ryan'
end
context "that has validations" do
context "that fail" do
subject { Person.new.valid? }
it { should eq(false) }
end
context "that pass" do
subject { Person.new(firstname: 'ryan').valid? }
it { should eq(true) }
end
it "should prevent creating invalid records" do
person = Person.create({})
expect(person.persisted?).to eq false
expect(person.errors).to be_present
expect(person.valid?).to eq false
end
it "should prevent saving invalid records" do
person = Person.new({})
ret = person.save
expect(ret).to eq false
expect(person.persisted?).to eq false
expect(person.errors).to be_present
expect(person.valid?).to eq false
end
end
end
describe :remote_class do
end
describe :has_field? do
context "a class with no fields" do
subject { Nothing.has_field?(:nope) }
it { should eq(false) }
end
context "a class with fields that has the field" do
subject { Item.has_field?(:name) }
it { should eq(true) }
end
end
describe :identifer do
it "should default to id" do
expect(Person._identifier).to eq :id
end
it "should be able to change the identifier" do
expect(Thing._identifier).to eq :uuid
end
end
describe :belongs_to do
end
describe :has_many do
end
describe :persisted? do
it "should be persisted if it has an id" do
person = Person.new
person.instance_variable_set(:@id, 1)
expect(person.persisted?).to eql(true)
end
it "should not be persisted if there is no id" do
expect(Person.new.persisted?).to eql(false)
end
end
describe :new_record? do
it "should not be a new record if it has an id" do
person = Person.new
person.instance_variable_set(:@id, 1)
expect(person.new_record?).to eql(false)
end
it "should be a new record if there is no id" do
expect(Person.new.new_record?).to eql(true)
end
end
describe :marked_for_destruction? do
it "should return false by default" do
expect(!!Person.new.marked_for_destruction?).to eql(false)
end
end
describe :_destroy do
it "should return false by default" do
expect(!!Person.new._destroy).to eql(false)
end
end
describe :== do
context "two objects of different classes" do
subject { Person.new(id: 1) == Item.new(id: 1) }
it { should eq(false) }
end
context "two objects of the same class but different ids" do
subject { Person.new(id: 1) == Person.new(id: 2) }
it { should eq(false) }
end
context "two objects with the same klass and id" do
subject { Person.new(id: 1) == Person.new(id: 1) }
it { should eq(true) }
end
context "the same instance" do
let(:person) { Person.new(id: 1) }
subject { person == person }
it { should eq(true) }
end
context "two objects with non standard identifiers" do
subject { Thing.new(uuid: 'x') == Thing.new(uuid: 'x') }
it { should eq(true) }
end
end
describe :hash do
context "two objects of different classes" do
subject { Person.new(id: 1).hash == Item.new(id: 1).hash }
it { should eq(true) }
end
context "two objects of the same class but different ids" do
subject { Person.new(id: 1).hash == Person.new(id: 2).hash }
it { should eq(false) }
end
context "two objects with the same klass and id" do
subject { Person.new(id: 1).hash == Person.new(id: 1).hash }
it { should eq(true) }
end
context "the same instance" do
let(:person) { Person.new(id: 1) }
subject { person.hash == person.hash }
it { should eq(true) }
end
context "two objects with non standard identifiers" do
subject { Thing.new(uuid: 'x').hash == Thing.new(uuid: 'x').hash }
it { should eq(true) }
end
end
end