Cotton code
73 lines · 4.0 KB · PythonRaw
#!/usr/bin/env python3import importlib.utilimport jsonfrom pathlib import Pathimport sysimport tempfileimport unittestfrom unittest.mock import patchspec = importlib.util.spec_from_file_location('package_tables', __file__.replace('test-package-tables.py', 'package-tables.py'))module = importlib.util.module_from_spec(spec)spec.loader.exec_module(module)class PackageTablesTests(unittest.TestCase):    # RULE: Native storage is compiled from the archive; a served definition    # may describe newer object columns and must never be rewritten from it.    def test_builtin_generation_updates_only_compiled_storage(self):        with tempfile.TemporaryDirectory() as scratch:            root = Path(scratch)            store = root / 'crates/cotton-store/src/schema/builtin_tables.json'            table = root / 'crates/cotton-core/kits/personal/tables/fixture.json'            store.parent.mkdir(parents=True)            table.parent.mkdir(parents=True)            store.write_text(json.dumps({'fixture': {'engine': 'fixture_engine', 'tables': ['stale']}}))            source = '{ "id": "fixture", "tables": [{"name": "newer_object_table"}] }\n'            table.write_text(source)            shape = [{'name': 'cotton_fixture'}]            with patch.multiple(module, ROOT=root, STORE=store), \                    patch.object(module, 'shapes', return_value=({'fixture': shape, 'plain': []}, [])), \                    patch.object(sys, 'argv', ['package-tables.py']):                module.main()                self.assertEqual(json.loads(store.read_text()), {                    'fixture': {'engine': 'fixture_engine', 'tables': shape},                    'plain': {'tables': []}})                compiled = store.read_bytes()                self.assertEqual(table.read_text(), source)                store.write_text(json.dumps({'fixture': {'engine': 'fixture_engine', 'tables': []}}))                with patch.object(sys, 'argv', ['package-tables.py', '--check']):                    with self.assertRaisesRegex(SystemExit, 'compiled built-in tables'):                        module.main()                module.main()                self.assertEqual(store.read_bytes(), compiled)                with patch.object(sys, 'argv', ['package-tables.py', '--check']):                    module.main()                self.assertEqual(table.read_text(), source)    # RULE: Mechanical shape extraction may only change the tables value, because other lanes own presentation bytes.    def test_section_scoped_and_idempotent(self):        source = '{\n "page": [ {"tables": "nested"} ], "text": "brace }",\n "tables": [1], "tail": true\n}\n'        result = module.with_tables(source, [{'name': 'example'}])        before = module.section_span(source, 'tables')        after = module.section_span(result, 'tables')        self.assertEqual(source[:before[0]], result[:after[0]])        self.assertEqual(source[before[1]:], result[after[1]:])        self.assertEqual(result, module.with_tables(result, [{'name': 'example'}]))        new = module.with_tables('{"page":[] }\n', [])        self.assertEqual(json.loads(new), {'page': [], 'tables': []})        self.assertEqual(new, module.with_tables(new, []))    # RULE: Calling companion rows belong to the Calls package, so sync and keep discover them from its definition rather than a hand-written table list.    def test_call_companions_follow_the_calls_package(self):        names = ['cotton_calls', 'cotton_call_future_companion']        owned = module.owners(names)        for name in names:            self.assertEqual(owned.get(name), 'call', name)    # RULE: SQL commas inside checks, defaults and comments are not column separators; dropping one changes stored validation.    def test_sql_parts(self):        self.assertEqual(module.split_parts("\n a TEXT DEFAULT 'a,b', -- c,d\n b INTEGER CHECK (b IN (1,2))"),                         ["\n a TEXT DEFAULT 'a,b'", " -- c,d\n b INTEGER CHECK (b IN (1,2))"])if __name__ == '__main__':    unittest.main()