s3db 0.0.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.
@@ -0,0 +1,286 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe S3DB::Record do
4
+ before :each do
5
+ S3DB::FileBackend.delete(TEST_DB_BASE_PATH)
6
+ @backend = S3DB::FileBackend.create(TEST_DB_BASE_PATH)
7
+ @database = S3DB::Database.create(@backend, 'testdb')
8
+ @collection = S3DB::Collection.create(@database, 'testcoll')
9
+ end
10
+
11
+ after :each do
12
+ S3DB::FileBackend.delete(TEST_DB_BASE_PATH)
13
+ end
14
+
15
+ class TestRecord
16
+ include S3DB::Record
17
+ end
18
+
19
+ describe S3DB::Record::ClassMethods do
20
+ before :each do
21
+ TestRecord.id_generator nil
22
+ TestRecord.id_field nil
23
+ TestRecord._database = nil
24
+ TestRecord._collection = nil
25
+ end
26
+
27
+ it 'responds to setters' do
28
+ expect(TestRecord).to respond_to(:database)
29
+ expect(TestRecord).to respond_to(:collection_name)
30
+ expect(TestRecord).to respond_to(:id_generator)
31
+ expect(TestRecord).to respond_to(:id_field)
32
+ expect(TestRecord).to respond_to(:string)
33
+ expect(TestRecord).to respond_to(:all)
34
+ expect(TestRecord).to respond_to(:find)
35
+ expect(TestRecord).to respond_to(:create)
36
+ end
37
+
38
+ describe '::database' do
39
+ it 'sets @_database' do
40
+ expect(TestRecord._database).to be_nil
41
+ expect(TestRecord.database(:db)).to eq :db
42
+ expect(TestRecord._database).to eq :db
43
+ end
44
+ end
45
+
46
+ describe '::collection_name' do
47
+ it 'sets @_collection to a new collection with the name' do
48
+ expect(TestRecord._collection).to be_nil
49
+ TestRecord.database(@database)
50
+ expect(TestRecord.collection_name(@collection.name)).to \
51
+ be_a(S3DB::Collection)
52
+ expect(TestRecord._collection).to be_a(S3DB::Collection)
53
+ expect(TestRecord._collection.name).to eq @collection.name
54
+ end
55
+ end
56
+
57
+ describe '::id_generator' do
58
+ it 'sets @_id_generator' do
59
+ proc = Proc.new {}
60
+ expect(TestRecord._id_generator).to be_nil
61
+ expect(TestRecord.id_generator(proc)).to eq proc
62
+ expect(TestRecord._id_generator).to eq proc
63
+ end
64
+ end
65
+
66
+ describe '::id_field' do
67
+ it 'sets @_database' do
68
+ TestRecord._id_field = nil
69
+ expect(TestRecord._id_field).to be_nil
70
+ expect(TestRecord.id_field(:id)).to eq 'id'
71
+ expect(TestRecord._id_field).to eq 'id'
72
+ end
73
+ end
74
+
75
+ describe '::string' do
76
+ it 'adds a key to the schema with String value' do
77
+ expect(TestRecord._schema).to be_nil
78
+ expect(TestRecord.string(:name)).to eq({'name' => 'String'})
79
+ expect(TestRecord._schema).to eq({'name' => 'String'})
80
+ expect(TestRecord.string(:word)).to eq({'name' => 'String', 'word' => 'String'})
81
+ expect(TestRecord._schema).to eq({'name' => 'String', 'word' => 'String'})
82
+ end
83
+ end
84
+
85
+ describe '::all' do
86
+ it 'lists all available records' do
87
+ TestRecord.database @database
88
+ TestRecord.collection_name @collection.name
89
+
90
+ expect(TestRecord._collection).to receive(:list_records) {[]}
91
+ TestRecord.all
92
+ end
93
+ end
94
+
95
+ describe '::find' do
96
+ let(:filename) { 'testfile' }
97
+
98
+ it 'looks up a record by filename' do
99
+ TestRecord.database @database
100
+ TestRecord.collection_name @collection.name
101
+
102
+ expect(TestRecord._database.backend).to \
103
+ receive(:read_record) \
104
+ .with(TestRecord._database.name, TestRecord._collection.name, filename + '.json') \
105
+ .and_return('{}')
106
+ TestRecord.find(filename)
107
+ end
108
+
109
+ it 'raises an error with a missing file' do
110
+ TestRecord.database @database
111
+ TestRecord.collection_name @collection.name
112
+
113
+ expect(TestRecord._database.backend).to \
114
+ receive(:read_record).with(
115
+ TestRecord._database.name,
116
+ TestRecord._collection.name,
117
+ filename + '.json'
118
+ )
119
+
120
+ expect do
121
+ TestRecord.find(filename)
122
+ end.to raise_error ArgumentError, 'missing record!'
123
+ end
124
+ end
125
+
126
+ describe 'create' do
127
+ before :each do
128
+ TestRecord.database @database
129
+ TestRecord.collection_name @collection.name
130
+ end
131
+
132
+ let(:data) do
133
+ Hash.new
134
+ end
135
+
136
+ it 'instantiates a record' do
137
+ expect(TestRecord).to receive(:new).with(data).and_call_original
138
+ allow_any_instance_of(TestRecord).to receive(:save)
139
+
140
+ TestRecord.create(data)
141
+ end
142
+
143
+ it 'saves a record' do
144
+ expect_any_instance_of(TestRecord).to receive(:save)
145
+ allow(TestRecord).to receive(:new).and_call_original
146
+
147
+ TestRecord.create(data)
148
+ end
149
+ end
150
+ end
151
+
152
+ describe 'instance methods' do
153
+ subject do
154
+ TestRecord.id_field nil
155
+ TestRecord.id_generator nil
156
+ TestRecord.database @database
157
+ TestRecord.collection_name @collection.name
158
+ TestRecord.new({})
159
+ end
160
+
161
+ describe '#initialize' do
162
+ it 'sets data' do
163
+ expect(TestRecord.new({}).instance_variable_get(:@data)).to eq({})
164
+ end
165
+ end
166
+
167
+ describe '#new_record?' do
168
+ it 'returns true when the record has no _id' do
169
+ subject.instance_variable_set(:@data, {})
170
+ expect(subject.new_record?).to eq true
171
+ end
172
+
173
+ it 'returns false when the record has an _id' do
174
+ subject.instance_variable_set(:@data, {'_id' => 'present'})
175
+ expect(subject.new_record?).to eq false
176
+ end
177
+ end
178
+
179
+ describe '#save' do
180
+ it 'sets the id' do
181
+ expect(subject).to receive(:_set_id)
182
+
183
+ subject.save
184
+ end
185
+
186
+ it 'writes to disk' do
187
+ subject.save
188
+ expect(subject.class._database.backend).to \
189
+ receive(:write_record).with(
190
+ subject.class._database.name,
191
+ subject.class._collection.name,
192
+ subject.__send__(:_filename),
193
+ subject.data.to_json,
194
+ )
195
+
196
+ subject.save
197
+ end
198
+ end
199
+
200
+ describe '#save!' do
201
+ it 'raises an error on save failure' do
202
+ expect(subject).to receive(:save).and_return(false)
203
+
204
+ expect { subject.save! }.to raise_error ArgumentError, 'failed to save!'
205
+ end
206
+ end
207
+
208
+ describe '#update' do
209
+ it 'updates the data' do
210
+ subject.save
211
+ expect(subject.data).to eq({'_id' => subject._id})
212
+ expect(subject.update({'name' => 'jack'})).to eq subject
213
+ expect(subject.data).to eq({'name' => 'jack', '_id' => subject._id})
214
+ end
215
+ end
216
+
217
+ describe '#_id' do
218
+ it 'returns the _id key from @data' do
219
+ expect(subject._id).to be_nil
220
+
221
+ subject.instance_variable_set(:@data, {'_id' => 'myid' })
222
+ expect(subject._id).to eq('myid')
223
+ end
224
+ end
225
+
226
+ describe '#_id=' do
227
+ it 'sets the _id key in @data' do
228
+ subject.instance_variable_set(:@data, {})
229
+ expect(subject._id=('otherid')).to eq('otherid')
230
+ expect(subject.instance_variable_get(:@data)).to eq({'_id' => 'otherid'})
231
+ end
232
+ end
233
+
234
+ describe 'private #_filename' do
235
+ it 'appends .json to the _id' do
236
+ subject._id = 'fileid'
237
+ expect(subject.__send__(:_filename)).to eq('fileid.json')
238
+ end
239
+ end
240
+
241
+ describe 'private #_set_id' do
242
+ it 'returns the _id if set' do
243
+ subject._id = 'newid'
244
+ TestRecord._id_generator = Proc.new {}
245
+ TestRecord._id_field = nil
246
+ expect(subject.class._id_generator).to_not receive(:call)
247
+ expect(UUIDTools::UUID).to_not receive(:random_create)
248
+ expect(subject.__send__(:_set_id)).to eq('newid')
249
+ expect(subject._id).to eq('newid')
250
+ end
251
+
252
+ it 'sets a uuid if _id is not set and _id_generator is nil' do
253
+ subject._id = nil
254
+ TestRecord._id_generator = nil
255
+ TestRecord._id_field = 'id'
256
+ expect(UUIDTools::UUID).to receive(:random_create).and_call_original
257
+
258
+ expect(subject.__send__(:_set_id)).to match(/\w+\-\w+\-\w+-\w+-\w+/)
259
+ end
260
+
261
+ it 'sets a uuid if _id is not set and _id_field is nil' do
262
+ subject._id = nil
263
+ TestRecord._id_generator = Proc.new { |n| 'proc' }
264
+ TestRecord._id_field = nil
265
+ expect(UUIDTools::UUID).to receive(:random_create).and_call_original
266
+
267
+ expect(subject.__send__(:_set_id)).to match(/\w+\-\w+\-\w+-\w+-\w+/)
268
+ end
269
+
270
+ it 'calls the proc if _id is not set but _id_field and _id_generator are' do
271
+ subject
272
+ TestRecord.id_generator Proc.new { |n| "#{n}proc" }
273
+ TestRecord.id_field 'name'
274
+ subject._id = nil
275
+ subject.instance_variable_set(:@data, {'name' => 'jack' })
276
+
277
+ expect(subject.__send__(:_set_id)).to eq('jackproc')
278
+ end
279
+ end
280
+
281
+ describe 'private #_valid' do
282
+ it 'checks the schema'
283
+ end
284
+ end
285
+ end
286
+
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe S3DB::Utils do
4
+ describe '::sanitize' do
5
+ it 'only allows word characters' do
6
+ [
7
+ 'test',
8
+ '383',
9
+ 'HjLoo',
10
+ ].each do |input|
11
+ expect do
12
+ S3DB::Utils.sanitize(input)
13
+ end.to_not raise_error
14
+ end
15
+
16
+ [
17
+ 'te st',
18
+ 383,
19
+ String,
20
+ ].each do |input|
21
+ expect do
22
+ S3DB::Utils.sanitize(input)
23
+ end.to raise_error ArgumentError, 'invalid input!'
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,110 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'simplecov'
4
+
5
+ SimpleCov.start do
6
+ add_filter '/spec'
7
+ end
8
+
9
+ require_relative '../lib/s3db'
10
+
11
+ TEST_DB_BASE_PATH = '/tmp/s3dbtestpath'
12
+
13
+ FileUtils.rm_rf(TEST_DB_BASE_PATH)
14
+
15
+ # This file was generated by the `rspec --init` command. Conventionally, all
16
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
17
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
18
+ # this file to always be loaded, without a need to explicitly require it in any
19
+ # files.
20
+ #
21
+ # Given that it is always loaded, you are encouraged to keep this file as
22
+ # light-weight as possible. Requiring heavyweight dependencies from this file
23
+ # will add to the boot time of your test suite on EVERY test run, even for an
24
+ # individual file that may not need all of that loaded. Instead, consider making
25
+ # a separate helper file that requires the additional dependencies and performs
26
+ # the additional setup, and require it from the spec files that actually need
27
+ # it.
28
+ #
29
+ # The `.rspec` file also contains a few flags that are not defaults but that
30
+ # users commonly want.
31
+ #
32
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
33
+ RSpec.configure do |config|
34
+ # rspec-expectations config goes here. You can use an alternate
35
+ # assertion/expectation library such as wrong or the stdlib/minitest
36
+ # assertions if you prefer.
37
+ config.expect_with :rspec do |expectations|
38
+ # This option will default to `true` in RSpec 4. It makes the `description`
39
+ # and `failure_message` of custom matchers include text for helper methods
40
+ # defined using `chain`, e.g.:
41
+ # be_bigger_than(2).and_smaller_than(4).description
42
+ # # => "be bigger than 2 and smaller than 4"
43
+ # ...rather than:
44
+ # # => "be bigger than 2"
45
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
46
+ end
47
+
48
+ # rspec-mocks config goes here. You can use an alternate test double
49
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
50
+ config.mock_with :rspec do |mocks|
51
+ # Prevents you from mocking or stubbing a method that does not exist on
52
+ # a real object. This is generally recommended, and will default to
53
+ # `true` in RSpec 4.
54
+ mocks.verify_partial_doubles = true
55
+ end
56
+
57
+ # The settings below are suggested to provide a good initial experience
58
+ # with RSpec, but feel free to customize to your heart's content.
59
+ =begin
60
+ # These two settings work together to allow you to limit a spec run
61
+ # to individual examples or groups you care about by tagging them with
62
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
63
+ # get run.
64
+ config.filter_run :focus
65
+ config.run_all_when_everything_filtered = true
66
+
67
+ # Allows RSpec to persist some state between runs in order to support
68
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
69
+ # you configure your source control system to ignore this file.
70
+ config.example_status_persistence_file_path = "spec/examples.txt"
71
+
72
+ # Limits the available syntax to the non-monkey patched syntax that is
73
+ # recommended. For more details, see:
74
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
75
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
76
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
77
+ config.disable_monkey_patching!
78
+
79
+ # This setting enables warnings. It's recommended, but in some cases may
80
+ # be too noisy due to issues in dependencies.
81
+ config.warnings = true
82
+
83
+ # Many RSpec users commonly either run the entire suite or an individual
84
+ # file, and it's useful to allow more verbose output when running an
85
+ # individual spec file.
86
+ if config.files_to_run.one?
87
+ # Use the documentation formatter for detailed output,
88
+ # unless a formatter has already been configured
89
+ # (e.g. via a command-line flag).
90
+ config.default_formatter = 'doc'
91
+ end
92
+
93
+ # Print the 10 slowest examples and example groups at the
94
+ # end of the spec run, to help surface which specs are running
95
+ # particularly slow.
96
+ config.profile_examples = 10
97
+
98
+ # Run specs in random order to surface order dependencies. If you find an
99
+ # order dependency and want to debug it, you can fix the order by providing
100
+ # the seed, which is printed after each run.
101
+ # --seed 1234
102
+ config.order = :random
103
+
104
+ # Seed global randomization in this process using the `--seed` CLI option.
105
+ # Setting this allows you to use `--seed` to deterministically reproduce
106
+ # test failures related to randomization by passing the same `--seed` value
107
+ # as the one that triggered the failure.
108
+ Kernel.srand config.seed
109
+ =end
110
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3db
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jack Brown
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: uuidtools
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.1.5
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.1.5
33
+ - !ruby/object:Gem::Dependency
34
+ name: json
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.8'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.8.3
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.8'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.8.3
53
+ - !ruby/object:Gem::Dependency
54
+ name: guard-rspec
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '4.6'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 4.6.4
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '4.6'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 4.6.4
73
+ - !ruby/object:Gem::Dependency
74
+ name: rspec
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '3.3'
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.3.0
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.3'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 3.3.0
93
+ - !ruby/object:Gem::Dependency
94
+ name: simplecov
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '0.10'
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 0.10.0
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '0.10'
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 0.10.0
113
+ description: S3-backed storage engine
114
+ email:
115
+ - jack@brownjohnf.com
116
+ executables:
117
+ - s3db
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - "./lib/s3db.rb"
122
+ - "./s3db.gemspec"
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - bin/s3db
126
+ - lib/s3db.rb
127
+ - lib/s3db/backend.rb
128
+ - lib/s3db/collection.rb
129
+ - lib/s3db/database.rb
130
+ - lib/s3db/file_backend.rb
131
+ - lib/s3db/record.rb
132
+ - lib/s3db/utils.rb
133
+ - spec/lib/s3db/collection_spec.rb
134
+ - spec/lib/s3db/database_spec.rb
135
+ - spec/lib/s3db/file_backend_spec.rb
136
+ - spec/lib/s3db/record_spec.rb
137
+ - spec/lib/s3db/utils_spec.rb
138
+ - spec/spec_helper.rb
139
+ homepage: https://github.com/brownjohnf/s3db
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.4.5
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Leverage the endless cheap capacity of s3 for high-latency storage
163
+ test_files:
164
+ - spec/lib/s3db/collection_spec.rb
165
+ - spec/lib/s3db/database_spec.rb
166
+ - spec/lib/s3db/file_backend_spec.rb
167
+ - spec/lib/s3db/record_spec.rb
168
+ - spec/lib/s3db/utils_spec.rb
169
+ - spec/spec_helper.rb