mattly-exegesis 0.0.10 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,120 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
-
3
- class FoosDesign < Exegesis::Design
4
- view_by :foo
5
- view_by :foo, :bar
6
- end
7
- class CustomDesignDirDesign < Exegesis::Design
8
- designs_directory File.join(File.dirname(__FILE__), 'fixtures')
9
- end
10
-
11
-
12
- class ComposingDesignDocTest < Test::Unit::TestCase
13
- before(:all) { Exegesis.designs_directory = fixtures_path('designs') }
14
-
15
- context "setting a custom designs directory" do
16
- before do
17
- @custom_design_dir = File.join(File.dirname(__FILE__), 'fixtures')
18
- end
19
-
20
- expect { CustomDesignDirDesign.designs_directory.to_s.will == @custom_design_dir }
21
-
22
- end
23
-
24
- context "composing design docs from local sources" do
25
- before do
26
- @design = FoosDesign.design_doc
27
- @file = File.read(fixtures_path('designs/foos.js'))
28
- @jsdoc = Johnson.evaluate("v=#{@file}")
29
- end
30
-
31
- expect { @design.has_key?('_id').will be(true) }
32
- expect { @design['_id'].will == '_design/foos' }
33
- expect { @design['views']['by_bar']['map'].will == @jsdoc['views']['by_bar']['map'].toString }
34
- end
35
-
36
- context "composing a design doc from view_by declarations" do
37
- before do
38
- @design = FoosDesign.design_doc
39
- @by_foo = @design['views']['by_foo']['map']
40
- @by_foo_and_bar = @design['views']['by_foo_and_bar']['map']
41
- end
42
-
43
- expect { @by_foo.will include("if (doc['.kind'] == 'Foos' && doc['foo'])") }
44
- expect { @by_foo.will include("emit(doc['foo'], null);") }
45
-
46
- expect { @by_foo_and_bar.will include("if (doc['.kind'] == 'Foos' && doc['foo'] && doc['bar'])") }
47
- expect { @by_foo_and_bar.will include("emit([doc['foo'], doc['bar']], null)") }
48
- end
49
-
50
- context "building a hash a design doc" do
51
- before do
52
- @design = {
53
- 'views' => {
54
- 'a' => {'map' => 'some value', 'reduce' => 'some value'},
55
- 'b' => {'map' => 'some value'}
56
- }
57
- }
58
- funcs = @design['views'].map{|name, view| "//view/#{name}/#{view['map']}/#{view['reduce']}" }.sort
59
- @hashed = Digest::MD5.hexdigest(funcs.join)
60
- end
61
- expect { FoosDesign.hash_for_design(@design).will == @hashed }
62
- end
63
-
64
- context "syncronising with a databae" do
65
- before do
66
- reset_db
67
- end
68
-
69
- context "pushing design doc when it doesn't exist yet" do
70
- before do
71
- foo = FoosDesign.new(@db)
72
- foo.push_design!
73
- @get_design = lambda { @db.get('_design/foos') }
74
- @design = @get_design.call rescue nil
75
- end
76
-
77
- expect { @get_design.wont raise_error }
78
- expect { @design['_rev'].will =~ /^[0-9]+$/ }
79
- expect { @design['language'].will == 'javascript' }
80
- expect { @design['views'].has_key?('by_bar').will be(true) }
81
- end
82
-
83
- context "reading the existing design document" do
84
- before do
85
- @db.save_doc(FoosDesign.design_doc)
86
- @foo = FoosDesign.new(@db)
87
- end
88
-
89
- expect { lambda {@foo.design_doc}.wont raise_error }
90
- expect { @foo.design_doc['_rev'].will =~ /^[0-9]+$/ }
91
- expect { @foo.design_doc['views'].keys.will == FoosDesign.design_doc['views'].keys }
92
- end
93
-
94
- context "updating an existing doc" do
95
-
96
- context "when it hasn't changed" do
97
- before do
98
- @db.save_doc(FoosDesign.design_doc)
99
- @foo = FoosDesign.new(@db)
100
- end
101
-
102
- expect { lambda { @foo.push_design! }.wont change { @db.get('_design/foos')['_rev'] } }
103
- end
104
-
105
- context "when it has chagned" do
106
- before do
107
- @db.save_doc({'_id' => '_design/foos',
108
- 'views' => {'a' => {'map' => 'function(doc) { emit(true, null)}'}}
109
- })
110
- @foo = FoosDesign.new(@db)
111
- @pushing = lambda{ @foo.push_design! }
112
- end
113
-
114
- expect { @pushing.will change { @db.get('_design/foos')['_rev'] } }
115
- expect { @pushing.call; @foo.design_doc_hash.will == FoosDesign.design_doc_hash }
116
- end
117
- end
118
-
119
- end
120
- end
@@ -1,284 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
-
3
- class Foo < Exegesis::Document
4
- expose :ref, :as => :reference
5
- end
6
- class Bar < Exegesis::Document; end
7
-
8
- class WithDefault < Exegesis::Document
9
- default :foo => 'bar'
10
- end
11
-
12
- class Exposer < Exegesis::Document
13
- expose :foo, :bar
14
- expose :read_only, :writer => false
15
- expose :custom_writer, :writer => lambda {|val| {'value' => val} }
16
- expose :castee, :castees, :as => :given
17
- expose :time, :times, :as => Time
18
- expose :regex, :regexen, :as => Regexp
19
- expose :other_doc, :other_docs, :as => :reference
20
- end
21
-
22
- class Timestamper < Exegesis::Document
23
- timestamps!
24
- end
25
-
26
- class UniqueSnowflake < Exegesis::Document
27
- unique_id :set_id
28
- def set_id
29
- @unique_id_attempt.zero? ? "snowflake" : "snowflake-#{@unique_id_attempt}"
30
- end
31
- end
32
-
33
- class ExegesisDocumentClassDefinitionsTest < Test::Unit::TestCase
34
-
35
- context "a bare Exegesis::Document" do
36
- before do
37
- reset_db
38
- @obj = Foo.new
39
- @obj.database = @db
40
- @obj.save
41
- end
42
-
43
- expect { @obj['.kind'].will == "Foo" }
44
- expect { @obj['foo'].will == nil }
45
- expect { @obj.will_not respond_to(:foo) }
46
- expect { @obj['created_at'].will == nil }
47
- expect { @obj['updated_at'].will == nil }
48
- end
49
-
50
- context "instantiating" do
51
- expect { Exegesis::Document.instantiate({'.kind' => 'Foo'}).will be_kind_of(Foo) }
52
-
53
- context "transitions" do
54
- before do
55
- @foo = Foo.new
56
- @foo['.kind'] = 'Bar'
57
- @bar = Exegesis::Document.instantiate(@foo)
58
- end
59
-
60
- expect { @bar.will be_kind_of(Bar) }
61
- end
62
- end
63
-
64
- context "default objects" do
65
- expect { WithDefault.new['foo'].will == 'bar' }
66
- expect { WithDefault.new({'foo' => 'baz'})['foo'].will == 'baz' }
67
- end
68
-
69
- context "exposing keys" do
70
- context "regular declarations" do
71
- before do
72
- @obj = Exposer.new(:foo => 'bar', :bar => 'foo')
73
- @writing = lambda { @obj.bar = "bee" }
74
- end
75
- expect { @obj.foo.will == 'bar' }
76
- expect { @obj.bar.will == 'foo' }
77
- expect { @writing.wont raise_error }
78
- expect { @writing.call; @obj.bar.will == "bee" }
79
- end
80
-
81
- context "with a custom writer" do
82
- context "when false" do
83
- before { @obj = Exposer.new(:read_only => 'value') }
84
- expect { @obj.read_only.will == 'value' }
85
- expect { lambda{@obj.read_only = "other value"}.will raise_error(NoMethodError) }
86
- end
87
-
88
- context "when lambda" do
89
- before do
90
- @obj = Exposer.new(:custom_writer => 'value')
91
- @obj.custom_writer = 'other value'
92
- @expected = {'value' => 'other value'}
93
- end
94
- expect { @obj.custom_writer.will == @expected }
95
- end
96
- end
97
-
98
- context "when casting a value" do
99
- context "when as given" do
100
- before do
101
- @obj = Exposer.new({
102
- :castee => {'foo' => 'foo', '.kind' => 'Foo'},
103
- :castees => [{'foo' => 'foo', '.kind' => 'Foo'}, {'foo' => 'bar', '.kind' => 'Bar'}]
104
- })
105
- end
106
-
107
- expect { @obj.castee.class.will == Foo }
108
- expect { @obj.castee['foo'].will == 'foo' }
109
- expect { @obj.castee.parent.will == @obj }
110
-
111
- expect { @obj.castees.class.will == Array }
112
- expect { @obj.castees[0].class.will == Foo }
113
- expect { @obj.castees[0]['foo'].will == 'foo' }
114
- expect { @obj.castees[0].parent.will == @obj }
115
- expect { @obj.castees[1].class.will == Bar }
116
- expect { @obj.castees[1]['foo'].will == 'bar' }
117
- expect { @obj.castees[1].parent.will == @obj }
118
- end
119
-
120
- context "when as time" do
121
- before do
122
- @obj = Exposer.new({:time => Time.now.to_json, :times => [Time.local(2009,3,1).to_json, Time.local(2009,2,1).to_json]})
123
- end
124
-
125
- expect { @obj.time.class.will == Time }
126
- expect { @obj.time.to_f.will be_close(Time.now.to_f, 1) }
127
-
128
- expect { @obj.times.class.will == Array }
129
- expect { @obj.times[0].class.will == Time }
130
- expect { @obj.times[0].will == Time.local(2009,3,1) }
131
- expect { @obj.times[1].class.will == Time }
132
- expect { @obj.times[1].will == Time.local(2009,2,1) }
133
- end
134
-
135
- context "when as non document class" do
136
- before do
137
- @obj = Exposer.new({
138
- :regex => 'foo',
139
- :regexen => ['foo', 'bar']
140
- })
141
- end
142
-
143
- expect { @obj.regex.will == /foo/ }
144
-
145
- expect { @obj.regexen.class.will == Array }
146
- expect { @obj.regexen[0].will == /foo/ }
147
- expect { @obj.regexen[1].will == /bar/ }
148
- end
149
-
150
- context "when as reference" do
151
- context "with a database present" do
152
- before do
153
- reset_db
154
- @obj = Exposer.new(:other_doc => "other_doc", :other_docs => ["other_docs_1", "other_docs_2"])
155
- @obj.database = @db
156
- end
157
-
158
- context "when the document exists" do
159
- before do
160
- @db.bulk_save([
161
- {'.kind' => 'Foo', '_id' => 'other_doc'},
162
- {'.kind' => 'Foo', '_id' => 'other_docs_1'},
163
- {'.kind' => 'Foo', '_id' => 'other_docs_2'}
164
- ])
165
- end
166
-
167
- expect { @obj.other_doc.rev.will == @db.get('other_doc')['_rev'] }
168
- expect { @obj.other_doc.class.will == Foo }
169
- expect { @obj.other_docs.class.will == Array }
170
- expect { @obj.other_docs[0].rev.will == @db.get('other_docs_1')['_rev'] }
171
- expect { @obj.other_docs[0].class.will == Foo }
172
- expect { @obj.other_docs[1].rev.will == @db.get('other_docs_2')['_rev'] }
173
- expect { @obj.other_docs[1].class.will == Foo }
174
-
175
- context "caching" do
176
- before do
177
- @obj.other_doc
178
- doc = @db.get('other_doc')
179
- doc['foo'] = "updated"
180
- doc.save
181
- end
182
-
183
- expect { @obj.other_doc['foo'].will be(nil) }
184
- expect { @obj.other_doc(true)['foo'].will == 'updated' }
185
- end
186
- end
187
-
188
- context "when the document is missing" do
189
- expect { lambda{@obj.other_doc}.will raise_error(RestClient::ResourceNotFound) }
190
- expect { lambda{@obj.other_docs}.will raise_error(RestClient::ResourceNotFound) }
191
- end
192
-
193
- context "when the doucment has a parent" do
194
- before do
195
- @obj.castee = Foo.new({})
196
- @obj.castee.ref = 'other_doc'
197
- @obj.save
198
- @db.save_doc({'.kind' => 'Foo', '_id' => 'other_doc'})
199
- end
200
-
201
- expect { @obj.castee.ref.rev.will == @db.get('other_doc')['_rev'] }
202
- end
203
- end
204
-
205
- context "without a database present" do
206
- before { @obj = Exposer.new(:other_doc => "some_doc_id") }
207
- expect { lambda{@obj.other_doc}.will raise_error(ArgumentError) }
208
- end
209
- end
210
-
211
- context "when the value is nil" do
212
- before do
213
- @obj = Exposer.new({:castee => nil, :castees => nil, :regexen => ['foo', nil]})
214
- end
215
- expect { @obj.castee.will be(nil) }
216
- expect { @obj.castees.will be(nil) }
217
- expect { @obj.regexen.will == [/foo/] }
218
- end
219
- end
220
- end
221
-
222
- context "with timestamps" do
223
- before do
224
- reset_db
225
- @obj = Timestamper.new
226
- @obj.database = @db
227
- @obj.save
228
- @obj = Timestamper.new(@db.get(@obj.id))
229
- end
230
-
231
- context "initial save" do
232
- expect { @obj.created_at.to_f.will be_close(Time.now.to_f, 1) }
233
- expect { @obj.updated_at.to_f.will be_close(Time.now.to_f, 1) }
234
- end
235
-
236
- context "when created_at already exists" do
237
- before do
238
- @obj.database = @db
239
- @obj['created_at'] = Time.now
240
- @obj.save
241
- @obj = Timestamper.new(@db.get(@obj.id))
242
- end
243
-
244
- expect { @obj.created_at.to_f.will be_close(Time.now.to_f, 1) }
245
- expect { @obj.updated_at.to_f.will be_close(Time.now.to_f, 1) }
246
- end
247
-
248
- end
249
-
250
- context "with a custom unique_id setter" do
251
- before do
252
- reset_db
253
- @obj = UniqueSnowflake.new
254
- @obj.database = @db
255
- end
256
-
257
- context "when the id isn't in use yet" do
258
- before do
259
- @obj.save
260
- end
261
-
262
- expect { @obj.id.will == "snowflake" }
263
- end
264
-
265
- context "when there is an id in place already" do
266
- before do
267
- @obj['_id'] = 'foo'
268
- @obj.save
269
- end
270
-
271
- expect { @obj.id.will == "foo" }
272
- end
273
-
274
- context "when the desired id is already in use" do
275
- before do
276
- @db.save_doc({'_id' => 'snowflake', 'foo' => 'bar'})
277
- @obj.save
278
- end
279
-
280
- expect { @obj.id.will == 'snowflake-1' }
281
- end
282
- end
283
-
284
- end
@@ -1,40 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
-
3
- class DocInstance < Exegesis::Document
4
- expose :foo
5
- end
6
-
7
- class ExegesisDocumentInstanceMethodsTest < Test::Unit::TestCase
8
-
9
- context "update_attributes" do
10
- before do
11
- reset_db
12
- end
13
-
14
- context "an existing doc" do
15
- before do
16
- @doc = DocInstance.new({'foo' => 'bar'})
17
- @doc.database = @db
18
- @doc.save
19
- @old_rev = @doc.rev
20
- end
21
-
22
- context "without a matching rev" do
23
- expect { lambda {@doc.update_attributes({'foo' => 'bee'})}.will raise_error(ArgumentError) }
24
- expect { lambda {@doc.update_attributes({'foo' => 'bee', '_rev' => 'z'})}.will raise_error(ArgumentError) }
25
- end
26
-
27
- context "with a matching rev" do
28
- before do
29
- @doc.update_attributes({'_rev' => @doc.rev, 'foo' => 'bee', 'bar' => 'boo'})
30
- end
31
-
32
- expect { @doc['foo'].will == 'bee' }
33
- expect { @doc['bar'].will be(nil) }
34
- expect { @doc.rev.wont == @old_rev }
35
- end
36
- end
37
-
38
- end
39
-
40
- end
@@ -1,28 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
-
3
- class ExegesisTest < Test::Unit::TestCase
4
-
5
- context "designs directory" do
6
- context "setting custom" do
7
- before do
8
- @custom_design_dir = File.join(File.dirname(__FILE__), 'fixtures', 'designs')
9
- Exegesis.designs_directory = @custom_design_dir
10
- end
11
-
12
- expect { Exegesis.designs_directory.to_s.will == @custom_design_dir }
13
- end
14
-
15
-
16
- end
17
-
18
- context "database template" do
19
- before do
20
- @template_string = "http://localhost:5984/appname-%s"
21
- @account = "foo"
22
- Exegesis.database_template = @template_string
23
- end
24
-
25
- expect { Exegesis.database_for(@account).will == @template_string % @account }
26
- end
27
-
28
- end