]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
tests: Add a simple test for deduplication
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 21 Feb 2024 15:09:04 +0000 (15:09 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 21 Feb 2024 15:09:04 +0000 (15:09 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
tests/python/networks-dedup.py [new file with mode: 0644]

index d4305507c8b6a5f8cd31911472c2caaec8e1af48..6b1a136f17213e6d44a1d300b1ee92a1fcda3256 100644 (file)
@@ -362,6 +362,7 @@ testdata.db: examples/python/create-database.py
                $(PYTHON) $< $@
 
 dist_check_SCRIPTS = \
+       tests/python/networks-dedup.py \
        tests/python/test-database.py \
        tests/python/test-export.py
 
diff --git a/tests/python/networks-dedup.py b/tests/python/networks-dedup.py
new file mode 100644 (file)
index 0000000..bcb8f9c
--- /dev/null
@@ -0,0 +1,56 @@
+#!/usr/bin/python3
+###############################################################################
+#                                                                             #
+# libloc - A library to determine the location of someone on the Internet     #
+#                                                                             #
+# Copyright (C) 2024 IPFire Development Team <info@ipfire.org>                #
+#                                                                             #
+# This library is free software; you can redistribute it and/or               #
+# modify it under the terms of the GNU Lesser General Public                  #
+# License as published by the Free Software Foundation; either                #
+# version 2.1 of the License, or (at your option) any later version.          #
+#                                                                             #
+# This library is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU           #
+# Lesser General Public License for more details.                             #
+#                                                                             #
+###############################################################################
+
+import location
+import os
+import tempfile
+import unittest
+
+class Test(unittest.TestCase):
+       def test_dudup_simple(self):
+               """
+                       Creates a couple of redundant networks and expects fewer being written
+               """
+               with tempfile.NamedTemporaryFile() as f:
+                       w = location.Writer()
+
+                       # Add 10.0.0.0/8
+                       n = w.add_network("10.0.0.0/8")
+
+                       # Add 10.0.0.0/16
+                       w.add_network("10.0.0.0/16")
+
+                       # Add 10.0.0.0/24
+                       w.add_network("10.0.0.0/24")
+
+                       # Write file
+                       w.write(f.name)
+
+                       # 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)
+
+                               self.assertTrue(i == 0)
+
+
+if __name__ == "__main__":
+       unittest.main()