|
2 | 2 | # |
3 | 3 | # This file is part of Pyosmium. |
4 | 4 | # |
5 | | -# Copyright (C) 2022 Sarah Hoffmann. |
| 5 | +# Copyright (C) 2023 Sarah Hoffmann. |
6 | 6 | """ Tests for the pyosmium-get-changes script. |
7 | 7 | """ |
8 | | -from io import BytesIO |
9 | 8 | from pathlib import Path |
10 | 9 | from textwrap import dedent |
11 | 10 |
|
|
18 | 17 | import cookielib as cookiejarlib |
19 | 18 |
|
20 | 19 |
|
21 | | -class RequestsResponses(BytesIO): |
22 | | - |
23 | | - def __init__(self, bytes): |
24 | | - super(RequestsResponses, self).__init__(bytes) |
25 | | - self.content = bytes |
26 | | - |
27 | | - def iter_lines(self): |
28 | | - return self.readlines() |
29 | | - |
30 | | - |
31 | 20 | class TestPyosmiumGetChanges: |
32 | 21 |
|
33 | 22 | @pytest.fixture(autouse=True) |
34 | | - def setUp(self, monkeypatch): |
| 23 | + def setup(self): |
35 | 24 | self.script = dict() |
36 | 25 |
|
37 | 26 | filename = (Path(__file__) / ".." / ".." / "tools"/ "pyosmium-get-changes").resolve() |
38 | 27 | with filename.open("rb") as f: |
39 | 28 | exec(compile(f.read(), str(filename), 'exec'), self.script) |
40 | 29 |
|
41 | | - self.urls = dict() |
42 | | - |
43 | | - |
44 | | - @pytest.fixture |
45 | | - def mock_requests(self, monkeypatch): |
46 | | - def mock_get(session, url, **kwargs): |
47 | | - return RequestsResponses(self.urls[url]) |
48 | | - monkeypatch.setattr(osmium.replication.server.requests.Session, "get", mock_get) |
49 | | - |
50 | | - |
51 | | - def url(self, url, result): |
52 | | - self.urls[url] = dedent(result).encode() |
53 | 30 |
|
54 | | - def main(self, *args): |
55 | | - return self.script['main'](args) |
| 31 | + def main(self, httpserver, *args): |
| 32 | + return self.script['main'](['--server', httpserver.url_for('')] + list(args)) |
56 | 33 |
|
57 | 34 |
|
58 | | - def test_init_id(self, capsys): |
59 | | - assert 0 == self.main('-I', '453') |
| 35 | + def test_init_id(self, capsys, httpserver): |
| 36 | + assert 0 == self.main(httpserver, '-I', '453') |
60 | 37 |
|
61 | 38 | output = capsys.readouterr().out.strip() |
62 | 39 |
|
63 | 40 | assert output == '453' |
64 | 41 |
|
65 | 42 |
|
66 | | - def test_init_date(self, capsys, mock_requests): |
67 | | - self.url('https://planet.osm.org/replication/minute//state.txt', |
68 | | - """\ |
| 43 | + def test_init_date(self, capsys, httpserver): |
| 44 | + httpserver.expect_request('/state.txt').respond_with_data(dedent("""\ |
69 | 45 | sequenceNumber=100 |
70 | 46 | timestamp=2017-08-26T11\\:04\\:02Z |
71 | | - """) |
72 | | - self.url('https://planet.osm.org/replication/minute//000/000/000.state.txt', |
73 | | - """\ |
| 47 | + """)) |
| 48 | + httpserver.expect_request('/000/000/000.state.txt').respond_with_data(dedent("""\ |
74 | 49 | sequenceNumber=0 |
75 | 50 | timestamp=2016-08-26T11\\:04\\:02Z |
76 | | - """) |
77 | | - assert 0 == self.main('-D', '2015-12-24T08:08:08Z') |
| 51 | + """)) |
| 52 | + assert 0 == self.main(httpserver, '-D', '2015-12-24T08:08:08Z') |
78 | 53 |
|
79 | 54 | output = capsys.readouterr().out.strip() |
80 | 55 |
|
81 | 56 | assert output == '-1' |
82 | 57 |
|
83 | 58 |
|
84 | | - def test_init_to_file(self, tmp_path): |
| 59 | + def test_init_to_file(self, tmp_path, httpserver): |
85 | 60 | fname = tmp_path / 'db.seq' |
86 | 61 |
|
87 | | - assert 0 == self.main('-I', '453', '-f', str(fname)) |
| 62 | + assert 0 == self.main(httpserver, '-I', '453', '-f', str(fname)) |
88 | 63 | assert fname.read_text() == '453' |
89 | 64 |
|
90 | 65 |
|
91 | | - def test_init_from_seq_file(self, tmp_path): |
| 66 | + def test_init_from_seq_file(self, tmp_path, httpserver): |
92 | 67 | fname = tmp_path / 'db.seq' |
93 | 68 | fname.write_text('453') |
94 | 69 |
|
95 | | - assert 0 == self.main('-f', str(fname)) |
| 70 | + assert 0 == self.main(httpserver, '-f', str(fname)) |
96 | 71 | assert fname.read_text() == '453' |
97 | 72 |
|
98 | 73 |
|
99 | | - def test_init_date_with_cookie(self, capsys, tmp_path, mock_requests): |
100 | | - self.url('https://planet.osm.org/replication/minute//state.txt', |
101 | | - """\ |
| 74 | + def test_init_date_with_cookie(self, capsys, tmp_path, httpserver): |
| 75 | + httpserver.expect_request('/state.txt').respond_with_data(dedent("""\ |
102 | 76 | sequenceNumber=100 |
103 | 77 | timestamp=2017-08-26T11\\:04\\:02Z |
104 | | - """) |
105 | | - self.url('https://planet.osm.org/replication/minute//000/000/000.state.txt', |
106 | | - """\ |
| 78 | + """)) |
| 79 | + httpserver.expect_request('/000/000/000.state.txt').respond_with_data(dedent("""\ |
107 | 80 | sequenceNumber=0 |
108 | 81 | timestamp=2016-08-26T11\\:04\\:02Z |
109 | | - """) |
| 82 | + """)) |
110 | 83 |
|
111 | 84 | fname = tmp_path / 'my.cookie' |
112 | 85 | cookie_jar = cookiejarlib.MozillaCookieJar(str(fname)) |
113 | 86 | cookie_jar.save() |
114 | 87 |
|
115 | | - assert 0 == self.main('--cookie', str(fname), '-D', '2015-12-24T08:08:08Z') |
| 88 | + assert 0 == self.main(httpserver, '--cookie', str(fname), |
| 89 | + '-D', '2015-12-24T08:08:08Z') |
116 | 90 |
|
117 | 91 | output = capsys.readouterr().out.strip() |
118 | 92 |
|
|
0 commit comments