Mercurial > personal > weather-server
comparison weather_server/logfile_test.py @ 21:beb42c835c52
Make weather server handle arbitrary data:
- Make logfile record arbitrary BSONs
- Make server handlers OK with same
- Make location type a normal class rather than attrs;
have it handle its own logger.
- Bump version number.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sat, 19 Oct 2019 18:40:48 -0400 |
parents | efe7a1eff167 |
children | 20c8ec56e447 |
comparison
equal
deleted
inserted
replaced
20:a7fe635d1c88 | 21:beb42c835c52 |
---|---|
28 def tearDown(self): | 28 def tearDown(self): |
29 self.temp_dir.cleanup() | 29 self.temp_dir.cleanup() |
30 super().tearDown() | 30 super().tearDown() |
31 | 31 |
32 def test_empty(self): | 32 def test_empty(self): |
33 with contextlib.closing(logfile.Logger(str(self.log_path))) as logger: | 33 lg = logfile.Logger( |
34 str(self.log_path), sample_field='x') | |
35 with contextlib.closing(lg) as logger: | |
34 self.assertEqual(logger.data, ()) | 36 self.assertEqual(logger.data, ()) |
35 | 37 |
36 def test_loading(self): | 38 def test_loading(self): |
37 with self.log_path.open('wb') as outfile: | 39 with self.log_path.open('wb') as outfile: |
38 outfile.write(common.bson_encode(dict( | 40 outfile.write(common.bson_encode(dict( |
40 temp_c=420, | 42 temp_c=420, |
41 rh_pct=69, | 43 rh_pct=69, |
42 ingest_time=ts(125), | 44 ingest_time=ts(125), |
43 ))) | 45 ))) |
44 outfile.write(b'garbage to ignore') | 46 outfile.write(b'garbage to ignore') |
45 with contextlib.closing(logfile.Logger(str(self.log_path))) as logger: | 47 with contextlib.closing(logfile.Logger( |
48 str(self.log_path), | |
49 sample_field='sample_time', | |
50 )) as logger: | |
46 self.assertEqual( | 51 self.assertEqual( |
47 logger.data, | 52 logger.data, |
48 (types.Reading(ts(123), 420, 69, ts(125)),)) | 53 ( |
54 dict( | |
55 sample_time=ts(123), | |
56 temp_c=420, | |
57 rh_pct=69, | |
58 ingest_time=ts(125)), | |
59 )) | |
49 | 60 |
50 def test_writing(self): | 61 def test_writing(self): |
51 with self.log_path.open('wb') as outfile: | 62 with self.log_path.open('wb') as outfile: |
52 outfile.write(common.bson_encode(dict( | 63 outfile.write(common.bson_encode(dict( |
53 sample_time=ts(123), | 64 sample_time=ts(123), |
54 temp_c=420, | 65 temp_c=420, |
55 rh_pct=69, | 66 rh_pct=69, |
56 ingest_time=ts(125), | 67 ingest_time=ts(125), |
57 ))) | 68 ))) |
58 with contextlib.closing(logfile.Logger(str(self.log_path))) as logger: | 69 with contextlib.closing(logfile.Logger( |
70 str(self.log_path), | |
71 sample_field='sample_time', | |
72 )) as logger: | |
59 logger.write_rows([ | 73 logger.write_rows([ |
60 types.Reading(ts(100), 999, 666, ts(101)), | 74 # Ignored, since it's older than the newest entry. |
61 types.Reading(ts(125), 333, 777, ts(200)), | 75 types.Reading(ts(100), 999, 666, ts(101)).as_dict(), |
76 types.Reading(ts(125), 333, 777, ts(200)).as_dict(), | |
62 ]) | 77 ]) |
63 self.assertEqual( | 78 self.assertEqual( |
64 logger.data, | 79 logger.data, |
65 ( | 80 ( |
66 types.Reading(ts(123), 420, 69, ts(125)), | 81 types.Reading(ts(123), 420, 69, ts(125)).as_dict(), |
67 types.Reading(ts(125), 333, 777, ts(200)), | 82 types.Reading(ts(125), 333, 777, ts(200)).as_dict(), |
68 ) | 83 ) |
69 ) | 84 ) |
70 | 85 |
71 self.assertEqual(self.read_bsons(), [ | 86 self.assertEqual(self.read_bsons(), [ |
72 dict( | 87 dict( |
82 ingest_time=ts(200), | 97 ingest_time=ts(200), |
83 ), | 98 ), |
84 ]) | 99 ]) |
85 | 100 |
86 def test_outside_writes(self): | 101 def test_outside_writes(self): |
87 with contextlib.closing(logfile.Logger(str(self.log_path))) as logger: | 102 with contextlib.closing(logfile.Logger( |
103 str(self.log_path), | |
104 sample_field='sample_time', | |
105 )) as logger: | |
88 logger.write_rows([ | 106 logger.write_rows([ |
89 types.Reading(ts(100), 999, 666, ts(101)), | 107 types.Reading(ts(100), 999, 666, ts(101)).as_dict(), |
90 types.Reading(ts(125), 333, 777, ts(200)), | 108 types.Reading(ts(125), 333, 777, ts(200)).as_dict(), |
91 ]) | 109 ]) |
92 with self.log_path.open('ab') as outfile: | 110 with self.log_path.open('ab') as outfile: |
93 outfile.write(common.bson_encode(dict( | 111 outfile.write(common.bson_encode(dict( |
94 sample_time=ts(1024), | 112 sample_time=ts(1024), |
95 temp_c=256, | 113 temp_c=256, |
96 rh_pct=128, | 114 rh_pct=128, |
97 ingest_time=ts(4096), | 115 ingest_time=ts(4096), |
98 ))) | 116 ))) |
99 outfile.flush() | 117 outfile.flush() |
100 self.assertEqual(logger.data, ( | 118 self.assertEqual(logger.data, ( |
101 types.Reading(ts(100), 999, 666, ts(101)), | 119 types.Reading(ts(100), 999, 666, ts(101)).as_dict(), |
102 types.Reading(ts(125), 333, 777, ts(200)), | 120 types.Reading(ts(125), 333, 777, ts(200)).as_dict(), |
103 types.Reading(ts(1024), 256, 128, ts(4096)), | 121 types.Reading(ts(1024), 256, 128, ts(4096)).as_dict(), |
104 )) | 122 )) |
105 | 123 |
106 def read_bsons(self): | 124 def read_bsons(self): |
107 with self.log_path.open('rb') as infile: | 125 with self.log_path.open('rb') as infile: |
108 return bson.decode_all( | 126 return bson.decode_all( |