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_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_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_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')