From: Michael Tremer Date: Wed, 21 Feb 2024 15:09:04 +0000 (+0000) Subject: tests: Add a simple test for deduplication X-Git-Tag: 0.9.18~187 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7f60f3c40932a76cf759a8229af2446cd5acb64;p=location%2Flibloc.git tests: Add a simple test for deduplication Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index d430550..6b1a136 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 0000000..bcb8f9c --- /dev/null +++ b/tests/python/networks-dedup.py @@ -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 # +# # +# 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()