File: test_identity.py

package info (click to toggle)
python-sqlalchemy-utils 0.41.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,252 kB
  • sloc: python: 13,566; makefile: 141
file content (46 lines) | stat: -rw-r--r-- 1,219 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import pytest
import sqlalchemy as sa

from sqlalchemy_utils.functions import identity


class IdentityTestCase:

    @pytest.fixture
    def init_models(self, Building):
        pass

    def test_for_transient_class_without_id(self, Building):
        assert identity(Building()) == (None, )

    def test_for_transient_class_with_id(self, session, Building):
        building = Building(name='Some building')
        session.add(building)
        session.flush()

        assert identity(building) == (building.id, )

    def test_identity_for_class(self, Building):
        assert identity(Building) == (Building.id, )


class TestIdentity(IdentityTestCase):

    @pytest.fixture
    def Building(self, Base):
        class Building(Base):
            __tablename__ = 'building'
            id = sa.Column(sa.Integer, primary_key=True)
            name = sa.Column(sa.Unicode(255))
        return Building


class TestIdentityWithColumnAlias(IdentityTestCase):

    @pytest.fixture
    def Building(self, Base):
        class Building(Base):
            __tablename__ = 'building'
            id = sa.Column('_id', sa.Integer, primary_key=True)
            name = sa.Column(sa.Unicode(255))
        return Building