Skip to content

Commit

Permalink
COST-2654: Only collect distinct tags from db. (project-koku#3666)
Browse files Browse the repository at this point in the history
* COST-2654: Only collect distinct tags from db.

* Fix mocked django values for tests.
  • Loading branch information
myersCody authored May 24, 2022
1 parent 1cff14c commit 5219983
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion koku/api/query_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def __str__(self):
def _get_tag_keys(self, model):
"""Get a list of tag keys to validate filters."""
with tenant_context(self.tenant):
tags = model.objects.values("key")
tags = model.objects.values("key").distinct()
tag_list = [":".join(["tag", tag.get("key")]) for tag in tags]
tag_list.extend([":".join(["and:tag", tag.get("key")]) for tag in tags])
tag_list.extend([":".join(["or:tag", tag.get("key")]) for tag in tags])
Expand Down
13 changes: 6 additions & 7 deletions koku/api/report/test/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,20 +198,19 @@ class ReportQueryHandlerTest(IamTestCase):
def setUp(self):
"""Test setup."""
self.mock_tag_key = FAKE.word()
tag_mock = Mock()
tag_mock.objects.values.return_value.distinct.return_value = [
{"key": self.mock_tag_key, "values": [FAKE.word(), FAKE.word()]}
]

self.mock_view = Mock(
spec=ReportView,
report="mock",
permission_classes=[Mock],
provider="mock",
serializer=Mock,
query_handler=Mock,
tag_handler=[
Mock(
objects=Mock(
values=Mock(return_value=[{"key": self.mock_tag_key, "values": [FAKE.word(), FAKE.word()]}])
)
)
],
tag_handler=[tag_mock],
)

def test_init(self):
Expand Down
24 changes: 11 additions & 13 deletions koku/api/test_query_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,11 @@ def test_constructor(self):
This test is a bit fatter than it needs to be to help show how to mock
out the Request and View objects.
"""

def fake_tags():
fake_tags = []
for _ in range(0, random.randint(2, 10)):
fake_tags.append({"key": self.FAKE.word(), "value": self.FAKE.word()})
return fake_tags

tag_keys = ["app", "az", "environment", "cost_center", "fake", "other", "this"]
fake_objects = Mock()
fake_objects.objects.values.return_value.distinct.return_value = [
{"key": key, "value": self.FAKE.word()} for key in tag_keys
]
fake_request = Mock(
spec=HttpRequest,
user=Mock(access=Mock(get=lambda key, default: default), customer=Mock(schema_name="acct10001")),
Expand All @@ -105,10 +103,7 @@ def fake_tags():
query_handler=Mock(provider=random.choice(PROVIDERS)),
report=self.FAKE.word(),
serializer=Mock,
tag_handler=[
Mock(objects=Mock(values=lambda _: fake_tags())),
Mock(objects=Mock(values=lambda _: fake_tags())),
],
tag_handler=[fake_objects, fake_objects],
)
self.assertIsInstance(QueryParameters(fake_request, fake_view), QueryParameters)

Expand Down Expand Up @@ -647,14 +642,17 @@ def test_process_tag_query_params(self):
user=Mock(access=Mock(get=lambda key, default: default), customer=Mock(schema_name="acct10001")),
GET=Mock(urlencode=Mock(return_value=fake_uri)),
)
fake_objects = Mock(values=lambda _: [{"key": key, "value": self.FAKE.word()} for key in tag_keys])
fake_objects = Mock()
fake_objects.objects.values.return_value.distinct.return_value = [
{"key": key, "value": self.FAKE.word()} for key in tag_keys
]
fake_view = Mock(
spec=ReportView,
provider=self.FAKE.word(),
query_handler=Mock(provider=random.choice(PROVIDERS)),
report=self.FAKE.word(),
serializer=Mock,
tag_handler=[Mock(objects=fake_objects)],
tag_handler=[fake_objects],
)
params = QueryParameters(fake_request, fake_view)
self.assertEqual(params.tag_keys, expected)
Expand Down

0 comments on commit 5219983

Please sign in to comment.