File: test_recipes.py

package info (click to toggle)
sqlite-utils 4.0~a0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,320 kB
  • sloc: python: 14,310; makefile: 33; ansic: 26; javascript: 21; sh: 5
file content (135 lines) | stat: -rw-r--r-- 3,762 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from sqlite_utils import recipes
from sqlite_utils.utils import sqlite3
import json
import pytest


@pytest.fixture
def dates_db(fresh_db):
    fresh_db["example"].insert_all(
        [
            {"id": 1, "dt": "5th October 2019 12:04"},
            {"id": 2, "dt": "6th October 2019 00:05:06"},
            {"id": 3, "dt": ""},
            {"id": 4, "dt": None},
        ],
        pk="id",
    )
    return fresh_db


def test_parsedate(dates_db):
    dates_db["example"].convert("dt", recipes.parsedate)
    assert list(dates_db["example"].rows) == [
        {"id": 1, "dt": "2019-10-05"},
        {"id": 2, "dt": "2019-10-06"},
        {"id": 3, "dt": ""},
        {"id": 4, "dt": None},
    ]


def test_parsedatetime(dates_db):
    dates_db["example"].convert("dt", recipes.parsedatetime)
    assert list(dates_db["example"].rows) == [
        {"id": 1, "dt": "2019-10-05T12:04:00"},
        {"id": 2, "dt": "2019-10-06T00:05:06"},
        {"id": 3, "dt": ""},
        {"id": 4, "dt": None},
    ]


@pytest.mark.parametrize(
    "recipe,kwargs,expected",
    (
        ("parsedate", {}, "2005-03-04"),
        ("parsedate", {"dayfirst": True}, "2005-04-03"),
        ("parsedatetime", {}, "2005-03-04T00:00:00"),
        ("parsedatetime", {"dayfirst": True}, "2005-04-03T00:00:00"),
    ),
)
def test_dayfirst_yearfirst(fresh_db, recipe, kwargs, expected):
    fresh_db["example"].insert_all(
        [
            {"id": 1, "dt": "03/04/05"},
        ],
        pk="id",
    )
    fresh_db["example"].convert(
        "dt", lambda value: getattr(recipes, recipe)(value, **kwargs)
    )
    assert list(fresh_db["example"].rows) == [
        {"id": 1, "dt": expected},
    ]


@pytest.mark.parametrize("fn", ("parsedate", "parsedatetime"))
@pytest.mark.parametrize("errors", (None, recipes.SET_NULL, recipes.IGNORE))
def test_dateparse_errors(fresh_db, fn, errors):
    fresh_db["example"].insert_all(
        [
            {"id": 1, "dt": "invalid"},
        ],
        pk="id",
    )
    if errors is None:
        # Should raise an error
        with pytest.raises(sqlite3.OperationalError):
            fresh_db["example"].convert("dt", lambda value: getattr(recipes, fn)(value))
    else:
        fresh_db["example"].convert(
            "dt", lambda value: getattr(recipes, fn)(value, errors=errors)
        )
        rows = list(fresh_db["example"].rows)
        expected = [{"id": 1, "dt": None if errors is recipes.SET_NULL else "invalid"}]
        assert rows == expected


@pytest.mark.parametrize("delimiter", [None, ";", "-"])
def test_jsonsplit(fresh_db, delimiter):
    fresh_db["example"].insert_all(
        [
            {"id": 1, "tags": (delimiter or ",").join(["foo", "bar"])},
            {"id": 2, "tags": (delimiter or ",").join(["bar", "baz"])},
        ],
        pk="id",
    )
    if delimiter is not None:

        def fn(value):
            return recipes.jsonsplit(value, delimiter=delimiter)

    else:
        fn = recipes.jsonsplit

    fresh_db["example"].convert("tags", fn)
    assert list(fresh_db["example"].rows) == [
        {"id": 1, "tags": '["foo", "bar"]'},
        {"id": 2, "tags": '["bar", "baz"]'},
    ]


@pytest.mark.parametrize(
    "type,expected",
    (
        (None, ["1", "2", "3"]),
        (float, [1.0, 2.0, 3.0]),
        (int, [1, 2, 3]),
    ),
)
def test_jsonsplit_type(fresh_db, type, expected):
    fresh_db["example"].insert_all(
        [
            {"id": 1, "records": "1,2,3"},
        ],
        pk="id",
    )
    if type is not None:

        def fn(value):
            return recipes.jsonsplit(value, type=type)

    else:
        fn = recipes.jsonsplit

    fresh_db["example"].convert("records", fn)
    assert json.loads(fresh_db["example"].get(1)["records"]) == expected