Exemplo n.º 1
0
 def test_replication_slots(self, connection):
     pg_base = Base(connection.engine.url.database)
     assert pg_base.replication_slots("noob") == []
     replication_slots = pg_base.replication_slots(
         f"{connection.engine.url.database}_testdb"
     )
     assert "testdb_testdb" == replication_slots[0][0]
Exemplo n.º 2
0
 def test_replication_slots(self, connection):
     pg_base = Base(connection.engine.url.database)
     assert pg_base.replication_slots('noob') == []
     replication_slots = pg_base.replication_slots(
         f'{connection.engine.url.database}_testdb'
     )
     assert 'testdb_testdb' == replication_slots[0][0]
Exemplo n.º 3
0
    def test_parse_logical_slot(
        self,
        connection,
    ):
        pg_base = Base(connection.engine.url.database)
        with pytest.raises(ParseLogicalSlotError) as excinfo:
            pg_base.parse_logical_slot('')
            assert 'No match for row:' in str(excinfo.value)

        row = '''
        table public."B1_XYZ": INSERT: "ID"[integer]:5 "CREATED_TIMESTAMP"[bigint]:222 "ADDRESS"[character varying]:'from3' "SOME_FIELD_KEY"[character varying]:'key3' "SOME_OTHER_FIELD_KEY"[character varying]:'issue3' "CHANNEL_ID"[integer]:3 "CHANNEL_NAME"[character varying]:'channel3' "ITEM_ID"[integer]:3 "MESSAGE"[character varying]:'message3' "RETRY"[integer]:4 "STATUS"[character varying]:'status' "SUBJECT"[character varying]:'sub3' "TIMESTAMP"[bigint]:33
        '''
        values = pg_base.parse_logical_slot(row)
        assert values == {
            'new': {
                'CHANNEL_ID': 3,
                'CHANNEL_NAME': 'channel3',
                'CREATED_TIMESTAMP': 222,
                'ADDRESS': 'from3',
                'ID': 5,
                'ITEM_ID': 3,
                'MESSAGE': 'message3',
                'RETRY': 4,
                'SOME_FIELD_KEY': 'key3',
                'SOME_OTHER_FIELD_KEY': 'issue3',
                'STATUS': 'status',
                'SUBJECT': 'sub3',
                'TIMESTAMP': 33
            },
            'old': {},
            'schema': 'public',
            'table': 'B1_XYZ',
            'tg_op': 'INSERT',
        }
Exemplo n.º 4
0
    def test_parse_logical_slot(
        self,
        connection,
    ):
        pg_base = Base(connection.engine.url.database)
        with pytest.raises(ParseLogicalSlotError) as excinfo:
            pg_base.parse_logical_slot("")
            assert "No match for row:" in str(excinfo.value)

        row = """
        table public."B1_XYZ": INSERT: "ID"[integer]:5 "CREATED_TIMESTAMP"[bigint]:222 "ADDRESS"[character varying]:'from3' "SOME_FIELD_KEY"[character varying]:'key3' "SOME_OTHER_FIELD_KEY"[character varying]:'issue3' "CHANNEL_ID"[integer]:3 "CHANNEL_NAME"[character varying]:'channel3' "ITEM_ID"[integer]:3 "MESSAGE"[character varying]:'message3' "RETRY"[integer]:4 "STATUS"[character varying]:'status' "SUBJECT"[character varying]:'sub3' "TIMESTAMP"[bigint]:33
        """
        values = pg_base.parse_logical_slot(row)
        assert values == {
            "new": {
                "CHANNEL_ID": 3,
                "CHANNEL_NAME": "channel3",
                "CREATED_TIMESTAMP": 222,
                "ADDRESS": "from3",
                "ID": 5,
                "ITEM_ID": 3,
                "MESSAGE": "message3",
                "RETRY": 4,
                "SOME_FIELD_KEY": "key3",
                "SOME_OTHER_FIELD_KEY": "issue3",
                "STATUS": "status",
                "SUBJECT": "sub3",
                "TIMESTAMP": 33,
            },
            "old": {},
            "schema": "public",
            "table": "B1_XYZ",
            "tg_op": "INSERT",
        }
Exemplo n.º 5
0
    def test_absolute_table(self, connection):
        pg_base = Base(connection.engine.url.database)

        table = pg_base._absolute_table('public', 'public.my_table')
        assert table == 'public.my_table'

        table = pg_base._absolute_table('public', 'my_table')
        assert table == 'public.my_table'
Exemplo n.º 6
0
 def test_model(self, connection):
     pg_base = Base(connection.engine.url.database)
     model = pg_base.model("book", "public")
     assert str(model.original) == "public.book"
     assert pg_base.models["public.book"] == model
     with pytest.raises(TableNotFoundError) as excinfo:
         pg_base.model("book", "bar")
         assert 'Table "bar.book" not found in registry' in str(
             excinfo.value)
Exemplo n.º 7
0
 def test_has_permission(self, connection):
     pg_base = Base(connection.engine.url.database)
     pg_base.verbose = False
     value = pg_base.has_permission(
         connection.engine.url.username,
         "usesuper",
     )
     assert value is True
     with pytest.raises(RuntimeError) as excinfo:
         value = pg_base.has_permission(
             connection.engine.url.username,
             "sudo",
         )
         assert "Invalid user permission sudo" in str(excinfo.value)
Exemplo n.º 8
0
def table_creator(base, connection, model_mapping):
    sa.orm.configure_mappers()
    base.metadata.create_all(connection)
    pg_base = Base(connection.engine.url.database)
    pg_base.create_triggers(
        connection.engine.url.database,
        DEFAULT_SCHEMA,
    )
    pg_base.drop_replication_slot(f"{connection.engine.url.database}_testdb")
    pg_base.create_replication_slot(f"{connection.engine.url.database}_testdb")
    yield
    pg_base.drop_replication_slot(f"{connection.engine.url.database}_testdb")
    base.metadata.drop_all(connection)
    try:
        os.unlink(f".{connection.engine.url.database}_testdb")
    except OSError:
        pass
Exemplo n.º 9
0
 def test_tables(self, connection):
     pg_base = Base(connection.engine.url.database)
     tables = [
         'public.continent',
         'public.country',
         'public.publisher',
         'public.book',
         'public.city',
         'public.book_subject',
         'public.subject',
         'public.book_language',
         'public.language',
         'public.book_shelf',
         'public.shelf',
         'public.author',
         'public.book_author',
         'public.rating',
     ]
     assert sorted(pg_base.tables('public')) == sorted(tables)
Exemplo n.º 10
0
 def test_tables(self, connection):
     pg_base = Base(connection.engine.url.database)
     tables = [
         "public.continent",
         "public.country",
         "public.publisher",
         "public.book",
         "public.city",
         "public.book_subject",
         "public.subject",
         "public.book_language",
         "public.language",
         "public.book_shelf",
         "public.shelf",
         "public.author",
         "public.book_author",
         "public.rating",
     ]
     assert sorted(pg_base.tables("public")) == sorted(tables)
Exemplo n.º 11
0
 def test_trigger_primary_key_function(self, connection):
     tables = {
         "book": ["isbn"],
         "publisher": ["id"],
         "book_language": ["id"],
         "author": ["id"],
         "language": ["id"],
         "subject": ["id"],
         "city": ["id"],
         "country": ["id"],
         "continent": ["id"],
     }
     pg_base = Base(connection.engine.url.database)
     for table_name, primary_keys in tables.items():
         query = (
             f"SELECT ARRAY_AGG(attname) "
             f"FROM pg_index "
             f"JOIN pg_attribute ON attrelid = indrelid AND attnum = ANY(indkey) "
             f"WHERE indrelid = '{table_name}'::regclass AND indisprimary")
         rows = pg_base.fetchall(query)[0]
         assert list(rows)[0] == primary_keys
Exemplo n.º 12
0
 def test_trigger_primary_key_function(self, connection):
     tables = {
         'book': ['isbn'],
         'publisher': ['id'],
         'book_language': ['id'],
         'author': ['id'],
         'language': ['id'],
         'subject': ['id'],
         'city': ['id'],
         'country': ['id'],
         'continent': ['id'],
     }
     pg_base = Base(connection.engine.url.database)
     for table_name, primary_keys in tables.items():
         query = (
             f"SELECT ARRAY_AGG(attname) "
             f"FROM pg_index "
             f"JOIN pg_attribute ON attrelid = indrelid AND attnum = ANY(indkey) "
             f"WHERE indrelid = '{table_name}'::regclass AND indisprimary")
         rows = pg_base.query(query)[0]
         assert list(rows)[0] == primary_keys
Exemplo n.º 13
0
 def test_trigger_foreign_key_function(self, connection):
     tables = {
         "book": ["publisher_id"],
         "publisher": None,
         "book_language": ["book_isbn", "language_id"],
         "author": ["city_id"],
         "language": None,
         "subject": None,
         "city": ["country_id"],
         "country": ["continent_id"],
         "continent": None,
     }
     pg_base = Base(connection.engine.url.database)
     for table_name, foreign_keys in tables.items():
         query = (
             f"SELECT ARRAY_AGG(column_name::TEXT) FROM information_schema.key_column_usage "
             f"WHERE constraint_catalog=current_catalog AND "
             f"table_name='{table_name}' AND position_in_unique_constraint NOTNULL "
         )
         rows = pg_base.fetchall(query)[0]
         assert rows[0] == foreign_keys
Exemplo n.º 14
0
 def test_trigger_foreign_key_function(self, connection):
     tables = {
         'book': ['publisher_id'],
         'publisher': None,
         'book_language': ['book_isbn', 'language_id'],
         'author': ['city_id'],
         'language': None,
         'subject': None,
         'city': ['country_id'],
         'country': ['continent_id'],
         'continent': None,
     }
     pg_base = Base(connection.engine.url.database)
     for table_name, foreign_keys in tables.items():
         query = (
             f"SELECT ARRAY_AGG(column_name::TEXT) FROM information_schema.key_column_usage "
             f"WHERE constraint_catalog=current_catalog AND "
             f"table_name='{table_name}' AND position_in_unique_constraint NOTNULL "
         )
         rows = pg_base.query(query)[0]
         assert rows[0] == foreign_keys
Exemplo n.º 15
0
    def test_has_permissions(self, connection):
        pg_base = Base(connection.engine.url.database)
        pg_base.verbose = False
        value = pg_base.has_permissions(
            connection.engine.url.username,
            ["usesuper"],
        )
        assert (pg_base.has_permissions(
            connection.engine.url.username,
            ["usesuper"],
        ) is True)

        assert (pg_base.has_permissions(
            "spiderman",
            ["usesuper"],
        ) is False)

        with pytest.raises(InvalidPermissionError) as excinfo:
            pg_base.has_permissions(
                connection.engine.url.username,
                ["sudo"],
            )
Exemplo n.º 16
0
    def test_get_schema(self, connection):
        pg_base = Base(connection.engine.url.database)

        pair1, pair2 = pg_base._get_schema('public', 'public1.my_table')
        assert pair1 == 'public1'
        assert pair2 == 'my_table'

        pair1, pair2 = pg_base._get_schema('public', 'my_table')
        assert pair1 == 'public'
        assert pair2 == 'my_table'

        with pytest.raises(ValueError) as excinfo:
            pg_base._get_schema('public', 'public1.my_table.foo')
            assert 'Invalid definition public1.my_table for public' == str(
                excinfo.value)
Exemplo n.º 17
0
    def test_get_schema(self, connection):
        pg_base = Base(connection.engine.url.database)

        pair1, pair2 = pg_base._get_schema("public", "public1.my_table")
        assert pair1 == "public1"
        assert pair2 == "my_table"

        pair1, pair2 = pg_base._get_schema("public", "my_table")
        assert pair1 == "public"
        assert pair2 == "my_table"

        with pytest.raises(ValueError) as excinfo:
            pg_base._get_schema("public", "public1.my_table.foo")
            assert "Invalid definition public1.my_table for public" == str(
                excinfo.value)
Exemplo n.º 18
0
 def test_pg_settings(self, connection):
     pg_base = Base(connection.engine.url.database)
     pg_base.verbose = False
     value = pg_base.pg_settings("max_replication_slots")
     assert int(value) > 0
     assert pg_base.pg_settings("xyz") is None
Exemplo n.º 19
0
 def test_drop_replication_slot(self, connection):
     pg_base = Base(connection.engine.url.database)
     pg_base.create_replication_slot("slot_name")
     pg_base.drop_replication_slot("slot_name")
Exemplo n.º 20
0
 def test_create_replication_slot(self, connection):
     pg_base = Base(connection.engine.url.database)
     row = pg_base.create_replication_slot("slot_name")
     assert row[0] == "slot_name"
     assert row[1] is not None
     pg_base.drop_replication_slot("slot_name")
Exemplo n.º 21
0
 def test_create_replication_slot(self, connection):
     pg_base = Base(connection.engine.url.database)
     row = pg_base.create_replication_slot('slot_name')
     assert row[0] == 'slot_name'
     assert row[1] != None
     pg_base.drop_replication_slot('slot_name')
Exemplo n.º 22
0
 def test_schemas(self, connection):
     pg_base = Base(connection.engine.url.database)
     assert pg_base.schemas == ["public"]
Exemplo n.º 23
0
 def test_database(self, connection):
     pg_base = Base(connection.engine.url.database)
     assert pg_base.database == "testdb"