Skip to content

Commit 489b1ce

Browse files
authored
get_last_entries_from_db: double json decode (ansible#258)
* get_last_entries_from_db: double json decode the settings model json-encodes everything when saving to the db, but we're already passing it a json.dumps output that's how it works for a while now, not changing that, but updating the parser to cope with the double json and updating mock db accordingly Fixes: File "/var/lib/awx/venv/awx/lib64/python3.11/site-packages/metrics_utility/base/collector.py", line 155, in last_gathered_entry_for return self.last_gathered_entries.get(key)', ' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^', "AttributeError: 'str' object has no attribute 'get' * adjust test accordingly
1 parent 7ef1628 commit 489b1ce

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

metrics_utility/automation_controller_billing/helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def get_last_entries_from_db() -> Dict:
2929
result = cursor.fetchone()
3030

3131
if result and result[0]:
32-
return json.loads(result[0], object_hook=datetime_hook) # This is the JSON value
32+
json_in_json = json.loads(result[0])
33+
return json.loads(json_in_json, object_hook=datetime_hook) # This is the JSON value
3334
except Exception as e:
3435
logger.error(f'Error getting AUTOMATION_ANALYTICS_LAST_ENTRIES from database: {e}')
3536
return {}

metrics_utility/test/test_automation_controller_billing_helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_successful_entries_retrieval(self, mock_connection):
7070
# Setup
7171
mock_cursor = MagicMock()
7272
mock_connection.cursor.return_value.__enter__.return_value = mock_cursor
73-
test_json = '{"config": "2024-01-01T00:00:00Z", "hosts": "2024-01-03T00:00:00Z", "jobs": "2024-01-02T00:00:00Z"}'
73+
test_json = '"{\\"config\\": \\"2024-01-01T00:00:00Z\\", \\"hosts\\": \\"2024-01-03T00:00:00Z\\", \\"jobs\\": \\"2024-01-02T00:00:00Z\\"}"'
7474
mock_cursor.fetchone.return_value = (test_json,)
7575
# Execute
7676
result = get_last_entries_from_db()
@@ -168,7 +168,8 @@ def test_functions_work_with_real_data(self, mock_connection):
168168
('SUBSCRIPTION_NAME', '"Red Hat AAP"'),
169169
('ABC', '"1.2.3"'),
170170
]
171-
mock_cursor.fetchone.return_value = ('{"config": "2024-01-01T00:00:00Z", "jobs": "2024-01-02T00:00:00Z"}',) # Last entries result
171+
test_json = '"{\\"config\\": \\"2024-01-01T00:00:00Z\\", \\"jobs\\": \\"2024-01-02T00:00:00Z\\"}"' # Last entries result
172+
mock_cursor.fetchone.return_value = (test_json,)
172173

173174
# Execute all functions
174175
license_info, settings_info = get_config_and_settings_from_db()

tools/docker/conf_setting.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ INSERT INTO public.conf_setting (created, modified, key, value) VALUES
66
(now(), now(), 'INSTALL_UUID', '"00000000-0000-0000-0000-000000000000"'),
77
(now(), now(), 'LICENSE', '{"license_type": "UNLICENSED", "product_name": "AWX", "subscription_name": null, "valid_key": false}'),
88
(now(), now(), 'TOWER_URL_BASE', '"https://platformhost"'),
9-
(now(), now(), 'AUTOMATION_ANALYTICS_LAST_ENTRIES', '{"config": "2024-01-01T10:00:00Z", "jobs": "2024-01-02T15:30:00Z"}');
9+
(now(), now(), 'AUTOMATION_ANALYTICS_LAST_ENTRIES', '"{\"config\": \"2024-01-01T10:00:00Z\", \"jobs\": \"2024-01-02T15:30:00Z\"}"');

0 commit comments

Comments
 (0)