From 4b0a387217456a9c47ff2224631edb8e2c48df52 Mon Sep 17 00:00:00 2001 From: Arne de Laat Date: Sat, 14 Sep 2024 22:27:28 +0100 Subject: [PATCH] Add tests for status display templatetags --- .../test_templatetags/__init__.py | 0 .../test_templatetags/test_fix_data.py | 49 +++++++++++++++++++ .../test_templatetags/test_fix_strings.py | 12 +++++ 3 files changed, 61 insertions(+) create mode 100644 tests/test_status_display/test_templatetags/__init__.py create mode 100644 tests/test_status_display/test_templatetags/test_fix_data.py create mode 100644 tests/test_status_display/test_templatetags/test_fix_strings.py diff --git a/tests/test_status_display/test_templatetags/__init__.py b/tests/test_status_display/test_templatetags/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_status_display/test_templatetags/test_fix_data.py b/tests/test_status_display/test_templatetags/test_fix_data.py new file mode 100644 index 000000000..5f0d106a7 --- /dev/null +++ b/tests/test_status_display/test_templatetags/test_fix_data.py @@ -0,0 +1,49 @@ +from django.test import TestCase + +from publicdb.status_display.templatetags import fix_data + + +class TestFixData(TestCase): + def test_fix_histogram_data(self): + self.assertEqual([[1, 2.1], [2, 2.2], [3, 2.2]], fix_data.fix_histogram_data([[1, 2.1], [2, 2.2]])) + self.assertEqual([[1, 2.1], [10, 2.2], [19, 2.2]], fix_data.fix_histogram_data([[1, 2.1], [10, 2.2]])) + + def test_fix_timestamps(self): + self.assertEqual([[1_000, 2.1], [2_000, 2.2]], fix_data.fix_timestamps([[1, 2.1], [2, 2.2]])) + + def test_fix_timestamps_in_data(self): + dataset = [ + [1647993600, 98], + [1647993780, 99], + [1648079820, 1], + ] + self.assertEqual( + [[0, 98], [0.05, 99], [23.95, 1]], + fix_data.fix_timestamps_in_data(dataset), + ) + + def test_slice_data(self): + result = fix_data.slice_data([1] * 2_000, 200) + self.assertEqual(10, len(result)) + self.assertEqual([1] * 10, result) + + def test_round_data(self): + self.assertEqual([[1, 2]], fix_data.round_data([[1, 2.11]], 0)) + self.assertEqual([[1, 2.1]], fix_data.round_data([[1, 2.11]], 1)) + self.assertEqual([[2.0, 2.2]], fix_data.round_data([[1.99, 2.16]], 1)) + + def test_shift_bins(self): + self.assertEqual([[2, 2.1], [3, 2.2]], fix_data.shift_bins([[1, 2.1], [2, 2.2]], 1)) + + def test_none_to_nan(self): + self.assertEqual('nan', fix_data.none_to_nan(None)) + + for value in [0., 0, 1, '', 'this']: + self.assertEqual(value, fix_data.none_to_nan(value)) + + def test_mv_to_adc(self): + self.assertEqual(30, fix_data.mv_to_adc(30)) + self.assertEqual(30, fix_data.mv_to_adc(30.)) + self.assertEqual(30.7, fix_data.mv_to_adc(30.7)) + self.assertEqual(323, fix_data.mv_to_adc(-70)) + self.assertEqual(253, fix_data.mv_to_adc(-30)) diff --git a/tests/test_status_display/test_templatetags/test_fix_strings.py b/tests/test_status_display/test_templatetags/test_fix_strings.py new file mode 100644 index 000000000..624d58d44 --- /dev/null +++ b/tests/test_status_display/test_templatetags/test_fix_strings.py @@ -0,0 +1,12 @@ +from django.test import TestCase + +from publicdb.status_display.templatetags import fix_strings + + +class TestFixStrings(TestCase): + def test_remove_hyphens(self): + self.assertEqual('', fix_strings.remove_hyphens('')) + self.assertEqual('', fix_strings.remove_hyphens('-')) + self.assertEqual('', fix_strings.remove_hyphens('--')) + self.assertEqual('WithoutHyphens', fix_strings.remove_hyphens('-Without-Hyphens')) +