hhursev / recipe-scrapers

Python package for scraping recipes data
MIT License
1.6k stars 505 forks source link

Adds a test to check JSON key order consistency #1151

Open jknndy opened 2 weeks ago

jknndy commented 2 weeks ago

Adds a test to compare the order of keys in each test JSON file against the order discussed in #1124 to make sure they match. 1 test case update was required

Example error:

======================================================================
FAIL: test_json_key_order (tests.library.test_json_order.TestJsonOrder.test_json_key_order)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "*/recipe-scrapers/tests/library/test_json_order.py", line 50, in test_json_key_order
    self.assertEqual(expected_keys, actual_keys)
AssertionError: File: tests/test_data/afghankitchenrecipes.com/afghankitchenrecipes.json
Lists differ: ['aut[76 chars]s', 'ingredient_groups', 'instructions', 'inst[66 chars]age'] != ['aut[76 chars]s', 'total_time', 'ingredient_groups', 'instru[66 chars]age']

First differing element 7:
'ingredient_groups'
'total_time'

  ['author',
   'canonical_url',
   'site_name',
   'host',
   'language',
   'title',
   'ingredients',
+  'total_time',
   'ingredient_groups',
   'instructions',
   'instructions_list',
   'category',
   'yields',
-  'total_time',
   'ratings',
   'image']

----------------------------------------------------------------------
Ran 1 test in 0.006s

FAILED (failures=1)
jayaddison commented 2 weeks ago

This generally looks good to me! It'll be reassuring to have this in place to maintain test style consistency (and therefore readability / reviewability!).

Related to the exception comment/suggestion: it would be helpful if we can provide a script to automatically fix otherwise-well-formed test JSON files that don't currently use the expected key ordering.

If we have that, then we can adjust the message in the test failure so that it both explains what the problem is, and also offers a ready-to-use command-line suggestion so that the developer can fix it (not a zero-frustration experience perhaps, but less frustrating than a hard-to-understand test failure that requires manual fixup for something that seems like it should be automatable).