|
1
|
|
|
# coding=utf-8 |
|
2
|
|
|
import json |
|
3
|
|
|
import os |
|
4
|
|
|
|
|
5
|
|
|
import pytest |
|
6
|
|
|
from aiohttp import web |
|
7
|
|
|
from asynctest import CoroutineMock |
|
8
|
|
|
|
|
9
|
|
|
from acme.api import Api |
|
10
|
|
|
from acme.asynchttpclient import AsyncHttpClient |
|
11
|
|
|
from acme.cache import MemoryCache |
|
12
|
|
|
from acme.megastore import Megastore |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
@pytest.fixture(scope='session') |
|
16
|
|
|
def config(): |
|
17
|
|
|
return { |
|
18
|
|
|
'ACME_DEBUG': True, |
|
19
|
|
|
'API_BASE_URL': 'http://localhost:6000/api/' |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
@pytest.fixture |
|
24
|
|
|
def http(): |
|
25
|
|
|
result_http = AsyncHttpClient() |
|
26
|
|
|
result_http.get = CoroutineMock() |
|
27
|
|
|
|
|
28
|
|
|
return result_http |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
@pytest.fixture |
|
32
|
|
|
def cache(): |
|
33
|
|
|
return MemoryCache() |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
@pytest.fixture |
|
37
|
|
|
def api(config, http, cache, purchases_by_user, purchases_by_product, |
|
38
|
|
|
products): |
|
39
|
|
|
""" TODO: For some reason it's not working, will have to debug later. """ |
|
40
|
|
|
result_api = Api(config, http, cache) |
|
41
|
|
|
result_api.purchases_by_user = CoroutineMock() |
|
42
|
|
|
result_api.purchases_by_product = CoroutineMock() |
|
43
|
|
|
result_api.products = CoroutineMock() |
|
44
|
|
|
result_api.purchases_by_user.return_value = purchases_by_user |
|
45
|
|
|
result_api.purchases_by_product.side_effect = purchases_by_product |
|
46
|
|
|
result_api.products.side_effect = products |
|
47
|
|
|
|
|
48
|
|
|
return result_api |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
@pytest.fixture |
|
52
|
|
|
def handler(config, api, loop): |
|
53
|
|
|
return Megastore(config, api, loop) |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
@pytest.fixture |
|
57
|
|
|
def app(handler): |
|
58
|
|
|
application = web.Application() |
|
59
|
|
|
|
|
60
|
|
|
application.router.add_route('get', '/api/recent_purchases/{username}', |
|
61
|
|
|
handler.recent_purchases) |
|
62
|
|
|
return application |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
@pytest.fixture(scope='session') |
|
66
|
|
|
def purchases_by_user(): |
|
67
|
|
|
return _load_json('purchases_by_user.json') |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
@pytest.fixture(scope='session') |
|
71
|
|
|
def purchases_by_product(): |
|
72
|
|
|
return _load_json('purchases_by_product.json') |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
@pytest.fixture(scope='session') |
|
76
|
|
|
def products(): |
|
77
|
|
|
return _load_json('products.json') |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
def _load_json(filename): |
|
81
|
|
|
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), |
|
82
|
|
|
'fixtures', filename) |
|
83
|
|
|
with open(path) as fp: |
|
84
|
|
|
return json.load(fp) |
|
85
|
|
|
|