her 0.8.2 → 0.10.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/.rubocop.yml +1291 -0
- data/.travis.yml +6 -1
- data/README.md +29 -11
- data/her.gemspec +3 -5
- data/lib/her/api.rb +16 -9
- data/lib/her/middleware/json_api_parser.rb +1 -1
- data/lib/her/model/associations/association.rb +32 -5
- data/lib/her/model/associations/association_proxy.rb +1 -1
- data/lib/her/model/associations/belongs_to_association.rb +1 -1
- data/lib/her/model/associations/has_many_association.rb +3 -3
- data/lib/her/model/attributes.rb +105 -75
- data/lib/her/model/http.rb +3 -3
- data/lib/her/model/introspection.rb +1 -1
- data/lib/her/model/orm.rb +96 -19
- data/lib/her/model/parse.rb +27 -17
- data/lib/her/model/relation.rb +46 -2
- data/lib/her/version.rb +1 -1
- data/spec/api_spec.rb +34 -31
- data/spec/collection_spec.rb +25 -10
- data/spec/json_api/model_spec.rb +75 -72
- data/spec/middleware/accept_json_spec.rb +1 -1
- data/spec/middleware/first_level_parse_json_spec.rb +20 -20
- data/spec/middleware/json_api_parser_spec.rb +26 -7
- data/spec/middleware/second_level_parse_json_spec.rb +8 -9
- data/spec/model/associations/association_proxy_spec.rb +2 -5
- data/spec/model/associations_spec.rb +617 -295
- data/spec/model/attributes_spec.rb +114 -107
- data/spec/model/callbacks_spec.rb +59 -27
- data/spec/model/dirty_spec.rb +70 -29
- data/spec/model/http_spec.rb +67 -35
- data/spec/model/introspection_spec.rb +26 -22
- data/spec/model/nested_attributes_spec.rb +31 -31
- data/spec/model/orm_spec.rb +332 -157
- data/spec/model/parse_spec.rb +250 -77
- data/spec/model/paths_spec.rb +109 -109
- data/spec/model/relation_spec.rb +89 -69
- data/spec/model/validations_spec.rb +6 -6
- data/spec/model_spec.rb +17 -17
- data/spec/spec_helper.rb +2 -3
- data/spec/support/macros/model_macros.rb +2 -2
- metadata +36 -63
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
3
3
|
|
4
4
|
describe "Her::Model and ActiveModel::Callbacks" do
|
5
5
|
before do
|
6
|
-
Her::API.setup :
|
6
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
7
7
|
builder.use Her::Middleware::FirstLevelParseJSON
|
8
8
|
end
|
9
9
|
|
@@ -11,11 +11,11 @@ describe "Her::Model and ActiveModel::Callbacks" do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
context :before_save do
|
14
|
-
subject { Foo::User.create(:
|
14
|
+
subject { Foo::User.create(name: "Tobias Funke") }
|
15
15
|
before do
|
16
16
|
Her::API.default_api.connection.adapter :test do |stub|
|
17
|
-
stub.post("/users") { |env| [200, {}, { :
|
18
|
-
stub.put("/users/1") { |env| [200, {}, { :
|
17
|
+
stub.post("/users") { |env| [200, {}, { id: 1, name: env[:body][:name] }.to_json] }
|
18
|
+
stub.put("/users/1") { |env| [200, {}, { id: 1, name: env[:body][:name] }.to_json] }
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -23,21 +23,29 @@ describe "Her::Model and ActiveModel::Callbacks" do
|
|
23
23
|
before do
|
24
24
|
class Foo::User
|
25
25
|
before_save :alter_name
|
26
|
-
def alter_name
|
26
|
+
def alter_name
|
27
|
+
name.upcase!
|
28
|
+
end
|
27
29
|
end
|
28
30
|
end
|
29
31
|
|
30
|
-
|
32
|
+
describe "#name" do
|
33
|
+
subject { super().name }
|
34
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
35
|
+
end
|
31
36
|
end
|
32
37
|
|
33
38
|
context "when using a block callback" do
|
34
39
|
before do
|
35
40
|
class Foo::User
|
36
|
-
before_save
|
41
|
+
before_save -> { name.upcase! }
|
37
42
|
end
|
38
43
|
end
|
39
44
|
|
40
|
-
|
45
|
+
describe "#name" do
|
46
|
+
subject { super().name }
|
47
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
48
|
+
end
|
41
49
|
end
|
42
50
|
|
43
51
|
context "when changing a value of an existing resource in a callback" do
|
@@ -50,19 +58,19 @@ describe "Her::Model and ActiveModel::Callbacks" do
|
|
50
58
|
end
|
51
59
|
end
|
52
60
|
|
53
|
-
it "should call the server with the
|
54
|
-
subject.name.
|
61
|
+
it "should call the server with the changed value" do
|
62
|
+
expect(subject.name).to eq("Tobias Funke")
|
55
63
|
subject.save
|
56
|
-
subject.name.
|
64
|
+
expect(subject.name).to eq("Lumberjack")
|
57
65
|
end
|
58
66
|
end
|
59
67
|
end
|
60
68
|
|
61
69
|
context :before_create do
|
62
|
-
subject { Foo::User.create(:
|
70
|
+
subject { Foo::User.create(name: "Tobias Funke") }
|
63
71
|
before do
|
64
72
|
Her::API.default_api.connection.adapter :test do |stub|
|
65
|
-
stub.post("/users") { |env| [200, {}, { :
|
73
|
+
stub.post("/users") { |env| [200, {}, { id: 1, name: env[:body][:name] }.to_json] }
|
66
74
|
end
|
67
75
|
end
|
68
76
|
|
@@ -70,21 +78,29 @@ describe "Her::Model and ActiveModel::Callbacks" do
|
|
70
78
|
before do
|
71
79
|
class Foo::User
|
72
80
|
before_create :alter_name
|
73
|
-
def alter_name
|
81
|
+
def alter_name
|
82
|
+
name.upcase!
|
83
|
+
end
|
74
84
|
end
|
75
85
|
end
|
76
86
|
|
77
|
-
|
87
|
+
describe "#name" do
|
88
|
+
subject { super().name }
|
89
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
90
|
+
end
|
78
91
|
end
|
79
92
|
|
80
93
|
context "when using a block callback" do
|
81
94
|
before do
|
82
95
|
class Foo::User
|
83
|
-
before_create
|
96
|
+
before_create -> { name.upcase! }
|
84
97
|
end
|
85
98
|
end
|
86
99
|
|
87
|
-
|
100
|
+
describe "#name" do
|
101
|
+
subject { super().name }
|
102
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
103
|
+
end
|
88
104
|
end
|
89
105
|
end
|
90
106
|
|
@@ -92,7 +108,7 @@ describe "Her::Model and ActiveModel::Callbacks" do
|
|
92
108
|
subject { Foo::User.find(1) }
|
93
109
|
before do
|
94
110
|
Her::API.default_api.connection.adapter :test do |stub|
|
95
|
-
stub.get("/users/1") {
|
111
|
+
stub.get("/users/1") { [200, {}, { id: 1, name: "Tobias Funke" }.to_json] }
|
96
112
|
end
|
97
113
|
end
|
98
114
|
|
@@ -100,46 +116,62 @@ describe "Her::Model and ActiveModel::Callbacks" do
|
|
100
116
|
before do
|
101
117
|
class Foo::User
|
102
118
|
after_find :alter_name
|
103
|
-
def alter_name
|
119
|
+
def alter_name
|
120
|
+
name.upcase!
|
121
|
+
end
|
104
122
|
end
|
105
123
|
end
|
106
124
|
|
107
|
-
|
125
|
+
describe "#name" do
|
126
|
+
subject { super().name }
|
127
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
128
|
+
end
|
108
129
|
end
|
109
130
|
|
110
131
|
context "when using a block callback" do
|
111
132
|
before do
|
112
133
|
class Foo::User
|
113
|
-
after_find
|
134
|
+
after_find -> { name.upcase! }
|
114
135
|
end
|
115
136
|
end
|
116
137
|
|
117
|
-
|
138
|
+
describe "#name" do
|
139
|
+
subject { super().name }
|
140
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
141
|
+
end
|
118
142
|
end
|
119
143
|
end
|
120
144
|
|
121
145
|
context :after_initialize do
|
122
|
-
subject { Foo::User.new(:
|
146
|
+
subject { Foo::User.new(name: "Tobias Funke") }
|
123
147
|
|
124
148
|
context "when using a symbol callback" do
|
125
149
|
before do
|
126
150
|
class Foo::User
|
127
151
|
after_initialize :alter_name
|
128
|
-
def alter_name
|
152
|
+
def alter_name
|
153
|
+
name.upcase!
|
154
|
+
end
|
129
155
|
end
|
130
156
|
end
|
131
157
|
|
132
|
-
|
158
|
+
describe "#name" do
|
159
|
+
subject { super().name }
|
160
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
161
|
+
end
|
133
162
|
end
|
134
163
|
|
135
164
|
context "when using a block callback" do
|
136
165
|
before do
|
137
166
|
class Foo::User
|
138
|
-
after_initialize
|
167
|
+
after_initialize -> { name.upcase! }
|
139
168
|
end
|
140
169
|
end
|
141
170
|
|
142
|
-
|
171
|
+
describe "#name" do
|
172
|
+
subject { super().name }
|
173
|
+
it { is_expected.to eq("TOBIAS FUNKE") }
|
174
|
+
end
|
143
175
|
end
|
144
176
|
end
|
145
177
|
end
|
data/spec/model/dirty_spec.rb
CHANGED
@@ -4,20 +4,28 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
4
4
|
describe "Her::Model and ActiveModel::Dirty" do
|
5
5
|
context "checking dirty attributes" do
|
6
6
|
before do
|
7
|
-
Her::API.setup :
|
7
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
8
8
|
builder.use Her::Middleware::FirstLevelParseJSON
|
9
9
|
builder.use Faraday::Request::UrlEncoded
|
10
10
|
builder.adapter :test do |stub|
|
11
|
-
stub.get("/users
|
12
|
-
stub.get("/users/
|
13
|
-
stub.get("/users/
|
14
|
-
stub.
|
15
|
-
stub.put("/users/
|
16
|
-
stub.
|
11
|
+
stub.get("/users") { [200, {}, [{ id: 1, fullname: "Lindsay Fünke" }, { id: 2, fullname: "Maeby Fünke" }].to_json] }
|
12
|
+
stub.get("/users/1") { [200, {}, { id: 1, fullname: "Lindsay Fünke" }.to_json] }
|
13
|
+
stub.get("/users/2") { [200, {}, { id: 2, fullname: "Maeby Fünke" }.to_json] }
|
14
|
+
stub.get("/users/3") { [200, {}, { user_id: 3, fullname: "Maeby Fünke" }.to_json] }
|
15
|
+
stub.put("/users/1") { [200, {}, { id: 1, fullname: "Tobias Fünke" }.to_json] }
|
16
|
+
stub.put("/users/2") { [400, {}, { errors: ["Email cannot be blank"] }.to_json] }
|
17
|
+
stub.post("/users") { [200, {}, { id: 1, fullname: "Tobias Fünke" }.to_json] }
|
18
|
+
stub.get("/users/1/posts") { [200, {}, [{ id: 1, user_id: 1, body: "Hello" }].to_json] }
|
19
|
+
stub.get("/users/1/posts/1") { [200, {}, { id: 1, user_id: 1, body: "Hello" }.to_json] }
|
17
20
|
end
|
18
21
|
end
|
19
22
|
|
23
|
+
spawn_model "Foo::Post" do
|
24
|
+
belongs_to :user
|
25
|
+
attributes :body
|
26
|
+
end
|
20
27
|
spawn_model "Foo::User" do
|
28
|
+
has_many :posts
|
21
29
|
attributes :fullname, :email
|
22
30
|
end
|
23
31
|
spawn_model "Dynamic::User" do
|
@@ -28,36 +36,36 @@ describe "Her::Model and ActiveModel::Dirty" do
|
|
28
36
|
context "for existing resource" do
|
29
37
|
let(:user) { Foo::User.find(1) }
|
30
38
|
it "has no changes" do
|
31
|
-
user.changes.
|
32
|
-
user.
|
39
|
+
expect(user.changes).to be_empty
|
40
|
+
expect(user).not_to be_changed
|
33
41
|
end
|
34
42
|
context "with successful save" do
|
35
43
|
it "tracks dirty attributes" do
|
36
44
|
user.fullname = "Tobias Fünke"
|
37
|
-
user.fullname_changed
|
38
|
-
user.email_changed
|
39
|
-
user.
|
45
|
+
expect(user.fullname_changed?).to be_truthy
|
46
|
+
expect(user.email_changed?).to be_falsey
|
47
|
+
expect(user).to be_changed
|
40
48
|
user.save
|
41
|
-
user.
|
49
|
+
expect(user).not_to be_changed
|
42
50
|
end
|
43
51
|
|
44
52
|
it "tracks only changed dirty attributes" do
|
45
53
|
user.fullname = user.fullname
|
46
|
-
user.fullname_changed
|
54
|
+
expect(user.fullname_changed?).to be_falsey
|
47
55
|
end
|
48
56
|
|
49
57
|
it "tracks previous changes" do
|
50
58
|
user.fullname = "Tobias Fünke"
|
51
59
|
user.save
|
52
|
-
user.previous_changes.
|
60
|
+
expect(user.previous_changes).to eq("fullname" => ["Lindsay Fünke", "Tobias Fünke"])
|
53
61
|
end
|
54
62
|
|
55
|
-
it
|
63
|
+
it "tracks dirty attribute for mass assign for dynamic created attributes" do
|
56
64
|
user = Dynamic::User.find(3)
|
57
|
-
user.assign_attributes(:
|
58
|
-
user.fullname_changed
|
59
|
-
user.
|
60
|
-
user.changes.length.
|
65
|
+
user.assign_attributes(fullname: "New Fullname")
|
66
|
+
expect(user.fullname_changed?).to be_truthy
|
67
|
+
expect(user).to be_changed
|
68
|
+
expect(user.changes.length).to eq(1)
|
61
69
|
end
|
62
70
|
end
|
63
71
|
|
@@ -65,26 +73,59 @@ describe "Her::Model and ActiveModel::Dirty" do
|
|
65
73
|
it "tracks dirty attributes" do
|
66
74
|
user = Foo::User.find(2)
|
67
75
|
user.fullname = "Tobias Fünke"
|
68
|
-
user.fullname_changed
|
69
|
-
user.email_changed
|
70
|
-
user.
|
76
|
+
expect(user.fullname_changed?).to be_truthy
|
77
|
+
expect(user.email_changed?).to be_falsey
|
78
|
+
expect(user).to be_changed
|
71
79
|
user.save
|
72
|
-
user.
|
80
|
+
expect(user).to be_changed
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "for an existing resource from an association" do
|
86
|
+
let(:post) { Foo::User.find(1).posts.find(1) }
|
87
|
+
it "has no changes" do
|
88
|
+
expect(post.changes).to be_empty
|
89
|
+
expect(post).to_not be_changed
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context "for an existing resource from an association collection" do
|
94
|
+
let(:post) { Foo::User.find(1).posts.first }
|
95
|
+
it "has no changes" do
|
96
|
+
expect(post.changes).to be_empty
|
97
|
+
expect(post).to_not be_changed
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context "for resources from a collection" do
|
102
|
+
let(:users) { Foo::User.all.fetch }
|
103
|
+
it "has no changes" do
|
104
|
+
users.each do |user|
|
105
|
+
expect(user.changes).to be_empty
|
106
|
+
expect(user).to_not be_changed
|
73
107
|
end
|
74
108
|
end
|
75
109
|
end
|
76
110
|
|
77
111
|
context "for new resource" do
|
78
|
-
let(:user) { Foo::User.new(:
|
112
|
+
let(:user) { Foo::User.new(fullname: "Lindsay Fünke") }
|
79
113
|
it "has changes" do
|
80
|
-
user.
|
114
|
+
expect(user).to be_changed
|
81
115
|
end
|
82
116
|
it "tracks dirty attributes" do
|
83
117
|
user.fullname = "Tobias Fünke"
|
84
|
-
user.fullname_changed
|
85
|
-
user.
|
118
|
+
expect(user.fullname_changed?).to be_truthy
|
119
|
+
expect(user).to be_changed
|
86
120
|
user.save
|
87
|
-
user.
|
121
|
+
expect(user).not_to be_changed
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
context "for a new resource from an association" do
|
126
|
+
let(:post) { Foo::User.find(1).posts.build }
|
127
|
+
it "has changes" do
|
128
|
+
expect(post).to be_changed
|
88
129
|
end
|
89
130
|
end
|
90
131
|
end
|
data/spec/model/http_spec.rb
CHANGED
@@ -3,13 +3,13 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
3
3
|
|
4
4
|
describe Her::Model::HTTP do
|
5
5
|
context "binding a model with an API" do
|
6
|
-
let(:api1) { Her::API.new :
|
7
|
-
let(:api2) { Her::API.new :
|
6
|
+
let(:api1) { Her::API.new url: "https://api1.example.com" }
|
7
|
+
let(:api2) { Her::API.new url: "https://api2.example.com" }
|
8
8
|
|
9
9
|
before do
|
10
10
|
spawn_model("Foo::User")
|
11
11
|
spawn_model("Foo::Comment")
|
12
|
-
Her::API.setup :
|
12
|
+
Her::API.setup url: "https://api.example.com"
|
13
13
|
end
|
14
14
|
|
15
15
|
context "when binding a model to its superclass' her_api" do
|
@@ -19,7 +19,7 @@ describe Her::Model::HTTP do
|
|
19
19
|
Foo::Subclass = Class.new(Foo::Superclass)
|
20
20
|
end
|
21
21
|
|
22
|
-
specify { Foo::Subclass.her_api.
|
22
|
+
specify { expect(Foo::Subclass.her_api).to eq(Foo::Superclass.her_api) }
|
23
23
|
end
|
24
24
|
|
25
25
|
context "when changing her_api without changing the parent class' her_api" do
|
@@ -30,23 +30,23 @@ describe Her::Model::HTTP do
|
|
30
30
|
Foo::Subclass.uses_api api2
|
31
31
|
end
|
32
32
|
|
33
|
-
specify { Foo::Subclass.her_api.
|
33
|
+
specify { expect(Foo::Subclass.her_api).not_to eq(Foo::Superclass.her_api) }
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
37
|
context "making HTTP requests" do
|
38
38
|
before do
|
39
|
-
Her::API.setup :
|
39
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
40
40
|
builder.use Her::Middleware::FirstLevelParseJSON
|
41
41
|
builder.use Faraday::Request::UrlEncoded
|
42
42
|
builder.adapter :test do |stub|
|
43
|
-
stub.get("/users") {
|
44
|
-
stub.get("/users/1") {
|
43
|
+
stub.get("/users") { [200, {}, [{ id: 1 }].to_json] }
|
44
|
+
stub.get("/users/1") { [200, {}, { id: 1 }.to_json] }
|
45
45
|
stub.get("/users/popular") do |env|
|
46
46
|
if env[:params]["page"] == "2"
|
47
|
-
[200, {}, [{ :
|
47
|
+
[200, {}, [{ id: 3 }, { id: 4 }].to_json]
|
48
48
|
else
|
49
|
-
[200, {}, [{ :
|
49
|
+
[200, {}, [{ id: 1 }, { id: 2 }].to_json]
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
@@ -57,61 +57,85 @@ describe Her::Model::HTTP do
|
|
57
57
|
|
58
58
|
describe :get do
|
59
59
|
subject { Foo::User.get(:popular) }
|
60
|
-
|
61
|
-
|
60
|
+
|
61
|
+
describe "#length" do
|
62
|
+
subject { super().length }
|
63
|
+
it { is_expected.to eq(2) }
|
64
|
+
end
|
65
|
+
specify { expect(subject.first.id).to eq(1) }
|
62
66
|
end
|
63
67
|
|
64
68
|
describe :get_raw do
|
65
69
|
context "with a block" do
|
66
70
|
specify do
|
67
|
-
Foo::User.get_raw("/users") do |parsed_data,
|
68
|
-
parsed_data[:data].
|
71
|
+
Foo::User.get_raw("/users") do |parsed_data, _response|
|
72
|
+
expect(parsed_data[:data]).to eq([{ id: 1 }])
|
69
73
|
end
|
70
74
|
end
|
71
75
|
end
|
72
76
|
|
73
77
|
context "with a return value" do
|
74
78
|
subject { Foo::User.get_raw("/users") }
|
75
|
-
specify { subject[:parsed_data][:data].
|
79
|
+
specify { expect(subject[:parsed_data][:data]).to eq([{ id: 1 }]) }
|
76
80
|
end
|
77
81
|
end
|
78
82
|
|
79
83
|
describe :get_collection do
|
80
84
|
context "with a String path" do
|
81
85
|
subject { Foo::User.get_collection("/users/popular") }
|
82
|
-
|
83
|
-
|
86
|
+
|
87
|
+
describe "#length" do
|
88
|
+
subject { super().length }
|
89
|
+
it { is_expected.to eq(2) }
|
90
|
+
end
|
91
|
+
specify { expect(subject.first.id).to eq(1) }
|
84
92
|
end
|
85
93
|
|
86
94
|
context "with a Symbol" do
|
87
95
|
subject { Foo::User.get_collection(:popular) }
|
88
|
-
|
89
|
-
|
96
|
+
|
97
|
+
describe "#length" do
|
98
|
+
subject { super().length }
|
99
|
+
it { is_expected.to eq(2) }
|
100
|
+
end
|
101
|
+
specify { expect(subject.first.id).to eq(1) }
|
90
102
|
end
|
91
103
|
|
92
104
|
context "with extra parameters" do
|
93
|
-
subject { Foo::User.get_collection(:popular, :
|
94
|
-
|
95
|
-
|
105
|
+
subject { Foo::User.get_collection(:popular, page: 2) }
|
106
|
+
|
107
|
+
describe "#length" do
|
108
|
+
subject { super().length }
|
109
|
+
it { is_expected.to eq(2) }
|
110
|
+
end
|
111
|
+
specify { expect(subject.first.id).to eq(3) }
|
96
112
|
end
|
97
113
|
end
|
98
114
|
|
99
115
|
describe :get_resource do
|
100
116
|
context "with a String path" do
|
101
117
|
subject { Foo::User.get_resource("/users/1") }
|
102
|
-
|
118
|
+
|
119
|
+
describe "#id" do
|
120
|
+
subject { super().id }
|
121
|
+
it { is_expected.to eq(1) }
|
122
|
+
end
|
103
123
|
end
|
104
124
|
|
105
125
|
context "with a Symbol" do
|
106
126
|
subject { Foo::User.get_resource(:"1") }
|
107
|
-
|
127
|
+
|
128
|
+
describe "#id" do
|
129
|
+
subject { super().id }
|
130
|
+
it { is_expected.to eq(1) }
|
131
|
+
end
|
108
132
|
end
|
109
133
|
end
|
110
134
|
|
111
135
|
describe :get_raw do
|
112
136
|
specify do
|
113
|
-
Foo::User.get_raw(:popular) do |parsed_data,
|
114
|
-
parsed_data[:data].
|
137
|
+
Foo::User.get_raw(:popular) do |parsed_data, _response|
|
138
|
+
expect(parsed_data[:data]).to eq([{ id: 1 }, { id: 2 }])
|
115
139
|
end
|
116
140
|
end
|
117
141
|
end
|
@@ -119,11 +143,11 @@ describe Her::Model::HTTP do
|
|
119
143
|
|
120
144
|
context "setting custom HTTP requests" do
|
121
145
|
before do
|
122
|
-
Her::API.setup :
|
146
|
+
Her::API.setup url: "https://api.example.com" do |connection|
|
123
147
|
connection.use Her::Middleware::FirstLevelParseJSON
|
124
148
|
connection.adapter :test do |stub|
|
125
|
-
stub.get("/users/popular") {
|
126
|
-
stub.post("/users/from_default") {
|
149
|
+
stub.get("/users/popular") { [200, {}, [{ id: 1 }, { id: 2 }].to_json] }
|
150
|
+
stub.post("/users/from_default") { [200, {}, { id: 4 }.to_json] }
|
127
151
|
end
|
128
152
|
end
|
129
153
|
|
@@ -135,23 +159,31 @@ describe Her::Model::HTTP do
|
|
135
159
|
describe :custom_get do
|
136
160
|
context "without cache" do
|
137
161
|
before { Foo::User.custom_get :popular, :recent }
|
138
|
-
it {
|
139
|
-
it {
|
162
|
+
it { is_expected.to respond_to(:popular) }
|
163
|
+
it { is_expected.to respond_to(:recent) }
|
140
164
|
|
141
165
|
context "making the HTTP request" do
|
142
166
|
subject { Foo::User.popular }
|
143
|
-
|
167
|
+
|
168
|
+
describe "#length" do
|
169
|
+
subject { super().length }
|
170
|
+
it { is_expected.to eq(2) }
|
171
|
+
end
|
144
172
|
end
|
145
173
|
end
|
146
174
|
end
|
147
175
|
|
148
176
|
describe :custom_post do
|
149
177
|
before { Foo::User.custom_post :from_default }
|
150
|
-
it {
|
178
|
+
it { is_expected.to respond_to(:from_default) }
|
151
179
|
|
152
180
|
context "making the HTTP request" do
|
153
|
-
subject { Foo::User.from_default(:
|
154
|
-
|
181
|
+
subject { Foo::User.from_default(name: "Tobias Fünke") }
|
182
|
+
|
183
|
+
describe "#id" do
|
184
|
+
subject { super().id }
|
185
|
+
it { is_expected.to eq(4) }
|
186
|
+
end
|
155
187
|
end
|
156
188
|
end
|
157
189
|
end
|
@@ -4,15 +4,15 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
4
4
|
describe Her::Model::Introspection do
|
5
5
|
context "introspecting a resource" do
|
6
6
|
before do
|
7
|
-
Her::API.setup :
|
7
|
+
Her::API.setup url: "https://api.example.com" do |builder|
|
8
8
|
builder.use Her::Middleware::FirstLevelParseJSON
|
9
9
|
builder.use Faraday::Request::UrlEncoded
|
10
10
|
builder.adapter :test do |stub|
|
11
|
-
stub.post("/users") {
|
12
|
-
stub.get("/users/1") {
|
13
|
-
stub.put("/users/1") {
|
14
|
-
stub.delete("/users/1") {
|
15
|
-
stub.get("/projects/1/comments") {
|
11
|
+
stub.post("/users") { [200, {}, { id: 1, name: "Tobias Funke" }.to_json] }
|
12
|
+
stub.get("/users/1") { [200, {}, { id: 1, name: "Tobias Funke" }.to_json] }
|
13
|
+
stub.put("/users/1") { [200, {}, { id: 1, name: "Tobias Funke" }.to_json] }
|
14
|
+
stub.delete("/users/1") { [200, {}, { id: 1, name: "Tobias Funke" }.to_json] }
|
15
|
+
stub.get("/projects/1/comments") { [200, {}, [{ id: 1, body: "Hello!" }].to_json] }
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -25,33 +25,37 @@ describe Her::Model::Introspection do
|
|
25
25
|
describe "#inspect" do
|
26
26
|
it "outputs resource attributes for an existing resource" do
|
27
27
|
@user = Foo::User.find(1)
|
28
|
-
["#<Foo::User(users/1) name=\"Tobias Funke\" id=1>", "#<Foo::User(users/1) id=1 name=\"Tobias Funke\">"].
|
28
|
+
expect(["#<Foo::User(users/1) name=\"Tobias Funke\" id=1>", "#<Foo::User(users/1) id=1 name=\"Tobias Funke\">"]).to include(@user.inspect)
|
29
29
|
end
|
30
30
|
|
31
31
|
it "outputs resource attributes for an not-saved-yet resource" do
|
32
|
-
@user = Foo::User.new(:
|
33
|
-
@user.inspect.
|
32
|
+
@user = Foo::User.new(name: "Tobias Funke")
|
33
|
+
expect(@user.inspect).to eq("#<Foo::User(users) name=\"Tobias Funke\">")
|
34
34
|
end
|
35
35
|
|
36
36
|
it "outputs resource attributes using getters" do
|
37
|
-
@user = Foo::User.new(:
|
38
|
-
@user.instance_eval
|
39
|
-
|
40
|
-
|
41
|
-
|
37
|
+
@user = Foo::User.new(name: "Tobias Funke", password: "Funke")
|
38
|
+
@user.instance_eval do
|
39
|
+
def password
|
40
|
+
"filtered"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
expect(@user.inspect).to include("name=\"Tobias Funke\"")
|
44
|
+
expect(@user.inspect).to include("password=\"filtered\"")
|
45
|
+
expect(@user.inspect).not_to include("password=\"Funke\"")
|
42
46
|
end
|
43
47
|
|
44
48
|
it "support dash on attribute" do
|
45
49
|
@user = Foo::User.new(:'life-span' => "3 years")
|
46
|
-
@user.inspect.
|
50
|
+
expect(@user.inspect).to include("life-span=\"3 years\"")
|
47
51
|
end
|
48
52
|
end
|
49
53
|
|
50
54
|
describe "#inspect with errors in resource path" do
|
51
55
|
it "prints the resource path as “unknown”" do
|
52
|
-
@comment = Foo::Comment.where(:
|
53
|
-
path =
|
54
|
-
["#<Foo::Comment(#{path}) body=\"Hello!\" id=1>", "#<Foo::Comment(#{path}) id=1 body=\"Hello!\">"].
|
56
|
+
@comment = Foo::Comment.where(project_id: 1).first
|
57
|
+
path = "<unknown path, missing `project_id`>"
|
58
|
+
expect(["#<Foo::Comment(#{path}) body=\"Hello!\" id=1>", "#<Foo::Comment(#{path}) id=1 body=\"Hello!\">"]).to include(@comment.inspect)
|
55
59
|
end
|
56
60
|
end
|
57
61
|
end
|
@@ -66,10 +70,10 @@ describe Her::Model::Introspection do
|
|
66
70
|
end
|
67
71
|
|
68
72
|
it "returns a sibling class, if found" do
|
69
|
-
Foo::User.her_nearby_class("AccessRecord").
|
70
|
-
AccessRecord.her_nearby_class("Log").
|
71
|
-
Foo::User.her_nearby_class("Log").
|
72
|
-
Foo::User.her_nearby_class("X").
|
73
|
+
expect(Foo::User.her_nearby_class("AccessRecord")).to eq(Foo::AccessRecord)
|
74
|
+
expect(AccessRecord.her_nearby_class("Log")).to eq(Log)
|
75
|
+
expect(Foo::User.her_nearby_class("Log")).to eq(Log)
|
76
|
+
expect{Foo::User.her_nearby_class("X")}.to raise_error(NameError)
|
73
77
|
end
|
74
78
|
end
|
75
79
|
end
|