Proc.new do
let(:data) do
{
'foo' => nil
}
end
Query = Struct.new(:name, :args)
it 'inherits from Store::DataMapper' do
assert_kind_of Store::DataMapper, subject
end
it 'has no uid by default' do
assert_nil data['uid']
end
describe 'insert' do
it 'adds item to collection' do
assert_equal 0, subject.count
subject.insert(data)
assert_equal 1, subject.count
end
it 'returns uid on insert' do
uid = subject.insert(data)
assert_kind_of String, uid
assert_kind_of Fixnum, uid.to_i
end
it 'sets uid on insert' do
uid = subject.insert(data)
assert_equal uid, subject.single_find(uid)['uid']
end
it 'auto increment uid on insert' do
uid = subject.insert(data)
expected_uid = (uid.to_i + 1).to_s
assert_equal expected_uid, subject.insert(data)
end
end
describe 'update' do
it 'adds no new item to collection' do
assert_equal 0, subject.count
subject.update(subject.insert(data), data)
assert_equal 1, subject.count
end
it 'returns uid on update' do
uid = subject.update(subject.insert(data), data)
assert_kind_of String, uid
assert_kind_of Fixnum, uid.to_i
end
it 'preserves the uid on update' do
uid = subject.update(subject.insert(data), data)
assert_equal uid, subject.single_find(uid)['uid']
end
it 'updates the data' do
uid = subject.insert(data)
assert_nil subject.single_find(uid)['foo']
uid = subject.update(uid, data.merge('foo' => 'bar'))
assert_equal 'bar', subject.single_find(uid)['foo']
end
end
describe 'delete' do
it 'deletes item from collection' do
uid = subject.insert(data)
assert_equal 1, subject.count
subject.delete(uid)
assert_equal 0, subject.count
end
it 'returns uid on delete' do
uid = subject.insert(data)
uid = subject.delete(uid)
assert_kind_of String, uid
assert_kind_of Fixnum, uid.to_i
end
end
describe 'single_find' do
it 'returns a single item' do
uid = subject.insert(data)
item = subject.single_find(uid)
assert_equal data, item.tap {|i| i.delete('uid')}
end
end
describe 'bulk_find' do
let(:datas) do
[
data.merge('foo' => 'here'),
data.merge('foo' => 'there'),
data.merge('foo' => 'not_me')
]
end
it 'returns multiple items' do
ids = []
ids << subject.insert(datas[0])
ids << subject.insert(datas[1])
subject.insert(datas[2])
expected = {ids[0] => {"foo"=>"here", "uid"=>ids[0]}, ids[1] => {"foo"=>"there", "uid"=>ids[1]}}
assert_equal expected, subject.bulk_find(ids)
end
end
describe 'all' do
let(:datas) do
[
data.merge('foo' => 'here'),
data.merge('foo' => 'there')
]
end
it 'returns all items' do
ids = []
ids << subject.insert(datas[0])
ids << subject.insert(datas[1])
expected = {ids[0] => {"foo"=>"here", "uid"=>ids[0]}, ids[1] => {"foo"=>"there", "uid"=>ids[1]}}
assert_equal expected, subject.all
end
end
end