source: src/mapservice/tests/test_logs.py@ 3db3d13

debian/bullseye
Last change on this file since 3db3d13 was 7be94aa, checked in by Joachim Langenbach <joachim.langenbach@…>, 7 years ago
  • Added yapf style config (src/python-style.yapf)
    • Updated all files to the yapf formatted version
  • Property mode set to 100644
File size: 5.7 KB
Line 
1'''
2 Copyright (C) 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16'''
17
18import unittest
19import json
20import os
21
22from . import BaseTestCase
23
24
25class TestLogs(BaseTestCase):
26 @classmethod
27 def setUpClass(self):
28 self.base_url = '/mapservice/v1/logs'
29
30 def test_get(self):
31 """Test case getting log (src)
32 """
33 headers = {'Accept': 'application/json'}
34 response = self.client.open(
35 '{}/{}'.format(self.base_url, self.db_content.test_log.id),
36 method='GET',
37 headers=headers
38 )
39 assert response.status_code == 200
40 data = json.loads(response.data)
41 assert len(data) == 2
42 assert 'entries' in data
43 assert 'metaData' in data
44 metaData = data['metaData']
45 assert "submitter" in metaData
46 self.check_key(
47 "submitter", self.db_content.test_log.submitter.username, metaData
48 )
49 assert 'water' in metaData
50 self.check_key(
51 "id", self.db_content.test_log.water.id, metaData['water']
52 )
53 self.check_key(
54 "label", self.db_content.test_log.water.name, metaData['water']
55 )
56 assert 'device' in metaData
57 self.check_key(
58 "id", self.db_content.test_log.device.id, metaData['device']
59 )
60 self.check_key(
61 "label", self.db_content.test_log.device.name, metaData['device']
62 )
63 assert 'gauge' in metaData
64 self.check_key(
65 "id", self.db_content.test_log.gauge.id, metaData['gauge']
66 )
67 self.check_key(
68 "label", self.db_content.test_log.gauge.name, metaData['gauge']
69 )
70 self.check_key(
71 "gaugeHeight", self.db_content.test_log.gauge_height, metaData
72 )
73
74 def test_get_by_manager(self):
75 """Test case getting log by submitter (src)
76 """
77 headers = {'Accept': 'application/json'}
78
79 response = self.client.open(
80 '{}?submitter=not_existent'.format(self.base_url),
81 method='GET',
82 headers=headers
83 )
84 assert response.status_code == 404, '{} != {}'.format(
85 response.status_code, 404
86 )
87
88 response = self.client.open(
89 '{}?submitter={}'.format(
90 self.base_url, self.db_content.test_log.submitter.username
91 ),
92 method='GET',
93 headers=headers
94 )
95 assert response.status_code == 200
96 data = json.loads(response.data)
97 assert len(data) == 1
98 data = data[0]
99 self.check_key(
100 "submitter", self.db_content.test_log.submitter.username, data
101 )
102 assert 'water' in data
103 self.check_key("id", self.db_content.test_log.water.id, data['water'])
104 self.check_key(
105 "label", self.db_content.test_log.water.name, data['water']
106 )
107 assert 'device' in data
108 self.check_key(
109 "id", self.db_content.test_log.device.id, data['device']
110 )
111 self.check_key(
112 "label", self.db_content.test_log.device.name, data['device']
113 )
114 assert 'gauge' in data
115 self.check_key("id", self.db_content.test_log.gauge.id, data['gauge'])
116 self.check_key(
117 "label", self.db_content.test_log.gauge.name, data['gauge']
118 )
119 self.check_key(
120 "gaugeHeight", self.db_content.test_log.gauge_height, data
121 )
122
123 def test_create_log(self):
124 """Test case create log (src)
125 """
126 headers = {
127 'X-API-KEY': 'test_key',
128 'Accept': 'application/json',
129 'Content-Type': 'application/json'
130 }
131 logCreation = {
132 'water': 1,
133 'device': 1,
134 'gauge': 1,
135 'gaugeHeight': 1.0,
136 'comment': 'Log from unit test'
137 }
138
139 response = self.client.open(
140 self.base_url,
141 method='POST',
142 headers=headers,
143 data=json.dumps(logCreation),
144 content_type='application/json'
145 )
146 assert response.status_code == 200
147 data = response.get_json()
148 assert len(data) >= 7
149 self.check_key("logId", 2, data)
150 assert 'water' in data
151 self.check_key("id", logCreation['water'], data['water'])
152 assert 'device' in data
153 self.check_key("id", logCreation['device'], data['device'])
154 assert 'gauge' in data
155 self.check_key("id", logCreation['gauge'], data['gauge'])
156 self.check_key("gaugeHeight", logCreation['gaugeHeight'], data)
157 self.check_key("comment", logCreation['comment'], data)
158
159 # could not be tested without running CELERY_BROKER
160 #filename = '{}/data/nmea0183-2017-12-09.log'.format(os.path.dirname(os.path.abspath(__file__)))
161 #fileContent = open(filename, 'rb')
162 # headers["Accept"] = "application/geo+json"
163 #response = self.client.post(
164 #"{}/{}".format(self.base_url, data["logId"]),
165 #data = fileContent,
166 #headers = headers
167 #)
168 #assert response.status_code == 200
169
170
171if __name__ == '__main__':
172 unittest.main()
Note: See TracBrowser for help on using the repository browser.