def test_ipv6_address_in_ipv4_database(self):
     reader = Reader('tests/data/test-data/MaxMind-DB-test-ipv4-24.mmdb')
     with self.assertRaisesRegex(ValueError,
                                 'Error looking up 2001::. '
                                 'You attempted to look up an IPv6 address '
                                 'in an IPv4-only database'):
         reader.get('2001::')
    def test_decoder(self):
        reader = Reader('tests/data/test-data/MaxMind-DB-test-decoder.mmdb')
        record = reader.get('::1.1.1.0')

        self.assertEqual(record['array'], [1, 2, 3])
        self.assertEqual(record['boolean'], True)
        self.assertEqual(record['bytes'], bytearray(b'\x00\x00\x00*'))
        self.assertEqual(record['double'], 42.123456)
        self.assertAlmostEqual(record['float'], 1.1)
        self.assertEqual(record['int32'], -268435456)
        self.assertEqual(
            {
                'mapX': {
                    'arrayX': [7, 8, 9],
                    'utf8_stringX': 'hello'
                },
            },
            record['map']
        )

        self.assertEqual(record['uint16'], 100)
        self.assertEqual(record['uint32'], 268435456)
        self.assertEqual(record['uint64'], 1152921504606846976)
        self.assertEqual(record['utf8_string'], 'unicode! ☯ - ♫')

        self.assertEqual(
            1329227995784915872903807060280344576,
            record['uint128']
        )
 def test_closed_metadata(self):
     reader = Reader(
         'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
     )
     reader.close()
     self.assertRaisesRegex(IOError,
                            'Attempt to read from a closed MaxMind DB.',
                            reader.metadata)
 def test_double_close(self):
     reader = Reader(
         'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
     )
     reader.close()
     self.assertRaisesRegex(IOError,
                            'Attempt to close a closed MaxMind DB.',
                            reader.close)
 def test_broken_database(self):
     reader = Reader('tests/data/test-data/'
                     'GeoIP2-City-Test-Broken-Double-Format.mmdb')
     with self.assertRaisesRegex(InvalidDatabaseError,
                                 "The MaxMind DB file's data "
                                 "section contains bad data \(unknown data "
                                 "type or corrupt data\)"
                                 ):
         reader.get('2001:220::')
 def test_closed_get(self):
     reader = Reader(
         'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
     )
     reader.close()
     self.assertRaisesRegex(ValueError,
                            'Attempt to read from a closed MaxMind DB.'
                            '|closed or invalid',
                            reader.get, ('1.1.1.1'))
 def test_metadata_unknown_attribute(self):
     reader = Reader(
         'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
     )
     metadata = reader.metadata()
     with self.assertRaisesRegex(AttributeError,
                                 "'Metadata' object has no "
                                 "attribute 'blah'"):
         metadata.blah
    def test_closed_metadata(self):
        reader = Reader(
            'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
        )
        reader.close()

        # The primary purpose of this is to ensure the extension doesn't
        # segfault
        try:
            metadata = reader.metadata()
        except IOError as ex:
            self.assertEqual('Attempt to read from a closed MaxMind DB.',
                             str(ex), 'extension throws exception')
        else:
            self.assertIsNotNone(
                metadata, 'pure Python implementation returns value')
    def test_no_ipv4_search_tree(self):
        reader = Reader(
            'tests/data/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb')

        self.assertEqual(reader.get('1.1.1.1'), '::0/64')
        self.assertEqual(reader.get('192.1.1.1'), '::0/64')
 def test_double_close(self):
     reader = Reader(
         'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
     )
     self.assertIsNone(
         reader.close(), 'Double close does not throw an exception')
 def test_close(self):
     reader = Reader(
         'tests/data/test-data/MaxMind-DB-test-decoder.mmdb'
     )
     reader.close()