Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(decorator): add operators equivalence support #20

Merged
merged 8 commits into from
Jan 2, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def self.parse_value(collection, leaf)
return values.map { |item| !%w[false 0 no].include?(item) } if schema.column_type == 'Boolean'

return values.map(&:to_f).select { |item| item.is_a? Numeric } if schema.column_type == 'Number'

return values
end

leaf['value']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def self.filterable?(type, supported_operators = [])
def self.get_required_operators(type)
return OPERATOR_BY_TYPE[type] if type.is_a?(String) && OPERATOR_BY_TYPE.key?(type)

return ['Includes_All'] if type.is_a? Array
return [Operators::INCLUDES_ALL] if type.is_a? Array

[]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Charts
review_class = Struct.new(:id, :book_id, :author)
stub_const('Review', review_class)

@datasource = Datasource.new
datasource = Datasource.new
collection_book = instance_double(
Collection,
name: 'book',
Expand Down Expand Up @@ -97,12 +97,14 @@ module Charts
}
)
allow(ForestAdminAgent::Builder::AgentFactory.instance).to receive(:send_schema).and_return(nil)
@datasource.add_collection(collection_book)
@datasource.add_collection(collection_review)
@datasource.add_collection(collection_book_review)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(@datasource)
datasource.add_collection(collection_book)
datasource.add_collection(collection_review)
datasource.add_collection(collection_book_review)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(datasource)
ForestAdminAgent::Builder::AgentFactory.instance.build

@datasource = ForestAdminAgent::Facades::Container.datasource

allow(ForestAdminAgent::Services::Permissions).to receive(:new).and_return(permissions)
allow(permissions).to receive_messages(
can_chart?: true,
Expand Down Expand Up @@ -171,6 +173,7 @@ module Charts
type: 'Value',
timezone: 'Europe/Paris'
})
@datasource.get_collection('book')
allow(@datasource.get_collection('book')).to receive(:aggregate).and_return(
[{ value: 10, group: [] }], # first call
[{ value: 5, group: [] }] # second call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ module Resources
let(:permissions) { instance_double(ForestAdminAgent::Services::Permissions) }

before do
@datasource = Datasource.new
collection = Collection.new(@datasource, 'user')
datasource = Datasource.new
collection = Collection.new(datasource, 'user')
allow(collection).to receive(:aggregate).and_return(
[
{ value: 1, group: [] }
]
)
allow(ForestAdminAgent::Builder::AgentFactory.instance).to receive(:send_schema).and_return(nil)

@datasource.add_collection(collection)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(@datasource)
datasource.add_collection(collection)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(datasource)
ForestAdminAgent::Builder::AgentFactory.instance.build
@datasource = ForestAdminAgent::Facades::Container.datasource

allow(ForestAdminAgent::Services::Permissions).to receive(:new).and_return(permissions)
allow(permissions).to receive_messages(can?: true, get_scope: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def respond_to?(arg)
end
stub_const('User', user_class)

@datasource = Datasource.new
datasource = Datasource.new

collection = instance_double(
Collection,
Expand All @@ -63,11 +63,12 @@ def respond_to?(arg)
)

allow(ForestAdminAgent::Builder::AgentFactory.instance).to receive(:send_schema).and_return(nil)
@datasource.add_collection(collection)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(@datasource)
datasource.add_collection(collection)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(datasource)
ForestAdminAgent::Builder::AgentFactory.instance.build

@datasource
@datasource = ForestAdminAgent::Facades::Container.datasource
allow(@datasource.get_collection('user')).to receive(:delete)
end

context 'with simple request' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Resources
user_class = Struct.new(:id, :first_name, :last_name)
stub_const('User', user_class)

@datasource = Datasource.new
datasource = Datasource.new
collection = instance_double(
Collection,
name: 'user',
Expand All @@ -42,9 +42,11 @@ module Resources
list: [User.new(1, 'foo', 'foo')]
)
allow(ForestAdminAgent::Builder::AgentFactory.instance).to receive(:send_schema).and_return(nil)
@datasource.add_collection(collection)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(@datasource)
datasource.add_collection(collection)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(datasource)
ForestAdminAgent::Builder::AgentFactory.instance.build
@datasource = ForestAdminAgent::Facades::Container.datasource
allow(@datasource.get_collection('user')).to receive(:list).and_return([User.new(1, 'foo', 'foo')])

allow(ForestAdminAgent::Services::Permissions).to receive(:new).and_return(permissions)
allow(permissions).to receive_messages(can?: true, get_scope: nil)
Expand Down Expand Up @@ -109,8 +111,8 @@ module Resources
{
aggregator: 'And',
conditions: [
{ field: 'id', operator: 'Greater_Than', value: 7 },
{ field: 'first_name', operator: 'Contains', value: 'foo' }
{ field: 'id', operator: 'greater_than', value: 7 },
{ field: 'first_name', operator: 'contains', value: 'foo' }
]
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Related
address_class = Struct.new(:id, :location)
stub_const('Address', address_class)

@datasource = Datasource.new
datasource = Datasource.new
collection_user = instance_double(
Collection,
name: 'user',
Expand Down Expand Up @@ -88,12 +88,14 @@ module Related
)

allow(ForestAdminAgent::Builder::AgentFactory.instance).to receive(:send_schema).and_return(nil)
@datasource.add_collection(collection_user)
@datasource.add_collection(collection_address_user)
@datasource.add_collection(collection_address)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(@datasource)
datasource.add_collection(collection_user)
datasource.add_collection(collection_address_user)
datasource.add_collection(collection_address)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(datasource)
ForestAdminAgent::Builder::AgentFactory.instance.build

@datasource = ForestAdminAgent::Facades::Container.datasource

allow(ForestAdminAgent::Services::Permissions).to receive(:new).and_return(permissions)
allow(permissions).to receive_messages(can?: true, get_scope: nil)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ module Related
include_context 'with caller'
subject(:dissociate) { described_class.new }

let(:datasource) { Datasource.new }
let(:permissions) { instance_double(ForestAdminAgent::Services::Permissions) }

before do
datasource = Datasource.new
collection_user = Collection.new(datasource, 'user')
collection_user.add_fields(
{
Expand Down Expand Up @@ -72,6 +72,8 @@ module Related
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(datasource)
ForestAdminAgent::Builder::AgentFactory.instance.build

@datasource = ForestAdminAgent::Facades::Container.datasource

allow(ForestAdminAgent::Services::Permissions).to receive(:new).and_return(permissions)
allow(permissions).to receive_messages(can?: true, get_scope: nil)
end
Expand Down Expand Up @@ -103,15 +105,15 @@ module Related
end

it 'call dissociate_or_delete_one_to_many without deletion' do
allow(datasource.get_collection('address_user')).to receive(:update).and_return(true)
allow(@datasource.get_collection('address_user')).to receive(:update).and_return(true)

args[:params]['relation_name'] = 'address_users'
args[:params]['data'] = [{ 'id' => 1 }]
args[:params]['id'] = 1

result = dissociate.handle_request(args)

expect(datasource.get_collection('address_user')).to have_received(:update) do |caller, filter, data|
expect(@datasource.get_collection('address_user')).to have_received(:update) do |caller, filter, data|
expect(caller).to be_instance_of(Components::Caller)
expect(filter).to have_attributes(
condition_tree: have_attributes(
Expand All @@ -134,8 +136,8 @@ module Related
end

it 'call dissociate_or_delete_one_to_many with deletion' do
allow(datasource.get_collection('address_user')).to receive(:delete).and_return(true)
allow(datasource.get_collection('address')).to receive(:delete).and_return(true)
allow(@datasource.get_collection('address_user')).to receive(:delete).and_return(true)
allow(@datasource.get_collection('address')).to receive(:delete).and_return(true)

args[:params][:delete] = true
args[:params]['relation_name'] = 'address_users'
Expand All @@ -144,7 +146,7 @@ module Related

result = dissociate.handle_request(args)

expect(datasource.get_collection('address_user')).to have_received(:delete) do |caller, filter|
expect(@datasource.get_collection('address_user')).to have_received(:delete) do |caller, filter|
expect(caller).to be_instance_of(Components::Caller)
expect(filter).to have_attributes(
condition_tree: have_attributes(
Expand All @@ -166,8 +168,8 @@ module Related
end

it 'call dissociate_or_delete_one_to_many with deletion on multiple records' do
allow(datasource.get_collection('address_user')).to receive(:delete).and_return(true)
allow(datasource.get_collection('address')).to receive(:delete).and_return(true)
allow(@datasource.get_collection('address_user')).to receive(:delete).and_return(true)
allow(@datasource.get_collection('address')).to receive(:delete).and_return(true)

args[:params][:delete] = true
args[:params]['relation_name'] = 'address_users'
Expand All @@ -181,7 +183,7 @@ module Related

result = dissociate.handle_request(args)

expect(datasource.get_collection('address_user')).to have_received(:delete) do |caller, filter|
expect(@datasource.get_collection('address_user')).to have_received(:delete) do |caller, filter|
expect(caller).to be_instance_of(Components::Caller)
expect(filter).to have_attributes(
condition_tree: have_attributes(
Expand Down Expand Up @@ -217,7 +219,7 @@ module Related
end

it 'call dissociate_or_delete_many_to_many without deletion' do
allow(datasource.get_collection('address_user'))
allow(@datasource.get_collection('address_user'))
.to receive_messages(list: [AddressUser.new(1, 1, 1)], delete: true)

args[:params]['relation_name'] = 'addresses'
Expand All @@ -226,7 +228,7 @@ module Related

result = dissociate.handle_request(args)

expect(datasource.get_collection('address_user')).to have_received(:delete) do |caller, filter|
expect(@datasource.get_collection('address_user')).to have_received(:delete) do |caller, filter|
expect(caller).to be_instance_of(Components::Caller)
expect(filter).to have_attributes(
condition_tree: have_attributes(
Expand All @@ -248,9 +250,9 @@ module Related
end

it 'call dissociate_or_delete_many_to_many with deletion' do
allow(datasource.get_collection('address_user')).to receive_messages(list: [AddressUser.new(1, 1, 1)],
delete: true)
allow(datasource.get_collection('address')).to receive(:delete).and_return(true)
allow(@datasource.get_collection('address_user')).to receive_messages(list: [AddressUser.new(1, 1, 1)],
delete: true)
allow(@datasource.get_collection('address')).to receive(:delete).and_return(true)

args[:params][:delete] = true
args[:params]['relation_name'] = 'addresses'
Expand All @@ -259,7 +261,7 @@ module Related

result = dissociate.handle_request(args)

expect(datasource.get_collection('address_user')).to have_received(:delete) do |caller, filter|
expect(@datasource.get_collection('address_user')).to have_received(:delete) do |caller, filter|
expect(caller).to be_instance_of(Components::Caller)
expect(filter).to have_attributes(
condition_tree: have_attributes(
Expand All @@ -277,7 +279,7 @@ module Related
)
end

expect(datasource.get_collection('address')).to have_received(:delete) do |caller, filter|
expect(@datasource.get_collection('address')).to have_received(:delete) do |caller, filter|
expect(caller).to be_instance_of(Components::Caller)
expect(filter).to have_attributes(
condition_tree: have_attributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Related
user_class = Struct.new(:id, :first_name, :last_name)
stub_const('User', user_class)

@datasource = Datasource.new
datasource = Datasource.new
collection_user = instance_double(
Collection,
name: 'user',
Expand Down Expand Up @@ -58,11 +58,13 @@ module Related
}
)
allow(ForestAdminAgent::Builder::AgentFactory.instance).to receive(:send_schema).and_return(nil)
@datasource.add_collection(collection_user)
@datasource.add_collection(collection_category)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(@datasource)
datasource.add_collection(collection_user)
datasource.add_collection(collection_category)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(datasource)
ForestAdminAgent::Builder::AgentFactory.instance.build

@datasource = ForestAdminAgent::Facades::Container.datasource

allow(ForestAdminAgent::Services::Permissions).to receive(:new).and_return(permissions)
allow(permissions).to receive_messages(can?: true, get_scope: Nodes::ConditionTreeBranch.new('Or', []))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ module Related
include_context 'with caller'
subject(:update) { described_class.new }

let(:datasource) { Datasource.new }
let(:permissions) { instance_double(ForestAdminAgent::Services::Permissions) }

before do
datasource = Datasource.new
collection_user = Collection.new(datasource, 'user')
collection_user.add_fields(
{
Expand Down Expand Up @@ -48,6 +48,7 @@ module Related
datasource.add_collection(collection_book)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(datasource)
ForestAdminAgent::Builder::AgentFactory.instance.build
@datasource = ForestAdminAgent::Facades::Container.datasource

allow(ForestAdminAgent::Services::Permissions).to receive(:new).and_return(permissions)
allow(permissions).to receive_messages(can?: true, get_scope: nil)
Expand Down Expand Up @@ -77,7 +78,7 @@ module Related
end

it 'call handle_request on a many_to_one relation' do
allow(datasource.get_collection('book')).to receive(:update).and_return(true)
allow(@datasource.get_collection('book')).to receive(:update).and_return(true)

args[:params]['collection_name'] = 'book'
args[:params]['relation_name'] = 'author'
Expand All @@ -86,7 +87,7 @@ module Related

result = update.handle_request(args)

expect(datasource.get_collection('book')).to have_received(:update) do |caller, filter, data|
expect(@datasource.get_collection('book')).to have_received(:update) do |caller, filter, data|
expect(caller).to be_instance_of(Components::Caller)
expect(filter).to have_attributes(
condition_tree: have_attributes(field: 'id', operator: Operators::EQUAL, value: 1),
Expand All @@ -102,7 +103,7 @@ module Related
end

it 'call handle_request on a one_to_one relation' do
allow(datasource.get_collection('book')).to receive_messages(aggregate: [{ value: 1 }], update: true)
allow(@datasource.get_collection('book')).to receive_messages(aggregate: [{ value: 1 }], update: true)

args[:params]['collection_name'] = 'user'
args[:params]['relation_name'] = 'book'
Expand Down Expand Up @@ -144,7 +145,7 @@ module Related
]
]

expect(datasource.get_collection('book')).to have_received(:update)
expect(@datasource.get_collection('book')).to have_received(:update)
.exactly(2).times do |caller, filter, data|
parameter = parameters.shift
expect(caller).to be_instance_of(parameter[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def respond_to?(arg)
end
stub_const('User', user_class)

@datasource = Datasource.new
datasource = Datasource.new

collection = instance_double(
Collection,
Expand All @@ -67,11 +67,11 @@ def respond_to?(arg)
)

allow(ForestAdminAgent::Builder::AgentFactory.instance).to receive(:send_schema).and_return(nil)
@datasource.add_collection(collection)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(@datasource)
datasource.add_collection(collection)
ForestAdminAgent::Builder::AgentFactory.instance.add_datasource(datasource)
ForestAdminAgent::Builder::AgentFactory.instance.build

@datasource
@datasource = ForestAdminAgent::Facades::Container.datasource
allow(@datasource.get_collection('user')).to receive(:list).and_return([User.new(1, 'foo', 'foo')])
end

it 'return an serialized content' do
Expand Down
Loading