Skip to content

Commit

Permalink
LITE-30341 Fix filtering customer list report by tier type
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatrios committed Aug 5, 2024
1 parent 6cb6376 commit 2b92be1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
19 changes: 17 additions & 2 deletions reports/customers_list/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)


Expand Down
29 changes: 26 additions & 3 deletions tests/test_customer_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# Copyright (c) 2023, CloudBlue
# All rights reserved.
#
import copy

import pytest

from reports.customers_list.entrypoint import (
generate,
Expand All @@ -18,14 +21,31 @@
}


@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,
response_factory,
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(
Expand All @@ -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,
),
)
Expand All @@ -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
Expand Down

0 comments on commit 2b92be1

Please sign in to comment.