def test_database_store_backend_duplicate_key_violation(caplog, sa):
    store_backend = DatabaseStoreBackend(
        credentials={
            "drivername": "postgresql",
            "username": "******",
            "password": "",
            "host": "localhost",
            "port": "5432",
            "database": "test_ci",
        },
        table_name="test_database_store_backend_duplicate_key_violation",
        key_columns=["k1", "k2", "k3"],
    )
    key = ("1", "2", "3")

    store_backend.set(key, "hello")
    assert "hello" == store_backend.get(key)

    # default behavior doesn't throw an error because the key is updated
    store_backend.set(key, "hello")
    assert "hello" == store_backend.get(key)

    assert len(caplog.messages) == 0
    caplog.set_level(logging.INFO, "great_expectations")

    store_backend.set(
        key, "hello",
        allow_update=False)  # the only place we are testing this flag
    assert len(caplog.messages) == 1
    assert "already exists with the same value" in caplog.messages[0]

    with pytest.raises(StoreBackendError) as exc:
        store_backend.set(key, "world", allow_update=False)

    assert "Integrity error" in str(exc.value)
def test_database_store_backend_schema_spec(caplog, sa, test_backends):
    if "postgresql" not in test_backends:
        pytest.skip("test_database_store_backend_get_url_for_key requires postgresql")

    store_backend = DatabaseStoreBackend(
        credentials={
            "drivername": "postgresql",
            "username": "******",
            "password": "",
            "host": os.getenv("GE_TEST_LOCAL_DB_HOSTNAME", "localhost"),
            "port": "5432",
            "schema": "special",
            "database": "test_ci",
        },
        table_name="test_database_store_backend_url_key",
        key_columns=["k1"],
    )

    # existing key
    key = ("2",)

    store_backend.set(key, "hello")
    assert "hello" == store_backend.get(key)

    # clean up values
    store_backend.engine.execute(f"DROP TABLE {store_backend._table};")
def test_database_store_backend_duplicate_key_violation(caplog):
    store_backend = DatabaseStoreBackend(
        credentials={
            "drivername": "postgresql",
            "username": "******",
            "password": "",
            "host": "localhost",
            "port": "5432",
            "database": "test_ci",
        },
        table_name="test_database_store_backend_duplicate_key_violation",
        key_columns=["k1", "k2", "k3"],
    )
    key = ("1", "2", "3")

    store_backend.set(key, "hello")
    assert "hello" == store_backend.get(key)

    assert len(caplog.messages) == 0
    caplog.set_level(logging.INFO, "great_expectations")
    store_backend.set(key, "hello")
    assert len(caplog.messages) == 1
    assert "already exists with the same value" in caplog.messages[0]

    with pytest.raises(StoreBackendError) as exc:
        store_backend.set(key, "world")

    assert "Integrity error" in str(exc.value)