def test_init_yaml(engine, sessionmaker, tmp_path): """ GIVEN specification stored in a YAML file WHEN init_yaml is called with the file THEN a valid model factory is returned. """ # Generate spec file directory = tmp_path / "specs" directory.mkdir() spec_file = directory / "spec.yaml" spec_file.write_text(yaml.dump(BASIC_SPEC)) # Creating model factory base, model_factory = open_alchemy.init_yaml(str(spec_file)) model = model_factory(name="Table") # Creating models base.metadata.create_all(engine) # Creating model instance value = 0 model_instance = model(column=value) session = sessionmaker() session.add(model_instance) session.flush() # Querying session queried_model = session.query(model).first() assert queried_model.column == value # Checking for cache assert cache.schemas_valid(str(spec_file)) is True
def test_dump(tmp_path): """ GIVEN path WHEN dump is called with the path THEN the expected directory structure is returned. """ dist_path = tmp_path / "dist" dist_path.mkdir() name = "name 1" setup = "setup file" manifest = "manifest file" spec_str = "spec file" init = "init file" build.dump( path=str(dist_path), name=name, setup=setup, manifest=manifest, spec_str=spec_str, init=init, ) # Define generated project directories project_path = dist_path / name package_path = project_path / name # Check setup file expected_setup_path = project_path / "setup.py" assert expected_setup_path.is_file() with open(expected_setup_path) as in_file: assert in_file.read() == setup # Check manifest file expected_manifest_path = project_path / "MANIFEST.in" assert expected_manifest_path.is_file() with open(expected_manifest_path) as in_file: assert in_file.read() == manifest # Check spec file expected_spec_path = package_path / "spec.json" assert expected_spec_path.is_file() with open(expected_spec_path) as in_file: assert in_file.read() == spec_str # Check cache expected_cache_path = cache.calculate_cache_path(expected_spec_path) assert expected_cache_path.is_file() assert cache.schemas_valid(str(expected_spec_path)) # Check init file expected_init_path = package_path / "__init__.py" assert expected_init_path.is_file() with open(expected_init_path) as in_file: assert in_file.read() == init
def test_schemas_valid_cache_file_missing(tmpdir): """ GIVEN spec file with contents and cache that does not exist WHEN schemas_valid is called with the filename THEN False is returned. """ path_tmpdir = pathlib.Path(tmpdir) spec_file = path_tmpdir / "spec.json" spec_file.write_text("spec 1", encoding="utf-8") returned_result = cache.schemas_valid(str(spec_file)) assert returned_result is False
def test_schemas_are_valid_cache_folder(tmpdir): """ GIVEN spec in a file WHEN schemas_are_valid is called with the spec filename THEN schemas_valid returns True if it is called afterwards. """ path_tmpdir = pathlib.Path(tmpdir) spec_filename = "spec.json" spec_file = path_tmpdir / spec_filename spec_file.write_text("spec 1", encoding="utf-8") cache.schemas_are_valid(str(spec_file)) assert cache.schemas_valid(str(spec_file)) is True
def test_schemas_valid_cache_is_folder(tmpdir): """ GIVEN spec file with contents and cache that is a folder WHEN schemas_valid is called with the filename THEN False is returned. """ path_tmpdir = pathlib.Path(tmpdir) spec_file = path_tmpdir / "spec.json" spec_file.write_text("spec 1", encoding="utf-8") cache_file = path_tmpdir / "__open_alchemy_spec_json_cache__" cache_file.mkdir() returned_result = cache.schemas_valid(str(spec_file)) assert returned_result is False
def test_schemas_valid_spec_file_not_exists(tmpdir): """ GIVEN spec file that does not exist and cache with contents WHEN schemas_valid is called with the filename THEN False is returned. """ path_tmpdir = pathlib.Path(tmpdir) spec_file = path_tmpdir / "spec.json" cache_file = path_tmpdir / "__open_alchemy_spec_json_cache__" cache_file.write_text( "cache 1", encoding="utf-8", ) returned_result = cache.schemas_valid(str(spec_file)) assert returned_result is False
def test_schemas_are_valid(tmpdir): """ GIVEN spec in a file and the cache is actually a folder WHEN schemas_are_valid is called with the spec filename THEN schemas_valid returns True if it is called afterwards. """ path_tmpdir = pathlib.Path(tmpdir) spec_filename = "spec.json" spec_file = path_tmpdir / spec_filename spec_file.write_text("spec 1", encoding="utf-8") cache_file = ( path_tmpdir / f"__open_alchemy_{cache.calculate_hash(spec_filename)}_cache__") cache_file.mkdir() cache_sub_file = cache_file / "some.file" cache_sub_file.write_text("some contents") cache.schemas_are_valid(str(spec_file)) assert cache.schemas_valid(str(spec_file)) is True
def test_schemas_are_valid_cache_exists(tmpdir, cache_contents): """ GIVEN spec in a file WHEN schemas_are_valid is called with the spec filename THEN schemas_valid returns True if it is called afterwards. """ path_tmpdir = pathlib.Path(tmpdir) spec_filename = "spec.json" spec_file = path_tmpdir / spec_filename spec_file.write_text("spec 1", encoding="utf-8") cache_file = ( path_tmpdir / f"__open_alchemy_{cache.calculate_hash(spec_filename)}_cache__") cache_file.write_text( cache_contents, encoding="utf-8", ) cache.schemas_are_valid(str(spec_file)) assert cache.schemas_valid(str(spec_file)) is True
def test_schemas_valid(tmpdir, spec_contents, cache_contents, expected_result): """ GIVEN spec file with contents and cache with contents WHEN schemas_valid is called with the filename THEN the expected result is returned. """ path_tmpdir = pathlib.Path(tmpdir) spec_filename = "spec.json" spec_file = path_tmpdir / spec_filename spec_file.write_text(spec_contents, encoding="utf-8") cache_file = ( path_tmpdir / f"__open_alchemy_{cache.calculate_hash(spec_filename)}_cache__") cache_file.write_text( cache_contents, encoding="utf-8", ) returned_result = cache.schemas_valid(str(spec_file)) assert returned_result == expected_result
def test_execute(tmp_path, package_format, extensions): """ GIVEN spec, name, path and a package format WHEN execute is called with the spec, name and path THEN the setup.py, MANIFEST.in, spec.json and __init__.py files are created. """ dist = tmp_path / "dist" dist.mkdir() name = "app_models" version = "version 1" spec = { "info": { "version": version, }, "components": { "schemas": { "Schema": { "type": "object", "x-tablename": "schema", "properties": {"id": {"type": "integer"}}, } } }, } build.execute(spec=spec, name=name, path=str(dist), format_=package_format) # Define generated project directories project_path = dist / name package_path = project_path / name # Check setup file expected_setup_path = project_path / "setup.py" assert expected_setup_path.is_file() with open(expected_setup_path) as in_file: setup_file_contents = in_file.read() assert name in setup_file_contents assert version in setup_file_contents # Check manifest file expected_manifest_path = project_path / "MANIFEST.in" assert expected_manifest_path.is_file() with open(expected_manifest_path) as in_file: manifest_file_contents = in_file.read() assert name in manifest_file_contents # Check spec file expected_spec_path = package_path / "spec.json" assert expected_spec_path.is_file() with open(expected_spec_path) as in_file: spec_file_contents = in_file.read() assert version in spec_file_contents assert "Schema" in spec_file_contents assert "x-tablename" in spec_file_contents # Check cache expected_cache_path = cache.calculate_cache_path(expected_spec_path) assert expected_cache_path.is_file() assert cache.schemas_valid(str(expected_spec_path)) # Check init file expected_init_path = package_path / "__init__.py" assert expected_init_path.is_file() with open(expected_init_path) as in_file: init_contents = in_file.read() assert ( """import pathlib from open_alchemy import init_json parent_path = pathlib.Path(__file__).parent.absolute() init_json(parent_path / "spec.json")""" in init_contents ) assert "class SchemaDict" in init_contents assert "id: typing.Optional[int]" in init_contents assert "class TSchema" in init_contents assert "id: 'sqlalchemy.Column[typing.Optional[int]]'" in init_contents assert "Schema: typing.Type[TSchema]" in init_contents # Assert one package per requested format is created. for extension in extensions: dist_dir = project_path / "dist" files = list(dist_dir.glob(f"{name}*{extension}")) assert len(files) == 1