Skip to content

Commit df5fb81

Browse files
committed
move pyosmium-get_changes tests to pytest-httpserver
1 parent e9f4563 commit df5fb81

File tree

1 file changed

+23
-49
lines changed

1 file changed

+23
-49
lines changed

test/test_pyosmium_get_changes.py

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
#
33
# This file is part of Pyosmium.
44
#
5-
# Copyright (C) 2022 Sarah Hoffmann.
5+
# Copyright (C) 2023 Sarah Hoffmann.
66
""" Tests for the pyosmium-get-changes script.
77
"""
8-
from io import BytesIO
98
from pathlib import Path
109
from textwrap import dedent
1110

@@ -18,101 +17,76 @@
1817
import cookielib as cookiejarlib
1918

2019

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-
3120
class TestPyosmiumGetChanges:
3221

3322
@pytest.fixture(autouse=True)
34-
def setUp(self, monkeypatch):
23+
def setup(self):
3524
self.script = dict()
3625

3726
filename = (Path(__file__) / ".." / ".." / "tools"/ "pyosmium-get-changes").resolve()
3827
with filename.open("rb") as f:
3928
exec(compile(f.read(), str(filename), 'exec'), self.script)
4029

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()
5330

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))
5633

5734

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')
6037

6138
output = capsys.readouterr().out.strip()
6239

6340
assert output == '453'
6441

6542

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("""\
6945
sequenceNumber=100
7046
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("""\
7449
sequenceNumber=0
7550
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')
7853

7954
output = capsys.readouterr().out.strip()
8055

8156
assert output == '-1'
8257

8358

84-
def test_init_to_file(self, tmp_path):
59+
def test_init_to_file(self, tmp_path, httpserver):
8560
fname = tmp_path / 'db.seq'
8661

87-
assert 0 == self.main('-I', '453', '-f', str(fname))
62+
assert 0 == self.main(httpserver, '-I', '453', '-f', str(fname))
8863
assert fname.read_text() == '453'
8964

9065

91-
def test_init_from_seq_file(self, tmp_path):
66+
def test_init_from_seq_file(self, tmp_path, httpserver):
9267
fname = tmp_path / 'db.seq'
9368
fname.write_text('453')
9469

95-
assert 0 == self.main('-f', str(fname))
70+
assert 0 == self.main(httpserver, '-f', str(fname))
9671
assert fname.read_text() == '453'
9772

9873

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("""\
10276
sequenceNumber=100
10377
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("""\
10780
sequenceNumber=0
10881
timestamp=2016-08-26T11\\:04\\:02Z
109-
""")
82+
"""))
11083

11184
fname = tmp_path / 'my.cookie'
11285
cookie_jar = cookiejarlib.MozillaCookieJar(str(fname))
11386
cookie_jar.save()
11487

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')
11690

11791
output = capsys.readouterr().out.strip()
11892

0 commit comments

Comments
 (0)