File: conftest.py

package info (click to toggle)
python-osmapi 3.1.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 760 kB
  • sloc: python: 3,340; xml: 1,591; makefile: 41; sh: 16
file content (95 lines) | stat: -rw-r--r-- 2,015 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
import osmapi
import pytest
import mock
import responses
import os
import re

__location__ = os.path.realpath(
    os.path.join(
        os.getcwd(),
        os.path.dirname(__file__)
    )
)


@pytest.fixture
def file_content():
    def _file_content(filename):
        path = os.path.join(__location__, 'fixtures', filename)
        if not os.path.exists(path):
            return ""
        with open(path) as f:
            return f.read()

    return _file_content


@pytest.fixture
def api():
    api_base = "http://api06.dev.openstreetmap.org"
    api = osmapi.OsmApi(
        api=api_base
    )
    api._session._sleep = mock.Mock()

    yield api
    api.close()


@pytest.fixture
def auth_api():
    api_base = "http://api06.dev.openstreetmap.org"
    api = osmapi.OsmApi(
        api=api_base,
        username='testuser',
        password='testpassword'
    )
    api._session._sleep = mock.Mock()

    yield api
    api.close()


@pytest.fixture
def prod_api():
    api_base = "https://www.openstreetmap.org"
    api = osmapi.OsmApi(
        api=api_base,
        username='testuser',
        password='testpassword'
    )
    api._session._sleep = mock.Mock()

    yield api
    api.close()


@pytest.fixture
def mocked_responses():
    with responses.RequestsMock() as rsps:
        yield rsps


@pytest.fixture
def add_response(mocked_responses, file_content, request):
    def _add_response(method, path=None, filename=None, body=None, status=200):
        if not filename:
            # use testname by default
            filename = f"{request.node.originalname}.xml"

        if path:
            url = f'http://api06.dev.openstreetmap.org/api/0.6{path}'
        else:
            url = re.compile(r'http:\/\/api06\.dev\.openstreetmap\.org.*')

        if not body:
            body = file_content(filename)
        mocked_responses.add(
            method,
            url=url,
            body=body,
            status=status
        )
        return mocked_responses
    return _add_response