Skip to content

Commit

Permalink
test(collection): add tests on crud methodes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasalexandre9 committed Jan 27, 2025
1 parent 028947e commit 937248f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def aggregate(_caller, filter, aggregation, limit = nil)
end

def create(_caller, data)
Utils::MongoidSerializer.new(@model.create(data)).to_hash(ProjectionFactory.all(self))
Utils::MongoidSerializer.new(model.create(data)).to_hash(ProjectionFactory.all(self))
end

def update(_caller, filter, data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def hash_object(object, projection = nil, with_associations: true)

return if object.nil?

object.attributes.each do |key, value|
object.attributes.select { |key, _value| projection.columns.include? key }.each do |key, value|
hash[key] = value
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'spec_helper'

module ForestAdminDatasourceMongoid
include ForestAdminDatasourceToolkit::Components::Query

describe Collection do
let(:datasource) { ForestAdminDatasourceMongoid::Datasource.new }
let(:collection_post) { described_class.new(datasource, Post) }
Expand Down Expand Up @@ -63,16 +65,96 @@ module ForestAdminDatasourceMongoid
expect(JSON.parse(json_schema)['fields'].keys).to include('title', 'body', 'comments')
end

# TODO: works but waiting crud is fully implemented
# describe '#list' do
# it 'returns a serialized list of records' do
# allow(Utils::Query).to receive_message_chain(:new, :get).and_return([Post.new(title: 'Test', body: 'Body')])
# allow(Utils::MongoidSerializer).to receive(:new).and_return(double(to_hash: { title: 'Test', body: 'Body' }))
#
# result = collection_post.list(nil, {}, nil)
#
# expect(result).to eq([{ title: 'Test', body: 'Body' }])
# end
# end
describe '#list' do
it 'returns a serialized list of records' do
post = Post.new(title: 'Test', body: 'Body')
query_result = [post]
query = instance_double(Utils::Query, get: query_result)
allow(Utils::Query).to receive_messages(new: query)

result = collection_post.list(nil, Filter.new, Projection.new(%w[_id title body]))

expect(result).to eq([{ '_id' => post._id, 'title' => post.title, 'body' => post.body }])
end

it 'returns a serialized list of records with many to one relation' do
post = Post.new(title: 'Test', body: 'Body')
comment = Comment.new(name: 'foo', message: 'lorem ipsum', post: post)
query_result = [comment]
query = instance_double(Utils::Query, get: query_result)
allow(Utils::Query).to receive_messages(new: query)

result = collection_comment.list(nil, Filter.new, Projection.new(%w[_id name message post:title]))

expect(result).to eq(
[
{
'_id' => comment._id,
'message' => comment.message,
'name' => comment.name,
'post' => { 'title' => post.title }
}
]
)
end
end

describe '#aggregate' do
it 'call QueryAggregate and return an aggregate result' do
Post.new(title: 'Test', body: 'Body')
query = instance_double(Utils::QueryAggregate, get: [{ 'value' => 1 }])
allow(Utils::QueryAggregate).to receive_messages(new: query)

filter = Filter.new
aggregation = Aggregation.new(operation: 'Count')
result = collection_post.aggregate(nil, filter, aggregation, 10)

expect(Utils::QueryAggregate).to(
have_received(:new) do |collection, aggregation_request, filter_request, limit|
expect(collection).to eq(collection_post)
expect(filter).to eq(filter_request)
expect(aggregation).to eq(aggregation_request)
expect(limit).to eq(10)
end
)
expect(result).to eq([{ 'value' => 1 }])
end
end

describe '#create' do
it 'call create method of model and return object with full projection' do
post = Post.new(title: 'Test', body: 'Body')
model = class_double(Post, create: post)
allow(collection_post).to receive(:model).and_return(model)

result = collection_post.create(nil, { title: 'Test', body: 'Body' })

expect(result).to eq({ '_id' => post._id, 'title' => post.title, 'body' => post.body, 'author' => nil })
end
end

describe '#update' do
it 'call update method of object' do
model = instance_double(Post, update: true)
query = instance_double(Utils::Query, build: [model])
allow(Utils::Query).to receive_messages(new: query)

collection_post.update(nil, Filter.new, { title: 'new title' })
expect(model).to have_received(:update) do |data|
expect(data).to eq({ title: 'new title' })
end
end
end

describe '#delete' do
it 'call destroy method of object' do
model = instance_double(Post, destroy: true)
query = instance_double(Utils::Query, build: [model])
allow(Utils::Query).to receive_messages(new: query)

collection_post.delete(nil, Filter.new)
expect(model).to have_received(:destroy)
end
end
end
end

0 comments on commit 937248f

Please sign in to comment.