def test_basic_both(mc):
    """ Initializes a sync project and does both a change in Mergin and in the database,
    and lets DB sync handle it: changes in PostgreSQL need to be rebased on top of
    changes in Mergin server.
    """

    project_name = 'test_sync_both'
    source_gpkg_path = os.path.join(TEST_DATA_DIR, 'base.gpkg')
    project_dir = os.path.join(TMP_DIR,
                               project_name + '_work')  # working directory

    init_sync_from_geopackage(mc, project_name, source_gpkg_path)

    conn = psycopg2.connect(DB_CONNINFO)

    # test that database schemas are created + tables are populated
    cur = conn.cursor()
    cur.execute(f"SELECT count(*) from {project_name}_main.simple")
    assert cur.fetchone()[0] == 3

    # make change in GPKG and push
    shutil.copy(os.path.join(TEST_DATA_DIR, 'inserted_1_A.gpkg'),
                os.path.join(project_dir, 'test_sync.gpkg'))
    mc.push_project(project_dir)

    # make a change in PostgreSQL
    cur = conn.cursor()
    cur.execute(
        f"INSERT INTO {project_name}_main.simple (name, rating) VALUES ('insert in postgres', 123)"
    )
    cur.execute("COMMIT")
    cur.execute(f"SELECT count(*) from {project_name}_main.simple")
    assert cur.fetchone()[0] == 4

    # first pull changes from Mergin to DB (+rebase changes in DB) and then push the changes from DB to Mergin
    dbsync_pull(mc)
    db_proj_info = _get_db_project_comment(conn, 'test_sync_both_base')
    assert db_proj_info["version"] == 'v2'
    dbsync_push(mc)
    db_proj_info = _get_db_project_comment(conn, 'test_sync_both_base')
    assert db_proj_info["version"] == 'v3'

    # pull new version of the project to the work project directory
    mc.pull_project(project_dir)

    # check that the insert has been applied to our GeoPackage
    gpkg_conn = sqlite3.connect(os.path.join(project_dir, 'test_sync.gpkg'))
    gpkg_cur = gpkg_conn.cursor()
    gpkg_cur.execute("SELECT count(*) FROM simple")
    assert gpkg_cur.fetchone()[0] == 5

    # check that the insert has been applied to the DB
    cur = conn.cursor()
    cur.execute(f"SELECT count(*) from {project_name}_main.simple")
    assert cur.fetchone()[0] == 5

    print("---")
    dbsync_status(mc)
def test_basic_push(mc):
    """ Initialize a project and test push of a new row from PostgreSQL to Mergin """

    project_name = 'test_sync_push'
    source_gpkg_path = os.path.join(TEST_DATA_DIR, 'base.gpkg')
    project_dir = os.path.join(TMP_DIR,
                               project_name + '_work')  # working directory

    init_sync_from_geopackage(mc, project_name, source_gpkg_path)

    conn = psycopg2.connect(DB_CONNINFO)

    # test that database schemas are created + tables are populated
    cur = conn.cursor()
    cur.execute("SELECT count(*) from test_sync_push_main.simple")
    assert cur.fetchone()[0] == 3

    # make a change in PostgreSQL
    cur = conn.cursor()
    cur.execute(
        "INSERT INTO test_sync_push_main.simple (name, rating) VALUES ('insert in postgres', 123)"
    )
    cur.execute("COMMIT")
    cur.execute("SELECT count(*) from test_sync_push_main.simple")
    assert cur.fetchone()[0] == 4

    # push the change from DB to PostgreSQL
    dbsync_push(mc)
    db_proj_info = _get_db_project_comment(conn, 'test_sync_push_base')
    assert db_proj_info["version"] == 'v2'

    # pull new version of the project to the work project directory
    mc.pull_project(project_dir)

    # check that the insert has been applied to our GeoPackage
    gpkg_conn = sqlite3.connect(os.path.join(project_dir, 'test_sync.gpkg'))
    gpkg_cur = gpkg_conn.cursor()
    gpkg_cur.execute("SELECT count(*) FROM simple")
    assert gpkg_cur.fetchone()[0] == 4

    print("---")
    dbsync_status(mc)
def test_basic_pull(mc):
    """
    Test initialization and one pull from Mergin to DB
    1. create a Mergin project using py-client with a testing gpkg
    2. run init, check that everything is fine
    3. make change in gpkg (copy new version), check everything is fine
    """

    project_name = 'test_sync_pull'
    source_gpkg_path = os.path.join(TEST_DATA_DIR, 'base.gpkg')
    project_dir = os.path.join(TMP_DIR,
                               project_name + '_work')  # working directory

    init_sync_from_geopackage(mc, project_name, source_gpkg_path)

    conn = psycopg2.connect(DB_CONNINFO)

    # test that database schemas are created + tables are populated
    cur = conn.cursor()
    cur.execute("SELECT count(*) from test_sync_pull_main.simple")
    assert cur.fetchone()[0] == 3

    # make change in GPKG and push
    shutil.copy(os.path.join(TEST_DATA_DIR, 'inserted_1_A.gpkg'),
                os.path.join(project_dir, 'test_sync.gpkg'))
    mc.push_project(project_dir)

    # pull the change from Mergin to DB
    dbsync_pull(mc)

    # check that a feature has been inserted
    cur = conn.cursor()
    cur.execute("SELECT count(*) from test_sync_pull_main.simple")
    assert cur.fetchone()[0] == 4
    db_proj_info = _get_db_project_comment(conn, 'test_sync_pull_base')
    assert db_proj_info["version"] == 'v2'

    print("---")
    dbsync_status(mc)