From c6b648195f13a1563c436254a1118d18bdedf25a Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Tue, 20 Sep 2022 15:39:43 -0600 Subject: [PATCH] test line chooser sticky --- src/test_writer.py | 36 ++++++++++++++++++++++++++++++++++++ src/writer.py | 4 ++-- 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/test_writer.py diff --git a/src/test_writer.py b/src/test_writer.py new file mode 100644 index 0000000..37e2b5e --- /dev/null +++ b/src/test_writer.py @@ -0,0 +1,36 @@ +import unittest + +import writer + +class TestLineChooser(unittest.TestCase): + def test_latest_sticky(self): + stale_something = self.new_line(False, 1, 1) + stale_nothing = self.new_line(False, 1, None) + old_something = self.new_line(True, 2, 2) + old_nothing = self.new_line(True, 2, None) + new_something = self.new_line(True, 3, 3) + new_nothing = self.new_line(True, 3, None) + + chooser = writer.LineChooserLatestSticky() + + for name, c in ({ + "nothing over stale": [new_nothing, stale_something], + "slightly old over nothing": [old_something, new_nothing], + "new and nonzero": [new_something, old_something], + "new over nothing": [new_something, old_nothing], + "new over stale nothing": [new_something, stale_nothing], + }).items(): + self.assertEqual( + c[0], + chooser.choose(c[0], c[1]), + name, + ) + + def new_line(self, is_recent, t, v): + result = writer.Line(v) + result.t = t + result.is_recent = lambda *args: is_recent + return result + +if __name__ == "__main__": + unittest.main() diff --git a/src/writer.py b/src/writer.py index 19e2fb4..eed945a 100644 --- a/src/writer.py +++ b/src/writer.py @@ -78,10 +78,10 @@ class Line: return not self.v def is_recent(self): - return time.time() - self.t < 1 + return time.time() - self.t < 5 class LineChooser: - def choose(a, b): + def choose(self, a, b): if not a: return b if not b: