def test_task1_2_with_diff_month() -> None: customers = create_customers(test_dict_medium) customers[0].new_month(1, 2018) customers[1].new_month(1, 2018) process_event_history(test_dict_medium, customers) customer = customers[0] history = customer.get_call_history('222-2222') assert len(history) == 1 assert len(history[0].incoming_calls) == 0 assert len(history[0].outgoing_calls) == 3, """222-2222 made calls in 3 diff months""" all_records = history[0].get_monthly_history() assert len(all_records[0]) == 4 assert len(all_records[1]) == 0 jan_records = history[0].get_monthly_history(1, 2018) assert len(jan_records[0]) == 1 assert len(jan_records[1]) == 0 feb_records = history[0].get_monthly_history(2, 2018) assert len(feb_records[0]) == 2 assert len(feb_records[1]) == 0 mar_records = history[0].get_monthly_history(3, 2018) assert len(mar_records[0]) == 1 assert len(mar_records[1]) == 0
def test_monthly_bill() -> None: log = create_log() customers = create_customers(log) process_event_history(log, customers) for cust in customers: if cust.get_id() == 1200: assert cust.generate_bill(11, 2018)[1] == 320 + 320 assert cust.generate_bill(12, 2018)[1] == 20 + 20 assert cust.generate_bill(1, 2019)[1] == 25 + 20 elif cust.get_id == 1020: assert cust.generate_bill(11, 2018)[1] == 95.5 + 50.7 assert cust.generate_bill(12, 2018)[1] == 141 + 50.7 assert cust.generate_bill(1, 2019)[1] == 186.5 + 50.7 elif cust.get_id == 1002: assert cust.generate_bill(11, 2018)[1] == 2*-99.65 assert cust.generate_bill(12, 2018)[1] == 2*-99.3 assert cust.generate_bill(1, 2019)[1] == 2*-98.95 elif cust.get_id == 2110: assert cust.generate_bill(11, 2018)[1] == 320*2 assert cust.generate_bill(12, 2018)[1] == 20*2 assert cust.generate_bill(1, 2019)[1] == 20*2 elif cust.get_id == 2101: assert cust.generate_bill(11, 2018)[1] == 320 assert cust.generate_bill(12, 2018)[1] == 20 + 25 assert cust.generate_bill(1, 2019)[1] == 20 + 37.5 elif cust.get_id == 2011: assert cust.generate_bill(11, 2018)[1] == 50.7 - 12.5 assert cust.generate_bill(12, 2018)[1] == 50.7 + 12.5 assert cust.generate_bill(1, 2019)[1] == 50.7 - 10 elif cust.get_id == 3111: assert cust.generate_bill(11, 2018)[1] == 324 + 50.7 - 22.5 assert cust.generate_bill(12, 2018)[1] == 24 + 50.7 - 21.25 assert cust.generate_bill(1, 2019)[1] == 24 + 50.7 - 17.5
def test_events() -> None: """ Test the ability to make calls, and ensure that the CallHistory objects are populated """ customers = create_customers(test_dict) customers[0].new_month(1, 2018) process_event_history(test_dict, customers) # Check the bill has been computed correctly bill = customers[0].generate_bill(1, 2018) assert bill[0] == 5555 assert bill[1] == pytest.approx(-29.925) assert bill[2][0]['total'] == pytest.approx(20) assert bill[2][0]['free_mins'] == 1 assert bill[2][1]['total'] == pytest.approx(50.05) assert bill[2][1]['billed_mins'] == 1 assert bill[2][2]['total'] == pytest.approx(-99.975) assert bill[2][2]['billed_mins'] == 1 # Check the CallHistory objects are populated history = customers[0].get_call_history('867-5309') assert len(history) == 1 assert len(history[0].incoming_calls) == 1 assert len(history[0].outgoing_calls) == 1 history = customers[0].get_call_history() assert len(history) == 3 assert len(history[0].incoming_calls) == 1 assert len(history[0].outgoing_calls) == 1
def test_mtm_contract() -> None: log = create_log() customers = create_customers(log) process_event_history(log, customers) for cust in customers: if cust.get_id() == 1020: for p_line in cust._phone_lines: if p_line.number == '010-1020': #Test different number of cals bill_summary = p_line.get_bill(11, 2018) assert bill_summary['type'] == 'MTM' assert bill_summary['billed_mins'] == 910 assert bill_summary['min_rate'] == 0.05 assert bill_summary['total'] == 95.5 bill_summary = p_line.get_bill(12, 2018) assert bill_summary['type'] == 'MTM' assert bill_summary['billed_mins'] == 1820 assert bill_summary['min_rate'] == 0.05 assert bill_summary['total'] == 141 bill_summary = p_line.get_bill(1, 2019) assert bill_summary['type'] == 'MTM' assert bill_summary['billed_mins'] == 2730 assert bill_summary['min_rate'] == 0.05 assert bill_summary['total'] == 186.5
def test_all_contract_cancel() -> None: """ Test whether one number of a customer for prepiad is being billed correctly """ # loading up the large data input_dictionary = import_data() customers = create_customers(input_dictionary) process_event_history(input_dictionary, customers) for cust in customers: if cust.get_id() == 3852: customer = cust break bill = customer.generate_bill(8, 2018) print(bill) number = customer.get_phone_numbers() print(number) # testing if prepaid contract returns 0 when balance < 0 returned = customer.cancel_phone_line(number[3]) assert returned == 0 returned = customer.cancel_phone_line(number[4]) assert returned == 0 # testing mtm contract returned = customer.cancel_phone_line(number[2]) assert returned == 50.2 # testing term contract returned = customer.cancel_phone_line(number[0]) assert returned == 20
def test_events_pure_calls() -> None: log = create_pure_log() customers = create_customers(log) process_event_history(log, customers) for cust in customers: if cust.get_id() == 3111: assert len(cust.get_history()[0]) == 126 else: assert len(cust.get_history()[0]) == 84 for p_line in cust._phone_lines: m1_history = p_line.callhistory.get_monthly_history(11, 2018) assert len(m1_history[0]) == 14 assert len(m1_history[1]) == 14 for out_call in m1_history[0]: assert out_call.src_loc == loc[int(out_call.src_number[4:])] for in_call in m1_history[1]: assert in_call.dst_loc == loc[int(in_call.dst_number[4:])] m2_history = p_line.callhistory.get_monthly_history(12, 2018) assert len(m2_history[0]) == 14 assert len(m2_history[1]) == 14 for out_call in m2_history[0]: assert out_call.src_loc == loc[int(out_call.src_number[4:])] for in_call in m2_history[1]: assert in_call.dst_loc == loc[int(in_call.dst_number[4:])] m3_history = p_line.callhistory.get_monthly_history(1, 2019) assert len(m3_history[0]) == 14 assert len(m3_history[1]) == 14 for out_call in m3_history[0]: assert out_call.src_loc == loc[int(out_call.src_number[4:])] for in_call in m3_history[1]: assert in_call.dst_loc == loc[int(in_call.dst_number[4:])]
def test_duration_filter() -> None: log = create_task4_log() customers = create_customers(log) process_event_history(log, customers) all_calls = [] for c in customers: hist = c.get_history() all_calls.extend(hist[0]) fil = DurationFilter() invalid_inputs = ['', 'LG40', 'l50', 'g65', '50', 'sdklfjeind', ' '] for input in invalid_inputs: filtered = fil.apply(customers, all_calls, input) assert filtered == all_calls filtered = fil.apply(customers, all_calls, 'L60') for call in filtered: assert call.duration < 60 filtered = fil.apply(customers, filtered, 'G60') assert filtered == [] filtered = fil.apply(customers, all_calls, 'G5400') for call in filtered: assert call.duration > 5400
def test_filters() -> None: """ Test the functionality of the filters. We are only giving you a couple of tests here, you should expand both the dataset and the tests for the different types of applicable filters """ customers = create_customers(test_dict) process_event_history(test_dict, customers) # Populate the list of calls: calls = [] hist = customers[0].get_history() # only consider outgoing calls, we don't want to duplicate calls in the test calls.extend(hist[0]) # The different filters we are testing filters = [DurationFilter(), CustomerFilter(), ResetFilter()] # These are the inputs to each of the above filters in order. # Each list is a test for this input to the filter filter_strings = [["L50", "G10", "L0", "50", "AA", ""], ["5555", "1111", "9999", "aaaaaaaa", ""], ["rrrr", ""]] # These are the expected outputs from the above filter application # onto the full list of calls expected_return_lengths = [[1, 2, 0, 3, 3, 3], [3, 3, 3, 3, 3], [3, 3]] for i in range(len(filters)): for j in range(len(filter_strings[i])): result = filters[i].apply(customers, calls, filter_strings[i][j]) assert len(result) == expected_return_lengths[i][j]
def test_location_filter_with_large_data() -> None: """ Test the functionality of the location filters. We are only giving you a couple of tests here, you should expand both the dataset and the tests for the different types of applicable filters """ # loading up the large data input_dictionary = import_data() customers = create_customers(input_dictionary) process_event_history(input_dictionary, customers) # Populate the list of calls: calls = [] for cust in customers: hist = cust.get_history() # only look at outgoing calls, we don't want to duplicate calls in test calls.extend(hist[0]) # The different filters we are testing filters = [CustomerFilter(), LocationFilter()] # These are the inputs to each of the above filters in order. # Each list is a test for this input to the filter filter_strings = [ # contents: non-existent id, valid id, valid id ["5555", "5524", "9210"], # contents: loc of one call, max and min, one cord out, letters, valid loc but no calls [ "-79.54717029563305, 43.58020061333403, -79.54717029563303, 43.58020061333405", # location of one call "-79.697878, 43.576959, -79.196382, 43.799568", # entire map "-80.697877, 43.576960, -79.196383, 43.799567", # one coordinate in not within range "hellolol, erferer, fefergerger, ferereeev", # isalpaha == true "-79.697878, 43.6882635, -79.196382, 43.799568", # half the map (this one took me hours to count) "-79.54717029563305,43.58020061333403,-79.54717029563303,43.58020061333405", # location of one call but no spaces "-79.54717029563305 , 43.58020061333403 , -79.54717029563303, 43.58020061333405", # ^ spaces "-79.196382, 43.799568, -79.697878, 43.576959", # both cross "-79.296382, 43.576959, -79.597878, 43.799568", # x coords cross "-79.697878, 43.576959, -79.196382, 43.499568", # y coords cross "-80.697877, 69.576960, -89.196383, 69.799567", # all coords not within range "hellolol, erferer, fefergergerferereeev", # alpha + nums "#@%#@%#@%,#%@#%@#%,#%#@%#@%#@$%#@%", # symbols "", # empty "SDF(*@$)(*&#!)(*&#HFLKDSJF:LDSJFLKJDSF", # no commas " " # just spaces....... ] ] # These are the expected outputs from the above filter application # onto the full list of calls expected_return_lengths = [[1000, 45, 33], [ 1, 1000, 1000, 1000, 755, 1000, 1, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 ]] for i in range(len(filters)): for j in range(len(filter_strings[i])): result = filters[i].apply(customers, calls, filter_strings[i][j]) assert len(result) == expected_return_lengths[i][j]
def test_process_event_history() -> None: """Test method process_event_history of application.py with given dictionary.MY THINGGGGGGGGG. """ process_event_history(test_dict, [Customer(test_dict['customers'][0]['id'])]) for customers in [Customer(test_dict['customers'][0]['id'])]: print(customers.get_history())
def test_task1_2_with_all() -> None: input_dictionary = import_data() customers = create_customers(input_dictionary) process_event_history(input_dictionary, customers) history = [] expect = [(27, 26), (13, 11), (39, 36), (18, 16), (3, 8), (42, 31), (25, 23), (16, 19), (37, 40), (27, 28), (7, 12), (19, 14), (29, 17), (27, 19), (31, 29), (4, 2), (10, 19), (15, 12), (3, 6), (10, 10), (4, 2), (3, 5), (20, 22), (22, 20), (22, 37), (30, 25), (27, 26), (16, 19), (30, 45), (19, 29), (8, 7), (44, 21), (16, 23), (37, 33), (15, 18), (28, 33), (4, 4), (30, 24), (21, 25), (28, 32), (25, 20), (13, 14), (13, 18), (25, 22), (7, 10), (21, 23), (26, 33), (11, 11), (12, 10), (21, 11)] for customer in customers: v = customer.get_history() history.append((len(v[0]), len(v[1]))) for i in range(len(history)): assert history[i] == expect[i], "Outgoing and incoming total for customer " + str(customer.get_id()) + " is wrong"
def test_prepaid_contract() -> None: log = create_log() customers = create_customers(log) process_event_history(log, customers) for cust in customers: if cust.get_id() == 2101: for p_line in cust._phone_lines: if p_line.number == '001-2101': #Test different starting balance bill_summary = p_line.get_bill(11, 2018) assert bill_summary['type'] == 'PREPAID' assert bill_summary['billed_mins'] == 1000 assert bill_summary['min_rate'] == 0.025 assert bill_summary['total'] == 0 bill_summary = p_line.get_bill(12, 2018) assert bill_summary['type'] == 'PREPAID' assert bill_summary['billed_mins'] == 2000 assert bill_summary['min_rate'] == 0.025 assert bill_summary['total'] == 25 bill_summary = p_line.get_bill(1, 2019) assert bill_summary['type'] == 'PREPAID' assert bill_summary['billed_mins'] == 1500 assert bill_summary['min_rate'] == 0.025 assert bill_summary['total'] == 37.5 elif cust.get_id() == 3011: for p_line in cust._phone_lines: if p_line.number == '001-3111': #Test different starting balance bill_summary = p_line.get_bill(11, 2018) assert bill_summary['type'] == 'PREPAID' assert bill_summary['billed_mins'] == 100 assert bill_summary['min_rate'] == 0.025 assert bill_summary['total'] == -22.5 assert bill_summary['free_mins'] == 0 bill_summary = p_line.get_bill(12, 2019) assert bill_summary['type'] == 'PREPAID' assert bill_summary['billed_mins'] == 50 assert bill_summary['min_rate'] == 0.025 assert bill_summary['total'] == -21.25 assert bill_summary['free_mins'] == 0 bill_summary = p_line.get_bill(1, 2019) assert bill_summary['type'] == 'PREPAID' assert bill_summary['billed_mins'] == 150 assert bill_summary['min_rate'] == 0.025 assert bill_summary['total'] == -17.5 assert bill_summary['free_mins'] == 0
def test_term_contract_calculations() -> None: # ADDED FEB 15 2019 """ Tests to see if term contracts are correctly computed and there are no bugs concerning end dates, cancellations and deposits """ customers = create_customers(test_dict_for_term_contract) # customers[0].new_month(12, 2017) process_event_history(test_dict_for_term_contract, customers) # Check the bill has been computed correctly bill = customers[0].generate_bill(12, 2017) assert bill[0] == 5555 # assert bill[1] == pytest.approx(975) assert bill[2][0]['total'] == pytest.approx(325) assert bill[2][0]['free_mins'] == 100 assert bill[2][0]['billed_mins'] == 50 assert bill[2][1]['total'] == pytest.approx(325) assert bill[2][1]['free_mins'] == 100 assert bill[2][1]['billed_mins'] == 50 assert bill[2][2]['total'] == pytest.approx(325) assert bill[2][2]['free_mins'] == 100 assert bill[2][2]['billed_mins'] == 50 assert bill[1] == pytest.approx(975) bill = customers[0].generate_bill(3, 2018) assert bill[1] == pytest.approx(75) assert bill[2][0]['total'] == pytest.approx(25) assert bill[2][1]['total'] == pytest.approx(25) assert bill[2][2]['total'] == pytest.approx(25) bill = customers[0].generate_bill(6, 2019) assert bill[1] == pytest.approx(100) assert bill[2][0]['total'] == pytest.approx(20) assert bill[2][1]['total'] == pytest.approx(40) assert bill[2][2]['total'] == pytest.approx(40) bill = customers[0].generate_bill(7, 2019) assert bill[1] == pytest.approx(62) assert bill[2][0]['total'] == pytest.approx(20) assert bill[2][0]['free_mins'] == 0 assert bill[2][1]['total'] == pytest.approx(20) assert bill[2][2]['total'] == pytest.approx(22) assert bill[2][2]['free_mins'] == 100 assert bill[2][2]['billed_mins'] == 20 assert customers[0]._phone_lines[0].cancel_line() == pytest.approx(-280) assert customers[0]._phone_lines[1].cancel_line() == pytest.approx(-280) assert customers[0]._phone_lines[2].cancel_line() == pytest.approx(-278)
def test_call_history_all_nums_one_cust() -> None: """ Test whether choosing all phone number from one customer it gives the right amount of histories """ # loading up the large data input_dictionary = import_data() customers = create_customers(input_dictionary) process_event_history(input_dictionary, customers) total_calls = 0 # customer id 3852 for all their numbers for cust in customers: if cust.get_id() == 3852: hist = cust.get_history() total_calls = len(hist[0]) + len((hist[1])) assert total_calls == 75
def test_term_contract() -> None: log = create_log() customers = create_customers(log) process_event_history(log, customers) for cust in customers: if cust.get_id() == 1200: for p_line in cust._phone_lines: if p_line.number == '100-1200': #Test Free Min bill_summary = p_line.get_bill(11, 2018) assert bill_summary['type'] == 'TERM' assert bill_summary['fixed'] == 320.00 assert bill_summary['free_mins'] == 90 assert bill_summary['billed_mins'] == 0 assert bill_summary['min_rate'] == 0.1 assert bill_summary['total'] == 320 bill_summary = p_line.get_bill(12, 2018) assert bill_summary['fixed'] == 20.00 assert bill_summary['free_mins'] == 100 assert bill_summary['billed_mins'] == 0 assert bill_summary['total'] == 20.00 bill_summary = p_line.get_bill(1, 2019) assert bill_summary['type'] == 'TERM' assert bill_summary['fixed'] == 20.00 assert bill_summary['free_mins'] == 100 assert bill_summary['billed_mins'] == 50 assert bill_summary['min_rate'] == 0.1 assert bill_summary['total'] == 25.00 #Test Cancel After for line in cust._phone_lines: if line.number == '200-1200': assert cust.cancel_phone_line(line.number) == -280 break elif cust.get_id() == 2101: # Test Cancel On for line in cust._phone_lines: if line.number == '100-2101': assert cust.cancel_phone_line(line.number) == 20 break elif cust.get_id() == 3111: # Test Cancel Before for line in cust._phone_lines: if line.number == '100-3111': assert cust.cancel_phone_line(line.number) == 24 break
def test_combined_filters() -> None: log = create_task4_log() customers = create_customers(log) process_event_history(log, customers) all_calls = [] for c in customers: hist = c.get_history() all_calls.extend(hist[0]) fil = LocationFilter() filtered = fil.apply(customers, all_calls, f'{x2}, {y3}, {x2}, {y3}') fil = DurationFilter() filtered = fil.apply(customers, filtered, f'G{69 * 60 - 1}') filtered = fil.apply(customers, filtered, f'L{69 * 60 + 1}') assert len(filtered) == 3 count = 0 for call in filtered: assert call.src_number == '001-3111' if call.time.month == 1: count += 1 assert count == 2 fil = CustomerFilter() filtered = fil.apply(customers, all_calls, "1020") fil = DurationFilter() filtered = fil.apply(customers, filtered, f'G{10 * 60 - 1}') filtered = fil.apply(customers, filtered, f'L{130 * 60 + 1}') for call in filtered: assert 10 * 60 - 1 < call.duration < 130 * 60 + 1 print( f'src: {call.src_number}, dst: {call.dst_number}, dur: {call.duration}' ) assert len(filtered) == 23 + 11 + 6 fil = LocationFilter() filtered = fil.apply(customers, all_calls, f'{x3}, {y2}, {x3}, {y2}') #2101 fil = CustomerFilter() filtered = fil.apply(customers, filtered, "1002") assert len(filtered) == 3 * 2 * 3 for call in filtered: assert call.src_number[4:] == '1002' or call.src_number[4:] == '2101' assert fil.apply(customers, filtered, "3111") == [] fil = DurationFilter() assert fil.apply(customers, filtered, 'L60') == []
def test_cancel_prepaid_contract_with_credit() -> None: """ Test for the correct creation of Customer, PhoneLine, and Contract classes """ customers = create_customers(test_dict) customers[0].new_month(12, 2017) process_event_history(test_dict, customers) bill = customers[0].generate_bill(12, 2017) assert bill[2][2]['total'] == pytest.approx(-100) customers[0].new_month(1, 2018) bill = customers[0].generate_bill(1, 2018) assert bill[2][2]['total'] == pytest.approx(-99.975) customers[0].new_month(2, 2018) bill = customers[0].generate_bill(2, 2018) assert bill[2][2]['total'] == pytest.approx(-99.975) assert customers[0].cancel_phone_line('649-2568') == 0
def test_prepaid_contract() -> None: """ Test whether one number of a customer for prepiad is being billed correctly """ # loading up the large data input_dictionary = import_data() customers = create_customers(input_dictionary) process_event_history(input_dictionary, customers) for cust in customers: if cust.get_id() == 3852: customer = cust break bill = customer.generate_bill(1, 2018) assert bill[2][-1]['billed_mins'] == 0 assert bill[2][-1]['fixed'] == -100
def test_call_history_one_num_one_cust() -> None: """ Test whether choosing one phone number it gives the right amount of histories """ # loading up the large data input_dictionary = import_data() customers = create_customers(input_dictionary) process_event_history(input_dictionary, customers) # customer id 3852 for number 938-6680 for cust in customers: if cust.get_id() == 3852: hist = cust.get_call_history('938-6680') total_calls = 0 for i in hist[0].outgoing_calls: total_calls += len(hist[0].outgoing_calls[i]) for i in hist[0].incoming_calls: total_calls += len(hist[0].incoming_calls[i]) assert total_calls == 17
def test_task4() -> None: # Filtering input_dictionary = import_data() customers = create_customers(input_dictionary) process_event_history(input_dictionary, customers) # Populate the list of calls: calls = [] for customer in customers: hist = customer.get_history() calls.extend(hist[0]) # The different filters we are testing # You need write LocationFilter test yourself filters = [ DurationFilter(), CustomerFilter(), ResetFilter() ] # These are the inputs to each of the above filters in order. # Each list is a test for this input to the filter filter_strings = [ ["L50", "G10", "L0", "50", "AA", "", "L100"], ["5555", "1111", "6020", "7930", "3087", "5524", "abc", ""], ["rrrr", ""] ] # These are the expected outputs from the above filter application # onto the full list of calls expected_return_lengths = [ [122, 975, 0, 1000, 1000, 1000, 261], [1000, 1000, 59, 22, 22, 45, 1000, 1000], [1000, 1000] ] for i in range(len(filters)): for j in range(len(filter_strings[i])): result = filters[i].apply(customers, calls, filter_strings[i][j]) assert len(result) == expected_return_lengths[i][j], str(filters[i].__class__.__name__) + ", with keyword " + filter_strings[i][j] + " produce wrong result"
def test_task1_2_simple() -> None: # Make sure you complete Task 1 and 2 # Reading and Recording all calls customers = create_customers(test_dict_small) customers[0].new_month(1, 2018) process_event_history(test_dict_small, customers) # Check the CallHistory objects are populated history = customers[0].get_call_history('111-1111') assert len(history) == 1 assert len(history[0].incoming_calls) == 1, """Should only be one history object""" assert len(history[0].outgoing_calls) == 1 all_records = history[0].get_monthly_history() all_incomings = all_records[1] all_outgoings = all_records[0] assert len(all_incomings) == 1, """All incoming calls across all month""" assert len(all_outgoings) == 1, """All outgoing calls across all month""" # Check if a call's data is populated correctly x = all_incomings[0] assert x.duration == 50 assert x.time.year == 2018 assert x.time.month == 1 assert x.time.day == 1 assert x.src_loc == (-79.42848154284123, 43.641401675960374) assert x.dst_loc == (-79.52745693913239, 43.750338501653374) # check another phone number history = customers[0].get_call_history('333-3333') assert len(history) == 1 assert len(history[0].incoming_calls) == 1 assert len(history[0].outgoing_calls) == 1 all_records = history[0].get_monthly_history() all_incomings = all_records[1] all_outgoings = all_records[0] assert len(all_incomings) == 2, """All incoming calls across all month""" assert len(all_outgoings) == 1, """All outgoing calls across all month"""
def test_events_prepaid() -> None: """ Test the ability to make calls, and ensure that the CallHistory objects are populated """ customers = create_customers(test_dict2) customers[0].new_month(12, 2017) process_event_history(test_dict2, customers) bill = customers[0].generate_bill(12, 2017) assert bill[2][2]['total'] == pytest.approx(-100) bill = customers[0].generate_bill(1, 2018) assert bill[2][2]['total'] == pytest.approx(-75) bill = customers[0].generate_bill(2, 2018) assert bill[2][2]['total'] == pytest.approx(-50) bill = customers[0].generate_bill(3, 2018) assert bill[2][2]['total'] == pytest.approx(-25) bill = customers[0].generate_bill(4, 2018) assert bill[2][2]['total'] == pytest.approx(0) bill = customers[0].generate_bill(5, 2018) assert bill[2][2]['total'] == pytest.approx(-25) bill = customers[0].generate_bill(6, 2018) assert bill[2][2]['total'] == pytest.approx(-22.5) assert customers[0].cancel_phone_line('649-2568') == 0
def test_customer_filter() -> None: log = create_pure_log() customers = create_customers(log) process_event_history(log, customers) all_calls = [] for c in customers: hist = c.get_history() all_calls.extend(hist[0]) fil = CustomerFilter() invalid_inputs = [ '', 'dskljgdf', '69.69', 'd1200', 'L200', '-79.6, 43.3, -79.5, 43.4', '3690', ' ' ] for input in invalid_inputs: filtered = fil.apply(customers, all_calls, input) assert filtered == all_calls line_of_customer = ['100-2101', '001-2101'] filtered = fil.apply(customers, all_calls, '2101') for call in filtered: assert call.src_number in line_of_customer \ or call.dst_number in line_of_customer assert loc[2101] == call.src_loc or loc[2101] == call.dst_loc
assert fil.apply(customers, filtered, "3111") == [] fil = DurationFilter() assert fil.apply(customers, filtered, 'L60') == [] if __name__ == '__main__': pytest.main(['task4_tests.py']) v = Visualizer() print("Toronto map coordinates:") print(" Lower-left corner: -79.697878, 43.576959") print(" Upper-right corner: -79.196382, 43.799568") log = create_task4_log() customers = create_customers(log) process_event_history(log, customers) # ---------------------------------------------------------------------- # NOTE: You do not need to understand any of the implementation below, # to be able to solve this assignment. However, feel free to # read it anyway, just to get a sense of how the application runs. # ---------------------------------------------------------------------- # Gather all calls to be drawn on screen for filtering, but we only want # to plot each call only once, so only plot the outgoing calls to screen. # (Each call is registered as both an incoming and outgoing) all_calls = [] for c in customers: hist = c.get_history() all_calls.extend(hist[0]) print("\n-----------------------------------------")
'customers': [ {'lines': [ {'number': '867-5309', 'contract': 'term'}, {'number': '273-8255', 'contract': 'mtm'}, {'number': '649-2568', 'contract': 'prepaid'} ], 'id': 5555} ] } customers = create_customers(test_dict) customers[0].new_month(1, 2018) process_event_history(test_dict, customers) bill = customers[0].generate_bill(1, 2018) bill[0] bill[1] bill[2][0]['total'] bill[2][0]['free_mins'] bill[2][1]['total'] bill[2][1]['billed_mins'] bill[2][2]['total'] bill[2][2]['billed_mins'] # Check the CallHistory objects are populated history = customers[0].get_call_history('867-5309') len(history) len(history[0].incoming_calls)
def test_events() -> None: """ Test the ability to make calls, and ensure that the CallHistory objects are populated """ customers = create_customers(test_dict) customers[0].new_month(1, 2018) customers[1].new_month(1, 2018) process_event_history(test_dict, customers) # Check bills for each month for each customer cust1_bill1 = customers[0].generate_bill(1, 2018) cust1_bill2 = customers[0].generate_bill(2, 2018) cust1_bill3 = customers[0].generate_bill(3, 2018) cust1_bill4 = customers[0].generate_bill(4, 2018) cust2_bill1 = customers[1].generate_bill(1, 2018) cust2_bill2 = customers[1].generate_bill(2, 2018) cust2_bill3 = customers[1].generate_bill(3, 2018) cust2_bill4 = customers[1].generate_bill(4, 2018) assert cust1_bill1[0] == 6666 assert cust1_bill2[0] == 6666 assert cust1_bill3[0] == 6666 assert cust1_bill4[0] == 6666 assert cust2_bill1[0] == 7777 assert cust2_bill2[0] == 7777 assert cust2_bill3[0] == 7777 assert cust2_bill4[0] == 7777 assert cust1_bill1[1] == pytest.approx(-29.925) assert cust1_bill2[1] == pytest.approx(-29.925) assert cust1_bill3[1] == pytest.approx(-29.925) assert cust1_bill4[1] == pytest.approx(-29.925) assert cust2_bill1[1] == pytest.approx(-29.925) assert cust2_bill2[1] == pytest.approx(-29.925) assert cust2_bill3[1] == pytest.approx(-29.925) assert cust1_bill4[1] == pytest.approx(-29.925) assert cust1_bill1[2][0]['total'] == pytest.approx(20) assert cust1_bill2[2][0]['total'] == pytest.approx(20) assert cust1_bill3[2][0]['total'] == pytest.approx(20) assert cust1_bill4[2][0]['total'] == pytest.approx(20) assert cust2_bill1[2][0]['total'] == pytest.approx(20) assert cust2_bill2[2][0]['total'] == pytest.approx(20) assert cust2_bill3[2][0]['total'] == pytest.approx(20) assert cust2_bill4[2][0]['total'] == pytest.approx(20) assert cust1_bill1[2][0]['free_mins'] == 1 assert cust1_bill2[2][0]['free_mins'] == 1 assert cust1_bill3[2][0]['free_mins'] == 1 assert cust1_bill4[2][0]['free_mins'] == 1 assert cust2_bill1[2][0]['free_mins'] == 1 assert cust2_bill2[2][0]['free_mins'] == 1 assert cust2_bill3[2][0]['free_mins'] == 1 assert cust2_bill4[2][0]['free_mins'] == 1 assert cust1_bill1[2][1]['total'] == pytest.approx(50.05) assert cust1_bill2[2][1]['total'] == pytest.approx(50.05) assert cust1_bill3[2][1]['total'] == pytest.approx(50.05) assert cust1_bill4[2][1]['total'] == pytest.approx(50.05) assert cust2_bill1[2][1]['total'] == pytest.approx(50.05) assert cust2_bill2[2][1]['total'] == pytest.approx(50.05) assert cust2_bill3[2][1]['total'] == pytest.approx(50.05) assert cust2_bill4[2][1]['total'] == pytest.approx(50.05) assert cust1_bill1[2][1]['billed_mins'] == 1 assert cust1_bill2[2][1]['billed_mins'] == 1 assert cust1_bill3[2][1]['billed_mins'] == 1 assert cust1_bill4[2][1]['billed_mins'] == 1 assert cust2_bill1[2][1]['billed_mins'] == 1 assert cust2_bill2[2][1]['billed_mins'] == 1 assert cust2_bill3[2][1]['billed_mins'] == 1 assert cust1_bill4[2][1]['billed_mins'] == 1 assert cust1_bill1[2][2]['total'] == pytest.approx(-99.975) assert cust1_bill2[2][2]['total'] == pytest.approx(-99.975) assert cust1_bill3[2][2]['total'] == pytest.approx(-99.975) assert cust1_bill4[2][2]['total'] == pytest.approx(-99.975) assert cust2_bill1[2][2]['total'] == pytest.approx(-99.975) assert cust2_bill2[2][2]['total'] == pytest.approx(-99.975) assert cust2_bill3[2][2]['total'] == pytest.approx(-99.975) assert cust2_bill4[2][2]['total'] == pytest.approx(-99.975) assert bill1[2][2]['billed_mins'] == 1 # Check the CallHistory objects are populated history = customers[0].get_call_history('867-5309') assert len(history) == 1 assert len(history[0].incoming_calls) == 1 assert len(history[0].outgoing_calls) == 1 history = customers[0].get_call_history() assert len(history) == 3 assert len(history[0].incoming_calls) == 1 assert len(history[0].outgoing_calls) == 1
""" Tests for the CustomerFilter, DurationFilter, and LocationFilter filters in A1. """ import pytest from hypothesis import settings, given, example, strategies as st from filter import CustomerFilter, DurationFilter, LocationFilter from data import tiny_data from application import import_data, create_customers, process_event_history, \ find_customer_by_number LOG = import_data() # gets data from dataset.json CUSTOMERS = create_customers(LOG) # creates customers based on the data in LOG process_event_history(LOG, CUSTOMERS) # gives customers their event history CALL_LIST = [] # list of calls for c in CUSTOMERS: hist = c.get_history() CALL_LIST.extend(hist[0]) @given(s=st.text(alphabet=st.characters(min_codepoint=0x0000, max_codepoint=0x02AF), max_size=100)) def test_absolute_garbage(s: str) -> None: """ Test that none of the filters raise errors when absolute garbage input strings are passed in. """ print(s) cf = CustomerFilter() df = DurationFilter() lf = LocationFilter()
def test_location_filter() -> None: log = create_pure_log() customers = create_customers(log) process_event_history(log, customers) all_calls = [] for c in customers: hist = c.get_history() all_calls.extend(hist[0]) rx = (x2 - x1) / 4 ry = (y1 - y2) / 4 fil = LocationFilter() MIN_LONGITUDE = -79.697878 MAX_LONGITUDE = -79.196382 MIN_LATITUDE = 43.576959 MAX_LATITUDE = 43.799568 invalid_inputs = [ '', f'{MIN_LONGITUDE}, {MIN_LATITUDE},{MAX_LONGITUDE},{MAX_LATITUDE}', f'-79.698, {MIN_LATITUDE}, {MAX_LONGITUDE}, {MAX_LATITUDE}', f'{MIN_LONGITUDE}, 43.576, {MAX_LONGITUDE}, {MAX_LATITUDE}', f'{MIN_LONGITUDE}, {MIN_LATITUDE}, -79.195, {MAX_LATITUDE}', f'{MIN_LONGITUDE}, {MIN_LATITUDE}, {MAX_LONGITUDE}, 43.8', f'{MIN_LONGITUDE},{MIN_LATITUDE}, -79.54, {MAX_LATITUDE}', f'{MIN_LONGITUDE}, {MIN_LATITUDE}, {MAX_LATITUDE}', f'-79.6, 43.60, -79.2, 43.75, -79.5', 'klsjdfohg[we', ' ' ] for input in invalid_inputs: filtered = fil.apply(customers, all_calls, input) assert filtered == all_calls for key in loc.keys(): x = loc[key][0] y = loc[key][1] fil_string = f'{x - rx}, {y - ry}, {x + rx}, {y + ry}' filtered = fil.apply(customers, all_calls, fil_string) lines_in_area = [] if key == 3111: assert len(filtered) == (24 * 3 + 6) * 3 else: assert len(filtered) == 27 * 2 * 3 for cust in customers: if cust.get_id() == key: lines_in_area = cust.get_phone_numbers() break for call in filtered: assert call.src_number in lines_in_area \ or call.dst_number in lines_in_area assert loc[int(call.src_number[4:])] == call.src_loc \ or loc[int(call.dst_number[4:])] == call.dst_loc fil_string = f'{x1 - rx}, {y2 - ry}, {x1 + rx}, {y1 + ry}' filtered = fil.apply(customers, all_calls, fil_string) lines_in_area = ['100-1200', '200-1200', '100-2110', '010-2110'] assert len(filtered) == (11 * 4 * 2 + 12) * 3 for call in filtered: assert call.src_number in lines_in_area \ or call.dst_number in lines_in_area assert loc[int(call.src_number[4:])] == call.src_loc \ or loc[int(call.dst_number[4:])] == call.dst_loc fil_string = f'{x1}, {y2}, {x2}, {y1}' filtered = fil.apply(customers, all_calls, fil_string) lines_in_area = [ '100-1200', '200-1200', '100-2110', '010-2110', '010-1020', '020-1020', '010-2011', '001-2011' ] assert len(filtered) == (7 * 8 * 3) * 3 for call in filtered: assert call.src_number in lines_in_area \ or call.dst_number in lines_in_area assert loc[int(call.src_number[4:])] == call.src_loc \ or loc[int(call.dst_number[4:])] == call.dst_loc