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"
示例#2
0
    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)
示例#3
0
    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
示例#4
0
 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,
         )
示例#5
0
 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,
         )
示例#6
0
 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)
示例#7
0
    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()
示例#8
0
 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()
示例#9
0
 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
示例#10
0
 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,
         )
示例#11
0
 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)
示例#12
0
    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)
示例#13
0
 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,
             )
示例#14
0
 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,
         )
示例#15
0
 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)
示例#16
0
 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()
示例#17
0
 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()
示例#18
0
 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",
     ]
示例#20
0
 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"
示例#22
0
    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"
示例#23
0
 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
示例#24
0
    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
示例#25
0
 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()
示例#26
0
 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
示例#27
0
 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
示例#28
0
 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,
         )
示例#29
0
    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