File: mocks.py

package info (click to toggle)
python-bel-resources 0.0.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 208 kB
  • sloc: python: 896; sh: 20; makefile: 3
file content (54 lines) | stat: -rw-r--r-- 1,527 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
# -*- coding: utf-8 -*-

"""Mocks for PyBEL testing."""

from unittest import mock

from tests.constants import TEST_ANNOTATION_PATH, TEST_NAMESPACE_EMPTY_PATH

__all__ = [
    'MockResponse',
    'MockSession',
    'mock_bel_resources',
]


class MockResponse:
    """See http://stackoverflow.com/questions/15753390/python-mock-requests-and-the-response."""

    def __init__(self, url_to_mock: str):
        """Build a mock for the requests Response object."""
        if url_to_mock.endswith('test_an_1.belanno'):
            self.path = TEST_ANNOTATION_PATH

        elif url_to_mock.endswith('test_ns_empty.belns'):
            self.path = TEST_NAMESPACE_EMPTY_PATH

        else:
            raise ValueError

    def iter_lines(self):
        """Iterate the lines of the mock file."""
        with open(self.path, 'rb') as file:
            yield from file

    def raise_for_status(self):
        """Mock raising an error, by not doing anything at all."""


class MockSession:
    """Patches the session object so requests can be redirected through the filesystem without rewriting BEL files."""

    def mount(self, prefix, adapter):
        """Mock mounting an adapter by not doing anything."""

    @staticmethod
    def get(url: str):
        """Mock getting a URL by returning a mock response."""
        return MockResponse(url)

    def close(self):
        """Mock closing a connection by not doing anything."""


mock_bel_resources = mock.patch('bel_resources.utils.requests.Session', side_effect=MockSession)