From 47e1c8f032873d656a8dd44f6f0e337596a6dbdf Mon Sep 17 00:00:00 2001 From: josidd <joseph.siddons@noc.ac.uk> Date: Tue, 24 Sep 2024 15:32:04 +0100 Subject: [PATCH] test: tests for find_nearest --- test/test_neighbours.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 test/test_neighbours.py diff --git a/test/test_neighbours.py b/test/test_neighbours.py new file mode 100644 index 0000000..fd2a6e2 --- /dev/null +++ b/test/test_neighbours.py @@ -0,0 +1,34 @@ +import unittest +from numpy import argmin +from random import choice, sample +from datetime import datetime, timedelta +from GeoSpatialTools import find_nearest + + +class TestFindNearest(unittest.TestCase): + dates = [ + datetime(2009, 1, 1, 0, 0) + timedelta(seconds=i * 3600) + for i in range(365 * 24) + ] + test_dates = sample(dates, 150) + test_dates = [ + d + timedelta(seconds=60 * choice(range(60))) for d in test_dates + ] + test_dates.append(dates[0]) + test_dates.append(dates[-1]) + test_dates.append(datetime(2004, 11, 15, 17, 28)) + test_dates.append(datetime(2013, 4, 22, 1, 41)) + + def test_nearest(self): + greedy = [ + argmin([abs(x - y) for x in self.dates]) for y in self.test_dates + ] + ours = find_nearest(self.dates, self.test_dates) + + assert ours == greedy + + pass + + +if __name__ == "__main__": + unittest.main() -- GitLab