1
+ import logging
2
+ import pytz
3
+
4
+ from datetime import datetime
1
5
from django .test import TestCase
2
6
from app .models import Sensor , Data
7
+ from app .tests .utils import setup_logger
8
+
9
+ setup_logger ()
10
+ logger = logging .getLogger ("TEST" )
3
11
4
12
class ByRoomSortTestCase (TestCase ):
5
13
def setUp (self ):
6
- pass
14
+ self .sensor_1 = Sensor .objects .create (
15
+ deveui = "24e124128c019417" ,
16
+ devicename = "AM107-33" ,
17
+ room = "E103" ,
18
+ building = "E" ,
19
+ floor = 1 ,
20
+ batterylevel = 60 ,
21
+ externalpowersource = False
22
+ )
23
+
24
+ # self.sensor_2 = Sensor.objects.create(
25
+ # deveui="24e124128c019416",
26
+ # devicename="AM107-33",
27
+ # room="E102",
28
+ # building="E",
29
+ # floor=1,
30
+ # batterylevel=11.7,
31
+ # externalpowersource=True
32
+ # )
33
+
34
+ timezone = pytz .timezone ('Europe/Paris' )
35
+ self .local_time_1 = datetime .now (timezone ).isoformat ()
36
+
37
+ self .data_1 = Data .objects .create (
38
+ time = self .local_time_1 ,
39
+ temperature = 23.5 ,
40
+ humidity = 45.3 ,
41
+ activity = 0 ,
42
+ co2 = 500 ,
43
+ tvoc = 10000 ,
44
+ illuminance = 20000 ,
45
+ infrared = 30000 ,
46
+ infrared_and_visible = 40000 ,
47
+ pressure = 1013.25 ,
48
+ sensor = Sensor .objects .get (deveui = self .sensor_1 .deveui )
49
+ )
50
+
51
+ self .local_time_2 = datetime .now (timezone ).isoformat ()
52
+
53
+ self .data_2 = Data .objects .create (
54
+ time = self .local_time_2 ,
55
+ temperature = 23.5 ,
56
+ humidity = 45.3 ,
57
+ activity = 0 ,
58
+ co2 = 500 ,
59
+ tvoc = 10000 ,
60
+ illuminance = 20000 ,
61
+ infrared = 30000 ,
62
+ infrared_and_visible = 40000 ,
63
+ pressure = 1013.25 ,
64
+ sensor = Sensor .objects .get (deveui = self .sensor_1 .deveui )
65
+ )
66
+
67
+ self .local_time_3 = datetime .now (timezone ).isoformat ()
68
+
69
+ self .data_3 = Data .objects .create (
70
+ time = self .local_time_3 ,
71
+ temperature = 23.5 ,
72
+ humidity = 45.3 ,
73
+ activity = 0 ,
74
+ co2 = 500 ,
75
+ tvoc = 10000 ,
76
+ illuminance = 20000 ,
77
+ infrared = 30000 ,
78
+ infrared_and_visible = 40000 ,
79
+ pressure = 1013.25 ,
80
+ sensor = Sensor .objects .get (deveui = self .sensor_1 .deveui )
81
+ )
82
+
83
+ self .local_time_1 = str (self .local_time_1 ).split ('+' )[0 ]
84
+ self .local_time_2 = str (self .local_time_2 ).split ('+' )[0 ]
85
+ self .local_time_3 = str (self .local_time_3 ).split ('+' )[0 ]
86
+
87
+ def test_endpoint_byroomsort_last_data_1 (self ):
88
+ response = self .client .get ('/ByRoom/E103/?last_data=2' )
89
+
90
+ expected = {
91
+ "room" : self .sensor_1 .room ,
92
+ "all_data" : [
93
+ self .data_2 .id ,
94
+ self .data_3 .id
95
+ ],
96
+ "sensor" : self .sensor_1 .deveui ,
97
+ "building" : self .sensor_1 .building ,
98
+ "floor" : self .sensor_1 .floor
99
+ }
100
+
101
+ self .assertEqual (response .status_code , 200 )
102
+ self .assertEqual (response .json (), expected )
103
+
104
+
105
+ def test_endpoint_byroomsort_last_data_2 (self ):
106
+ response = self .client .get ('/ByRoom/E103/?last_data=1' )
107
+
108
+ expected = {
109
+ "room" : self .sensor_1 .room ,
110
+ "all_data" : [
111
+ self .data_3 .id
112
+ ],
113
+ "sensor" : self .sensor_1 .deveui ,
114
+ "building" : self .sensor_1 .building ,
115
+ "floor" : self .sensor_1 .floor
116
+ }
117
+
118
+ self .assertEqual (response .status_code , 200 )
119
+ self .assertEqual (response .json (), expected )
120
+
121
+ def test_endpoint_byroomsort_from_to_1 (self ):
122
+ response = self .client .get ('/ByRoom/E103/?from=' + self .local_time_1 + '&to=' + self .local_time_2 )
123
+
124
+ expected = {
125
+ "room" : self .sensor_1 .room ,
126
+ "all_data" : [
127
+ self .data_1 .id ,
128
+ self .data_2 .id
129
+ ],
130
+ "sensor" : self .sensor_1 .deveui ,
131
+ "building" : self .sensor_1 .building ,
132
+ "floor" : self .sensor_1 .floor
133
+ }
134
+
135
+ self .assertEqual (response .status_code , 200 )
136
+ self .assertEqual (response .json (), expected )
137
+
0 commit comments