ソースを参照

[conf][tests] Wrote tests and maxe the configuration module pass these tests

Maxime Alves LIRMM@home 4年前
コミット
da9552a5d2
6個のファイルの変更397行の追加10行の削除
  1. 313
    1
      poetry.lock
  2. 28
    9
      pyheatpump/conf.py
  3. 4
    0
      pyproject.toml
  4. 3
    0
      pytest.ini
  5. 0
    0
      tests/__init__.py
  6. 49
    0
      tests/test_config.py

+ 313
- 1
poetry.lock ファイルの表示

@@ -1,3 +1,50 @@
1
+[[package]]
2
+category = "dev"
3
+description = "Enhance the standard unittest package with features for testing asyncio libraries"
4
+name = "asynctest"
5
+optional = false
6
+python-versions = ">=3.5"
7
+version = "0.13.0"
8
+
9
+[[package]]
10
+category = "dev"
11
+description = "Atomic file writes."
12
+marker = "sys_platform == \"win32\""
13
+name = "atomicwrites"
14
+optional = false
15
+python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
16
+version = "1.4.0"
17
+
18
+[[package]]
19
+category = "dev"
20
+description = "Classes Without Boilerplate"
21
+name = "attrs"
22
+optional = false
23
+python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
24
+version = "19.3.0"
25
+
26
+[package.extras]
27
+azure-pipelines = ["coverage", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "zope.interface", "pytest-azurepipelines"]
28
+dev = ["coverage", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "zope.interface", "sphinx", "pre-commit"]
29
+docs = ["sphinx", "zope.interface"]
30
+tests = ["coverage", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "zope.interface"]
31
+
32
+[[package]]
33
+category = "dev"
34
+description = "Python package for providing Mozilla's CA Bundle."
35
+name = "certifi"
36
+optional = false
37
+python-versions = "*"
38
+version = "2020.6.20"
39
+
40
+[[package]]
41
+category = "dev"
42
+description = "Universal encoding detector for Python 2 and 3"
43
+name = "chardet"
44
+optional = false
45
+python-versions = "*"
46
+version = "3.0.4"
47
+
1 48
 [[package]]
2 49
 category = "main"
3 50
 description = "Composable command line interface toolkit"
@@ -6,6 +53,15 @@ optional = false
6 53
 python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
7 54
 version = "7.1.2"
8 55
 
56
+[[package]]
57
+category = "dev"
58
+description = "Cross-platform colored terminal text."
59
+marker = "sys_platform == \"win32\""
60
+name = "colorama"
61
+optional = false
62
+python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
63
+version = "0.4.3"
64
+
9 65
 [[package]]
10 66
 category = "main"
11 67
 description = "A pure-Python, bring-your-own-I/O implementation of HTTP/1.1"
@@ -26,6 +82,82 @@ version = "0.1.1"
26 82
 [package.extras]
27 83
 test = ["Cython (0.29.14)"]
28 84
 
85
+[[package]]
86
+category = "dev"
87
+description = "Internationalized Domain Names in Applications (IDNA)"
88
+name = "idna"
89
+optional = false
90
+python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
91
+version = "2.10"
92
+
93
+[[package]]
94
+category = "dev"
95
+description = "Read metadata from Python packages"
96
+marker = "python_version < \"3.8\""
97
+name = "importlib-metadata"
98
+optional = false
99
+python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7"
100
+version = "1.7.0"
101
+
102
+[package.dependencies]
103
+zipp = ">=0.5"
104
+
105
+[package.extras]
106
+docs = ["sphinx", "rst.linker"]
107
+testing = ["packaging", "pep517", "importlib-resources (>=1.3)"]
108
+
109
+[[package]]
110
+category = "dev"
111
+description = "More routines for operating on iterables, beyond itertools"
112
+name = "more-itertools"
113
+optional = false
114
+python-versions = ">=3.5"
115
+version = "8.4.0"
116
+
117
+[[package]]
118
+category = "dev"
119
+description = "Core utilities for Python packages"
120
+name = "packaging"
121
+optional = false
122
+python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
123
+version = "20.4"
124
+
125
+[package.dependencies]
126
+pyparsing = ">=2.0.2"
127
+six = "*"
128
+
129
+[[package]]
130
+category = "dev"
131
+description = "plugin and hook calling mechanisms for python"
132
+name = "pluggy"
133
+optional = false
134
+python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
135
+version = "0.13.1"
136
+
137
+[package.dependencies]
138
+[package.dependencies.importlib-metadata]
139
+python = "<3.8"
140
+version = ">=0.12"
141
+
142
+[package.extras]
143
+dev = ["pre-commit", "tox"]
144
+
145
+[[package]]
146
+category = "dev"
147
+description = "library with cross-python path, ini-parsing, io, code, log facilities"
148
+name = "py"
149
+optional = false
150
+python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
151
+version = "1.9.0"
152
+
153
+[[package]]
154
+category = "dev"
155
+description = "Python parsing module"
156
+name = "pyparsing"
157
+optional = false
158
+python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*"
159
+version = "2.4.7"
160
+
29 161
 [[package]]
30 162
 category = "main"
31 163
 description = "Python Serial Port Extension"
@@ -34,6 +166,72 @@ optional = false
34 166
 python-versions = "*"
35 167
 version = "3.4"
36 168
 
169
+[[package]]
170
+category = "dev"
171
+description = "pytest: simple powerful testing with Python"
172
+name = "pytest"
173
+optional = false
174
+python-versions = ">=3.5"
175
+version = "5.4.3"
176
+
177
+[package.dependencies]
178
+atomicwrites = ">=1.0"
179
+attrs = ">=17.4.0"
180
+colorama = "*"
181
+more-itertools = ">=4.0.0"
182
+packaging = "*"
183
+pluggy = ">=0.12,<1.0"
184
+py = ">=1.5.0"
185
+wcwidth = "*"
186
+
187
+[package.dependencies.importlib-metadata]
188
+python = "<3.8"
189
+version = ">=0.12"
190
+
191
+[package.extras]
192
+checkqa-mypy = ["mypy (v0.761)"]
193
+testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xmlschema"]
194
+
195
+[[package]]
196
+category = "dev"
197
+description = "Pytest support for asyncio."
198
+name = "pytest-asyncio"
199
+optional = false
200
+python-versions = ">= 3.5"
201
+version = "0.14.0"
202
+
203
+[package.dependencies]
204
+pytest = ">=5.4.0"
205
+
206
+[package.extras]
207
+testing = ["async-generator (>=1.3)", "coverage", "hypothesis (>=5.7.1)"]
208
+
209
+[[package]]
210
+category = "dev"
211
+description = "Python HTTP for Humans."
212
+name = "requests"
213
+optional = false
214
+python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
215
+version = "2.24.0"
216
+
217
+[package.dependencies]
218
+certifi = ">=2017.4.17"
219
+chardet = ">=3.0.2,<4"
220
+idna = ">=2.5,<3"
221
+urllib3 = ">=1.21.1,<1.25.0 || >1.25.0,<1.25.1 || >1.25.1,<1.26"
222
+
223
+[package.extras]
224
+security = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)"]
225
+socks = ["PySocks (>=1.5.6,<1.5.7 || >1.5.7)", "win-inet-pton"]
226
+
227
+[[package]]
228
+category = "dev"
229
+description = "Python 2 and 3 compatibility utilities"
230
+name = "six"
231
+optional = false
232
+python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*"
233
+version = "1.15.0"
234
+
37 235
 [[package]]
38 236
 category = "main"
39 237
 description = "The little ASGI library that shines."
@@ -56,6 +254,19 @@ version = "1.0.3"
56 254
 [package.dependencies]
57 255
 pyserial = ">=3.4,<4.0"
58 256
 
257
+[[package]]
258
+category = "dev"
259
+description = "HTTP library with thread-safe connection pooling, file post, and more."
260
+name = "urllib3"
261
+optional = false
262
+python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4"
263
+version = "1.25.10"
264
+
265
+[package.extras]
266
+brotli = ["brotlipy (>=0.6.0)"]
267
+secure = ["certifi", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "pyOpenSSL (>=0.14)", "ipaddress"]
268
+socks = ["PySocks (>=1.5.6,<1.5.7 || >1.5.7,<2.0)"]
269
+
59 270
 [[package]]
60 271
 category = "main"
61 272
 description = "The lightning-fast ASGI server."
@@ -83,6 +294,14 @@ optional = false
83 294
 python-versions = "*"
84 295
 version = "0.14.0"
85 296
 
297
+[[package]]
298
+category = "dev"
299
+description = "Measures the displayed width of unicode strings in a terminal"
300
+name = "wcwidth"
301
+optional = false
302
+python-versions = "*"
303
+version = "0.2.5"
304
+
86 305
 [[package]]
87 306
 category = "main"
88 307
 description = "An implementation of the WebSocket Protocol (RFC 6455 & 7692)"
@@ -91,16 +310,53 @@ optional = false
91 310
 python-versions = ">=3.6.1"
92 311
 version = "8.1"
93 312
 
313
+[[package]]
314
+category = "dev"
315
+description = "Backport of pathlib-compatible object wrapper for zip files"
316
+marker = "python_version < \"3.8\""
317
+name = "zipp"
318
+optional = false
319
+python-versions = ">=3.6"
320
+version = "3.1.0"
321
+
322
+[package.extras]
323
+docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"]
324
+testing = ["jaraco.itertools", "func-timeout"]
325
+
94 326
 [metadata]
95
-content-hash = "36622646955f0f090946e71846f990540339b72a6632f770b158ae7cab49b518"
327
+content-hash = "c2eebf612ea0d48c4591173dd75bd97efa526046adc29b805ec6edc81e6f9598"
96 328
 lock-version = "1.0"
97 329
 python-versions = "^3.7"
98 330
 
99 331
 [metadata.files]
332
+asynctest = [
333
+    {file = "asynctest-0.13.0-py3-none-any.whl", hash = "sha256:5da6118a7e6d6b54d83a8f7197769d046922a44d2a99c21382f0a6e4fadae676"},
334
+    {file = "asynctest-0.13.0.tar.gz", hash = "sha256:c27862842d15d83e6a34eb0b2866c323880eb3a75e4485b079ea11748fd77fac"},
335
+]
336
+atomicwrites = [
337
+    {file = "atomicwrites-1.4.0-py2.py3-none-any.whl", hash = "sha256:6d1784dea7c0c8d4a5172b6c620f40b6e4cbfdf96d783691f2e1302a7b88e197"},
338
+    {file = "atomicwrites-1.4.0.tar.gz", hash = "sha256:ae70396ad1a434f9c7046fd2dd196fc04b12f9e91ffb859164193be8b6168a7a"},
339
+]
340
+attrs = [
341
+    {file = "attrs-19.3.0-py2.py3-none-any.whl", hash = "sha256:08a96c641c3a74e44eb59afb61a24f2cb9f4d7188748e76ba4bb5edfa3cb7d1c"},
342
+    {file = "attrs-19.3.0.tar.gz", hash = "sha256:f7b7ce16570fe9965acd6d30101a28f62fb4a7f9e926b3bbc9b61f8b04247e72"},
343
+]
344
+certifi = [
345
+    {file = "certifi-2020.6.20-py2.py3-none-any.whl", hash = "sha256:8fc0819f1f30ba15bdb34cceffb9ef04d99f420f68eb75d901e9560b8749fc41"},
346
+    {file = "certifi-2020.6.20.tar.gz", hash = "sha256:5930595817496dd21bb8dc35dad090f1c2cd0adfaf21204bf6732ca5d8ee34d3"},
347
+]
348
+chardet = [
349
+    {file = "chardet-3.0.4-py2.py3-none-any.whl", hash = "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"},
350
+    {file = "chardet-3.0.4.tar.gz", hash = "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae"},
351
+]
100 352
 click = [
101 353
     {file = "click-7.1.2-py2.py3-none-any.whl", hash = "sha256:dacca89f4bfadd5de3d7489b7c8a566eee0d3676333fbb50030263894c38c0dc"},
102 354
     {file = "click-7.1.2.tar.gz", hash = "sha256:d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a"},
103 355
 ]
356
+colorama = [
357
+    {file = "colorama-0.4.3-py2.py3-none-any.whl", hash = "sha256:7d73d2a99753107a36ac6b455ee49046802e59d9d076ef8e47b61499fa29afff"},
358
+    {file = "colorama-0.4.3.tar.gz", hash = "sha256:e96da0d330793e2cb9485e9ddfd918d456036c7149416295932478192f4436a1"},
359
+]
104 360
 h11 = [
105 361
     {file = "h11-0.9.0-py2.py3-none-any.whl", hash = "sha256:4bc6d6a1238b7615b266ada57e0618568066f57dd6fa967d1290ec9309b2f2f1"},
106 362
     {file = "h11-0.9.0.tar.gz", hash = "sha256:33d4bca7be0fa039f4e84d50ab00531047e53d6ee8ffbc83501ea602c169cae1"},
@@ -119,10 +375,54 @@ httptools = [
119 375
     {file = "httptools-0.1.1-cp38-cp38-win_amd64.whl", hash = "sha256:0a4b1b2012b28e68306575ad14ad5e9120b34fccd02a81eb08838d7e3bbb48be"},
120 376
     {file = "httptools-0.1.1.tar.gz", hash = "sha256:41b573cf33f64a8f8f3400d0a7faf48e1888582b6f6e02b82b9bd4f0bf7497ce"},
121 377
 ]
378
+idna = [
379
+    {file = "idna-2.10-py2.py3-none-any.whl", hash = "sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0"},
380
+    {file = "idna-2.10.tar.gz", hash = "sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6"},
381
+]
382
+importlib-metadata = [
383
+    {file = "importlib_metadata-1.7.0-py2.py3-none-any.whl", hash = "sha256:dc15b2969b4ce36305c51eebe62d418ac7791e9a157911d58bfb1f9ccd8e2070"},
384
+    {file = "importlib_metadata-1.7.0.tar.gz", hash = "sha256:90bb658cdbbf6d1735b6341ce708fc7024a3e14e99ffdc5783edea9f9b077f83"},
385
+]
386
+more-itertools = [
387
+    {file = "more-itertools-8.4.0.tar.gz", hash = "sha256:68c70cc7167bdf5c7c9d8f6954a7837089c6a36bf565383919bb595efb8a17e5"},
388
+    {file = "more_itertools-8.4.0-py3-none-any.whl", hash = "sha256:b78134b2063dd214000685165d81c154522c3ee0a1c0d4d113c80361c234c5a2"},
389
+]
390
+packaging = [
391
+    {file = "packaging-20.4-py2.py3-none-any.whl", hash = "sha256:998416ba6962ae7fbd6596850b80e17859a5753ba17c32284f67bfff33784181"},
392
+    {file = "packaging-20.4.tar.gz", hash = "sha256:4357f74f47b9c12db93624a82154e9b120fa8293699949152b22065d556079f8"},
393
+]
394
+pluggy = [
395
+    {file = "pluggy-0.13.1-py2.py3-none-any.whl", hash = "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"},
396
+    {file = "pluggy-0.13.1.tar.gz", hash = "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0"},
397
+]
398
+py = [
399
+    {file = "py-1.9.0-py2.py3-none-any.whl", hash = "sha256:366389d1db726cd2fcfc79732e75410e5fe4d31db13692115529d34069a043c2"},
400
+    {file = "py-1.9.0.tar.gz", hash = "sha256:9ca6883ce56b4e8da7e79ac18787889fa5206c79dcc67fb065376cd2fe03f342"},
401
+]
402
+pyparsing = [
403
+    {file = "pyparsing-2.4.7-py2.py3-none-any.whl", hash = "sha256:ef9d7589ef3c200abe66653d3f1ab1033c3c419ae9b9bdb1240a85b024efc88b"},
404
+    {file = "pyparsing-2.4.7.tar.gz", hash = "sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1"},
405
+]
122 406
 pyserial = [
123 407
     {file = "pyserial-3.4-py2.py3-none-any.whl", hash = "sha256:e0770fadba80c31013896c7e6ef703f72e7834965954a78e71a3049488d4d7d8"},
124 408
     {file = "pyserial-3.4.tar.gz", hash = "sha256:6e2d401fdee0eab996cf734e67773a0143b932772ca8b42451440cfed942c627"},
125 409
 ]
410
+pytest = [
411
+    {file = "pytest-5.4.3-py3-none-any.whl", hash = "sha256:5c0db86b698e8f170ba4582a492248919255fcd4c79b1ee64ace34301fb589a1"},
412
+    {file = "pytest-5.4.3.tar.gz", hash = "sha256:7979331bfcba207414f5e1263b5a0f8f521d0f457318836a7355531ed1a4c7d8"},
413
+]
414
+pytest-asyncio = [
415
+    {file = "pytest-asyncio-0.14.0.tar.gz", hash = "sha256:9882c0c6b24429449f5f969a5158b528f39bde47dc32e85b9f0403965017e700"},
416
+    {file = "pytest_asyncio-0.14.0-py3-none-any.whl", hash = "sha256:2eae1e34f6c68fc0a9dc12d4bea190483843ff4708d24277c41568d6b6044f1d"},
417
+]
418
+requests = [
419
+    {file = "requests-2.24.0-py2.py3-none-any.whl", hash = "sha256:fe75cc94a9443b9246fc7049224f75604b113c36acb93f87b80ed42c44cbb898"},
420
+    {file = "requests-2.24.0.tar.gz", hash = "sha256:b3559a131db72c33ee969480840fff4bb6dd111de7dd27c8ee1f820f4f00231b"},
421
+]
422
+six = [
423
+    {file = "six-1.15.0-py2.py3-none-any.whl", hash = "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced"},
424
+    {file = "six-1.15.0.tar.gz", hash = "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259"},
425
+]
126 426
 starlette = [
127 427
     {file = "starlette-0.13.6-py3-none-any.whl", hash = "sha256:bd2ffe5e37fb75d014728511f8e68ebf2c80b0fa3d04ca1479f4dc752ae31ac9"},
128 428
     {file = "starlette-0.13.6.tar.gz", hash = "sha256:ebe8ee08d9be96a3c9f31b2cb2a24dbdf845247b745664bd8a3f9bd0c977fdbc"},
@@ -131,6 +431,10 @@ umodbus = [
131 431
     {file = "uModbus-1.0.3-py2.py3-none-any.whl", hash = "sha256:39487b5dffb27928d9202eeeb64a04c638d00f9171f4d79e93b7a853414fbfca"},
132 432
     {file = "uModbus-1.0.3.tar.gz", hash = "sha256:bfb3989939db4672289da4c5986e2bf46ac4b41f976fbed2f5b682bf17403990"},
133 433
 ]
434
+urllib3 = [
435
+    {file = "urllib3-1.25.10-py2.py3-none-any.whl", hash = "sha256:e7983572181f5e1522d9c98453462384ee92a0be7fac5f1413a1e35c56cc0461"},
436
+    {file = "urllib3-1.25.10.tar.gz", hash = "sha256:91056c15fa70756691db97756772bb1eb9678fa585d9184f24534b100dc60f4a"},
437
+]
134 438
 uvicorn = [
135 439
     {file = "uvicorn-0.11.6-py3-none-any.whl", hash = "sha256:d19a20b17445708fd222e5a7cfc3eacfb31ac269bc8fefa4920833334e199782"},
136 440
     {file = "uvicorn-0.11.6.tar.gz", hash = "sha256:467c333c743ec6a3eb545517a0e3cac603becfa40c73c65ed46c3a4bf1c8e2d0"},
@@ -146,6 +450,10 @@ uvloop = [
146 450
     {file = "uvloop-0.14.0-cp38-cp38-manylinux2010_x86_64.whl", hash = "sha256:4315d2ec3ca393dd5bc0b0089d23101276778c304d42faff5dc4579cb6caef09"},
147 451
     {file = "uvloop-0.14.0.tar.gz", hash = "sha256:123ac9c0c7dd71464f58f1b4ee0bbd81285d96cdda8bc3519281b8973e3a461e"},
148 452
 ]
453
+wcwidth = [
454
+    {file = "wcwidth-0.2.5-py2.py3-none-any.whl", hash = "sha256:beb4802a9cebb9144e99086eff703a642a13d6a0052920003a230f3294bbe784"},
455
+    {file = "wcwidth-0.2.5.tar.gz", hash = "sha256:c4d647b99872929fdb7bdcaa4fbe7f01413ed3d98077df798530e5b04f116c83"},
456
+]
149 457
 websockets = [
150 458
     {file = "websockets-8.1-cp36-cp36m-macosx_10_6_intel.whl", hash = "sha256:3762791ab8b38948f0c4d281c8b2ddfa99b7e510e46bd8dfa942a5fff621068c"},
151 459
     {file = "websockets-8.1-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:3db87421956f1b0779a7564915875ba774295cc86e81bc671631379371af1170"},
@@ -170,3 +478,7 @@ websockets = [
170 478
     {file = "websockets-8.1-cp38-cp38-win_amd64.whl", hash = "sha256:f8a7bff6e8664afc4e6c28b983845c5bc14965030e3fb98789734d416af77c4b"},
171 479
     {file = "websockets-8.1.tar.gz", hash = "sha256:5c65d2da8c6bce0fca2528f69f44b2f977e06954c8512a952222cea50dad430f"},
172 480
 ]
481
+zipp = [
482
+    {file = "zipp-3.1.0-py3-none-any.whl", hash = "sha256:aa36550ff0c0b7ef7fa639055d797116ee891440eac1a56f378e2d3179e0320b"},
483
+    {file = "zipp-3.1.0.tar.gz", hash = "sha256:c599e4d75c98f6798c509911d08a22e6c021d074469042177c8c86fb92eefd96"},
484
+]

+ 28
- 9
pyheatpump/conf.py ファイルの表示

@@ -3,8 +3,10 @@ import os
3 3
 from datetime import datetime
4 4
 from configparser import ConfigParser
5 5
 from starlette.routing import Route, Router
6
+from starlette.responses import PlainTextResponse, JSONResponse
6 7
 from pprint import pprint
7 8
 import uvicorn
9
+import json
8 10
 
9 11
 default_config = {
10 12
     'heatpump': {
@@ -12,7 +14,7 @@ default_config = {
12 14
         'baudrate': 19200,
13 15
         'mac_address': '00:00:00:00:00:00',
14 16
         'ip_address': 'dhcp',
15
-        'read_only': False,
17
+        'read_only': 'False',
16 18
         'database': '/var/run/pyheatpump/pyheatpump.sqlite3'
17 19
     },
18 20
     'supervisor': {
@@ -21,9 +23,9 @@ default_config = {
21 23
         'get_path': '/',
22 24
         'post_path': '/',
23 25
         'interval': 10000,
24
-        'auth': None,
25
-        'initialization': datetime.now(),
26
-        'heatpump_id': None
26
+        'auth': 'None',
27
+        'initialization': str(datetime.now()),
28
+        'heatpump_id': 'None'
27 29
     }
28 30
 }
29 31
 
@@ -32,15 +34,31 @@ CONFIG_FILES=[
32 34
     '/etc/pyheatpump.ini']
33 35
 
34 36
 config = ConfigParser(
35
-    defaults=default_config,
36 37
     default_section='heatpump')
38
+config.read_dict(default_config)
37 39
 config.read(filenames=CONFIG_FILES)
38 40
 
39 41
 async def get_config(request):
40
-    pprint(request)
42
+    items = dict([ (
43
+        section, dict([ (key, config.get(section, key)) for key in items ])
44
+        ) for section, items in config.items() ])
45
+    return JSONResponse(items)
41 46
 
42 47
 async def set_config(request):
43
-    pass
48
+
49
+    body = json.loads(await request.json())
50
+
51
+    for sect in body.keys():
52
+        for item in body[sect].keys():
53
+            if not config.has_option(sect, item):
54
+                raise HTTPException(404, f'The option {sect}.{item} does not exists')
55
+            else:
56
+                config.set(sect, item, value=body[sect][item])
57
+
58
+    with open(CONFIG_FILES[0], 'w') as conf_file:
59
+        config.write(conf_file)
60
+
61
+    return PlainTextResponse('OK')
44 62
 
45 63
 async def config_route(request, *args, **kwargs):
46 64
     if request['method'] == 'GET':
@@ -48,7 +66,9 @@ async def config_route(request, *args, **kwargs):
48 66
     elif request['method'] == 'POST':
49 67
         return await set_config(request)
50 68
 
51
-ROUTES=[Route('/', config_route, methods=['GET', 'POST'])]
69
+ROUTES=[
70
+    Route('/', config_route, methods=['GET', 'POST'])
71
+]
52 72
 
53 73
 app = Router(routes=ROUTES)
54 74
 
@@ -56,4 +76,3 @@ if __name__ == '__main__':
56 76
     uvicorn.run('pyHeatpump:conf.app',
57 77
         host='127.0.0.1',
58 78
         port=8000)
59
-

+ 4
- 0
pyproject.toml ファイルの表示

@@ -12,6 +12,10 @@ umodbus = "^1.0.3"
12 12
 uvicorn = "^0.11.6"
13 13
 
14 14
 [tool.poetry.dev-dependencies]
15
+pytest = "^5.4.3"
16
+requests = "^2.24.0"
17
+asynctest = "^0.13.0"
18
+pytest-asyncio = "^0.14.0"
15 19
 
16 20
 [build-system]
17 21
 requires = ["poetry>=0.12"]

+ 3
- 0
pytest.ini ファイルの表示

@@ -0,0 +1,3 @@
1
+[pytest]
2
+testpaths = tests
3
+addopts = -rP

+ 0
- 0
tests/__init__.py ファイルの表示


+ 49
- 0
tests/test_config.py ファイルの表示

@@ -0,0 +1,49 @@
1
+#!/usr/bin/env python3
2
+import pytest
3
+from starlette.authentication import UnauthenticatedUser
4
+from starlette.testclient import TestClient
5
+from pyheatpump.conf import app, config, default_config, CONFIG_FILES, get_config, set_config, config_route, ROUTES
6
+from unittest.mock import patch, MagicMock
7
+from pprint import pprint
8
+import json
9
+from tempfile import mkstemp
10
+from configparser import ConfigParser
11
+import os
12
+
13
+def test_get_():
14
+    c = TestClient(app)
15
+    r = c.get('/')
16
+    assert r.status_code == 200
17
+
18
+class RequestMock(MagicMock):
19
+    def __get__(self, key):
20
+        if key == 'method':
21
+            return 'GET'
22
+
23
+@pytest.mark.asyncio
24
+async def test_get_config():
25
+    resp = await get_config(RequestMock())
26
+    assert resp.status_code  == 200
27
+    d_resp = json.loads(resp.body.decode())
28
+    for sect in default_config.keys():
29
+        assert sect in d_resp.keys()
30
+        for item in default_config[sect].keys():
31
+            assert item in d_resp[sect].keys()
32
+
33
+def test_set_config():
34
+    c = TestClient(app)
35
+    _, tmpconf = mkstemp()
36
+    CONFIG_FILES.insert(0, tmpconf)
37
+    r = c.post('/', json=json.dumps({
38
+        'supervisor': {
39
+            'scheme':'test'
40
+        }
41
+    }))
42
+
43
+    config = ConfigParser()
44
+    config.read(filenames=[tmpconf])
45
+
46
+    assert r.status_code == 200
47
+    assert config.get('supervisor', 'scheme') == 'test'
48
+
49
+    os.unlink(tmpconf)

読み込み中…
キャンセル
保存