File: test_comparison.py

package info (click to toggle)
python-pytest-toolbox 0.4-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 196 kB
  • sloc: python: 391; makefile: 27; sh: 6
file content (71 lines) | stat: -rw-r--r-- 1,730 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
import uuid
from datetime import datetime, timedelta, timezone

import pytest

from pytest_toolbox.comparison import AnyInt, CloseToNow, IsUUID, RegexStr


def test_close_to_now_true():
    c2n = CloseToNow()
    dt = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')
    assert dt == c2n
    assert str(c2n) == repr(dt)


def test_close_to_now_true_dt():
    assert datetime.utcnow() == CloseToNow()


def test_close_to_now_false():
    c2n = CloseToNow()
    with pytest.raises(AssertionError):
        assert datetime(2000, 1, 1).strftime('%Y-%m-%dT%H:%M:%S') == c2n
    assert str(c2n).startswith('<CloseToNow(delta=2, now=')


def test_clow_to_now_tz():
    diff = timedelta(hours=2)
    dt = datetime.utcnow().replace(tzinfo=timezone(offset=diff)) + diff
    assert dt == CloseToNow()


def test_any_int_true():
    any_int = AnyInt()
    assert 123 == any_int
    assert str(any_int) == '123'


def test_any_int_false():
    any_int = AnyInt()
    with pytest.raises(AssertionError):
        assert '123' == any_int
    assert str(any_int) == '<AnyInt>'


def test_regex_true():
    assert 'whatever' == RegexStr('whatever')
    reg = RegexStr('wh.*er')
    assert 'whatever' == reg
    assert str(reg) == "'whatever'"


def test_regex_false():
    reg = RegexStr('wh.*er')
    with pytest.raises(AssertionError):
        assert 'WHATEVER' == reg
    assert str(reg) == "<RegexStr(regex=re.compile('wh.*er', re.DOTALL)>"


def test_is_uuid_true():
    is_uuid = IsUUID()
    uuid_ = uuid.uuid4()
    assert uuid_ == is_uuid
    assert str(is_uuid) == repr(uuid_)


def test_is_uuid_false():
    is_uuid = IsUUID()
    with pytest.raises(AssertionError):
        assert '123' == is_uuid
    assert str(is_uuid) == '<UUID(*)>'