def test_matcher_no_result(user, origins, destinations): airports_by_user = { user: { 'origins': [each.code for each in origins], 'destinations': [each.code for each in destinations] } } matched_result = match([], airports_by_user) assert matched_result == {}
def test_matcher_single_hit(user, origins, destinations): airports_by_user = { user: { 'origins': [each.code for each in origins], 'destinations': [each.code for each in destinations] } } result = Result(origins[0].code, destinations[0].code, datetime.date(2019, 10, 10), business_seats=6, plus_seats=0) matched_result = match([result], airports_by_user) assert matched_result == {user: [result]}
def results_to_email(subject, results): """ :type subject: str :type results: list[sas_api.requester.Result] """ if not results: return def all_active_users(): return User.objects.filter(is_active=True) # TODO: Turn this around to {('ORG', 'DST): [user1, user2, ..], ('ORG2', 'DST2): [user3, user4, ...]}? _org_dst_by_user = org_dst_by_user(all_active_users()) results_by_user = match(results, _org_dst_by_user) for user, filtered_results in results_by_user.items(): message = render_to_string('email_template.html', { 'results': filtered_results, 'unsubscribe_url': unsubscribe_url(user) }) send_email(user.email, subject, message) print('Results e-mailed to {}'.format(user.email))
def test_matcher_mutiple_users_same_hit(user, origins, destinations): another_user = User.objects.create(email='*****@*****.**', username='******') airports_by_user = { user: { 'origins': [each.code for each in origins], 'destinations': [each.code for each in destinations] }, another_user: { 'origins': [each.code for each in origins], 'destinations': [each.code for each in destinations] }, } result = Result(origins[0].code, destinations[0].code, datetime.date(2019, 10, 10), business_seats=6, plus_seats=0) matched_result = match([result], airports_by_user) assert matched_result == {user: [result], another_user: [result]}
def test_matcher_return_trip(user, origins, destinations): airports_by_user = { user: { 'origins': [each.code for each in origins], 'destinations': [each.code for each in destinations] } } result_out = Result(origins[0].code, destinations[0].code, datetime.date(2019, 10, 10), business_seats=6, plus_seats=0) result_return = Result(destinations[0].code, origins[0].code, datetime.date(2019, 10, 10), business_seats=4, plus_seats=4) matched_result = match([result_out, result_return], airports_by_user) assert len(matched_result[user]) == 2 for expected in [result_out, result_return]: assert expected in matched_result[user]
def test_matcher_mutiple_users_different_hit(user, origins, destinations): another_user = User.objects.create(email='*****@*****.**', username='******') xxx = Airport.objects.create(code='XXX', currently_fetching=True, destination=False) yyy = Airport.objects.create(code='YYY', currently_fetching=True, destination=True) airports_by_user = { user: { 'origins': [each.code for each in origins], 'destinations': [each.code for each in destinations] }, another_user: { 'origins': [xxx.code], 'destinations': [yyy.code] }, } result = Result(origins[0].code, destinations[0].code, datetime.date(2019, 10, 10), business_seats=6, plus_seats=0) result2 = Result(xxx.code, yyy.code, datetime.date(2019, 10, 10), business_seats=2, plus_seats=0) matched_result = match([result, result2], airports_by_user) assert matched_result == {user: [result], another_user: [result2]}