Skip to content

Commit dd54db6

Browse files
committed
format code, improve tests
1 parent d32be40 commit dd54db6

File tree

8 files changed

+118
-56
lines changed

8 files changed

+118
-56
lines changed

.github/workflows/tests.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: tests
2+
on: ["push", "pull_request"]
3+
jobs:
4+
tests:
5+
runs-on: ${{ matrix.os }}
6+
strategy:
7+
matrix:
8+
os: ['ubuntu-latest']
9+
python-version:
10+
- '3.8'
11+
- '3.9'
12+
- '3.10'
13+
- '3.11'
14+
- '3.12'
15+
include:
16+
- os: macos-latest
17+
python-version: '3.x'
18+
- os: windows-latest
19+
python-version: '3.x'
20+
steps:
21+
- uses: actions/checkout@v4
22+
23+
- name: Set up Python ${{ matrix.python-version }} on ${{ matrix.architecture }}
24+
uses: actions/setup-python@v4
25+
with:
26+
python-version: ${{ matrix.python-version }}
27+
28+
- name: Install dependencies
29+
run: |
30+
pip install --quiet --upgrade pip
31+
pip install pip install -r requirements.txt -r requirements-dev.txt
32+
33+
- name: Run tests
34+
run: |
35+
pytest

api.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright 2021 Michael Samoglyadov
3+
# Copyright 2021-2024 Michael Samoglyadov
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -119,10 +119,12 @@ def format():
119119
@app.route('/api/v1/patch', methods=['POST'])
120120
def patch():
121121
try:
122-
return jsonify(Patcher().patch(
123-
request.json['target'],
124-
request.json['patch'],
125-
))
122+
return jsonify(
123+
Patcher().patch(
124+
request.json['target'],
125+
request.json['patch'],
126+
)
127+
)
126128
except Exception:
127129
return Response('Incorrect arguments', status=400)
128130

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[tool.pytest.ini_options]
2+
addopts = '-vv --cov=api --cov-fail-under=96 --no-cov-on-fail --ruff --ruff-format'
3+
4+
[tool.ruff]
5+
line-length = 79
6+
7+
[tool.ruff.format]
8+
quote-style = 'single'

pytest.ini

Lines changed: 0 additions & 6 deletions
This file was deleted.

requirements-dev.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
flake8 < 5
2-
flake8-commas
3-
flake8-bugbear
4-
flake8_quotes
51
pytest
6-
pytest-flake8
2+
pytest-cov
3+
pytest-ruff

tests/test_api_diff.py

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ def test_invalid_request(client):
66

77

88
def test_invalid_diff_opts(client):
9-
rv = client.post('/api/v1/diff', json={
10-
'a': 'a',
11-
'b': 'b',
12-
'diff_opts': {'__unsupported__': True},
13-
})
9+
rv = client.post(
10+
'/api/v1/diff',
11+
json={
12+
'a': 'a',
13+
'b': 'b',
14+
'diff_opts': {'__unsupported__': True},
15+
},
16+
)
1417

1518
assert rv.status_code == 400
1619
assert rv.data == b'Incorrect diff options'
@@ -25,63 +28,81 @@ def test_default(client):
2528

2629

2730
def test_diff_opts(client):
28-
rv = client.post('/api/v1/diff', json={
29-
'a': 'a',
30-
'b': 'b',
31-
'diff_opts': {'O': False},
32-
})
31+
rv = client.post(
32+
'/api/v1/diff',
33+
json={
34+
'a': 'a',
35+
'b': 'b',
36+
'diff_opts': {'O': False},
37+
},
38+
)
3339

3440
assert rv.status_code == 200
3541
assert rv.content_type == 'application/json'
3642
assert rv.get_json() == {'N': 'b'}
3743

3844

3945
def test_ofmt_text(client):
40-
rv = client.post('/api/v1/diff', json={
41-
'a': 'a',
42-
'b': 'b',
43-
'diff_opts': {'O': False},
44-
'ofmt': 'text',
45-
})
46+
rv = client.post(
47+
'/api/v1/diff',
48+
json={
49+
'a': 'a',
50+
'b': 'b',
51+
'diff_opts': {'O': False},
52+
'ofmt': 'text',
53+
},
54+
)
4655

4756
assert rv.status_code == 200
4857
assert rv.content_type.startswith('text/plain')
4958
assert rv.data == b"+ 'b'\n"
5059

5160

5261
def test_ofmt_term(client):
53-
rv = client.post('/api/v1/diff', json={
54-
'a': 'a',
55-
'b': 'b',
56-
'diff_opts': {'O': False},
57-
'ofmt': 'term',
58-
})
62+
rv = client.post(
63+
'/api/v1/diff',
64+
json={
65+
'a': 'a',
66+
'b': 'b',
67+
'diff_opts': {'O': False},
68+
'ofmt': 'term',
69+
},
70+
)
5971

6072
assert rv.status_code == 200
6173
assert rv.content_type.startswith('text/plain')
6274
assert rv.data == b"\x1b[32m+ 'b'\x1b[0m\n"
6375

6476

6577
def test_ofmt_html(client):
66-
rv = client.post('/api/v1/diff', json={
67-
'a': 'a',
68-
'b': 'b',
69-
'diff_opts': {'O': False},
70-
'ofmt': 'html',
71-
})
78+
rv = client.post(
79+
'/api/v1/diff',
80+
json={
81+
'a': 'a',
82+
'b': 'b',
83+
'diff_opts': {'O': False},
84+
'ofmt': 'html',
85+
},
86+
)
7287

7388
assert rv.status_code == 200
7489
assert rv.content_type.startswith('text/plain')
75-
assert rv.data == b'<div class="nDvD"><div>+ <div class="nDvN">&#x27;b&#x27;</div></div></div>'
90+
assert (
91+
rv.data
92+
== b'<div class="nDvD"><div>+ <div class="nDvN">&#x27;b&#x27;</div></div></div>'
93+
)
7694

7795

7896
def test_ofmt_unsupported(client):
79-
rv = client.post('/api/v1/diff', json={
80-
'a': 'a',
81-
'b': 'b',
82-
'diff_opts': {'O': False},
83-
'ofmt': '__unsupported__',
84-
})
97+
rv = client.post(
98+
'/api/v1/diff',
99+
json={
100+
'a': 'a',
101+
'b': 'b',
102+
'diff_opts': {'O': False},
103+
'ofmt': '__unsupported__',
104+
},
105+
)
85106

86107
assert rv.status_code == 400
87108
assert rv.content_type.startswith('text/html')

tests/test_api_format.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ def test_invalid_request(client):
55

66

77
def test_format(client):
8-
rv = client.post('/api/v1/format', json={
9-
'diff': {'N': 'b', 'O': 'a'},
10-
'ofmt': 'text',
11-
})
8+
rv = client.post(
9+
'/api/v1/format',
10+
json={
11+
'diff': {'N': 'b', 'O': 'a'},
12+
'ofmt': 'text',
13+
},
14+
)
1215

1316
assert rv.status_code == 200
1417
assert rv.content_type.startswith('text/plain')

tests/test_api_patch.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ def test_invalid_request(client):
55

66

77
def test_patch(client):
8-
rv = client.post('/api/v1/patch', json={'target': 'a', 'patch': {'N': 'b'}})
8+
rv = client.post(
9+
'/api/v1/patch', json={'target': 'a', 'patch': {'N': 'b'}}
10+
)
911

1012
assert rv.status_code == 200
1113
assert rv.content_type == 'application/json'

0 commit comments

Comments
 (0)