def test_cli__multiple_files(self, tmpdir): sample_yaml = Path(__file__).parent / "mapping_mixins.recipe.yml" load_yaml = Path(__file__).parent / "mapping_mixins.load.yml" override_yaml = Path( __file__).parent / "mapping_mixins-override.load.yml" mapping_yaml = Path(tmpdir) / "mapping.yml" generate_cli.main( [ str(sample_yaml), "--generate-cci-mapping-file", str(mapping_yaml), "--load-declarations", str(load_yaml), "--load-declarations", str(override_yaml), ], standalone_mode=False, ) map_data = yaml.safe_load(mapping_yaml.read_text()) assert list(map_data.keys()) == [ "Insert Account", "Insert Contact", "Insert Opportunity", ] assert map_data["Insert Account"]["api"] == "rest" assert map_data["Insert Contact"]["api"] == "rest" assert map_data["Insert Opportunity"]["api"] == "bulk"
def test_mutually_exclusive(self): with pytest.raises(ClickException) as e: with TemporaryDirectory() as t: generate_cli.main( [ str(sample_yaml), "--dburl", f"sqlite:///{t}/foo.db", "--output-format", "JSON", ], standalone_mode=False, ) assert "mutually exclusive" in str(e.value) with pytest.raises(ClickException) as e: generate_cli.main( [ str(sample_yaml), "--cci-mapping-file", str(sample_yaml), "--output-format", "JSON", ], standalone_mode=False, ) assert "apping-file" in str(e.value)
def test_from_cli__target_number(self, capsys): generate_cli.main( [str(sample_yaml), "--target-number", "Account", "5"], standalone_mode=False) stdout = capsys.readouterr().out assert len(re.findall(r"Account\(", stdout)) == 5
def test_from_cli__continuation(self, capsys): with TemporaryDirectory() as t: mapping_file_path = Path(t) / "mapping.yml" database_path = f"{t}/foo.db" database_url = f"sqlite:///{database_path}" continuation_file = Path(t) / "continuation.yml" assert not mapping_file_path.exists() generate_cli.main( [ str(sample_yaml), "--generate-cci-mapping-file", mapping_file_path, "--dburl", database_url, "--generate-continuation-file", continuation_file, ], standalone_mode=False, ) assert mapping_file_path.exists() Path(database_path).unlink() generate_cli.main( [ str(sample_yaml), "--cci-mapping-file", mapping_file_path, "--dburl", database_url, "--continuation-file", continuation_file, ], standalone_mode=False, )
def test_json_output_file(self): with TemporaryDirectory() as t: generate_cli.main( [str(sample_yaml), "--output-file", Path(t) / "foo.json"], standalone_mode=False, )
def test_from_cli__checks_tables_are_empty(self, capsys): with TemporaryDirectory() as t: mapping_file_path = Path(t) / "mapping.yml" database_path = f"{t}/foo.db" database_url = f"sqlite:///{database_path}" continuation_file = Path(t) / "continuation.yml" assert not mapping_file_path.exists() generate_cli.main( [ str(sample_yaml), "--generate-cci-mapping-file", mapping_file_path, "--dburl", database_url, "--generate-continuation-file", continuation_file, ], standalone_mode=False, ) assert mapping_file_path.exists() with pytest.raises(ClickException) as e: generate_cli.main( [ str(sample_yaml), "--cci-mapping-file", mapping_file_path, "--dburl", database_url, "--continuation-file", continuation_file, ], standalone_mode=False, ) assert "Table already exists" in str(e.value)
def test_image_outputs(self): if not graphviz_available(): pytest.skip("Graphviz is not installed") with TemporaryDirectory() as t: png = Path(t) / "out.png" svg = Path(t) / "out.svg" txt = Path(t) / "out.txt" dot = Path(t) / "out.dot" generate_cli.main( [ str(sample_yaml), "--output-file", png, "--output-file", svg, "--output-file", txt, "--output-file", dot, ], standalone_mode=False, ) assert png.read_bytes().startswith(b"\x89PNG\r\n") assert svg.read_bytes().startswith(b"<?xml"), svg.read_bytes()[0:5] assert svg.read_bytes().startswith(b"<?xml"), svg.read_bytes()[0:5] assert dot.read_bytes().startswith(b"/* Ge"), dot.read_bytes()[0:5] assert txt.exists()
def test_image_outputs(self): with TemporaryDirectory() as t: dot = Path(t) / "out.dot" generate_cli.main( [str(sample_yaml), "--output-file", dot], standalone_mode=False, ) assert dot.exists()
def test_version__json_corrupt(self, capsys): responses.add("GET", "https://pypi.org/pypi/snowfakery/json", body="}}") with pytest.raises(SystemExit): generate_cli.main(["--version"]) captured = capsys.readouterr() assert "Error checking snowfakery version" in captured.out
def test_mapping_file_no_dburl(self): with pytest.raises(ClickException): generate_cli.main( ["--mapping_file", str(str(sample_yaml)), str(sample_yaml)], standalone_mode=False, )
def test_mutually_exclusive_targets(self): with pytest.raises(ClickException) as e: generate_cli.main( [ str(sample_yaml), "--reps", "50", "--target-count", "Account", "100" ], standalone_mode=False, ) assert "mutually exclusive" in str(e.value)
def test_cli_errors__mutex3(self): with named_temporary_file_path() as tempfile: with open(tempfile, "w") as t: t.write("") with pytest.raises(ClickException) as e: generate_cli.main( [str(sample_yaml), "--cci-mapping-file", tempfile], standalone_mode=False, ) assert "--cci-mapping-file" in str(e.value)
def test_cli_errors__cannot_infer_output_format(self): with pytest.raises(ClickException, match="No format supplied"): with TemporaryDirectory() as t: generate_cli.main( [ str(sample_yaml), "--output-file", Path(t) / "bob", ], standalone_mode=False, )
def test_output_folder__error(self): with TemporaryDirectory() as tempdir, pytest.raises(ClickException): generate_cli.main( [ str(sample_yaml), "--output-folder", tempdir, "--output-format", "json", ], standalone_mode=False, )
def test_cli_errors__mutex2(self): with pytest.raises(ClickException) as e: generate_cli.main( [ str(sample_yaml), "--dburl", "sqlite:////tmp/foo.db", "--output-format", "svg", ], standalone_mode=False, ) assert "output-format" in str(e.value)
def test_output_folder__csv_new_folder(self): with TemporaryDirectory() as tempdir: generate_cli.main( [ str(sample_yaml), "--output-folder", Path(tempdir, "foo"), "--output-format", "csv", ], standalone_mode=False, ) assert Path(tempdir, "foo", "Account.csv").exists()
def test_from_cli__pluggable_output_stream(self): with named_temporary_file_path(suffix=".yml") as t: generate_cli.main( [ str(sample_yaml), "--output-format", "examples.YamlOutputStream", "--output-file", t, ], standalone_mode=False, ) assert t.exists()
def test_output_folder(self): with TemporaryDirectory() as tempdir: generate_cli.main( [ str(sample_yaml), "--output-folder", tempdir, "--output-file", "foo.json", ], standalone_mode=False, ) assert isinstance(json.load(Path(tempdir, "foo.json").open()), list)
def test_cli__file_no_dots(self, tmpdir): sample_yaml = Path(__file__).parent / "no_extension_filename" mapping_yaml = Path(tmpdir) / "mapping.yml" generate_cli.main( [ str(sample_yaml), "--generate-cci-mapping-file", str(mapping_yaml), ], standalone_mode=False, ) map_data = yaml.safe_load(mapping_yaml.read_text()) assert list(map_data.keys()) == [ "Insert foo", ]
def test_json(self): with mock.patch("snowfakery.cli.sys.stdout", new=StringIO()) as fake_out: generate_cli.main(["--output-format", "json", str(sample_yaml)], standalone_mode=False) assert json.loads(fake_out.getvalue()) == [{ "ShippingCountry": "Canada", "_table": "Account", "id": 1, "name": "Default Company Name", }]
def test_cli__infers_load_file(self, tmpdir): sample_yaml = Path(__file__).parent / "mapping_mixins.recipe.yml" mapping_yaml = Path(tmpdir) / "mapping.yml" generate_cli.main( [ str(sample_yaml), "--generate-cci-mapping-file", str(mapping_yaml) ], standalone_mode=False, ) map_data = yaml.safe_load(mapping_yaml.read_text()) assert list(map_data.keys()) == [ "Insert Account", "Insert Contact", "Insert Opportunity", ] assert map_data["Insert Account"]["api"] == "rest"
def test_mapping_file(self): with TemporaryDirectory() as t: url = f"sqlite:///{t}/foo.db" generate_cli.main( [ str(sample_accounts_yaml), "--dburl", url, ], standalone_mode=False, ) engine = create_engine(url) with engine.connect() as connection: result = list(connection.execute("select * from Account")) assert result[0]["id"] == 1 assert result[0]["BillingCountry"] == "Canada"
def test_from_cli__explicit_format_txt(self, capsys): with named_temporary_file_path() as t: generate_cli.main( [ str(sample_yaml), "--target-number", "Account", "5", "--output-format", "txt", "--output-file", str(t), ], standalone_mode=False, ) with t.open() as f: output = f.read() assert len(re.findall(r"Account\(", output)) == 5
def test_version_report__current_version(self, capsys, vcr, snowfakery_rootdir): # hand-minimized VCR cassette cassette = ( snowfakery_rootdir / "tests/cassettes/ManualEditTestCLIOptionChecking.test_version_report__current_version.yaml" ) assert cassette.exists() with pytest.raises(SystemExit), mock.patch("snowfakery.cli.version", "2.0.3"), vcr.use_cassette( str(cassette)): generate_cli.main(["--version"]) captured = capsys.readouterr() assert captured.out.startswith("snowfakery") assert "Python: 3." in captured.out assert "Properly installed" in captured.out assert "You have the latest version of Snowfakery" in captured.out
def test_image_outputs(self): pytest.importorskip("pygraphviz") with TemporaryDirectory() as t: png = Path(t) / "out.png" svg = Path(t) / "out.svg" txt = Path(t) / "out.txt" generate_cli.main( [ str(sample_yaml), "--output-file", png, "--output-file", svg, "--output-file", txt, ], standalone_mode=False, ) assert png.exists() assert svg.exists() assert txt.exists()
def test_version_report__error(self, capsys, vcr, snowfakery_rootdir): with pytest.raises(SystemExit), mock.patch( "requests.get", side_effect=RequestException): generate_cli.main(["--version"]) captured = capsys.readouterr() assert "Error checking snowfakery version:" in captured.out
def test_version_mod__called(self): with pytest.raises(SystemExit), mock.patch.object( VersionMessage, "__mod__", wraps=lambda vars: "") as mod: generate_cli.main(["--version"]) assert len(mod.mock_calls) == 1
def test_from_cli__bad_target_number(self): with pytest.raises(BadParameter): generate_cli.main( [str(sample_yaml), "--target-number", "abc", "def"], standalone_mode=False, )
def test_from_cli__reps(self, capsys): generate_cli.main([str(sample_yaml), "--reps", "3"], standalone_mode=False) stdout = capsys.readouterr().out assert len(re.findall(r"Account\(", stdout)) == 3