From f74f1d3c0c9252fe9ba48419893dc0a14d2a8f43 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Tue, 27 Aug 2024 07:51:04 -0700 Subject: [PATCH] Explicitly test mode setting Closes #178. --- tests/database_test.py | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/tests/database_test.py b/tests/database_test.py index 0974b74..73279dc 100644 --- a/tests/database_test.py +++ b/tests/database_test.py @@ -6,6 +6,7 @@ import ipaddress import sys import unittest +from unittest.mock import patch, MagicMock sys.path.append("..") @@ -19,7 +20,7 @@ maxminddb.extension = None # type: ignore -class BaseTestReader(unittest.TestCase): +class TestReader(unittest.TestCase): def test_language_list(self) -> None: reader = geoip2.database.Reader( "tests/data/test-data/GeoIP2-Country-Test.mmdb", @@ -262,27 +263,13 @@ def test_context_manager(self) -> None: record = reader.country("81.2.69.160") self.assertEqual(record.traits.ip_address, "81.2.69.160") + @patch("maxminddb.open_database") + def test_modes(self, mock_open) -> None: + mock_open.return_value = MagicMock() -@unittest.skipUnless(maxminddb.extension, "No C extension module found. Skipping tests") -class TestExtensionReader(BaseTestReader): - mode = geoip2.database.MODE_MMAP_EXT - - -class TestMMAPReader(BaseTestReader): - mode = geoip2.database.MODE_MMAP - - -class TestFileReader(BaseTestReader): - mode = geoip2.database.MODE_FILE - - -class TestMemoryReader(BaseTestReader): - mode = geoip2.database.MODE_MEMORY - - -class TestFDReader(unittest.TestCase): - mode = geoip2.database.MODE_FD - - -class TestAutoReader(BaseTestReader): - mode = geoip2.database.MODE_AUTO + path = "tests/data/test-data/GeoIP2-Country-Test.mmdb" + with geoip2.database.Reader( + path, + mode=geoip2.database.MODE_MMAP_EXT, + ) as reader: + mock_open.assert_called_once_with(path, geoip2.database.MODE_MMAP_EXT)