Skip to content

Commit 569f8d0

Browse files
committed
Adds server calculates.
0 parents  commit 569f8d0

File tree

4 files changed

+386
-0
lines changed

4 files changed

+386
-0
lines changed

Diff for: .gitignore

+300
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Vue template
3+
# gitignore template for Vue.js projects
4+
#
5+
# Recommended template: Node.gitignore
6+
7+
# TODO: where does this rule come from?
8+
docs/_book
9+
10+
# TODO: where does this rule come from?
11+
test/
12+
13+
### JetBrains template
14+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
15+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
16+
17+
# User-specific stuff
18+
.idea/**/workspace.xml
19+
.idea/**/tasks.xml
20+
.idea/**/usage.statistics.xml
21+
.idea/**/dictionaries
22+
.idea/**/shelf
23+
24+
# Generated files
25+
.idea/**/contentModel.xml
26+
27+
# Sensitive or high-churn files
28+
.idea/**/dataSources/
29+
.idea/**/dataSources.ids
30+
.idea/**/dataSources.local.xml
31+
.idea/**/sqlDataSources.xml
32+
.idea/**/dynamic.xml
33+
.idea/**/uiDesigner.xml
34+
.idea/**/dbnavigator.xml
35+
36+
# Gradle
37+
.idea/**/gradle.xml
38+
.idea/**/libraries
39+
40+
# Gradle and Maven with auto-import
41+
# When using Gradle or Maven with auto-import, you should exclude module files,
42+
# since they will be recreated, and may cause churn. Uncomment if using
43+
# auto-import.
44+
# .idea/artifacts
45+
# .idea/compiler.xml
46+
# .idea/jarRepositories.xml
47+
# .idea/modules.xml
48+
# .idea/*.iml
49+
# .idea/modules
50+
# *.iml
51+
# *.ipr
52+
53+
# CMake
54+
cmake-build-*/
55+
56+
# Mongo Explorer plugin
57+
.idea/**/mongoSettings.xml
58+
59+
# File-based project format
60+
*.iws
61+
62+
# IntelliJ
63+
out/
64+
65+
# mpeltonen/sbt-idea plugin
66+
.idea_modules/
67+
68+
# JIRA plugin
69+
atlassian-ide-plugin.xml
70+
71+
# Cursive Clojure plugin
72+
.idea/replstate.xml
73+
74+
# Crashlytics plugin (for Android Studio and IntelliJ)
75+
com_crashlytics_export_strings.xml
76+
crashlytics.properties
77+
crashlytics-build.properties
78+
fabric.properties
79+
80+
# Editor-based Rest Client
81+
.idea/httpRequests
82+
83+
# Android studio 3.1+ serialized cache file
84+
.idea/caches/build_file_checksums.ser
85+
86+
### JetBrains template
87+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
88+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
89+
90+
# User-specific stuff
91+
.idea/**/workspace.xml
92+
.idea/**/tasks.xml
93+
.idea/**/usage.statistics.xml
94+
.idea/**/dictionaries
95+
.idea/**/shelf
96+
97+
# Generated files
98+
.idea/**/contentModel.xml
99+
100+
# Sensitive or high-churn files
101+
.idea/**/dataSources/
102+
.idea/**/dataSources.ids
103+
.idea/**/dataSources.local.xml
104+
.idea/**/sqlDataSources.xml
105+
.idea/**/dynamic.xml
106+
.idea/**/uiDesigner.xml
107+
.idea/**/dbnavigator.xml
108+
109+
# Gradle
110+
.idea/**/gradle.xml
111+
.idea/**/libraries
112+
113+
# Gradle and Maven with auto-import
114+
# When using Gradle or Maven with auto-import, you should exclude module files,
115+
# since they will be recreated, and may cause churn. Uncomment if using
116+
# auto-import.
117+
# .idea/artifacts
118+
# .idea/compiler.xml
119+
# .idea/jarRepositories.xml
120+
# .idea/modules.xml
121+
# .idea/*.iml
122+
# .idea/modules
123+
# *.iml
124+
# *.ipr
125+
126+
# CMake
127+
cmake-build-*/
128+
129+
# Mongo Explorer plugin
130+
.idea/**/mongoSettings.xml
131+
132+
# File-based project format
133+
*.iws
134+
135+
# IntelliJ
136+
out/
137+
138+
# mpeltonen/sbt-idea plugin
139+
.idea_modules/
140+
141+
# JIRA plugin
142+
atlassian-ide-plugin.xml
143+
144+
# Cursive Clojure plugin
145+
.idea/replstate.xml
146+
147+
# Crashlytics plugin (for Android Studio and IntelliJ)
148+
com_crashlytics_export_strings.xml
149+
crashlytics.properties
150+
crashlytics-build.properties
151+
fabric.properties
152+
153+
# Editor-based Rest Client
154+
.idea/httpRequests
155+
156+
# Android studio 3.1+ serialized cache file
157+
.idea/caches/build_file_checksums.ser
158+
159+
### Python template
160+
# Byte-compiled / optimized / DLL files
161+
__pycache__/
162+
*.py[cod]
163+
*$py.class
164+
165+
# C extensions
166+
*.so
167+
168+
# Distribution / packaging
169+
.Python
170+
build/
171+
develop-eggs/
172+
dist/
173+
downloads/
174+
eggs/
175+
.eggs/
176+
lib/
177+
lib64/
178+
parts/
179+
sdist/
180+
var/
181+
wheels/
182+
share/python-wheels/
183+
*.egg-info/
184+
.installed.cfg
185+
*.egg
186+
MANIFEST
187+
188+
# PyInstaller
189+
# Usually these files are written by a python script from a template
190+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
191+
*.manifest
192+
*.spec
193+
194+
# Installer logs
195+
pip-log.txt
196+
pip-delete-this-directory.txt
197+
198+
# Unit test / coverage reports
199+
htmlcov/
200+
.tox/
201+
.nox/
202+
.coverage
203+
.coverage.*
204+
.cache
205+
nosetests.xml
206+
coverage.xml
207+
*.cover
208+
*.py,cover
209+
.hypothesis/
210+
.pytest_cache/
211+
cover/
212+
213+
# Translations
214+
*.mo
215+
*.pot
216+
217+
# Django stuff:
218+
*.log
219+
local_settings.py
220+
db.sqlite3
221+
db.sqlite3-journal
222+
223+
# Flask stuff:
224+
instance/
225+
.webassets-cache
226+
227+
# Scrapy stuff:
228+
.scrapy
229+
230+
# Sphinx documentation
231+
docs/_build/
232+
233+
# PyBuilder
234+
.pybuilder/
235+
target/
236+
237+
# Jupyter Notebook
238+
.ipynb_checkpoints
239+
240+
# IPython
241+
profile_default/
242+
ipython_config.py
243+
244+
# pyenv
245+
# For a library or package, you might want to ignore these files since the code is
246+
# intended to run in multiple environments; otherwise, check them in:
247+
# .python-version
248+
249+
# pipenv
250+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
251+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
252+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
253+
# install all needed dependencies.
254+
#Pipfile.lock
255+
256+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
257+
__pypackages__/
258+
259+
# Celery stuff
260+
celerybeat-schedule
261+
celerybeat.pid
262+
263+
# SageMath parsed files
264+
*.sage.py
265+
266+
# Environments
267+
.env
268+
.venv
269+
env/
270+
venv/
271+
ENV/
272+
env.bak/
273+
venv.bak/
274+
275+
# Spyder project settings
276+
.spyderproject
277+
.spyproject
278+
279+
# Rope project settings
280+
.ropeproject
281+
282+
# mkdocs documentation
283+
/site
284+
285+
# mypy
286+
.mypy_cache/
287+
.dmypy.json
288+
dmypy.json
289+
290+
# Pyre type checker
291+
.pyre/
292+
293+
# pytype static type analyzer
294+
.pytype/
295+
296+
# Cython debug symbols
297+
cython_debug
298+
299+
.idea
300+

Diff for: main.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import operator
2+
3+
from fastapi import FastAPI
4+
from pydantic import BaseModel, Field
5+
6+
7+
MAP_OPERATOR = {
8+
'+': operator.add,
9+
'-': operator.sub,
10+
'*': operator.mul,
11+
'/': operator.truediv,
12+
'^': operator.pow,
13+
}
14+
15+
16+
app = FastAPI()
17+
18+
19+
class CalcRequestSchema(BaseModel):
20+
number_1: int = Field(gt=0, description='First number of an algebraic expression')
21+
number_2: int = Field(ge=0, description='Second number of an algebraic expression')
22+
operator: str = Field(max_length=1, )
23+
24+
25+
class CalcResponseSchema(BaseModel):
26+
result: int = Field(gt=0)
27+
28+
29+
@app.post('/calc', response_model=CalcResponseSchema)
30+
def calculate(request: CalcRequestSchema) -> CalcResponseSchema:
31+
result = MAP_OPERATOR[request.operator](request.number_1, request.number_2)
32+
return CalcResponseSchema(result=result)

Diff for: requirements.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fastapi~=0.63.0
2+
uvicorn~=0.13.4
3+
requests~=2.25.1
4+
pytest~=6.2.2
5+
pydantic~=1.8.1

Diff for: test_calc.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pytest
2+
from fastapi.testclient import TestClient
3+
from pydantic import ValidationError
4+
5+
from main import app, CalcRequestSchema, CalcResponseSchema
6+
7+
8+
client = TestClient(app)
9+
10+
11+
@pytest.mark.parametrize(
12+
'number_1, number_2, operator, result',
13+
[
14+
(1, 1, '+', 2),
15+
(2, 3, '*', 6),
16+
(123, 101, '-', 22),
17+
(5, 2, '^', 25),
18+
]
19+
)
20+
def test_calc(number_1, number_2, operator, result):
21+
response = client.post(
22+
'/calc',
23+
CalcRequestSchema(
24+
number_1=number_1,
25+
number_2=number_2,
26+
operator=operator).json(),
27+
)
28+
assert response.status_code == 200
29+
assert response.json() == CalcResponseSchema(result=result).dict()
30+
31+
32+
@pytest.mark.parametrize(
33+
'number_1, number_2, operator, result',
34+
[
35+
(-1, 1, '+', 2),
36+
(2, -3, '*', 6),
37+
(10, 101, '-', 22),
38+
(0, 2, '^', 25),
39+
]
40+
)
41+
def test_calc_value_error(number_1, number_2, operator, result):
42+
with pytest.raises(ValidationError):
43+
client.post(
44+
'/calc',
45+
CalcRequestSchema(
46+
number_1=number_1,
47+
number_2=number_2,
48+
operator=operator).json(),
49+
)

0 commit comments

Comments
 (0)