collector 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +0 -1
- data/lib/collector/model.rb +5 -0
- data/lib/collector/version.rb +1 -1
- data/test/collector/model_spec.rb +23 -0
- data/test/collector/repository_spec.rb +50 -51
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: abed7c5e4060dc56b6f1bfd6c1d663ccb99d3266
|
4
|
+
data.tar.gz: c15c846510d601f66b03ca4dee9bd5961cce54f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1df54655d7eb6080b59a5c7f8916e31770f46849739097da2f7dae472f8c9d35de2dce56c3eb7fb1fe028edbac5c037e2b41cf125e476af207de92e1944b4d8
|
7
|
+
data.tar.gz: cbbbcc86b3b015f76112a2c2d375e1d97dd16c482196f526e61ac0423489ae40bf0c1dbf22fde3953ac70f6a5801472d6739b179a5cc85e9676124c8e1967bdd
|
data/Gemfile
CHANGED
data/lib/collector/model.rb
CHANGED
data/lib/collector/version.rb
CHANGED
@@ -68,4 +68,27 @@ describe Collector::Model do
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
+
describe "#==" do
|
72
|
+
it "compares two models with the same id" do
|
73
|
+
model_1 = TestModel.new(id: "foobar")
|
74
|
+
model_2 = TestModel.new(id: "foobar")
|
75
|
+
|
76
|
+
model_1.must_equal model_2
|
77
|
+
end
|
78
|
+
|
79
|
+
it "compares two models with different ids" do
|
80
|
+
model_1 = TestModel.new(id: "foobar")
|
81
|
+
model_2 = TestModel.new(id: "barfoo")
|
82
|
+
|
83
|
+
model_1.wont_equal model_2
|
84
|
+
end
|
85
|
+
|
86
|
+
it "compares two models with no ids" do
|
87
|
+
model_1 = TestModel.new(id: nil)
|
88
|
+
model_2 = TestModel.new(id: nil)
|
89
|
+
|
90
|
+
model_1.wont_equal model_2
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
71
94
|
end
|
@@ -9,27 +9,27 @@ describe Collector::Repository do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
Object.send(:remove_const, :TestRepository) if Object.const_defined?(:TestRepository)
|
12
|
-
class
|
12
|
+
class TestModelRepository
|
13
13
|
include Collector::Repository
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
it "has a model derived from its class name" do
|
18
|
-
|
18
|
+
TestModelRepository.model.name.must_equal "TestModel"
|
19
19
|
end
|
20
20
|
|
21
21
|
it "has a collection_name derived from its model" do
|
22
|
-
|
22
|
+
TestModelRepository.collection_name.must_equal "test_models"
|
23
23
|
end
|
24
24
|
|
25
25
|
describe "collection" do
|
26
26
|
describe "when a connection is set" do
|
27
27
|
it "returns the mongo collection" do
|
28
28
|
collection = mock()
|
29
|
-
connection = mock { stubs(:[]).with("
|
29
|
+
connection = mock { stubs(:[]).with("test_models").returns(collection) }
|
30
30
|
Collector.stubs(:connection).returns(connection)
|
31
31
|
|
32
|
-
|
32
|
+
TestModelRepository.collection.must_equal collection
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
@@ -37,33 +37,32 @@ describe Collector::Repository do
|
|
37
37
|
describe "save" do
|
38
38
|
it "touches the model and then saves it" do
|
39
39
|
model = mock(:touch)
|
40
|
-
|
41
|
-
|
40
|
+
TestModelRepository.expects(:save_without_updating_timestamps).with(model)
|
41
|
+
TestModelRepository.save(model)
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
describe "save_without_updating_timestamps" do
|
46
46
|
it "serializes the model and then saves it into the collection" do
|
47
47
|
model = stub()
|
48
|
-
|
48
|
+
TestModelRepository.expects(:serialize!).with(model).returns({ foo: "bar" })
|
49
49
|
|
50
50
|
collection = mock(save: { foo: "bar" })
|
51
|
-
|
51
|
+
TestModelRepository.stubs(:collection).returns(collection)
|
52
52
|
|
53
|
-
|
53
|
+
TestModelRepository.save_without_updating_timestamps(model)
|
54
54
|
end
|
55
55
|
|
56
|
-
it "returns the model with
|
57
|
-
model = TestModel.new
|
58
|
-
|
56
|
+
it "returns the model with its id set" do
|
57
|
+
model = TestModel.new(id: "123abc")
|
58
|
+
TestModelRepository.expects(:serialize!).with(model).returns({ foo: "bar" })
|
59
59
|
|
60
60
|
object_id = stub(to_s: "123abc")
|
61
61
|
collection = mock { expects(:save).with({ foo: "bar" }).returns(object_id) }
|
62
|
-
|
62
|
+
TestModelRepository.stubs(:collection).returns(collection)
|
63
63
|
|
64
|
-
updated_model =
|
64
|
+
updated_model = TestModelRepository.save_without_updating_timestamps(model)
|
65
65
|
updated_model.must_equal model
|
66
|
-
updated_model.id.must_equal "123abc"
|
67
66
|
end
|
68
67
|
end
|
69
68
|
|
@@ -72,43 +71,43 @@ describe Collector::Repository do
|
|
72
71
|
model = stub(id: "50c58f4ab392d4381a000001")
|
73
72
|
|
74
73
|
collection = mock { expects(:remove).with(_id: BSON::ObjectId("50c58f4ab392d4381a000001")) }
|
75
|
-
|
74
|
+
TestModelRepository.stubs(:collection).returns(collection)
|
76
75
|
|
77
|
-
|
76
|
+
TestModelRepository.delete(model)
|
78
77
|
end
|
79
78
|
end
|
80
79
|
|
81
80
|
describe "serialize!" do
|
82
81
|
it "normalize id to _id and converts to a BSON::ObjectId" do
|
83
82
|
model = mock(attributes: { id: "50c58f4ab392d4381a000001", foo: "bar" })
|
84
|
-
|
83
|
+
TestModelRepository.serialize!(model).must_equal({ "_id" => BSON::ObjectId("50c58f4ab392d4381a000001"), "foo" => "bar" })
|
85
84
|
end
|
86
85
|
|
87
86
|
it "returns a model's attributes without nil values" do
|
88
87
|
model = mock(attributes: { foo: "bar", nothing: nil })
|
89
|
-
|
88
|
+
TestModelRepository.serialize!(model).must_equal({ "foo" => "bar" })
|
90
89
|
end
|
91
90
|
end
|
92
91
|
|
93
92
|
describe "serialize" do
|
94
93
|
it "returns a model's attributes" do
|
95
94
|
model = mock(attributes: { foo: "bar" })
|
96
|
-
|
95
|
+
TestModelRepository.serialize(model).must_equal({ "foo" => "bar" })
|
97
96
|
end
|
98
97
|
end
|
99
98
|
|
100
99
|
describe "deserialize!" do
|
101
100
|
it "normalizes _id to id and converts to a string id" do
|
102
|
-
|
103
|
-
|
101
|
+
TestModelRepository.expects(:deserialize).with("id" => "50c58f4ab392d4381a000001", "name" => "Brandon")
|
102
|
+
TestModelRepository.deserialize!(_id: BSON::ObjectId("50c58f4ab392d4381a000001"), name: "Brandon")
|
104
103
|
end
|
105
104
|
end
|
106
105
|
|
107
106
|
describe "deserialize" do
|
108
107
|
it "instantiates a new model from a hash of attributes" do
|
109
108
|
attributes = { first_name: "Brandon", last_name: "Weiss" }
|
110
|
-
|
111
|
-
|
109
|
+
TestModelRepository.model.expects(:new).with(attributes)
|
110
|
+
TestModelRepository.deserialize(attributes)
|
112
111
|
end
|
113
112
|
end
|
114
113
|
|
@@ -117,82 +116,82 @@ describe Collector::Repository do
|
|
117
116
|
document_1 = stub
|
118
117
|
document_2 = stub
|
119
118
|
documents = [document_1, document_2]
|
120
|
-
|
121
|
-
|
119
|
+
TestModelRepository.expects(:deserialize!).with(document_1)
|
120
|
+
TestModelRepository.expects(:deserialize!).with(document_2)
|
122
121
|
collection = mock { expects(:find).with(attribute: "value").returns(documents) }
|
123
|
-
|
124
|
-
|
122
|
+
TestModelRepository.expects(:collection).returns(collection)
|
123
|
+
TestModelRepository.find_by(attribute: "value")
|
125
124
|
end
|
126
125
|
|
127
126
|
it "finds all documents if no attributes are given" do
|
128
127
|
document_1 = stub
|
129
128
|
document_2 = stub
|
130
129
|
documents = [document_1, document_2]
|
131
|
-
|
132
|
-
|
130
|
+
TestModelRepository.expects(:deserialize!).with(document_1)
|
131
|
+
TestModelRepository.expects(:deserialize!).with(document_2)
|
133
132
|
collection = mock { expects(:find).with({}).returns(documents) }
|
134
|
-
|
135
|
-
|
133
|
+
TestModelRepository.expects(:collection).returns(collection)
|
134
|
+
TestModelRepository.find_by
|
136
135
|
end
|
137
136
|
|
138
137
|
it "normalizes the id into a BSON::ObjectId if it's valid ObjectId" do
|
139
138
|
collection = mock { expects(:find).with(_id: BSON::ObjectId("50c58f4ab392d4381a000001")).returns([]) }
|
140
|
-
|
141
|
-
|
139
|
+
TestModelRepository.expects(:collection).returns(collection)
|
140
|
+
TestModelRepository.find_by(_id: "50c58f4ab392d4381a000001")
|
142
141
|
end
|
143
142
|
|
144
143
|
it "does nothing with the id if it's not a valid BSON::ObjectId" do
|
145
144
|
collection = mock { expects(:find).with(_id: "lols").returns([]) }
|
146
|
-
|
147
|
-
|
145
|
+
TestModelRepository.expects(:collection).returns(collection)
|
146
|
+
TestModelRepository.find_by(_id: "lols")
|
148
147
|
end
|
149
148
|
end
|
150
149
|
|
151
150
|
describe "find_first_by" do
|
152
151
|
it "finds the first document by a hash of attributes" do
|
153
|
-
|
154
|
-
|
152
|
+
TestModelRepository.expects(:find_by).with(attribute: "value").returns(mock(:first))
|
153
|
+
TestModelRepository.find_first_by(attribute: "value")
|
155
154
|
end
|
156
155
|
end
|
157
156
|
|
158
157
|
describe "all" do
|
159
158
|
it "finds by attributes without any attributes" do
|
160
|
-
|
161
|
-
|
159
|
+
TestModelRepository.expects(:find_by).with()
|
160
|
+
TestModelRepository.all
|
162
161
|
end
|
163
162
|
end
|
164
163
|
|
165
164
|
describe "find_by_id" do
|
166
165
|
it "finds by id" do
|
167
|
-
|
168
|
-
|
166
|
+
TestModelRepository.expects(:find_by).with(_id: "bson-id")
|
167
|
+
TestModelRepository.find_by_id("bson-id")
|
169
168
|
end
|
170
169
|
end
|
171
170
|
|
172
171
|
describe "find_first_by_id" do
|
173
172
|
it "finds first by id" do
|
174
|
-
|
175
|
-
|
173
|
+
TestModelRepository.expects(:find_first_by).with(_id: "bson-id")
|
174
|
+
TestModelRepository.find_first_by_id("bson-id")
|
176
175
|
end
|
177
176
|
end
|
178
177
|
|
179
178
|
describe "dynamic finders" do
|
180
179
|
it "dynamically matches find_by_ finders" do
|
181
|
-
|
182
|
-
|
180
|
+
TestModelRepository.expects(:find_by).with(email: "foobar@fibroblast.com")
|
181
|
+
TestModelRepository.find_by_email("foobar@fibroblast.com")
|
183
182
|
end
|
184
183
|
|
185
184
|
it "dynamically matches find_first_by_ finders" do
|
186
|
-
|
187
|
-
|
185
|
+
TestModelRepository.expects(:find_first_by).with(email: "foobar@fibroblast.com")
|
186
|
+
TestModelRepository.find_first_by_email("foobar@fibroblast.com")
|
188
187
|
end
|
189
188
|
|
190
189
|
it "responds to dynamically matched find_by_ finders" do
|
191
|
-
|
190
|
+
TestModelRepository.respond_to?(:find_by_email).must_equal true
|
192
191
|
end
|
193
192
|
|
194
193
|
it "responds to dynamically matched find_first_by_ finders" do
|
195
|
-
|
194
|
+
TestModelRepository.respond_to?(:find_first_by_email).must_equal true
|
196
195
|
end
|
197
196
|
end
|
198
197
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: collector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Weiss
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-06-
|
11
|
+
date: 2013-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
95
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.0.
|
96
|
+
rubygems_version: 2.0.3
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: An implementation of the Repository Pattern for MongoDB
|