Skip to content

Commit

Permalink
Fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
zhulik committed Jun 6, 2021
1 parent 75337ff commit 236a76a
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 75 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ isort:
pipenv run isort --check .

flake8:
pipenv run flake8 aiophotoprism
pipenv run flake8 aiosyncthing

pylint:
pipenv run pylint aiophotoprism
pipenv run pylint aiosyncthing

lint: pylint flake8 isort black

Expand Down
12 changes: 6 additions & 6 deletions aiosyncthing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class API:

DEFAULT_TIMEOUT = 10

def __init__(
def __init__( # pylint: disable=too-many-arguments
self,
api_key,
url="http://127.0.0.1:8384",
Expand Down Expand Up @@ -60,15 +60,15 @@ async def raw_request(self, uri, params=None, data=None, method="GET"):
method,
self._url.join(URL(uri)).update_query(params),
json=data,
headers={"Accept": "application/json", "X-API-Key": self._api_key,},
headers={
"Accept": "application/json",
"X-API-Key": self._api_key,
},
timeout=self._timeout,
verify_ssl=self._verify_ssl,
) as response:
response.raise_for_status()
if (
"Content-Type" in response.headers
and "application/json" in response.headers["Content-Type"]
):
if "Content-Type" in response.headers and "application/json" in response.headers["Content-Type"]:
return await response.json()
return await response.read()

Expand Down
6 changes: 2 additions & 4 deletions aiosyncthing/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ def __init__(self, api):
async def status(self, folder_id):
"""Get folder status."""
try:
return await self._api.request(
"rest/db/status", params={"folder": folder_id}
)
return await self._api.request("rest/db/status", params={"folder": folder_id})
except SyncthingError as error:
cause = error.__cause__
if isinstance(cause, aiohttp.client_exceptions.ClientResponseError):
if cause.status == 404: # pylint: disable=no-member
raise UnknownFolderError
raise UnknownFolderError from error
raise error
4 changes: 1 addition & 3 deletions aiosyncthing/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ async def listen(self):

while self._running:
try:
events = await self._api.raw_request(
"rest/events", params={"since": self._last_seen_id}
)
events = await self._api.raw_request("rest/events", params={"since": self._last_seen_id})
for event in events:
yield event
self._last_seen_id = events[-1]["id"]
Expand Down
14 changes: 3 additions & 11 deletions aiosyncthing/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ async def ping(self):
"""Check server availability."""
try:
result = await self._api.raw_request("rest/system/ping")
if (
not isinstance(result, dict)
or "ping" not in result
or result["ping"] != "pong"
):
if not isinstance(result, dict) or "ping" not in result or result["ping"] != "pong":
raise PingError
except Exception as error:
raise PingError from error
Expand All @@ -38,18 +34,14 @@ async def version(self):
async def pause(self, device_id=None):
"""Pause synchronization."""
try:
await self._api.request(
"rest/system/pause", method="POST", params=device_params(device_id)
)
await self._api.request("rest/system/pause", method="POST", params=device_params(device_id))
except NotFoundError as error:
raise UnknownDeviceError from error

async def resume(self, device_id=None):
"""Resume synchronization."""
try:
await self._api.request(
"rest/system/resume", method="POST", params=device_params(device_id)
)
await self._api.request("rest/system/resume", method="POST", params=device_params(device_id))
except NotFoundError as error:
raise UnknownDeviceError from error

Expand Down
19 changes: 19 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[tool.black]
line-length = 120
target-version = ['py37']
extend-exclude = '''
^/\.git
^/\.vscode
^/\.github
'''

[tool.isort]
line_length=120
profile="black"

[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-s -ra -q --cov=aiosyncthing --cov-report=html"
testpaths = [
"tests"
]
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest
from aioresponses import aioresponses as responses

from aiosyncthing import Syncthing


Expand Down
11 changes: 4 additions & 7 deletions tests/test_database.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Tests for System."""

import pytest
from aiosyncthing.exceptions import UnknownFolderError
from expects import equal, expect

from aiosyncthing.exceptions import UnknownFolderError

# pylint: disable=redefined-outer-name


Expand All @@ -20,16 +21,12 @@ async def test_status_happy(database, aioresponses):
"http://127.0.0.1:8384/rest/db/status?folder=folder-id",
payload={"errors": 0, "globalBytes": 0},
)
expect(await database.status("folder-id")).to(
equal({"errors": 0, "globalBytes": 0})
)
expect(await database.status("folder-id")).to(equal({"errors": 0, "globalBytes": 0}))


@pytest.mark.asyncio
async def test_status_unknown_folder(database, aioresponses):
"""Test happy path."""
aioresponses.get(
"http://127.0.0.1:8384/rest/db/status?folder=folder-id", status=404
)
aioresponses.get("http://127.0.0.1:8384/rest/db/status?folder=folder-id", status=404)
with pytest.raises(UnknownFolderError):
await database.status("folder-id")
9 changes: 6 additions & 3 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import asyncio

import pytest
from aiosyncthing.exceptions import SyncthingError
from expects import be_false, be_true, equal, expect

from aiosyncthing.exceptions import SyncthingError

# pylint: disable=redefined-outer-name


Expand All @@ -19,10 +20,12 @@ def events(syncthing_client):
async def test_listen_happy(events, aioresponses):
"""Test happy path."""
aioresponses.get(
"http://127.0.0.1:8384/rest/events?since=0", payload=[{"id": 123}],
"http://127.0.0.1:8384/rest/events?since=0",
payload=[{"id": 123}],
)
aioresponses.get(
"http://127.0.0.1:8384/rest/events?since=123", payload=[{"id": 124}],
"http://127.0.0.1:8384/rest/events?since=123",
payload=[{"id": 124}],
)
async for event in events.listen():
if event["id"] == 123:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_syncthing.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Tests for System."""

import aiosyncthing
import pytest
from expects import be_a, equal, expect

import aiosyncthing


def test_url(syncthing_client):
"""Test."""
Expand Down
53 changes: 15 additions & 38 deletions tests/test_system.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
"""Tests for System."""

import pytest
from aiosyncthing import Syncthing
from aiosyncthing.exceptions import (
PingError,
SyncthingError,
UnauthorizedError,
UnknownDeviceError,
)
from expects import be_none, equal, expect

from aiosyncthing import Syncthing
from aiosyncthing.exceptions import PingError, SyncthingError, UnauthorizedError, UnknownDeviceError

# pylint: disable=redefined-outer-name


Expand All @@ -29,9 +25,7 @@ async def test_ping_happy(system, aioresponses):
@pytest.mark.asyncio
async def test_ping_with_syncthing_namespaced(aioresponses):
"""Test happy path."""
aioresponses.get(
"http://127.0.0.1:8384/syncthing/rest/system/ping", payload={"ping": "pong"}
)
aioresponses.get("http://127.0.0.1:8384/syncthing/rest/system/ping", payload={"ping": "pong"})

async with Syncthing("token", "http://127.0.0.1:8384/syncthing/") as client:
expect(await client.system.ping()).to(be_none)
Expand All @@ -49,9 +43,7 @@ async def test_ping_error(system, aioresponses):
@pytest.mark.asyncio
async def test_ping_unknown_response(system, aioresponses):
"""Test error path."""
aioresponses.get(
"http://127.0.0.1:8384/rest/system/ping", status=200, payload="test"
)
aioresponses.get("http://127.0.0.1:8384/rest/system/ping", status=200, payload="test")

with pytest.raises(PingError):
await system.ping()
Expand Down Expand Up @@ -82,9 +74,7 @@ async def test_status_happy(system, aioresponses):
"http://127.0.0.1:8384/rest/system/status",
payload={"alloc": 147081968, "connectionServiceStatus": {}},
)
expect(await system.status()).to(
equal({"alloc": 147081968, "connectionServiceStatus": {}})
)
expect(await system.status()).to(equal({"alloc": 147081968, "connectionServiceStatus": {}}))


@pytest.mark.asyncio
Expand All @@ -111,7 +101,8 @@ def mock_load(*args):
raise Exception

mocker.patch(
"aiosyncthing.API.raw_request", mock_load,
"aiosyncthing.API.raw_request",
mock_load,
)

with pytest.raises(SyncthingError):
Expand All @@ -121,18 +112,14 @@ def mock_load(*args):
@pytest.mark.asyncio
async def test_version_happy(system, aioresponses):
"""Test happy path."""
aioresponses.get(
"http://127.0.0.1:8384/rest/system/version", payload={"version": "v1.7.0"}
)
aioresponses.get("http://127.0.0.1:8384/rest/system/version", payload={"version": "v1.7.0"})
expect(await system.version()).to(equal({"version": "v1.7.0"}))


@pytest.mark.asyncio
async def test_pause_no_arguments_happy(system, aioresponses):
"""Test happy path."""
aioresponses.post(
"http://127.0.0.1:8384/rest/system/pause", headers={"Content-Type": ""}
)
aioresponses.post("http://127.0.0.1:8384/rest/system/pause", headers={"Content-Type": ""})
expect(await system.pause()).to(be_none)


Expand All @@ -157,29 +144,23 @@ async def test_pause_happy(system, aioresponses):
@pytest.mark.asyncio
async def test_pause_error(system, aioresponses):
"""Test error path."""
aioresponses.post(
"http://127.0.0.1:8384/rest/system/pause?device=device_id", status=500
)
aioresponses.post("http://127.0.0.1:8384/rest/system/pause?device=device_id", status=500)
with pytest.raises(SyncthingError):
await system.pause("device_id")


@pytest.mark.asyncio
async def test_pause_unknown_device(system, aioresponses):
"""Test error path."""
aioresponses.post(
"http://127.0.0.1:8384/rest/system/pause?device=device_id", status=404
)
aioresponses.post("http://127.0.0.1:8384/rest/system/pause?device=device_id", status=404)
with pytest.raises(UnknownDeviceError):
await system.pause("device_id")


@pytest.mark.asyncio
async def test_resume_no_arguments_happy(system, aioresponses):
"""Test happy path."""
aioresponses.post(
"http://127.0.0.1:8384/rest/system/resume", headers={"Content-Type": ""}
)
aioresponses.post("http://127.0.0.1:8384/rest/system/resume", headers={"Content-Type": ""})
expect(await system.resume()).to(be_none)


Expand All @@ -204,18 +185,14 @@ async def test_resume_happy(system, aioresponses):
@pytest.mark.asyncio
async def test_resume_error(system, aioresponses):
"""Test error path."""
aioresponses.post(
"http://127.0.0.1:8384/rest/system/resume?device=device_id", status=500
)
aioresponses.post("http://127.0.0.1:8384/rest/system/resume?device=device_id", status=500)
with pytest.raises(SyncthingError):
await system.resume("device_id")


@pytest.mark.asyncio
async def test_resume_unknown_device(system, aioresponses):
"""Test error path."""
aioresponses.post(
"http://127.0.0.1:8384/rest/system/resume?device=device_id", status=404
)
aioresponses.post("http://127.0.0.1:8384/rest/system/resume?device=device_id", status=404)
with pytest.raises(UnknownDeviceError):
await system.resume("device_id")

0 comments on commit 236a76a

Please sign in to comment.