示例#1
0
def test_reset(doltdb):
    db = Dolt(doltdb)
    db.reset()
    db.reset(hard=True)
    db.reset(soft=True)
    db.reset(tables="t1")
    db.reset(tables=["t1"])
示例#2
0
def dolt_audit1(doltdb):
    db = Dolt(doltdb)
    lg = db.log()
    commit = lg.popitem(last=False)[0]
    yield {
        "actions": {
            "bar": {
                "key": "bar",
                "config_id": "dd9f1f38-6802-4657-b869-602dde993180",
                "pathspec": "VersioningDemo/1611853111934656/start/1",
                "table_name": "bar",
                "kind": "read",
                "query": "SELECT * FROM `bar`",
                "commit": commit,
                "artifact_name": None,
                "timestamp": 1611853112.794624,
            }
        },
        "configs": {
            "dd9f1f38-6802-4657-b869-602dde993180": {
                "id": "dd9f1f38-6802-4657-b869-602dde993180",
                "database": doltdb,
                "branch": "master",
                "commit": commit,
                "dolthub_remote": False,
                "push_on_commit": False,
            }
        },
    }
def test_auditdt_cm_diff(active_run, dolt_audit1, doltdb):
    doltdb = Dolt(doltdb)
    logs = list(doltdb.log(2).keys())

    with DoltDT(run=active_run, audit=dolt_audit1) as dolt:
        df = dolt.sql("SELECT * FROM `bar`", as_key="akey")
        diff = dolt.diff(from_commit=logs[1], to_commit=logs[0], table="bar")
示例#4
0
def dolt_sql_to_csv(db: dolt.Dolt,
                    sql: str,
                    filename: str,
                    load_args: dict = None):
    if load_args is None:
        load_args = {}
    db.sql(query=sql, result_file=filename, result_format="csv", **load_args)
    return
示例#5
0
def test_reset_errors(doltdb):
    db = Dolt(doltdb)
    with pytest.raises(ValueError):
        db.reset(hard=True, soft=True)
    with pytest.raises(ValueError):
        db.reset(tables="t1", hard=True)
    with pytest.raises(ValueError):
        db.reset(tables="t1", soft=True)
    with pytest.raises(ValueError):
        db.reset(tables={"t1": True})
示例#6
0
    def __call__(self, db: dolt.Dolt):
        starting_head = db.head
        starting_branch = db.active_branch

        try:
            self.checkout(db)
            yield db
        finally:
            self.merge(db, starting_branch)
            db.checkout(starting_branch, error=False)
示例#7
0
 def checkout(self, db: dolt.Dolt):
     res = db.sql(
         f"select * from dolt_branches where name = '{self.branch}'",
         result_format="csv")
     if len(res) == 0:
         db.sql(f"select dolt_checkout('-b', '{self.branch}')",
                result_format="csv")
     else:
         db.sql(f"select dolt_checkout('{self.branch}')",
                result_format="csv")
示例#8
0
def test_dolt_sql_errors(doltdb):
    db = Dolt(doltdb)

    with pytest.raises(ValueError):
        db.sql(result_parser=lambda x: x, query=None)
    with pytest.raises(ValueError):
        db.sql(result_parser=2, query="select active_branch()")
    with pytest.raises(ValueError):
        db.sql(result_file="file.csv", query=None)
    with pytest.raises(ValueError):
        db.sql(result_format="csv", query=None)
示例#9
0
def test_detached_head_cm(doltdb):
    db = Dolt(doltdb)
    commits = list(db.log().keys())

    with detach_head(db, commits[1]):
        sum1 = db.sql("select sum(a) as sum from t1", result_format="csv")[0]

    with detach_head(db, commits[0]):
        sum2 = db.sql("select sum(a) as sum from t1", result_format="csv")[0]

    assert sum1["sum"] == "3"
    assert sum2["sum"] == "6"
示例#10
0
def test_config_local(init_empty_test_repo):
    repo = init_empty_test_repo
    current_global_config = Dolt.config_global(list=True)
    test_username, test_email = "test_user", "test_email"
    repo.config_local(add=True, name="user.name", value=test_username)
    repo.config_local(add=True, name="user.email", value=test_email)
    local_config = repo.config_local(list=True)
    global_config = Dolt.config_global(list=True)
    assert (local_config["user.name"] == test_username
            and local_config["user.email"] == test_email)
    assert global_config["user.name"] == current_global_config["user.name"]
    assert global_config["user.email"] == current_global_config["user.email"]
示例#11
0
def test_custom_query_branch(active_run, dolt_config, doltdb):
    doltdb = Dolt(doltdb)
    logs = list(doltdb.log(2).keys())
    dolt_config.commit = logs[1]

    with DoltDT(run=active_run, config=dolt_config) as dolt:
        df = dolt.sql("SELECT * FROM `bar` LIMIT 2", as_key="akey")
    np.testing.assert_array_equal(df.A.values, [1, 1])
    audit = active_run.dolt
    assert "akey" in audit["actions"]
    assert audit["actions"]["akey"]["kind"] == "read"
    assert audit["actions"]["akey"]["query"] == "SELECT * FROM `bar` LIMIT 2"
示例#12
0
def test_branchdt_diff(inactive_run, dolt_config, doltdb):
    doltdb = Dolt(doltdb)
    logs = list(doltdb.log(2).keys())

    dolt = DoltDT(config=dolt_config)
    diff = dolt.diff(from_commit=logs[1], to_commit=logs[0], table="bar")

    row = diff["bar"].iloc[0]
    assert row.from_A == 1
    assert row.from_B == 1
    assert row.to_A == 2
    assert row.to_B == 2
示例#13
0
def doltdb():
    db_path = os.path.join(os.path.dirname(__file__), "foo")
    try:
        db = Dolt.init(db_path)
        df_v1 = pd.DataFrame({"A": [1, 1, 1], "B": [1, 1, 1]})
        write_pandas(
            dolt=db,
            table="bar",
            df=df_v1.reset_index(),
            primary_key=["index"],
            import_mode="create",
        )
        db.add("bar")
        db.commit("Initialize bar")

        df_v2 = pd.DataFrame({"A": [2, 2, 2], "B": [2, 2, 2]})
        write_pandas(
            dolt=db,
            table="bar",
            df=df_v2.reset_index(),
            primary_key=["index"],
            import_mode="create",
        )
        db.add("bar")
        db.commit("Edit bar")
        yield db_path
    finally:
        if os.path.exists(db_path):
            shutil.rmtree(db_path)
示例#14
0
def dolt_import_csv(db: dolt.Dolt,
                    tablename: str,
                    filename: str,
                    save_args: dict = None):
    mode = "-c"
    tables = db.ls()
    for t in tables:
        if t.name == tablename:
            mode = "-r"
            break

    filetype = "--file-type csv"

    imp = ["table", "import", filetype, mode, tablename]
    if save_args and "primary_key" in save_args:
        pks = ",".join(save_args["primary_key"])
        imp.append(f"--pk {pks}")

    imp.append(filename)
    db.execute(imp)
示例#15
0
def test_branchdt_cm_write(active_run, dolt_config, doltdb):
    input_df = pd.DataFrame({"A": [2, 2, 2], "B": [2, 2, 2]})
    with DoltDT(run=active_run, config=dolt_config) as dolt:
        dolt.write(df=input_df, table_name="baz")

    db = Dolt(doltdb)
    output_df = read_pandas_sql(db, "SELECT * from `baz`")
    np.testing.assert_array_equal(output_df.A.values, [2, 2, 2])

    audit = active_run.dolt
    print(audit["actions"]["baz"])
    assert "baz" in audit["actions"]
    assert audit["actions"]["baz"]["kind"] == "write"
    assert audit["actions"]["baz"]["query"] == "SELECT * FROM `baz`"
示例#16
0
def doltdb():
    db_path = os.path.join(os.path.dirname(__file__), "foo")
    try:
        db = Dolt.init(db_path)
        db.sql("create table  t1 (a bigint primary key, b bigint, c bigint)")
        db.sql("insert into t1 values (1,1,1), (2,2,2)")
        db.sql("select dolt_add('t1')")
        db.sql("select dolt_commit('-m', 'initialize t1')")

        db.sql("insert into t1 values (3,3,3)")
        db.sql("select dolt_add('t1')")
        db.sql("select dolt_commit('-m', 'edit t1')")
        yield db_path
    finally:
        if os.path.exists(db_path):
            shutil.rmtree(db_path)
示例#17
0
def test_set_dolt_path_error(doltdb):
    db = Dolt(doltdb)
    set_dolt_path("dolt")
    test_cmd = "show tables"
    db.sql(test_cmd, result_format="csv")
    try:
        with pytest.raises(FileNotFoundError):
            set_dolt_path("notdolt")
            from doltcli.utils import DOLT_PATH

            assert DOLT_PATH == "notdolt"
            db.sql(test_cmd, result_format="csv")
    finally:
        set_dolt_path("dolt")
示例#18
0
    def _get_db(self, config: DoltConfig):
        if config.id in self._dbcache:
            return self._dbcache[config.id]

        # TODO: clone remote
        try:
            Dolt.init(repo_dir=config.database)
        except DoltException as e:
            pass

        if ".dolt" not in os.listdir(config.database):
            raise ValueError(
                f"Passed a path {config.database} that is not a Dolt database directory"
            )

        doltdb = Dolt(repo_dir=config.database)
        current_branch, branches = doltdb.branch()

        logger.info(
            f"Dolt database in {config.database} at branch {current_branch.name}, using branch {config.branch}"
        )
        if config.branch == current_branch.name:
            pass
        elif config.branch not in [branch.name for branch in branches]:
            raise ValueError(f"Passed branch '{config.branch}' that does not exist")
        else:
            doltdb.checkout(config.branch, checkout_branch=False)

        if not doltdb.status().is_clean:
            raise Exception(
                "DoltDT as context manager requires clean working set for transaction semantics"
            )

        if not config.commit:
            config.commit = self._get_latest_commit_hash(doltdb)

        self._dbcache[config.id] = doltdb
        return doltdb
示例#19
0
def dolt_export_csv(db: dolt.Dolt,
                    tablename: str,
                    filename: str,
                    load_args: dict = None):
    exp = ["table", "export", "-f", "--file-type", "csv", tablename, filename]
    db.execute(exp)
示例#20
0
def test_get_clone_dir_no_remote(tmp_path):
    new_dir = os.path.join(tmp_path, "new_dir")
    res = Dolt._get_clone_dir(new_dir)
    assert new_dir == res
示例#21
0
def _init_helper(path: str, ext: str = None):
    repo_path, repo_data_dir = get_repo_path_tmp_path(path, ext)
    return Dolt.init(repo_path)
示例#22
0
def test_clone_new_dir(tmp_path):
    target = os.path.join(tmp_path, "state_age")
    Dolt.clone("max-hoffman/state-age", new_dir=target)
    db = Dolt(target)
    assert db.head is not None
示例#23
0
 def checkout(self, db: dolt.Dolt):
     # branch must exist
     db.checkout(branch=self.branch, error=False)
示例#24
0
def test_repo_name_trailing_slash(tmp_path):
    repo_path, repo_data_dir = get_repo_path_tmp_path(tmp_path)
    assert Dolt.init(str(repo_path) +
                     "/").repo_name == "test_repo_name_trailing_slash0"
    shutil.rmtree(repo_data_dir)
示例#25
0
def test_get_clone_dir_remote_only(tmp_path):
    new_dir = os.path.join(os.getcwd(), "remote")
    res = Dolt._get_clone_dir(remote_url="some/remote")
    assert new_dir == res
示例#26
0
def test_get_clone_dir_new_dir_only(tmp_path):
    res = Dolt._get_clone_dir("new_dir")
    assert "new_dir" == res
示例#27
0
def test_bad_repo_path(tmp_path):
    bad_repo_path = tmp_path
    with pytest.raises(ValueError):
        Dolt(bad_repo_path)
示例#28
0
def test_working(doltdb):
    db = Dolt(doltdb)
    assert db.head != db.working
示例#29
0
def test_init(tmp_path):
    repo_path, repo_data_dir = get_repo_path_tmp_path(tmp_path)
    assert not os.path.exists(repo_data_dir)
    Dolt.init(repo_path)
    assert os.path.exists(repo_data_dir)
    shutil.rmtree(repo_data_dir)
示例#30
0
def test_get_clone_dir_new_dir_and_remote(tmp_path):
    new_dir = os.path.join("foo/bar", "remote")
    res = Dolt._get_clone_dir(new_dir="foo/bar", remote_url="some/remote")
    assert new_dir == res