active_remote 5.1.1 → 5.2.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,223 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe ::ActiveRemote::Attributes do
4
- let(:model_class) do
5
- ::Class.new do
6
- include ::ActiveRemote::Attributes
7
-
8
- attribute :name
9
- attribute :address
10
-
11
- def self.name
12
- "TestClass"
13
- end
14
- end
15
- end
16
- subject { ::Author.new }
17
-
18
- describe "type casting" do
19
- let(:test_class) { ::TypecastedAuthor }
20
-
21
- describe "boolean" do
22
- it "casts to boolean" do
23
- record = test_class.new(:writes_fiction => "f")
24
- expect(record.writes_fiction).to eq(false)
25
- end
26
- end
27
-
28
- describe "datetime" do
29
- it "casts to datetime" do
30
- record = test_class.new(:birthday => "2016-01-01")
31
- expect(record.birthday).to eq(DateTime.parse("2016-01-01"))
32
- end
33
-
34
- context "invalid date" do
35
- it "sets attribute to nil" do
36
- record = test_class.new(:birthday => "23451234")
37
- expect(record.birthday).to be_nil
38
- end
39
- end
40
- end
41
-
42
- describe "float" do
43
- it "casts to float" do
44
- record = test_class.new(:net_sales => "2000.20")
45
- expect(record.net_sales).to eq(2000.2)
46
- end
47
- end
48
-
49
- describe "integer" do
50
- it "casts to integer" do
51
- record = test_class.new(:age => "40")
52
- expect(record.age).to eq(40)
53
- end
54
- end
55
-
56
- describe "string" do
57
- it "casts to string" do
58
- record = test_class.new(:guid => 1000)
59
- expect(record.guid).to eq("1000")
60
- end
61
- end
62
- end
63
-
64
- describe ".attribute" do
65
- context "a dangerous attribute" do
66
- it "raises an error" do
67
- expect { model_class.attribute(:timeout) }.to raise_error(::ActiveRemote::DangerousAttributeError)
68
- end
69
- end
70
-
71
- context "a harmless attribute" do
72
- it "creates an attribute with no options" do
73
- expect(model_class.attributes.values).to include(::ActiveRemote::AttributeDefinition.new(:name))
74
- end
75
-
76
- it "returns the attribute definition" do
77
- expect(model_class.attribute(:name)).to eq(::ActiveRemote::AttributeDefinition.new(:name))
78
- end
79
-
80
- it "defines an attribute reader that calls #attribute" do
81
- expect(subject).to receive(:attribute).with("name")
82
- subject.name
83
- end
84
-
85
- it "defines an attribute writer that calls #attribute=" do
86
- expect(subject).to receive(:attribute=).with("name", "test")
87
- subject.name = "test"
88
- end
89
- end
90
- end
91
-
92
- describe ".attribute!" do
93
- it "can create an attribute with no options" do
94
- model_class.attribute!(:first_name)
95
- expect(model_class.attributes.values).to include(::ActiveRemote::AttributeDefinition.new(:first_name))
96
- end
97
-
98
- it "returns the attribute definition" do
99
- expect(model_class.attribute!(:address)).to eq(::ActiveRemote::AttributeDefinition.new(:address))
100
- end
101
- end
102
-
103
- describe ".attributes" do
104
- it "can access AttributeDefinition with a Symbol" do
105
- expect(::Author.attributes[:name]).to eq(::ActiveRemote::AttributeDefinition.new(:name))
106
- end
107
-
108
- it "can access AttributeDefinition with a String" do
109
- expect(::Author.attributes["name"]).to eq(::ActiveRemote::AttributeDefinition.new(:name))
110
- end
111
- end
112
-
113
- describe ".inspect" do
114
- it "renders the class name" do
115
- expect(model_class.inspect).to match(/^TestClass\(.*\)$/)
116
- end
117
-
118
- it "renders the attribute names in alphabetical order" do
119
- expect(model_class.inspect).to match("(address, name)")
120
- end
121
- end
122
-
123
- describe "#==" do
124
- it "returns true when all attributes are equal" do
125
- expect(::Author.new(:guid => "test")).to eq(::Author.new(:guid => "test"))
126
- end
127
-
128
- it "returns false when compared to another type" do
129
- expect(::Category.new(:guid => "test")).to_not eq(::Author.new(:name => "test"))
130
- end
131
- end
132
-
133
- describe "#attributes" do
134
- context "when no attributes are defined" do
135
- it "returns an empty Hash" do
136
- expect(::NoAttributes.new.attributes).to eq({})
137
- end
138
- end
139
-
140
- context "when an attribute is defined" do
141
- it "returns the key value pairs" do
142
- subject.name = "test"
143
- expect(subject.attributes).to include("name" => "test")
144
- end
145
-
146
- it "returns a new Hash " do
147
- subject.attributes["foobar"] = "foobar"
148
- expect(subject.attributes).to_not include("foobar" => "foobar")
149
- end
150
-
151
- it "returns all attributes" do
152
- expect(subject.attributes.keys).to eq(["guid", "name", "user_guid", "chief_editor_guid", "editor_guid", "category_guid"])
153
- end
154
- end
155
- end
156
-
157
- describe "#inspect" do
158
- before { subject.name = "test" }
159
-
160
- it "includes the class name and all attribute values in alphabetical order by attribute name" do
161
- expect(subject.inspect).to eq(%(#<Author category_guid: nil, chief_editor_guid: nil, editor_guid: nil, guid: nil, name: "test", user_guid: nil>))
162
- end
163
-
164
- it "doesn't format the inspection string for attributes if the model does not have any" do
165
- expect(::NoAttributes.new.inspect).to eq(%(#<NoAttributes>))
166
- end
167
- end
168
-
169
- [:[], :read_attribute].each do |method|
170
- describe "##{method}" do
171
- context "when an attribute is not set" do
172
- it "returns nil" do
173
- expect(subject.send(method, :name)).to be_nil
174
- end
175
- end
176
-
177
- context "when an attribute is set" do
178
- before { subject.write_attribute(:name, "test") }
179
-
180
- it "returns the attribute using a Symbol" do
181
- expect(subject.send(method, :name)).to eq("test")
182
- end
183
-
184
- it "returns the attribute using a String" do
185
- expect(subject.send(method, "name")).to eq("test")
186
- end
187
- end
188
-
189
- it "raises when getting an undefined attribute" do
190
- expect { subject.send(method, :foobar) }.to raise_error(::ActiveRemote::UnknownAttributeError)
191
- end
192
- end
193
- end
194
-
195
- [:[]=, :write_attribute].each do |method|
196
- describe "##{method}" do
197
- it "raises ArgumentError with one argument" do
198
- expect { subject.send(method, :name) }.to raise_error(::ArgumentError)
199
- end
200
-
201
- it "raises ArgumentError with no arguments" do
202
- expect { subject.send(method) }.to raise_error(::ArgumentError)
203
- end
204
-
205
- it "sets an attribute using a Symbol and value" do
206
- expect { subject.send(method, :name, "test") }.to change { subject.attributes["name"] }.from(nil).to("test")
207
- end
208
-
209
- it "sets an attribute using a String and value" do
210
- expect { subject.send(method, "name", "test") }.to change { subject.attributes["name"] }.from(nil).to("test")
211
- end
212
-
213
- it "is able to set an attribute to nil" do
214
- subject.name = "test"
215
- expect { subject.send(method, :name, nil) }.to change { subject.attributes["name"] }.from("test").to(nil)
216
- end
217
-
218
- it "raises when setting an undefined attribute" do
219
- expect { subject.send(method, :foobar, "test") }.to raise_error(::ActiveRemote::UnknownAttributeError)
220
- end
221
- end
222
- end
223
- end
@@ -1,8 +0,0 @@
1
- class TypecastedAuthor < ::ActiveRemote::Base
2
- attribute :guid, :string
3
- attribute :name, :string
4
- attribute :age, :integer
5
- attribute :birthday, :datetime
6
- attribute :writes_fiction, :boolean
7
- attribute :net_sales, :float
8
- end