File: test_uuid.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 (41 lines) | stat: -rw-r--r-- 962 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
import uuid

import sqlalchemy as sa

from sqlalchemy_utils import UUIDType
from tests import TestCase


class TestUUIDType(TestCase):
    def create_models(self):
        class User(self.Base):
            __tablename__ = 'user'
            id = sa.Column(UUIDType, default=uuid.uuid4, primary_key=True)

            def __repr__(self):
                return 'User(%r)' % self.id

        self.User = User

    def test_commit(self):
        obj = self.User()
        obj.id = uuid.uuid4().hex

        self.session.add(obj)
        self.session.commit()

        u = self.session.query(self.User).one()

        assert u.id == obj.id

    def test_coerce(self):
        obj = self.User()
        obj.id = identifier = uuid.uuid4().hex

        assert isinstance(obj.id, uuid.UUID)
        assert obj.id.hex == identifier

        obj.id = identifier = uuid.uuid4().bytes

        assert isinstance(obj.id, uuid.UUID)
        assert obj.id.bytes == identifier