From 2b92be1b9a4fbf43d8ed71015c0a039be0eb4c26 Mon Sep 17 00:00:00 2001 From: Jonathan Rios Date: Fri, 2 Aug 2024 12:15:31 +0200 Subject: [PATCH] LITE-30341 Fix filtering customer list report by tier type --- reports/customers_list/entrypoint.py | 19 ++++++++++++++++-- tests/test_customer_list.py | 29 +++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/reports/customers_list/entrypoint.py b/reports/customers_list/entrypoint.py index b0cc0fa..f06e274 100644 --- a/reports/customers_list/entrypoint.py +++ b/reports/customers_list/entrypoint.py @@ -17,6 +17,12 @@ 'Extended Information', ) +TIER_TYPE = { + 'customer': ['customer'], + 'reseller': ['tier1', 'tier2'], +} +ALL_TYPE = {*TIER_TYPE['customer'], *TIER_TYPE['reseller']} + def generate( client=None, @@ -50,13 +56,22 @@ def generate( def _get_customers(client, parameters): query = R() + parameter_choices = set((parameters.get('tier_type', {}) or {}).get('choices', [])) if parameters.get('date') and parameters['date'].get('after'): query &= R().events.created.at.ge(parameters['date']['after']) query &= R().events.created.at.le(parameters['date']['before']) + if parameter_choices == ALL_TYPE: + # In case all 3 scopes are present in parameter choices, is the same + # as all=True + parameters['tier_type']['all'] = True if parameters.get('tier_type') and parameters['tier_type']['all'] is False: - query &= R().scopes.oneof(parameters['tier_type']['choices']) - + # (tier1 or tier2) and customer in choices -> all (no RLQ filter for tier_type) + # one or both of tier1/2 in choices -> R().type.eq('reseller') + # only customer in choices -> R().type.eq('customer') + for t_type, choices in TIER_TYPE.items(): + if not parameter_choices.difference(choices): + query &= R().type.eq(t_type) return client.ns('tier').accounts.filter(query).order_by('-events.created.at').limit(1000) diff --git a/tests/test_customer_list.py b/tests/test_customer_list.py index c7e48e4..9c7f24d 100644 --- a/tests/test_customer_list.py +++ b/tests/test_customer_list.py @@ -3,6 +3,9 @@ # Copyright (c) 2023, CloudBlue # All rights reserved. # +import copy + +import pytest from reports.customers_list.entrypoint import ( generate, @@ -18,6 +21,19 @@ } +@pytest.mark.parametrize( + 'parameter_choices,expected_rql', + ( + ({'choices': ['customer']}, ',eq(type,customer)'), + ({'choices': ['tier1']}, ',eq(type,reseller)'), + ({'choices': ['tier2']}, ',eq(type,reseller)'), + ({'choices': ['tier2', 'tier1']}, ',eq(type,reseller)'), + ({'choices': ['customer', 'tier1']}, ''), + ({'choices': ['customer', 'tier2']}, ''), + ({'choices': ['customer', 'tier1', 'tier1', 'tier2']}, ''), + + ), +) def test_generate( progress, client_factory, @@ -25,7 +41,11 @@ def test_generate( mkp_list, ta_list, tier_account, + parameter_choices, + expected_rql, ): + parameters = copy.deepcopy(PARAMETERS) + parameters['tier_type'] = {**parameter_choices, 'all': False} responses = [] responses.append( response_factory( @@ -37,10 +57,13 @@ def test_generate( count=1, ), ) + ta_list_query = ( + 'and(ge(events.created.at,2020-12-01T00:00:00),le(events.created.at,' + '2021-01-01T00:00:00){0})' + ) responses.append( response_factory( - query='and(ge(events.created.at,2020-12-01T00:00:00),le(events.created.at,' - '2021-01-01T00:00:00))', + query=ta_list_query.format(expected_rql), value=ta_list, ), ) @@ -50,7 +73,7 @@ def test_generate( ), ) client = client_factory(responses) - result = list(generate(client, PARAMETERS, progress)) + result = list(generate(client, parameters, progress)) assert len(result) == 1 i = 0