import importlib.util,json,unittest from pathlib import Path spec=importlib.util.spec_from_file_location('book',Path(__file__).with_name('publish-cookbook-route.py'));book=importlib.util.module_from_spec(spec);spec.loader.exec_module(book) class CookbookRoute(unittest.TestCase): def original(self):return '\n'.join('location '+('^~' if p.endswith('/') else '=')+' /'+p+' { alias '+book.LEGACY+p+'; }' for p in (*book.FILES,'recipe-icons/'))+'\n'+book.shared.ROUTE def test_routes_entire_cookbook_and_preserves_shared_design(self): after=book.route_content(self.original());self.assertNotIn('proxy_pass',after);self.assertNotIn('recipe-catalogue.json',after);self.assertIn(book.shared.ROUTE,after);self.assertEqual(book.route_content(after),after) def test_unknown_or_duplicate_route_is_refused(self): for old in (self.original().replace(book.LEGACY+'recipe-catalogue.json','/unexpected.json'),self.original()+'\nlocation = /recipe-catalogue.json {}'): with self.assertRaises(ValueError):book.route_content(old) def test_serving_port_tracks_both_promotion_slots(self): for port in (3100,3200):self.assertEqual(book.serving_port(f'proxy_pass http://127.0.0.1:{port};'),port) with self.assertRaises(ValueError):book.serving_port('proxy_pass http://127.0.0.1:3100; proxy_pass http://127.0.0.1:3200;') def test_old_catalogue_or_missing_recipe_grid_is_refused(self): recipes=[{'id':key,'grid':[None]*9} for key in book.IDS];self.assertEqual(book.validate_catalogue(json.dumps(recipes)),6) with self.assertRaises(ValueError):book.validate_catalogue('[]') recipes[0]['grid']=[] with self.assertRaises(ValueError):book.validate_catalogue(json.dumps(recipes)) if __name__=='__main__':unittest.main()