Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date formatting bug in CSV tests #3419

Closed
snaselj opened this issue Mar 14, 2023 · 0 comments · Fixed by #3420
Closed

Date formatting bug in CSV tests #3419

snaselj opened this issue Mar 14, 2023 · 0 comments · Fixed by #3420
Labels
type: bug Something isn't working as expected

Comments

@snaselj
Copy link
Contributor

snaselj commented Mar 14, 2023

Environment

  • Nautobot version: 1.5.2
  • Python version: 3.10
  • Database platform, version: postgres:13-alpine
  • Middleware(s): nautobot-circuit-maintenance

Steps to Reproduce

git clone git@github.com:nautobot/nautobot-plugin-circuit-maintenance.git
cd nautobot-plugin-circuit-maintenance

# This commit has failing tests enabled, following commit disables failing tests
git checkout 973ac25

export INVOKE_NAUTOBOT_CIRCUIT_MAINTENANCE_PYTHON_VER=3.10
export INVOKE_NAUTOBOT_CIRCUIT_MAINTENANCE_NAUTOBOT_VER=1.5.12
cp development/creds.example.env development/creds.env 
invoke build
invoke unittest

Expected Behavior

Tests passed successfully.

Observed Behavior

CSV tests are not passing due to the different date format.

...
======================================================================
FAIL: test_queryset_to_csv (nautobot_circuit_maintenance.tests.test_views.CircuitMaintenanceTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/django/test/utils.py", line 387, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/nautobot/utilities/testing/views.py", line 827, in test_queryset_to_csv
    self.assertEqual(instance1_csv_data, list(data.values()))
AssertionError: Lists differ: ['UT-[15 chars]10-04 10:00:00+00:00', '2020-10-04 12:00:00+00[26 chars]lse'] != ['UT-[15 chars]10-04T10:00:00+00:00', '2020-10-04T12:00:00+00[21 chars], '']

First differing element 1:
'2020-10-04 10:00:00+00:00'
'2020-10-04T10:00:00+00:00'

  ['UT-TEST-1',
-  '2020-10-04 10:00:00+00:00',
?             ^

+  '2020-10-04T10:00:00+00:00',
?             ^

-  '2020-10-04 12:00:00+00:00',
?             ^

+  '2020-10-04T12:00:00+00:00',
?             ^

   '',
   'TENTATIVE',
-  'False']
+  '']

======================================================================
FAIL: test_queryset_to_csv (nautobot_circuit_maintenance.tests.test_views.NoteTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/django/test/utils.py", line 387, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/nautobot/utilities/testing/views.py", line 827, in test_queryset_to_csv
    self.assertEqual(instance1_csv_data, list(data.values()))
AssertionError: Lists differ: ['UT-TEST-1', 'Note 1', 'INFO', 'comment 1', '2023-03-14 09:58:52.690915+00:00'] != ['UT-TEST-1', 'Note 1', 'INFO', 'comment 1', '2023-03-14T09:58:52.690915+00:00']

First differing element 4:
'2023-03-14 09:58:52.690915+00:00'
'2023-03-14T09:58:52.690915+00:00'

- ['UT-TEST-1', 'Note 1', 'INFO', 'comment 1', '2023-03-14 09:58:52.690915+00:00']
?                                                         ^

+ ['UT-TEST-1', 'Note 1', 'INFO', 'comment 1', '2023-03-14T09:58:52.690915+00:00']
?                                                         ^


----------------------------------------------------------------------
Ran 260 tests in 42.177s

FAILED (failures=2, skipped=23)

Proposed Fix

To fix it, it's possible to use csv_format on data fetched from Nautobot model.

diff --git a/nautobot/utilities/testing/views.py b/nautobot/utilities/testing/views.py
index ca62089c3..ea94f4a99 100644
--- a/nautobot/utilities/testing/views.py
+++ b/nautobot/utilities/testing/views.py
@@ -20,7 +20,7 @@ from nautobot.extras.models import ChangeLoggedModel, CustomField, Relationship
 from nautobot.users.models import ObjectPermission
 from nautobot.utilities.templatetags.helpers import bettertitle, validated_viewname
 from nautobot.utilities.testing.mixins import NautobotTestCaseMixin
-from nautobot.utilities.utils import get_changes_for_model, get_filterset_for_model
+from nautobot.utilities.utils import get_changes_for_model, get_filterset_for_model, csv_format
 from .utils import disable_warnings, extract_page_body, get_deletable_objects, post_data
 
 
@@ -815,12 +815,14 @@ class ViewTestCases:
             # {'name': 'AFRINIC', 'slug': 'afrinic', 'is_private': '', 'description': ...'}
             data = [dict(row) for row in reader][0]
 
-            instance1_csv_data = [str(x) if x is not None else "" for x in instance1.to_csv()]
-            # append instance
-            instance1_cf_values = ["" if value is None else value for value in instance1.get_custom_fields().values()]
-            instance1_csv_data += instance1_cf_values
-            # Since values in `data` are all in str; cast all values in instance1_csv_data to str
-            instance1_csv_data = [str(val) for val in instance1_csv_data]
+            # Get expected data
+            instance1_unformatted_data = [
+                *instance1.to_csv(),
+                *instance1.get_custom_fields().values(),
+            ]
+            # Format expected data using `csv_format`, parse back with `csv` and get first row
+            instance1_csv_data = next(iter(csv.reader([csv_format(instance1_unformatted_data)])))
+
             instance1_cf_headers = ["cf_" + str(cf.slug) for cf in instance1.get_custom_fields().keys()]
             instance1_csv_headers = list(self.model.csv_headers) + instance1_cf_headers
             self.assertEqual(instance1_csv_headers, list(data.keys()))
@bryanculver bryanculver added the type: bug Something isn't working as expected label Mar 14, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug Something isn't working as expected
Projects
No open projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants