diff --git a/koku/api/query_params.py b/koku/api/query_params.py index 57c842f12d..efea219cf1 100644 --- a/koku/api/query_params.py +++ b/koku/api/query_params.py @@ -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]) diff --git a/koku/api/report/test/test_queries.py b/koku/api/report/test/test_queries.py index 58ed92e97c..efe6539006 100644 --- a/koku/api/report/test/test_queries.py +++ b/koku/api/report/test/test_queries.py @@ -198,6 +198,11 @@ 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", @@ -205,13 +210,7 @@ def setUp(self): 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): diff --git a/koku/api/test_query_params.py b/koku/api/test_query_params.py index 17c7b0d359..913d3e3777 100644 --- a/koku/api/test_query_params.py +++ b/koku/api/test_query_params.py @@ -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")), @@ -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) @@ -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)