File: test_client.py

package info (click to toggle)
python-picnic-api2 1.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 224 kB
  • sloc: python: 670; makefile: 3
file content (158 lines) | stat: -rw-r--r-- 4,122 bytes parent folder | download
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import os
import time

import pytest
from dotenv import load_dotenv

from python_picnic_api2 import PicnicAPI

load_dotenv()

username = os.getenv("USERNAME")
password = os.getenv("PASSWORD")
country_code = os.getenv("COUNTRY_CODE")

picnic = PicnicAPI(username, password, country_code=country_code)


@pytest.fixture(autouse=True)
def slow_down_tests():
    yield
    time.sleep(2)


def _get_amount(cart: dict, product_id: str):
    items = cart["items"][0]["items"]
    product = next((item for item in items if item["id"] == product_id), None)
    return product["decorators"][0]["quantity"]


def test_get_user():
    response = picnic.get_user()
    assert isinstance(response, dict)
    assert "contact_email" in response
    assert response["contact_email"] == username


def test_search():
    response = picnic.search("kaffee")
    assert isinstance(response, list)
    assert isinstance(response[0], dict)
    assert "items" in response[0]
    assert isinstance(response[0]["items"], list)
    assert "id" in response[0]["items"][0]


def test_get_article():
    response = picnic.get_article("s1018620")
    assert isinstance(response, dict)
    assert "id" in response
    assert response["id"] == "s1018620"
    assert response["name"] == "Gut&Günstig H-Milch 3,5%"


def test_get_article_with_category_name():
    with pytest.raises(NotImplementedError):
        picnic.get_article("s1018620", add_category_name=True)


def test_get_article_by_gtin():
    response = picnic.get_article_by_gtin("4311501044209")
    assert response["id"] == "s1018620"
    assert response["name"] == "Gut&Günstig H-Milch 3,5%"


def test_get_article_by_gtin_unknown():
    response = picnic.get_article_by_gtin("4311501040000")
    assert response is None


def test_get_cart():
    response = picnic.get_cart()
    assert isinstance(response, dict)
    assert "id" in response
    assert response["id"] == "shopping_cart"


def test_add_product():
    # need a clear cart for reproducibility
    picnic.clear_cart()
    response = picnic.add_product("s1018620", count=2)

    assert isinstance(response, dict)
    assert "items" in response
    assert any(item["id"] == "s1018620" for item in response["items"][0]["items"])
    assert _get_amount(response, "s1018620") == 2


def test_remove_product():
    # need a clear cart for reproducibility
    picnic.clear_cart()
    # add two milk to the cart so we can remove 1
    picnic.add_product("s1018620", count=2)

    response = picnic.remove_product("s1018620", count=1)
    amount = _get_amount(response, "s1018620")

    assert isinstance(response, dict)
    assert "items" in response
    assert amount == 1


def test_clear_cart():
    # need a clear cart for reproducibility
    picnic.clear_cart()
    # add two coffee to the cart so we can clear it
    picnic.add_product("s1018620", count=2)

    response = picnic.clear_cart()

    assert isinstance(response, dict)
    assert "items" in response
    assert len(response["items"]) == 0


def test_get_delivery_slots():
    response = picnic.get_delivery_slots()
    assert isinstance(response, dict)
    assert "delivery_slots" in response
    assert isinstance(response["delivery_slots"], list)


def test_get_deliveries():
    response = picnic.get_deliveries()

    assert isinstance(response, list)
    assert isinstance(response[0], dict)
    assert response[0]["status"] == "COMPLETED"


def test_get_delivery():
    # get a id to test against
    response = picnic.get_deliveries()
    deliveryId = response[0]["delivery_id"]

    response = picnic.get_delivery(deliveryId)
    assert isinstance(response, dict)
    assert response["status"] == "COMPLETED"
    assert response["id"] == deliveryId


def test_get_current_deliveries():
    response = picnic.get_current_deliveries()
    assert isinstance(response, list)


def test_get_categories():
    response = picnic.get_categories()
    assert isinstance(response, list)


def test_print_categories(capsys):
    picnic.print_categories()
    captured = capsys.readouterr()

    assert isinstance(captured.out, str)


# TODO: add test for re-logging