def test_get_ip_lookup_cache_invalid_syntax(_cache_file): with _cache_file.open(mode="w", encoding="utf-8") as f: f.write(u"{...") ip_lookup_cache = ip_lookup._get_ip_lookup_cache() assert ip_lookup_cache == {}
def test_get_ip_lookup_cache_not_existing(_cache_file): if _cache_file.exists(): _cache_file.unlink() ip_lookup_cache = ip_lookup._get_ip_lookup_cache() assert ip_lookup_cache == {}
def test_get_ip_lookup_cache_existing(_cache_file): cache_id1 = "host1", 4 with _cache_file.open(mode="w", encoding="utf-8") as f: f.write(u"%r" % {cache_id1: "1"}) ip_lookup_cache = ip_lookup._get_ip_lookup_cache() assert ip_lookup_cache == {cache_id1: "1"}
def test_get_ip_lookup_cache_existing(_cache_file): cache_id1 = "host1", socket.AF_INET with _cache_file.open(mode="w", encoding="utf-8") as f: f.write(u"%r" % {ip_lookup.serialize_cache_id(cache_id1): "1"}) ip_lookup_cache = ip_lookup._get_ip_lookup_cache() assert ip_lookup_cache == {cache_id1: "1"}
def test_ip_lookup_cache_update_without_persistence(_cache_file): cache_id1 = "host1", 4 ip_lookup_cache = ip_lookup._get_ip_lookup_cache() ip_lookup_cache.persist_on_update = False ip_lookup_cache.update_cache(cache_id1, "127.0.0.1") assert ip_lookup_cache[cache_id1] == "127.0.0.1" assert not _cache_file.exists()
def test_update_ip_lookup_cache_empty_file(_cache_file): cache_id = "host1", 4 ip_lookup_cache = ip_lookup._get_ip_lookup_cache() ip_lookup_cache.update_cache(cache_id, "127.0.0.1") cache = ip_lookup._load_ip_lookup_cache(lock=False) assert cache[cache_id] == "127.0.0.1" cache = ip_lookup._load_ip_lookup_cache(lock=False) assert cache[cache_id] == "127.0.0.1"
def test_update_ip_lookup_cache_extend_existing_file(_cache_file): cache_id1 = "host1", 4 cache_id2 = "host2", 4 ip_lookup_cache = ip_lookup._get_ip_lookup_cache() ip_lookup_cache.update_cache(cache_id1, "127.0.0.1") ip_lookup_cache.update_cache(cache_id2, "127.0.0.2") cache = ip_lookup._load_ip_lookup_cache(lock=False) assert cache[cache_id1] == "127.0.0.1" assert cache[cache_id2] == "127.0.0.2"
def test_clear_ip_lookup_cache(_cache_file): with _cache_file.open(mode="w", encoding="utf-8") as f: f.write(u"%r" % {("host1", 4): "127.0.0.1"}) ip_lookup_cache = ip_lookup._get_ip_lookup_cache() assert ip_lookup_cache[("host1", 4)] == "127.0.0.1" ip_lookup._clear_ip_lookup_cache(ip_lookup_cache) assert len(ip_lookup_cache) == 0 assert not _cache_file.exists()
def test_update_ip_lookup_cache_update_existing_entry(_cache_file): cache_id1 = "host1", 4 cache_id2 = "host2", 4 with _cache_file.open(mode="w", encoding="utf-8") as f: f.write(u"%r" % {cache_id1: "1", cache_id2: "2"}) ip_lookup_cache = ip_lookup._get_ip_lookup_cache() ip_lookup_cache.update_cache(cache_id1, "127.0.0.1") cache = ip_lookup._load_ip_lookup_cache(lock=False) assert cache[cache_id1] == "127.0.0.1" assert cache[cache_id2] == "2"
def test_clear_ip_lookup_cache(_cache_file): with _cache_file.open(mode="w", encoding="utf-8") as f: f.write(u"%r" % { ip_lookup.serialize_cache_id(("host1", socket.AF_INET)): "127.0.0.1" }) ip_lookup_cache = ip_lookup._get_ip_lookup_cache() assert ip_lookup_cache[("host1", socket.AF_INET)] == "127.0.0.1" ip_lookup_cache.clear() assert len(ip_lookup_cache) == 0 assert not _cache_file.exists()
def test_update_ip_lookup_cache_update_existing_entry(_cache_file): cache_id1 = "host1", socket.AF_INET cache_id2 = "host2", socket.AF_INET with _cache_file.open(mode="w", encoding="utf-8") as f: f.write( u"%r" % { ip_lookup.serialize_cache_id(cache_id1): "1", ip_lookup.serialize_cache_id(cache_id2): "2", }) ip_lookup_cache = ip_lookup._get_ip_lookup_cache() ip_lookup_cache.update_cache(cache_id1, "127.0.0.1") cache = ip_lookup._load_ip_lookup_cache(lock=False) assert cache[cache_id1] == "127.0.0.1" assert cache[cache_id2] == "2"
def test_repr(): ip_lookup_cache = ip_lookup._get_ip_lookup_cache() assert isinstance(repr(ip_lookup_cache), str)