def test_has_valid_address_returns_proper_value(address): # GIVEN an Entry instance with the test address entry = Entry(address, True, "30/01/20 17:00:00") # WHEN calling has_valid_address # THEN it returns the proper value expected = __VALID_IP_ADDRESS[address] assert entry.has_valid_address() is expected,\ f"Address {address} should {'' if expected else 'not '}be valid."
def test_entry_with_bigger_ip_is_bigger(): # GIVEN and entry with ip 192.168.1.1 entry1 = Entry("192.168.1.1", True, "30/01/20 17:02:09") # and one with 192.168.2.1 entry2 = Entry("192.168.2.1", True, "30/01/20 17:02:09") # WHEN comparing them # THEN the one with 192.168.2.1 is bigger assert entry2 > entry1
def test_entry_with_same_ip_is_smaller_or_equal(): # GIVEN and entry with ip 192.168.1.1 entry1 = Entry("192.168.1.1", True, "30/01/20 17:02:09") # and one with 192.168.1.1 entry2 = Entry("192.168.1.1", True, "30/01/20 17:02:09") # WHEN comparing them # THEN <= works both ways assert entry1 <= entry2 <= entry1
def test_entry_with_smaller_ip_is_smaller_or_equal(): # GIVEN and entry with ip 192.167.1.1 entry1 = Entry("192.167.1.1", True, "30/01/20 17:02:09") # and one with 192.168.1.1 entry2 = Entry("192.168.1.1", True, "30/01/20 17:02:09") # WHEN comparing them # THEN the one with 192.167.1.11 is smaller or equal assert entry1 <= entry2
def test_comparision_operators_work_between_equal_entries(): # GIVEN and entries with same ip entry1 = Entry("192.168.1.1", True, "30/01/20 17:02:09") entry2 = Entry("192.168.1.1", False, "28/02/20 17:04:19") # WHEN comparing them # THEN all comparison operators work assert entry1 <= entry2 <= entry1 assert entry1 >= entry2 >= entry1 assert not entry1 < entry2 assert not entry2 < entry1 assert not entry1 > entry2 assert not entry2 > entry1
def test_comparision_operators_work_between_different_entries(): # GIVEN and entries with different IPs entry1 = Entry("191.168.1.1", True, "30/01/20 17:02:09") entry2 = Entry("192.168.1.1", True, "30/01/19 17:02:09") # WHEN comparing them # THEN all comparison operators work assert entry1 <= entry2 assert not entry2 <= entry1 assert not entry1 >= entry2 assert entry2 >= entry1 assert entry1 < entry2 assert not entry2 < entry1 assert not entry1 > entry2 assert entry2 > entry1
def __init__(self, ipv4_network, raw_entry_list): """ Constructor for NetworkCollection data structure. self.ipv4_network -> ipaddress.IPv4Network self.entries -> list(Entry) """ try: self.ipv4_network = IPv4Network(ipv4_network) except Exception: raise TypeError(f"Invalid 'ipv4_network' filed for " f"NetworkCollection - {ipv4_network} ") self.entries = [] if isinstance(raw_entry_list, list): for entry in raw_entry_list: if isinstance(entry, dict): try: self.entries.append( Entry(entry.get("address"), entry.get("available"), entry.get("last_used"))) except Exception as e: logger.error(str(e)) else: raise TypeError(f"Invalid 'raw_entry_list' argument for " f"NetworkCollection. Expected 'list', " f"got {type(raw_entry_list)}") logger.info(f"Removing invalid records for network " f"{self.ipv4_network} ...") self.remove_invalid_records() logger.info(f"Sorting records for network {self.ipv4_network} ...") self.sort_records()
def __init__(self, ipv4_network, raw_entry_list): """ Constructor for NetworkCollection data structure. self.ipv4_network -> ipaddress.IPv4Network self.entries -> list(Entry) """ self.ipv4_network = ipaddress.ip_network(ipv4_network) self.entries = [] for i in raw_entry_list: self.entries.append(Entry(i['address'],i['available'], i['last_used']))
def __init__(self, ipv4_network, raw_entry_list): """ Constructor for NetworkCollection data structure. self.ipv4_network -> ipaddress.IPv4Network self.entries -> list(Entry) """ self.ipv4_network = ip_network(ipv4_network) self.entries = [ Entry(raw_entry["address"], raw_entry["available"], raw_entry["last_used"]) for raw_entry in raw_entry_list ]
def test_entry_inits_last_used_field(): # WHEN creating an Entry instance entry = Entry("192.168.1.1", True, "30/01/20 17:02:09") # THEN entry last_used is initialized assert isinstance(entry.last_used, datetime) assert entry.last_used.year == 2020 assert entry.last_used.month == 1 assert entry.last_used.day == 30 assert entry.last_used.hour == 17 assert entry.last_used.minute == 2 assert entry.last_used.second == 9
def test_entry_inits_from_json_example(response_json_example): # GIVEN some data from th example json some_entries_data = response_json_example["Berlin"]["BER-1"]["networks"][ "192.168.0.0/24"] # WHEN creating entry instances based on these entries entries = [ Entry(entry_data["address"], entry_data["available"], entry_data["last_used"]) for entry_data in some_entries_data ] # THEN all entries are valid for entry in entries: assert isinstance(entry.address, str) assert isinstance(entry.available, bool) assert isinstance(entry.last_used, datetime)
def test_wrong_format(self): with self.assertRaises(ValueError): Entry('192.168.1.1', False, '90/01/20 18:30:30')
def test_init(self): self.assertIsInstance(Entry('192.168.1.1', False, '30/01/20 18:30:30'), Entry)
def test_entry_inits_available_field(): # WHEN creating an Entry instance entry = Entry("192.168.1.1", True, "30/01/20 17:00:00") # THEN entry availabe is initialized assert entry.available is True
def test_entry_ordering(self): """ Tests that the ordering operator works correctly. """ e1 = Entry("192.168.2.1", False, "30/01/20 17:00:00") e2 = Entry("192.168.11.1", False, "30/01/20 18:00:00") self.assertTrue(e1 < e2)
def test_entry_inits_address(): # WHEN creating Entry instance entry = Entry("192.168.1.1", True, "30/01/20 17:00:00") # THEN entry address is initialized assert entry.address == "192.168.1.1"
def test_ip_decimal_value(self): print("Start ip_to_decimal Test...\n") entry = Entry('192.168.203.20', True, '30/01/20 17:00:00') self.assertEqual(entry.ip_to_decimal(), 3232287508)