sqlalchemy_utils.get_class_by_table

Here are the examples of the python api sqlalchemy_utils.get_class_by_table taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

6 Examples 7

Example 1

Project: sqlalchemy-utils Source File: test_get_class_by_table.py
    def test_returns_class(self, Base, User, Entity):
        assert get_class_by_table(Base, User.__table__) == User
        assert get_class_by_table(
            Base,
            Entity.__table__
        ) == Entity

Example 2

Project: sqlalchemy-utils Source File: test_get_class_by_table.py
    def test_table_with_no_associated_class(self, Base):
        table = sa.Table(
            'some_table',
            Base.metadata,
            sa.Column('id', sa.Integer)
        )
        assert get_class_by_table(Base, table) is None

Example 3

Project: sqlalchemy-utils Source File: test_get_class_by_table.py
    def test_multiple_classes_without_data_parameter(self, Base, Entity, User):
        with pytest.raises(ValueError):
            assert get_class_by_table(
                Base,
                Entity.__table__
            )

Example 4

Project: sqlalchemy-utils Source File: test_get_class_by_table.py
    def test_multiple_classes_with_data_parameter(self, Base, Entity, User):
        assert get_class_by_table(
            Base,
            Entity.__table__,
            {'type': 'entity'}
        ) == Entity
        assert get_class_by_table(
            Base,
            Entity.__table__,
            {'type': 'user'}
        ) == User

Example 5

Project: sqlalchemy-utils Source File: test_get_class_by_table.py
    def test_multiple_classes_with_bogus_data(self, Base, Entity, User):
        with pytest.raises(ValueError):
            assert get_class_by_table(
                Base,
                Entity.__table__,
                {'type': 'unknown'}
            )

Example 6

Project: postgresql-audit Source File: base.py
def activity_base(Base, schema=None):
    class ActivityBase(Base):
        __abstract__ = True
        __table_args__ = {'schema': schema}
        id = sa.Column(sa.BigInteger, primary_key=True)
        schema_name = sa.Column(sa.Text)
        table_name = sa.Column(sa.Text)
        relid = sa.Column(sa.Integer)
        issued_at = sa.Column(sa.DateTime)
        native_transaction_id = sa.Column(sa.BigInteger)
        verb = sa.Column(sa.Text)
        old_data = sa.Column(JSONB)
        changed_data = sa.Column(JSONB)

        @hybrid_property
        def data(self):
            data = self.old_data.copy() if self.old_data else {}
            if self.changed_data:
                data.update(self.changed_data)
            return data

        @data.expression
        def data(cls):
            return jsonb_merge(cls.old_data, cls.changed_data)

        @property
        def object(self):
            table = Base.metadata.tables[self.table_name]
            cls = get_class_by_table(Base, table, self.data)
            return cls(**self.data)

        def __repr__(self):
            return (
                '<{cls} table_name={table_name!r} '
                'id={id!r}>'
            ).format(
                cls=self.__class__.__name__,
                table_name=self.table_name,
                id=self.id
            )
    return ActivityBase