aboutsummaryrefslogtreecommitdiff
path: root/tests/helper.py
blob: 935e0601f1ad9bbd82433ea841fd89474f1ad2c5 (plain) (blame)
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
import sys
import unittest
from decimal import Decimal as D
from unittest import mock
import requests_mock
from io import StringIO
import portfolio, market, main, store, dbs

__all__ = ["limits", "unittest", "WebMockTestCase", "mock", "D",
        "StringIO"]

limits = ["acceptance", "unit"]
for test_type in limits:
    if "--no{}".format(test_type) in sys.argv:
        sys.argv.remove("--no{}".format(test_type))
        limits.remove(test_type)
    if "--only{}".format(test_type) in sys.argv:
        sys.argv.remove("--only{}".format(test_type))
        limits = [test_type]
        break

class WebMockTestCase(unittest.TestCase):
    import time

    def market_args(self, debug=False, quiet=False, report_path=None, **kwargs):
        return main.configargparse.Namespace(report_path=report_path,
                debug=debug, quiet=quiet, **kwargs)

    def setUp(self):
        super().setUp()
        self.wm = requests_mock.Mocker()
        self.wm.start()

        # market
        self.m = mock.Mock(name="Market", spec=market.Market)
        self.m.debug = False

        self.patchers = [
                mock.patch.multiple(market.Portfolio,
                    data=store.LockedVar(None),
                    liquidities=store.LockedVar({}),
                    last_date=store.LockedVar(None),
                    report=mock.Mock(),
                    worker=None,
                    worker_tag="",
                    worker_notify=None,
                    worker_started=False,
                    callback=None),
                mock.patch.multiple(portfolio.Computation,
                    computations=portfolio.Computation.computations),
                mock.patch.multiple(dbs,
                    redis=None, psql=None)
                ]
        for patcher in self.patchers:
            patcher.start()

    def tearDown(self):
        for patcher in self.patchers:
            patcher.stop()
        self.wm.stop()
        super().tearDown()