forked from rom-rb/rom-mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway_spec.rb
103 lines (79 loc) · 2.61 KB
/
gateway_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require 'rom/repository'
RSpec.describe 'Mongo gateway' do
include_context 'database'
include_context 'users'
let(:gateway) { container.gateways[:default] }
describe 'auto-struct' do
it 'returns mapped object' do
jane = users.by_name('Jane').one!
expect(jane._id.to_s).to eql(users.find(name: 'Jane').one._id.to_s)
expect(jane.name).to eql('Jane')
expect(jane.email).to eql('[email protected]')
end
end
describe 'with a repository' do
let(:repo) do
Class.new(ROM::Repository[:users]) do
commands :create, update: :by_pk
end.new(container)
end
it 'returns auto-mapped structs' do
jane = repo.users.by_name('Jane').one!
expect(jane._id.to_s)
.to eql(users.find(name: 'Jane').one['_id'].to_s)
expect(jane.name).to eql('Jane')
expect(jane.email).to eql('[email protected]')
end
it 'uses #by_pk for update commands' do
repo.update(jane_id, name: 'Jane Doe')
expect(users.by_pk(jane_id).one!.name).to eql('Jane Doe')
end
end
describe 'gateway#dataset?' do
it 'returns true if a collection exists' do
expect(gateway.dataset?(:users)).to be(true)
end
it 'returns false if a does not collection exist' do
expect(gateway.dataset?(:not_here)).to be(false)
end
end
describe 'commands' do
let(:commands) { container.commands[:users] }
describe 'create' do
it 'inserts a document into collection' do
id = BSON::ObjectId.new
result = commands.try do
commands.create.call(_id: id, name: 'joe', email: '[email protected]')
end
expect(result)
.to match_array([{ _id: id, name: 'joe', email: '[email protected]' }])
end
end
describe 'update' do
it 'updates a document in the collection' do
jane = users.by_name('Jane').one!
result = commands.try do
commands.update.by_name('Jane').call(email: '[email protected]')
end
expect(result).to match_array(
[{ '_id' => BSON::ObjectId.from_string(jane._id),
'name' => 'Jane',
'email' => '[email protected]' }]
)
end
end
describe 'delete' do
it 'deletes documents from the collection' do
jane = users.by_name('Jane').one!
joe = users.by_name('Joe').one!
result = commands.delete.by_name('Joe').call
expect(result.map(&:to_h)).to match_array(
[{ _id: BSON::ObjectId.from_string(joe._id),
name: 'Joe',
email: '[email protected]' }]
)
expect(users.all).to match_array([jane])
end
end
end
end