def test(host, pguser):
    pg_conn_info = "dbname=epanet_test_db host=" + host + " user="******"dropdb --if-exists -h " + host + " -U " + pguser +
                  " epanet_test_db")
        os.system("createdb -h " + host + " -U " + pguser + " epanet_test_db")
        os.system("psql -h " + host + " -U " + pguser +
                  " epanet_test_db -c 'CREATE EXTENSION postgis'")
        os.system("psql -h " + host + " -U " + pguser + " epanet_test_db -f " +
                  test_data_dir + "/epanet_test_db.sql")

        pcur = versioning.Db(psycopg2.connect(pg_conn_info))

        tables = [
            'epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes'
        ]
        pgversioning1 = versioning.pgServer(pg_conn_info, 'wc1')
        pgversioning2 = versioning.pgServer(pg_conn_info, 'wc2')
        pgversioning1.checkout(tables)
        pgversioning2.checkout(tables)
        print("checkout done")

        pcur.execute("UPDATE wc1.pipes_view SET length = 4 WHERE pid = 1")
        prtTab(pcur, "wc1.pipes_diff")
        pcur.commit()
        #pcur.close()
        pgversioning1.commit("msg1")

        #pcur = versioning.Db(psycopg2.connect(pg_conn_info))

        print("commited")
        pcur.execute("UPDATE wc2.pipes_view SET length = 5 WHERE pid = 1")
        prtTab(pcur, "wc2.pipes_diff")
        pcur.commit()
        pgversioning2.update()
        print("updated")
        prtTab(pcur, "wc2.pipes_diff")
        prtTab(pcur, "wc2.pipes_conflicts")

        pcur.execute(
            "SELECT COUNT(*) FROM wc2.pipes_conflicts WHERE origin = 'mine'")
        assert (1 == pcur.fetchone()[0])
        pcur.execute(
            "SELECT COUNT(*) FROM wc2.pipes_conflicts WHERE origin = 'theirs'")
        assert (1 == pcur.fetchone()[0])

        pcur.execute("DELETE FROM wc2.pipes_conflicts WHERE origin = '" +
                     resolution + "'")
        prtTab(pcur, "wc2.pipes_conflicts")

        pcur.execute("SELECT COUNT(*) FROM wc2.pipes_conflicts")
        assert (0 == pcur.fetchone()[0])
        pcur.close()
def test(host, pguser):
    pg_conn_info = "dbname=epanet_test_db host=" + host + " user="******"dropdb --if-exists -h " + host + " -U "+pguser+" epanet_test_db")
        os.system("createdb -h " + host + " -U "+pguser+" epanet_test_db")
        os.system("psql -h " + host + " -U "+pguser+" epanet_test_db -c 'CREATE EXTENSION postgis'")
        os.system("psql -h " + host + " -U "+pguser+" epanet_test_db -f "+test_data_dir+"/epanet_test_db.sql")
        versioning.historize("dbname=epanet_test_db host={} user={}".format(host,pguser), "epanet")

        pcur = versioning.Db(psycopg2.connect(pg_conn_info))

        tables = ['epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes']
        pgversioning1 = versioning.pgServer(pg_conn_info, 'wc1')
        pgversioning2 = versioning.pgServer(pg_conn_info, 'wc2')
        pgversioning1.checkout(tables)
        pgversioning2.checkout(tables)
        print("checkout done")

        pcur.execute("UPDATE wc1.pipes_view SET length = 4 WHERE versioning_id = 1")
        prtTab( pcur, "wc1.pipes_diff")
        pcur.commit()
        #pcur.close()
        pgversioning1.commit("msg1")

        #pcur = versioning.Db(psycopg2.connect(pg_conn_info))

        print("commited")
        pcur.execute("UPDATE wc2.pipes_view SET length = 5 WHERE versioning_id = 1")
        prtTab( pcur, "wc2.pipes_diff")
        pcur.commit()
        pgversioning2.update()
        print("updated")
        prtTab( pcur, "wc2.pipes_diff")
        prtTab( pcur, "wc2.pipes_conflicts")

        pcur.execute("SELECT COUNT(*) FROM wc2.pipes_conflicts WHERE origin = 'mine'")
        assert( 1 == pcur.fetchone()[0] )
        pcur.execute("SELECT COUNT(*) FROM wc2.pipes_conflicts WHERE origin = 'theirs'")
        assert( 1 == pcur.fetchone()[0] )

        pcur.execute("DELETE FROM wc2.pipes_conflicts WHERE origin = '"+resolution+"'")
        prtTab( pcur, "wc2.pipes_conflicts")

        pcur.execute("SELECT COUNT(*) FROM wc2.pipes_conflicts")
        assert( 0 == pcur.fetchone()[0] )
        pcur.close()
    def __init__(self, host, pguser):

        wc_schema = "epanet_workingcopy"
        super().__init__(host, pguser, wc_schema)

        self.versioning = versioning.pgServer(self.pg_conn_info,
                                              wc_schema)
Exemple #4
0
def test(host, pguser):
    pg_conn_info = "dbname=epanet_test_db host=" + host + " user="******"dropdb --if-exists -h " + host + " -U " + pguser +
              " epanet_test_db")
    os.system("createdb -h " + host + " -U " + pguser + " epanet_test_db")
    os.system("psql -h " + host + " -U " + pguser +
              " epanet_test_db -c 'CREATE EXTENSION postgis'")
    os.system("psql -h " + host + " -U " + pguser + " epanet_test_db -f " +
              test_data_dir + "/epanet_test_db.sql")

    # chechout
    pgversioning = versioning.pgServer(pg_conn_info, 'epanet_working_copy')
    pgversioning.checkout(
        ['epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes'])

    pcur = versioning.Db(psycopg2.connect(pg_conn_info))

    pcur.execute(
        "UPDATE epanet_working_copy.pipes_view SET length = 4 WHERE pid = 1")
    prtTab(pcur, 'epanet_working_copy.pipes_diff')

    prtHid(pcur, 'epanet_working_copy.pipes_view')
    pcur.execute("SElECT COUNT(pid) FROM epanet_working_copy.pipes_view")
    assert (1 == pcur.fetchone()[0])
Exemple #5
0
def test(host, pguser):
    pg_conn_info = "dbname=epanet_test_db host=" + host + " user="******"partial_checkout_test.sqlite")
    if os.path.isfile(sqlite_test_filename):
        os.remove(sqlite_test_filename)

    spversioning = versioning.spatialite(sqlite_test_filename, pg_conn_info)

    # create the test database
    os.system("dropdb --if-exists -h " + host + " -U " + pguser +
              " epanet_test_db")
    os.system("createdb -h " + host + " -U " + pguser + " epanet_test_db")
    os.system("psql -h " + host + " -U " + pguser +
              " epanet_test_db -c 'CREATE EXTENSION postgis'")
    os.system("psql -h " + host + " -U " + pguser + " epanet_test_db -f " +
              test_data_dir + "/epanet_test_db_unversioned.sql")

    pcon = psycopg2.connect(pg_conn_info)
    pcur = pcon.cursor()
    for i in range(10):
        pcur.execute("""
            INSERT INTO epanet.junctions
                (id, elevation, geom)
                VALUES
                ('{id}', {elev}, ST_GeometryFromText('POINT({x} {y})',2154));
            """.format(id=i + 2, elev=float(i), x=float(i + 1),
                       y=float(i + 1)))
    pcon.commit()
    pcon.close()

    versioning.historize(pg_conn_info, 'epanet')

    # spatialite working copy
    spversioning.checkout(
        ["epanet_trunk_rev_head.junctions", "epanet_trunk_rev_head.pipes"],
        [[1, 2, 3], []])
    assert (os.path.isfile(sqlite_test_filename)
            and "sqlite file must exist at this point")

    scon = dbapi2.connect(sqlite_test_filename)
    scur = scon.cursor()
    scur.execute("SELECT * from junctions")
    assert len(scur.fetchall()) == 3

    # postgres working copy
    pgversioning = versioning.pgServer(pg_conn_info, 'my_working_copy')
    pgversioning.checkout(
        ["epanet_trunk_rev_head.junctions", "epanet_trunk_rev_head.pipes"],
        [[1, 2, 3], []])

    pcon = psycopg2.connect(pg_conn_info)
    pcur = pcon.cursor()
    pcur.execute("SELECT * from my_working_copy.junctions_view")
    assert len(pcur.fetchall()) == 3
def test(host, pguser):
    pg_conn_info = "dbname=epanet_test_db host=" + host + " user="******"dropdb --if-exists -h " + host + " -U " + pguser +
              " epanet_test_db")
    os.system("createdb -h " + host + " -U " + pguser + " epanet_test_db")
    os.system("psql -h " + host + " -U " + pguser +
              " epanet_test_db -c 'CREATE EXTENSION postgis'")
    os.system("psql -h " + host + " -U " + pguser + " epanet_test_db -f " +
              test_data_dir + "/epanet_test_db.sql")
    versioning.historize(
        "dbname=epanet_test_db host={} user={}".format(host, pguser), "epanet")

    # branch
    versioning.add_branch(pg_conn_info, "epanet", "mybranch", "add 'branch")

    # chechout from branch : epanet_brwcs_rev_head
    #tables = ['epanet_trunk_rev_head.junctions','epanet_trunk_rev_head.pipes']
    tables = [
        'epanet_mybranch_rev_head.junctions', 'epanet_mybranch_rev_head.pipes'
    ]
    pgversioning = versioning.pgServer(pg_conn_info, 'epanet_brwcs_rev_head')
    pgversioning.checkout(tables)

    pcur = versioning.Db(psycopg2.connect(pg_conn_info))

    # insert into epanet_brwcs_rev_head
    pcur.execute(
        "INSERT INTO epanet_brwcs_rev_head.pipes_view(id, start_node, end_node, geom) VALUES ('2','1','2',ST_GeometryFromText('LINESTRING(1 1,0 1)',2154))"
    )
    pcur.execute(
        "INSERT INTO epanet_brwcs_rev_head.pipes_view(id, start_node, end_node, geom) VALUES ('3','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))"
    )
    pcur.execute("DELETE FROM epanet_brwcs_rev_head.pipes_view WHERE id=3")
    pcur.commit()

    pgversioning.commit("commit", "postgres")

    versioning.merge(pg_conn_info, "epanet", "mybranch")

    pcur.execute("SELECT max(rev) FROM epanet.revisions")
    assert (pcur.fetchone()[0] == 4)

    pcur.execute(
        "SELECT rev, commit_msg, branch FROM epanet.revisions WHERE rev=4")
    assert (pcur.fetchall() == [(4, 'Merge branch mybranch into trunk',
                                 'trunk')])

    pcur.execute(
        "SELECT versioning_id, trunk_rev_begin, trunk_rev_end, mybranch_rev_begin,mybranch_rev_end FROM epanet.pipes"
    )
    assert (pcur.fetchall() == [(1, 1, None, 2, None), (2, 3, None, 3, None)])

    pcur.close()
    def __init__(self, host, pguser, additional_sql=None):

        wc_schema = "myschema_workingcopy"
        super().__init__(host, pguser, wc_schema, additional_sql)

        self.versioning = versioning.pgServer(self.pg_conn_info,
                                              wc_schema)
        self.versioning.checkout(["myschema_trunk_rev_head.referencing",
                                  "myschema_trunk_rev_head.referenced"])

        self.con = self.pcon
        self.cur = self.pcur
    def __init__(self, host, pguser, additional_sql=None):

        wc_schema = "myschema_workingcopy"
        super().__init__(host, pguser, wc_schema, additional_sql)

        self.versioning = versioning.pgServer(self.pg_conn_info, wc_schema)
        self.versioning.checkout([
            "myschema_trunk_rev_head.referencing",
            "myschema_trunk_rev_head.referenced"
        ])

        self.con = self.pcon
        self.cur = self.pcur
def test(host, pguser):
    pg_conn_info = "dbname=epanet_test_db host=" + host + " user="******"dropdb --if-exists -h " + host +
              " -U "+pguser+" epanet_test_db")
    os.system("createdb -h " + host + " -U "+pguser+" epanet_test_db")
    os.system("psql -h " + host + " -U "+pguser +
              " epanet_test_db -c 'CREATE EXTENSION postgis'")
    os.system("psql -h " + host + " -U "+pguser +
              " epanet_test_db -f "+test_data_dir+"/epanet_test_db.sql")
    versioning.historize("dbname=epanet_test_db host={} user={}".format(host,pguser), "epanet")

    # branch
    versioning.add_branch(pg_conn_info, "epanet", "mybranch", "add 'branch")

    # chechout from branch : epanet_brwcs_rev_head
    #tables = ['epanet_trunk_rev_head.junctions','epanet_trunk_rev_head.pipes']
    tables = ['epanet_mybranch_rev_head.junctions',
              'epanet_mybranch_rev_head.pipes']
    pgversioning = versioning.pgServer(pg_conn_info, 'epanet_brwcs_rev_head')
    pgversioning.checkout(tables)

    pcur = versioning.Db(psycopg2.connect(pg_conn_info))

    # insert into epanet_brwcs_rev_head
    pcur.execute("INSERT INTO epanet_brwcs_rev_head.pipes_view(id, start_node, end_node, geom) VALUES ('2','1','2',ST_GeometryFromText('LINESTRING(1 1,0 1)',2154))")
    pcur.execute("INSERT INTO epanet_brwcs_rev_head.pipes_view(id, start_node, end_node, geom) VALUES ('3','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))")
    pcur.execute("DELETE FROM epanet_brwcs_rev_head.pipes_view WHERE id=3")
    pcur.commit()

    pgversioning.commit("commit", "postgres")

    versioning.merge(pg_conn_info, "epanet", "mybranch")

    pcur.execute("SELECT max(rev) FROM epanet.revisions")
    assert(pcur.fetchone()[0] == 4)

    pcur.execute(
        "SELECT rev, commit_msg, branch FROM epanet.revisions WHERE rev=4")
    assert(pcur.fetchall() == [
           (4, 'Merge branch mybranch into trunk', 'trunk')])

    pcur.execute(
        "SELECT versioning_id, trunk_rev_begin, trunk_rev_end, mybranch_rev_begin,mybranch_rev_end FROM epanet.pipes")
    assert(pcur.fetchall() == [(1, 1, None, 2, None), (2, 3, None, 3, None)])

    pcur.close()
def test(host, pguser):
    pg_conn_info = "dbname=epanet_test_db host=" + host + " user="******"dropdb --if-exists -h " + host + " -U "+pguser+" epanet_test_db")
    os.system("createdb -h " + host + " -U "+pguser+" epanet_test_db")
    os.system("psql -h " + host + " -U "+pguser+" epanet_test_db -c 'CREATE EXTENSION postgis'")
    os.system("psql -h " + host + " -U "+pguser+" epanet_test_db -f "+test_data_dir+"/epanet_test_db.sql")
    versioning.historize("dbname=epanet_test_db host={} user={}".format(host,pguser), "epanet")

    # checkout
    pgversioning = versioning.pgServer(pg_conn_info, 'epanet_working_copy')
    pgversioning.checkout(['epanet_trunk_rev_head.junctions','epanet_trunk_rev_head.pipes'])

    pcur = versioning.Db(psycopg2.connect(pg_conn_info))

    pcur.execute("UPDATE epanet_working_copy.pipes_view SET length = 4 WHERE id = 1")
    prtTab(pcur, 'epanet_working_copy.pipes_diff')

    prtHid( pcur, 'epanet_working_copy.pipes_view')
    pcur.execute("SElECT COUNT(id) FROM epanet_working_copy.pipes_view")
    assert(1 == pcur.fetchone()[0])
def test(host, pguser):
    pg_conn_info = "dbname=epanet_test_db host=" + host + " user="******"dropdb --if-exists -h " + host + " -U "+pguser+" epanet_test_db")
    os.system("createdb -h " + host + " -U "+pguser+" epanet_test_db")
    os.system("psql -h " + host + " -U "+pguser+" epanet_test_db -c 'CREATE EXTENSION postgis'")
    os.system("psql -h " + host + " -U "+pguser+" epanet_test_db -f "+test_data_dir+"/epanet_test_db.sql")
    versioning.historize("dbname=epanet_test_db host={} user={}".format(host,pguser), "epanet")

    # chechout
    #tables = ['epanet_trunk_rev_head.junctions','epanet_trunk_rev_head.pipes']
    tables = ['epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes']
    pgversioning = versioning.pgServer(pg_conn_info, 'epanet_working_copy')
    pgversioning.checkout(tables)

    pcur = versioning.Db(psycopg2.connect(pg_conn_info))


    pcur.execute("INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('2','1','2',ST_GeometryFromText('LINESTRING(1 1,0 1)',2154))")
    pcur.commit()
    pgversioning.commit("rev 1")
    pcur.execute("INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('3','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))")
    pcur.commit()
    pgversioning.commit("rev 2")
    pcur.execute("INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('4','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))")
    pcur.commit()
    pgversioning.commit("rev 3")
    pcur.execute("INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('5','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))")
    pcur.commit()
    pgversioning.commit("rev 4")
    pcur.execute("DELETE FROM epanet_working_copy.pipes_view S WHERE versioning_id = 5")
    pcur.commit()
    pgversioning.commit("rev 5")
    pcur.execute("INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('6','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))")
    pcur.commit()
    pgversioning.commit("rev 6")
    pcur.execute("UPDATE epanet_working_copy.pipes_view SET length = 4 WHERE versioning_id = 3")
    pcur.commit()
    pgversioning.commit("rev 7")
    pcur.execute("UPDATE epanet_working_copy.pipes_view SET length = 4 WHERE versioning_id = 1")
    pcur.commit()
    pgversioning.commit("rev 8")
    pcur.execute("INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('7','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))")
    pcur.commit()
    pgversioning.commit("rev 9")
    pcur.execute("INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('8','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))")
    pcur.commit()
    pgversioning.commit("rev 10")
    pcur.execute("DELETE FROM epanet_working_copy.pipes_view S WHERE versioning_id = 7")
    pcur.commit()
    pgversioning.commit("rev 11")
    pcur.execute("INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('9','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))")
    pcur.commit()
    pgversioning.commit("rev 12")
    
    pcur.execute("SELECT * FROM epanet.pipes ORDER BY versioning_id")
    end = pcur.fetchall()
    
    printTab(pcur, 'epanet', 'pipes')
    pcur.execute("SELECT count(*) FROM epanet.pipes")
    [ret] = pcur.fetchone()
    assert(ret == 11)
    
    versioning.archive(pg_conn_info, 'epanet', 7)
    printTab(pcur, 'epanet', 'pipes')
    pcur.execute("SELECT count(*) FROM epanet.pipes")
    [ret] = pcur.fetchone()
    assert(ret == 9)
    pcur.execute("SELECT versioning_id FROM epanet.pipes ORDER BY versioning_id")
    assert([i[0] for i in pcur.fetchall()] == [1, 2, 4, 6, 7, 8, 9, 10, 11])
    printTab(pcur, 'epanet_archive', 'pipes')
    pcur.execute("SELECT count(*) FROM epanet_archive.pipes")
    [ret] = pcur.fetchone()
    assert(ret == 2)
    pcur.execute("SELECT versioning_id FROM epanet_archive.pipes ORDER BY versioning_id")
    assert([i[0] for i in pcur.fetchall()] == [3, 5])
    
    versioning.archive(pg_conn_info, 'epanet', 11)
    printTab(pcur, 'epanet', 'pipes')
    pcur.execute("SELECT count(*) FROM epanet.pipes")
    [ret] = pcur.fetchone()
    assert(ret == 7)
    pcur.execute("SELECT versioning_id FROM epanet.pipes ORDER BY versioning_id")
    assert([i[0] for i in pcur.fetchall()] == [2, 4, 6, 8, 9, 10, 11])
    printTab(pcur, 'epanet_archive', 'pipes')
    pcur.execute("SELECT count(*) FROM epanet_archive.pipes")
    [ret] = pcur.fetchone()
    assert(ret == 4)
    pcur.execute("SELECT versioning_id FROM epanet_archive.pipes ORDER BY versioning_id")
    assert([i[0] for i in pcur.fetchall()] == [1, 3, 5, 7])
    
    # view
    printTab(pcur, 'epanet_archive', 'pipes_all')
    pcur.execute("SELECT count(*) FROM epanet_archive.pipes_all")
    [ret] = pcur.fetchone()
    assert(ret == 11)
    pcur.execute("SELECT * FROM epanet_archive.pipes_all ORDER BY versioning_id")
    endv = pcur.fetchall()
    assert(end==endv)
    
    pcur.close()
Exemple #12
0
def test(host, pguser):
    pg_conn_info = "dbname=epanet_test_db host=" + host + " user="******"dropdb --if-exists -h " + host + " -U "+pguser+" epanet_test_db")
    os.system("createdb -h " + host + " -U "+pguser+" epanet_test_db")
    os.system("psql -h " + host + " -U "+pguser+" epanet_test_db -c 'CREATE EXTENSION postgis'")
    os.system("psql -h " + host + " -U "+pguser+" epanet_test_db -f "+test_data_dir+"/epanet_test_db.sql")

    # chechout
    #tables = ['epanet_trunk_rev_head.junctions','epanet_trunk_rev_head.pipes']
    tables = ['epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes']
    pgversioning1 = versioning.pgServer(pg_conn_info, 'epanet_working_copy')
    pgversioning2 = versioning.pgServer(pg_conn_info, 'epanet_working_copy_cflt')
    pgversioning1.checkout(tables)

    pgversioning2.checkout(tables)

    pcur = versioning.Db(psycopg2.connect(pg_conn_info))


    pcur.execute("INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('2','1','2',ST_GeometryFromText('LINESTRING(1 1,0 1)',2154))")
    pcur.execute("INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('3','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))")
    pcur.commit()


    prtHid(pcur, 'epanet_working_copy.pipes_view')

    pcur.execute("SELECT pid FROM epanet_working_copy.pipes_view")
    assert( len(pcur.fetchall()) == 3 )
    pcur.execute("SELECT pid FROM epanet_working_copy.pipes_diff")
    assert( len(pcur.fetchall()) == 2 )
    pcur.execute("SELECT pid FROM epanet.pipes")
    assert( len(pcur.fetchall()) == 1 )


    prtTab(pcur, 'epanet.pipes')
    prtTab(pcur, 'epanet_working_copy.pipes_diff')
    pcur.execute("UPDATE epanet_working_copy.pipes_view SET length = 4 WHERE pid = 1")
    prtTab(pcur, 'epanet_working_copy.pipes_diff')
    pcur.execute("UPDATE epanet_working_copy.pipes_view SET length = 5 WHERE pid = 4")
    prtTab(pcur, 'epanet_working_copy.pipes_diff')

    pcur.execute("DELETE FROM epanet_working_copy.pipes_view WHERE pid = 4")
    prtTab(pcur, 'epanet_working_copy.pipes_diff')
    pcur.commit()

    pgversioning1.commit("test commit msg")
    prtTab(pcur, 'epanet.pipes')

    pcur.execute("SELECT trunk_rev_end FROM epanet.pipes WHERE pid = 1")
    assert( 1 == pcur.fetchone()[0] )
    pcur.execute("SELECT COUNT(*) FROM epanet.pipes WHERE trunk_rev_begin = 2")
    assert( 2 == pcur.fetchone()[0] )


    # modify the second working copy to create conflict
    prtTab(pcur, 'epanet.pipes')
    pcur.execute("SELECT * FROM epanet_working_copy_cflt.initial_revision")
    print('-- epanet_working_copy_cflt.initial_revision ---')
    for r in pcur.fetchall(): print(r)

    prtHid(pcur, 'epanet_working_copy_cflt.pipes_view')
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')
    pcur.execute("UPDATE epanet_working_copy_cflt.pipes_view SET length = 8 WHERE pid = 1")
    pcur.commit()
    prtTab(pcur, 'epanet.pipes')
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')
    pcur.execute("SELECT COUNT(*) FROM epanet_working_copy_cflt.pipes_diff")
    for l in pcur.con.notices: print(l)
    assert( 2 == pcur.fetchone()[0] )


    pcur.execute("INSERT INTO epanet_working_copy_cflt.pipes_view(id, start_node, end_node, geom) VALUES ('3','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))")
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')
    pcur.commit()
    pgversioning2.update(  )
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_update_diff')

    pcur.execute("SELECT COUNT(*) FROM epanet_working_copy_cflt.pipes_conflicts")
    assert( 2 == pcur.fetchone()[0] )
    pcur.execute("SELECT COUNT(*) FROM epanet_working_copy_cflt.pipes_conflicts WHERE origin = 'mine'")
    assert( 1 == pcur.fetchone()[0] )
    pcur.execute("SELECT COUNT(*) FROM epanet_working_copy_cflt.pipes_conflicts WHERE origin = 'theirs'")
    assert( 1 == pcur.fetchone()[0] )

    prtTab(pcur, 'epanet_working_copy_cflt.pipes_conflicts')

    pcur.execute("DELETE FROM epanet_working_copy_cflt.pipes_conflicts WHERE origin = 'theirs'")
    pcur.execute("SELECT COUNT(*) FROM epanet_working_copy_cflt.pipes_conflicts")
    assert( 0 == pcur.fetchone()[0] )
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_conflicts')
    pcur.commit()

    pgversioning2.commit("second test commit msg")


    pcur.execute("SELECT * FROM epanet_working_copy_cflt.initial_revision")
    print('-- epanet_working_copy_cflt.initial_revision ---')
    for r in pcur.fetchall(): print(r)

    prtHid(pcur, 'epanet_working_copy_cflt.pipes_view')
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')

    pcur.execute("UPDATE epanet_working_copy_cflt.pipes_view SET length = 8")
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')
    pcur.commit()

    pgversioning2.commit("third test commit msg")


    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')
    pcur.execute("UPDATE epanet_working_copy_cflt.pipes_view SET length = 12")
    pcur.commit()
def test(host, pguser):
    pg_conn_info = "dbname=epanet_test_db host=" + host + " user="******"dropdb --if-exists -h " + host + " -U " + pguser +
              " epanet_test_db")
    os.system("createdb -h " + host + " -U " + pguser + " epanet_test_db")
    os.system("psql -h " + host + " -U " + pguser +
              " epanet_test_db -c 'CREATE EXTENSION postgis'")
    os.system("psql -h " + host + " -U " + pguser + " epanet_test_db -f " +
              test_data_dir + "/epanet_test_db.sql")

    # chechout
    #tables = ['epanet_trunk_rev_head.junctions','epanet_trunk_rev_head.pipes']
    tables = ['epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes']
    pgversioning = versioning.pgServer(pg_conn_info, 'epanet_working_copy')
    pgversioning.checkout(tables)

    pcur = versioning.Db(psycopg2.connect(pg_conn_info))

    pcur.execute(
        "INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('2','1','2',ST_GeometryFromText('LINESTRING(1 1,0 1)',2154))"
    )
    pcur.commit()
    pgversioning.commit("rev 1")
    pcur.execute(
        "INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('3','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))"
    )
    pcur.commit()
    pgversioning.commit("rev 2")
    pcur.execute(
        "INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('4','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))"
    )
    pcur.commit()
    pgversioning.commit("rev 3")
    pcur.execute(
        "INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('5','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))"
    )
    pcur.commit()
    pgversioning.commit("rev 4")
    pcur.execute("DELETE FROM epanet_working_copy.pipes_view S WHERE pid = 5")
    pcur.commit()
    pgversioning.commit("rev 5")
    pcur.execute(
        "INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('6','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))"
    )
    pcur.commit()
    pgversioning.commit("rev 6")
    pcur.execute(
        "UPDATE epanet_working_copy.pipes_view SET length = 4 WHERE pid = 3")
    pcur.commit()
    pgversioning.commit("rev 7")
    pcur.execute(
        "UPDATE epanet_working_copy.pipes_view SET length = 4 WHERE pid = 1")
    pcur.commit()
    pgversioning.commit("rev 8")
    pcur.execute(
        "INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('7','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))"
    )
    pcur.commit()
    pgversioning.commit("rev 9")
    pcur.execute(
        "INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('8','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))"
    )
    pcur.commit()
    pgversioning.commit("rev 10")
    pcur.execute("DELETE FROM epanet_working_copy.pipes_view S WHERE pid = 7")
    pcur.commit()
    pgversioning.commit("rev 11")
    pcur.execute(
        "INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('9','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))"
    )
    pcur.commit()
    pgversioning.commit("rev 12")

    pcur.execute("SELECT * FROM epanet.pipes ORDER BY pid")
    end = pcur.fetchall()

    printTab(pcur, 'epanet', 'pipes')
    pcur.execute("SELECT count(*) FROM epanet.pipes")
    [ret] = pcur.fetchone()
    assert (ret == 11)

    versioning.archive(pg_conn_info, 'epanet', 7)
    printTab(pcur, 'epanet', 'pipes')
    pcur.execute("SELECT count(*) FROM epanet.pipes")
    [ret] = pcur.fetchone()
    assert (ret == 9)
    pcur.execute("SELECT pid FROM epanet.pipes ORDER BY pid")
    ret = pcur.fetchall()
    assert (list(zip(*ret)[0]) == [1, 2, 4, 6, 7, 8, 9, 10, 11])
    printTab(pcur, 'epanet_archive', 'pipes')
    pcur.execute("SELECT count(*) FROM epanet_archive.pipes")
    [ret] = pcur.fetchone()
    assert (ret == 2)
    pcur.execute("SELECT pid FROM epanet_archive.pipes ORDER BY pid")
    ret = pcur.fetchall()
    assert (list(zip(*ret)[0]) == [3, 5])

    versioning.archive(pg_conn_info, 'epanet', 11)
    printTab(pcur, 'epanet', 'pipes')
    pcur.execute("SELECT count(*) FROM epanet.pipes")
    [ret] = pcur.fetchone()
    assert (ret == 7)
    pcur.execute("SELECT pid FROM epanet.pipes ORDER BY pid")
    ret = pcur.fetchall()
    assert (list(zip(*ret)[0]) == [2, 4, 6, 8, 9, 10, 11])
    printTab(pcur, 'epanet_archive', 'pipes')
    pcur.execute("SELECT count(*) FROM epanet_archive.pipes")
    [ret] = pcur.fetchone()
    assert (ret == 4)
    pcur.execute("SELECT pid FROM epanet_archive.pipes ORDER BY pid")
    ret = pcur.fetchall()
    assert (list(zip(*ret)[0]) == [1, 3, 5, 7])

    # view
    printTab(pcur, 'epanet_archive', 'pipes_all')
    pcur.execute("SELECT count(*) FROM epanet_archive.pipes_all")
    [ret] = pcur.fetchone()
    assert (ret == 11)
    pcur.execute("SELECT * FROM epanet_archive.pipes_all ORDER BY pid")
    endv = pcur.fetchall()
    assert (end == endv)

    pcur.close()
def test(host, pguser):
    pg_conn_info = "dbname=epanet_test_db host=" + host + " user="******"dropdb --if-exists -h " + host + " -U "+pguser+" epanet_test_db")
    os.system("createdb -h " + host + " -U "+pguser+" epanet_test_db")
    os.system("psql -h " + host + " -U "+pguser+" epanet_test_db -c 'CREATE EXTENSION postgis'")
    os.system("psql -h " + host + " -U "+pguser+" epanet_test_db -f "+test_data_dir+"/epanet_test_db.sql")
    versioning.historize("dbname=epanet_test_db host={} user={}".format(host,pguser), "epanet")

    # chechout
    #tables = ['epanet_trunk_rev_head.junctions','epanet_trunk_rev_head.pipes']
    tables = ['epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes']
    pgversioning1 = versioning.pgServer(pg_conn_info, 'epanet_working_copy')
    pgversioning2 = versioning.pgServer(pg_conn_info, 'epanet_working_copy_cflt')
    pgversioning1.checkout(tables)

    pgversioning2.checkout(tables)

    pcur = versioning.Db(psycopg2.connect(pg_conn_info))


    pcur.execute("INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('2','1','2',ST_GeometryFromText('LINESTRING(1 1,0 1)',2154))")
    pcur.execute("INSERT INTO epanet_working_copy.pipes_view(id, start_node, end_node, geom) VALUES ('3','1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))")
    pcur.commit()


    prtHid(pcur, 'epanet_working_copy.pipes_view')

    pcur.execute("SELECT versioning_id FROM epanet_working_copy.pipes_view")
    assert( len(pcur.fetchall()) == 3 )
    pcur.execute("SELECT versioning_id FROM epanet_working_copy.pipes_diff")
    assert( len(pcur.fetchall()) == 2 )
    pcur.execute("SELECT versioning_id FROM epanet.pipes")
    assert( len(pcur.fetchall()) == 1 )


    prtTab(pcur, 'epanet.pipes')
    prtTab(pcur, 'epanet_working_copy.pipes_diff')
    pcur.execute("UPDATE epanet_working_copy.pipes_view SET length = 4 WHERE versioning_id = 1")
    prtTab(pcur, 'epanet_working_copy.pipes_diff')
    pcur.execute("UPDATE epanet_working_copy.pipes_view SET length = 5 WHERE versioning_id = 4")
    prtTab(pcur, 'epanet_working_copy.pipes_diff')

    pcur.execute("DELETE FROM epanet_working_copy.pipes_view WHERE versioning_id = 4")
    prtTab(pcur, 'epanet_working_copy.pipes_diff')
    pcur.commit()

    pgversioning1.commit("test commit msg")
    prtTab(pcur, 'epanet.pipes')

    pcur.execute("SELECT trunk_rev_end FROM epanet.pipes WHERE versioning_id = 1")
    assert( 1 == pcur.fetchone()[0] )
    pcur.execute("SELECT COUNT(*) FROM epanet.pipes WHERE trunk_rev_begin = 2")
    assert( 2 == pcur.fetchone()[0] )


    # modify the second working copy to create conflict
    prtTab(pcur, 'epanet.pipes')
    pcur.execute("SELECT * FROM epanet_working_copy_cflt.initial_revision")
    print('-- epanet_working_copy_cflt.initial_revision ---')
    for r in pcur.fetchall(): print(r)

    prtHid(pcur, 'epanet_working_copy_cflt.pipes_view')
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')
    pcur.execute("UPDATE epanet_working_copy_cflt.pipes_view SET length = 8 WHERE versioning_id = 1")
    pcur.commit()
    prtTab(pcur, 'epanet.pipes')
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')
    pcur.execute("SELECT COUNT(*) FROM epanet_working_copy_cflt.pipes_diff")
    for l in pcur.con.notices: print(l)
    assert( 2 == pcur.fetchone()[0] )


    pcur.execute("INSERT INTO epanet_working_copy_cflt.pipes_view(id, start_node, end_node, geom) VALUES (4,'1','2',ST_GeometryFromText('LINESTRING(1 -1,0 1)',2154))")
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')
    pcur.commit()
    pgversioning2.update(  )
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_update_diff')

    pcur.execute("SELECT COUNT(*) FROM epanet_working_copy_cflt.pipes_conflicts")
    assert( 2 == pcur.fetchone()[0] )
    pcur.execute("SELECT COUNT(*) FROM epanet_working_copy_cflt.pipes_conflicts WHERE origin = 'mine'")
    assert( 1 == pcur.fetchone()[0] )
    pcur.execute("SELECT COUNT(*) FROM epanet_working_copy_cflt.pipes_conflicts WHERE origin = 'theirs'")
    assert( 1 == pcur.fetchone()[0] )

    prtTab(pcur, 'epanet_working_copy_cflt.pipes_conflicts')

    pcur.execute("DELETE FROM epanet_working_copy_cflt.pipes_conflicts WHERE origin = 'theirs'")
    pcur.execute("SELECT COUNT(*) FROM epanet_working_copy_cflt.pipes_conflicts")
    assert( 0 == pcur.fetchone()[0] )
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_conflicts')
    pcur.commit()

    pgversioning2.commit("second test commit msg")


    pcur.execute("SELECT * FROM epanet_working_copy_cflt.initial_revision")
    print('-- epanet_working_copy_cflt.initial_revision ---')
    for r in pcur.fetchall(): print(r)

    prtHid(pcur, 'epanet_working_copy_cflt.pipes_view')
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')

    pcur.execute("UPDATE epanet_working_copy_cflt.pipes_view SET length = 8")
    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')
    pcur.commit()

    pgversioning2.commit("third test commit msg")


    prtTab(pcur, 'epanet_working_copy_cflt.pipes_diff')
    pcur.execute("UPDATE epanet_working_copy_cflt.pipes_view SET length = 12")
    pcur.commit()