File: test_arrow.py

package info (click to toggle)
python-sqlalchemy-utils 0.41.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,252 kB
  • sloc: python: 13,566; makefile: 141
file content (85 lines) | stat: -rw-r--r-- 2,706 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from datetime import datetime

import pytest
import sqlalchemy as sa
from dateutil import tz

from sqlalchemy_utils.compat import _select_args
from sqlalchemy_utils.types import arrow


@pytest.fixture
def Article(Base):
    class Article(Base):
        __tablename__ = 'article'
        id = sa.Column(sa.Integer, primary_key=True)
        created_at = sa.Column(arrow.ArrowType)
        published_at = sa.Column(arrow.ArrowType(timezone=True))
        published_at_dt = sa.Column(sa.DateTime(timezone=True))
    return Article


@pytest.fixture
def init_models(Article):
    pass


@pytest.mark.skipif('arrow.arrow is None')
class TestArrowDateTimeType:
    def test_parameter_processing(self, session, Article):
        article = Article(
            created_at=arrow.arrow.get(datetime(2000, 11, 1))
        )

        session.add(article)
        session.commit()

        article = session.query(Article).first()
        assert article.created_at.datetime

    def test_string_coercion(self, Article):
        article = Article(
            created_at='2013-01-01'
        )
        assert article.created_at.year == 2013

    def test_utc(self, session, Article):
        time = arrow.arrow.utcnow()
        article = Article(created_at=time)
        session.add(article)
        assert article.created_at == time
        session.commit()
        assert article.created_at == time

    def test_other_tz(self, session, Article):
        time = arrow.arrow.utcnow()
        local = time.to('US/Pacific')
        article = Article(created_at=local)
        session.add(article)
        assert article.created_at == time == local
        session.commit()
        assert article.created_at == time

    def test_literal_param(self, session, Article):
        clause = Article.created_at > datetime(2015, 1, 1)
        compiled = str(clause.compile(compile_kwargs={"literal_binds": True}))
        assert compiled == "article.created_at > '2015-01-01 00:00:00'"

    @pytest.mark.usefixtures('postgresql_dsn')
    def test_timezone(self, session, Article):
        timezone = tz.gettz('Europe/Stockholm')
        dt = arrow.arrow.get(datetime(2015, 1, 1, 15, 30, 45), timezone)
        article = Article(published_at=dt, published_at_dt=dt.datetime)

        session.add(article)
        session.commit()
        session.expunge_all()

        item = session.query(Article).one()
        assert item.published_at.datetime == item.published_at_dt
        assert item.published_at.to(timezone) == dt

    def test_compilation(self, Article, session):
        query = sa.select(*_select_args(Article.created_at))
        # the type should be cacheable and not throw exception
        session.execute(query)