File: test_get_column_key.py

package info (click to toggle)
python-sqlalchemy-utils 0.30.12-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 1,056 kB
  • sloc: python: 10,350; makefile: 160
file content (44 lines) | stat: -rw-r--r-- 1,309 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
from copy import copy

import sqlalchemy as sa
from pytest import raises
from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy_utils import get_column_key


class TestGetColumnKey(object):
    def setup_method(self, method):
        Base = declarative_base()

        class Building(Base):
            __tablename__ = 'building'
            id = sa.Column(sa.Integer, primary_key=True)
            name = sa.Column('_name', sa.Unicode(255))

        class Movie(Base):
            __tablename__ = 'movie'
            id = sa.Column(sa.Integer, primary_key=True)

        self.Building = Building
        self.Movie = Movie

    def test_supports_aliases(self):
        assert (
            get_column_key(self.Building, self.Building.__table__.c.id)
            ==
            'id'
        )
        assert (
            get_column_key(self.Building, self.Building.__table__.c._name)
            ==
            'name'
        )

    def test_supports_vague_matching_of_column_objects(self):
        column = copy(self.Building.__table__.c._name)
        assert get_column_key(self.Building, column) == 'name'

    def test_throws_value_error_for_unknown_column(self):
        with raises(sa.orm.exc.UnmappedColumnError):
            get_column_key(self.Building, self.Movie.__table__.c.id)