diff 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
line wrap: on
line diff
--- a/weather_server/logfile_test.py	Sun Oct 13 18:44:12 2019 -0400
+++ b/weather_server/logfile_test.py	Sat Oct 19 18:40:48 2019 -0400
@@ -30,7 +30,9 @@
         super().tearDown()
 
     def test_empty(self):
-        with contextlib.closing(logfile.Logger(str(self.log_path))) as logger:
+        lg = logfile.Logger(
+            str(self.log_path), sample_field='x')
+        with contextlib.closing(lg) as logger:
             self.assertEqual(logger.data, ())
 
     def test_loading(self):
@@ -42,10 +44,19 @@
                 ingest_time=ts(125),
             )))
             outfile.write(b'garbage to ignore')
-        with contextlib.closing(logfile.Logger(str(self.log_path))) as logger:
+        with contextlib.closing(logfile.Logger(
+            str(self.log_path),
+            sample_field='sample_time',
+        )) as logger:
             self.assertEqual(
                 logger.data,
-                (types.Reading(ts(123), 420, 69, ts(125)),))
+                (
+                    dict(
+                        sample_time=ts(123),
+                        temp_c=420,
+                        rh_pct=69,
+                        ingest_time=ts(125)),
+                ))
 
     def test_writing(self):
         with self.log_path.open('wb') as outfile:
@@ -55,16 +66,20 @@
                 rh_pct=69,
                 ingest_time=ts(125),
             )))
-        with contextlib.closing(logfile.Logger(str(self.log_path))) as logger:
+        with contextlib.closing(logfile.Logger(
+            str(self.log_path),
+            sample_field='sample_time',
+        )) as logger:
             logger.write_rows([
-                types.Reading(ts(100), 999, 666, ts(101)),
-                types.Reading(ts(125), 333, 777, ts(200)),
+                # Ignored, since it's older than the newest entry.
+                types.Reading(ts(100), 999, 666, ts(101)).as_dict(),
+                types.Reading(ts(125), 333, 777, ts(200)).as_dict(),
             ])
             self.assertEqual(
                 logger.data,
                 (
-                    types.Reading(ts(123), 420, 69, ts(125)),
-                    types.Reading(ts(125), 333, 777, ts(200)),
+                    types.Reading(ts(123), 420, 69, ts(125)).as_dict(),
+                    types.Reading(ts(125), 333, 777, ts(200)).as_dict(),
                 )
             )
 
@@ -84,10 +99,13 @@
         ])
 
     def test_outside_writes(self):
-        with contextlib.closing(logfile.Logger(str(self.log_path))) as logger:
+        with contextlib.closing(logfile.Logger(
+            str(self.log_path),
+            sample_field='sample_time',
+        )) as logger:
             logger.write_rows([
-                types.Reading(ts(100), 999, 666, ts(101)),
-                types.Reading(ts(125), 333, 777, ts(200)),
+                types.Reading(ts(100), 999, 666, ts(101)).as_dict(),
+                types.Reading(ts(125), 333, 777, ts(200)).as_dict(),
             ])
             with self.log_path.open('ab') as outfile:
                 outfile.write(common.bson_encode(dict(
@@ -98,9 +116,9 @@
                 )))
                 outfile.flush()
             self.assertEqual(logger.data, (
-                types.Reading(ts(100), 999, 666, ts(101)),
-                types.Reading(ts(125), 333, 777, ts(200)),
-                types.Reading(ts(1024), 256, 128, ts(4096)),
+                types.Reading(ts(100), 999, 666, ts(101)).as_dict(),
+                types.Reading(ts(125), 333, 777, ts(200)).as_dict(),
+                types.Reading(ts(1024), 256, 128, ts(4096)).as_dict(),
             ))
 
     def read_bsons(self):