]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
tests: Build out more dedup tests
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 21 Feb 2024 16:19:57 +0000 (16:19 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 21 Feb 2024 16:19:57 +0000 (16:19 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/python/networks-dedup.py

index bcb8f9c88138739af0330d3af611833ba9375c5d..abceaae9a6910161f9d914dc9f1c6754362db31c 100644 (file)
@@ -23,21 +23,25 @@ import tempfile
 import unittest
 
 class Test(unittest.TestCase):
-       def test_dudup_simple(self):
+       def __test(self, inputs, outputs):
                """
-                       Creates a couple of redundant networks and expects fewer being written
+                       Takes a list of networks that are written to the database and
+                       compares the result with the second argument.
                """
                with tempfile.NamedTemporaryFile() as f:
                        w = location.Writer()
 
-                       # Add 10.0.0.0/8
-                       n = w.add_network("10.0.0.0/8")
+                       # Add all inputs
+                       for network, cc, asn in inputs:
+                               n = w.add_network(network)
 
-                       # Add 10.0.0.0/16
-                       w.add_network("10.0.0.0/16")
+                               # Add CC
+                               if cc:
+                                       n.country_code = cc
 
-                       # Add 10.0.0.0/24
-                       w.add_network("10.0.0.0/24")
+                               # Add ASN
+                               if asn:
+                                       n.asn = asn
 
                        # Write file
                        w.write(f.name)
@@ -45,11 +49,46 @@ class Test(unittest.TestCase):
                        # Re-open the database
                        db = location.Database(f.name)
 
-                       for i, network in enumerate(db.networks):
-                               # The only network we should see is 10.0.0.0/8
-                               self.assertEqual(network, n)
+                       # Check if the output matches what we expect
+                       self.assertCountEqual(
+                               outputs, ["%s" % network for network in db.networks],
+                       )
+
+       def test_dudup_simple(self):
+               """
+                       Creates a couple of redundant networks and expects fewer being written
+               """
+               self.__test(
+                       (
+                               ("10.0.0.0/8", None, None),
+                               ("10.0.0.0/16", None, None),
+                               ("10.0.0.0/24", None, None),
+                       ),
+
+                       # Everything should be put into the /8 subnet
+                       ("10.0.0.0/8",),
+               )
+
+       def test_dedup_noop(self):
+               """
+                       Nothing should be changed here
+               """
+               self.maxDiff = None
+
+               networks = (
+                       ("10.0.0.0/8", None, None),
+                       ("20.0.0.0/8", None, None),
+                       ("30.0.0.0/8", None, None),
+                       ("40.0.0.0/8", None, None),
+                       ("50.0.0.0/8", None, None),
+                       ("60.0.0.0/8", None, None),
+                       ("70.0.0.0/8", None, None),
+                       ("80.0.0.0/8", None, None),
+                       ("90.0.0.0/8", None, None),
+               )
 
-                               self.assertTrue(i == 0)
+               # The input should match the output
+               self.__test(networks, [network for network, cc, asn in networks])
 
 
 if __name__ == "__main__":