Skip to content

Commit

Permalink
Merge pull request #32 from zstyblik/pytest-localserver_update
Browse files Browse the repository at this point in the history
Update pytest-localserver to v0.9.0 and sunset MyContentServer
  • Loading branch information
zstyblik authored Aug 20, 2024
2 parents c2c9a07 + 537d35b commit 7851d77
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 58 deletions.
9 changes: 4 additions & 5 deletions ci/run-black.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ else
exit 1
fi

python3 \
-m black \
${black_arg} \
-l 80 \
`find . ! -path '*/\.*' -name '*.py'`

# shellcheck disable=SC2086
find . ! -path '*/\.*' -name '*.py' -print0 | \
xargs -0 -- python3 -m black ${black_arg} -l 80
3 changes: 2 additions & 1 deletion ci/run-reorder-python-imports.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
set -e
set -u

reorder-python-imports --py311-plus `find . ! -path '*/\.*' -name '*.py'`
find . ! -path '*/\.*' -name '*.py' -print0 | \
xargs -0 -- reorder-python-imports --py311-plus
2 changes: 1 addition & 1 deletion requirements-ci.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ flake8-import-order
reorder-python-imports
# tests
pytest
pytest-localserver==0.8.1
pytest-localserver==0.9.0
requests-mock==1.12.1
25 changes: 1 addition & 24 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,6 @@
import pytest
import requests_mock
from pytest_localserver.http import ContentServer
from werkzeug.wrappers import Request


class MyContentServer(ContentServer):
"""Wrapper around `pytest_localserver.http.ContentServer`.
It actually stores intercepted requests and data. It's not the best
implementation, but it gets job done - for now.
"""

def __init__(self, capture_requests=False, *args, **kwargs):
"""Init."""
self.captured_requests = []
self.capture_requests = capture_requests
super(MyContentServer, self).__init__(*args, **kwargs)

def __call__(self, environ, start_response):
"""Intercept HTTP request and store it, if desired."""
if self.capture_requests:
request = Request(environ)
self.captured_requests.append((request.method, request.get_data()))

return super(MyContentServer, self).__call__(environ, start_response)


@pytest.fixture
Expand All @@ -47,7 +24,7 @@ def fixture_cache_file():
@pytest.fixture
def fixture_http_server():
"""Return instance of HTTP server for testing."""
server = MyContentServer()
server = ContentServer()
server.start()
yield server

Expand Down
14 changes: 7 additions & 7 deletions tests/test_gh2slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def test_main_ideal(
200,
{"Content-Type": "application/json"},
)
fixture_http_server.capture_requests = True
fixture_http_server.store_request_data = True
expected_slack_requests = [
{
"blocks": [
Expand Down Expand Up @@ -330,14 +330,14 @@ def test_main_ideal(
# Note: this is just a shallow check, but it's better than nothing.
assert len(fixture_http_server.requests) == 2
# NOTE(zstyblik): this isn't really optimal.
req0 = fixture_http_server.captured_requests[0]
assert req0[0] == "POST"
data = json.loads(req0[1])
req0 = fixture_http_server.requests[0]
assert req0.method == "POST"
data = req0.get_json()
assert data in expected_slack_requests

req1 = fixture_http_server.captured_requests[1]
assert req1[0] == "POST"
data = json.loads(req1[1])
req1 = fixture_http_server.requests[1]
assert req1.method == "POST"
data = req1.get_json()
assert data in expected_slack_requests


Expand Down
9 changes: 4 additions & 5 deletions tests/test_git_commits2slack.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3
"""Unit tests for git_commits2slack.py."""
import io
import json
import os
import subprocess
import sys
Expand Down Expand Up @@ -311,7 +310,7 @@ def test_main_ideal(
200,
{"Content-Type": "application/json"},
)
fixture_http_server.capture_requests = True
fixture_http_server.store_request_data = True
expected_slack_requests = [
{
"blocks": [
Expand Down Expand Up @@ -384,7 +383,7 @@ def test_main_ideal(
# Note: this is just a shallow check, but it's better than nothing.
assert len(fixture_http_server.requests) == 1

req0 = fixture_http_server.captured_requests[0]
assert req0[0] == "POST"
data = json.loads(req0[1])
req0 = fixture_http_server.requests[0]
assert req0.method == "POST"
data = req0.get_json()
assert data == expected_slack_requests[0]
11 changes: 5 additions & 6 deletions tests/test_phpbb2slack.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3
"""Unit tests for phpbb2slack.py."""
import io
import json
import logging
import os
import sys
Expand Down Expand Up @@ -156,7 +155,7 @@ def test_main_ideal(
200,
{"Content-Type": "application/json"},
)
fixture_http_server.capture_requests = True
fixture_http_server.store_request_data = True

cache = CachedData()
source1 = cache.get_source_by_url(rss_url)
Expand Down Expand Up @@ -232,9 +231,9 @@ def test_main_ideal(
# Note: this is just a shallow check, but it's better than nothing.
assert len(fixture_http_server.requests) == 1

req0 = fixture_http_server.captured_requests[0]
assert req0[0] == "POST"
data = json.loads(req0[1])
req0 = fixture_http_server.requests[0]
assert req0.method == "POST"
data = req0.get_json()
assert data == expected_slack_requests[0]


Expand Down Expand Up @@ -263,7 +262,7 @@ def test_main_cache_hit(
500,
{"Content-Type": "application/json"},
)
fixture_http_server.capture_requests = True
fixture_http_server.store_request_data = True

cache = CachedData()
source1 = cache.get_source_by_url(rss_url)
Expand Down
16 changes: 7 additions & 9 deletions tests/test_rss2slack.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3
"""Unit tests for rss2slack.py."""
import io
import json
import logging
import os
import sys
Expand Down Expand Up @@ -150,7 +149,7 @@ def test_main_ideal(
200,
{"Content-Type": "application/json"},
)
fixture_http_server.capture_requests = True
fixture_http_server.store_request_data = True

cache = CachedData()
source1 = cache.get_source_by_url(rss_url)
Expand Down Expand Up @@ -224,14 +223,14 @@ def test_main_ideal(
# Note: this is just a shallow check, but it's better than nothing.
assert len(fixture_http_server.requests) == 2

req0 = fixture_http_server.captured_requests[0]
assert req0[0] == "POST"
data = json.loads(req0[1])
req0 = fixture_http_server.requests[0]
assert req0.method == "POST"
data = req0.get_json()
assert data == expected_slack_requests[0]

req1 = fixture_http_server.captured_requests[1]
assert req1[0] == "POST"
data = json.loads(req1[1])
req1 = fixture_http_server.requests[1]
assert req1.method == "POST"
data = req1.get_json()
assert data == expected_slack_requests[1]


Expand Down Expand Up @@ -262,7 +261,6 @@ def test_main_cache_hit(
500,
{"Content-Type": "application/json"},
)
fixture_http_server.capture_requests = True

cache = CachedData()
source1 = cache.get_source_by_url(rss_url)
Expand Down

0 comments on commit 7851d77

Please sign in to comment.