Mercurial > personal > weather-server
view weather_server/logfile_test.py @ 23:88249e451566
server: show date when last report was >12h ago.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 10 Nov 2019 19:42:04 -0500 |
parents | beb42c835c52 |
children | 20c8ec56e447 |
line wrap: on
line source
import contextlib import datetime import pathlib import tempfile import unittest import bson import pytz from . import common from . import logfile from . import types def ts(n): return datetime.datetime.utcfromtimestamp(n).replace(tzinfo=pytz.UTC) class LoggerTest(unittest.TestCase): maxDiff = None def setUp(self): super().setUp() self.temp_dir = tempfile.TemporaryDirectory() self.log_path = pathlib.Path(self.temp_dir.name) / 'test.bson' def tearDown(self): self.temp_dir.cleanup() super().tearDown() def test_empty(self): lg = logfile.Logger( str(self.log_path), sample_field='x') with contextlib.closing(lg) as logger: self.assertEqual(logger.data, ()) def test_loading(self): with self.log_path.open('wb') as outfile: outfile.write(common.bson_encode(dict( sample_time=ts(123), temp_c=420, rh_pct=69, ingest_time=ts(125), ))) outfile.write(b'garbage to ignore') with contextlib.closing(logfile.Logger( str(self.log_path), sample_field='sample_time', )) as logger: self.assertEqual( logger.data, ( 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: outfile.write(common.bson_encode(dict( sample_time=ts(123), temp_c=420, rh_pct=69, ingest_time=ts(125), ))) with contextlib.closing(logfile.Logger( str(self.log_path), sample_field='sample_time', )) as logger: logger.write_rows([ # 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)).as_dict(), types.Reading(ts(125), 333, 777, ts(200)).as_dict(), ) ) self.assertEqual(self.read_bsons(), [ dict( sample_time=ts(123), temp_c=420, rh_pct=69, ingest_time=ts(125), ), dict( sample_time=ts(125), temp_c=333, rh_pct=777, ingest_time=ts(200), ), ]) def test_outside_writes(self): 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)).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( sample_time=ts(1024), temp_c=256, rh_pct=128, ingest_time=ts(4096), ))) outfile.flush() self.assertEqual(logger.data, ( 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): with self.log_path.open('rb') as infile: return bson.decode_all( infile.read(), common.BSON_OPTIONS) if __name__ == '__main__': unittest.main()