Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only disallow a pair if they matched earlier for the same subscription #94

Merged
merged 1 commit into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions tests/match_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from yelp_beans.logic.subscription import get_specs_from_subscription
from yelp_beans.logic.subscription import store_specs_from_subscription
from yelp_beans.matching.pair_match import generate_pair_meetings
from yelp_beans.matching.match import generate_meetings
from yelp_beans.matching.pair_match import get_previous_pair_meetings
from yelp_beans.matching.pair_match import save_pair_meetings
from yelp_beans.models import Meeting
Expand Down Expand Up @@ -38,7 +38,7 @@ def test_generate_meetings_same_department(minimal_database, subscription):
user_list = [user1, user2]

_, specs = get_specs_from_subscription(subscription)
matches, unmatched = generate_pair_meetings(user_list, specs[0])
matches, unmatched = generate_meetings(user_list, specs[0])
assert len(unmatched) == 2
assert len(matches) == 0

Expand All @@ -63,7 +63,7 @@ def test_generate_meetings_with_history(minimal_database, subscription):
week_start, specs = get_specs_from_subscription(subscription)
store_specs_from_subscription(subscription.key, week_start, specs)

matches, unmatched = generate_pair_meetings(user_list, specs[0])
matches, unmatched = generate_meetings(user_list, specs[0])
assert len(matches) == 2
assert len(unmatched) == 0

Expand All @@ -73,7 +73,7 @@ def test_generate_meetings_with_history(minimal_database, subscription):
(user2.key.id(), user3.key.id()),
(user1.key.id(), user4.key.id()),
])
matches, unmatched = generate_pair_meetings(user_list, specs[0], meeting_history)
matches, unmatched = generate_meetings(user_list, specs[0], meeting_history)
assert len(matches) == 0
assert len(unmatched) == 4

Expand All @@ -82,7 +82,7 @@ def test_generate_meetings_with_history(minimal_database, subscription):
(user3.key.id(), user4.key.id()),
(user2.key.id(), user3.key.id()),
])
matches, unmatched = generate_pair_meetings(user_list, specs[0], meeting_history)
matches, unmatched = generate_meetings(user_list, specs[0], meeting_history)
assert len(matches) == 1
assert len(unmatched) == 2

Expand All @@ -98,7 +98,24 @@ def test_get_previous_meetings(minimal_database):
MeetingParticipant(meeting=meeting, user=user2).put()
MeetingParticipant(meeting=meeting, user=user1).put()

assert get_previous_pair_meetings() == set([(user1.id(), user2.id())])
assert get_previous_pair_meetings(subscription) == set([(user1.id(), user2.id())])


def test_get_previous_meetings_multi_subscription(minimal_database):
pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1)).put()
subscription1 = MeetingSubscription(title='all engineering weekly', datetime=[pref_1]).put()
subscription2 = MeetingSubscription(title='all sales weekly', datetime=[pref_1]).put()
user_pref1 = UserSubscriptionPreferences(preference=pref_1, subscription=subscription1).put()
user_pref2 = UserSubscriptionPreferences(preference=pref_1, subscription=subscription2).put()
user1 = User(email='[email protected]', metadata={'department': 'dept'}, subscription_preferences=[user_pref1, user_pref2]).put()
user2 = User(email='[email protected]', metadata={'department': 'dept2'}, subscription_preferences=[user_pref1, user_pref2]).put()
meeting_spec1 = MeetingSpec(meeting_subscription=subscription1, datetime=pref_1.get().datetime).put()
meeting = Meeting(meeting_spec=meeting_spec1, cancelled=False).put()
MeetingParticipant(meeting=meeting, user=user2).put()
MeetingParticipant(meeting=meeting, user=user1).put()

assert get_previous_pair_meetings(subscription1) == set([(user1.id(), user2.id())])
assert get_previous_pair_meetings(subscription2) == set([])


def test_get_previous_meetings_no_specs(database_no_specs):
Expand All @@ -112,7 +129,7 @@ def test_get_previous_meetings_no_specs(database_no_specs):
MeetingParticipant(meeting=meeting, user=user2).put()
MeetingParticipant(meeting=meeting, user=user1).put()

assert get_previous_pair_meetings() == set([])
assert get_previous_pair_meetings(subscription) == set([])


def test_generate_save_meetings(minimal_database, subscription):
Expand All @@ -126,7 +143,7 @@ def test_generate_save_meetings(minimal_database, subscription):
MeetingRequest(user=user1, meeting_spec=meeting_spec.key).put()
MeetingRequest(user=user2, meeting_spec=meeting_spec.key).put()

matches, unmatched = generate_pair_meetings([user1.get(), user2.get()], meeting_spec)
matches, unmatched = generate_meetings([user1.get(), user2.get()], meeting_spec)
save_pair_meetings(matches, meeting_spec)

assert unmatched == []
Expand Down Expand Up @@ -157,6 +174,6 @@ def test_no_re_matches(minimal_database):

previous_meetings = {pair for pair in itertools.combinations([user.key.id() for user in users], 2)}
previous_meetings = previous_meetings - {(users[0].key.id(), users[1].key.id())}
matches, unmatched = generate_pair_meetings(users, meeting_spec, previous_meetings)
matches, unmatched = generate_meetings(users, meeting_spec, previous_meetings)
assert len(unmatched) == num_users - 2
assert [(match[0].key.id(), match[1].key.id()) for match in matches] == [(users[0].key.id(), users[1].key.id())]
4 changes: 2 additions & 2 deletions tests/send_email_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from __future__ import unicode_literals

from yelp_beans.logic.meeting_spec import get_specs_for_current_week
from yelp_beans.matching.pair_match import generate_pair_meetings
from yelp_beans.matching.match import generate_meetings
from yelp_beans.models import User
from yelp_beans.models import UserSubscriptionPreferences
from yelp_beans.send_email import send_batch_initial_opt_in_email
Expand Down Expand Up @@ -63,5 +63,5 @@ def test_send_batch_meeting_confirmation_email(database):


def test_send_batch_unmatched_email(database, fake_user):
matches, unmatched = generate_pair_meetings([fake_user], database.specs[0])
matches, unmatched = generate_meetings([fake_user], database.specs[0])
send_batch_unmatched_email(unmatched)
13 changes: 13 additions & 0 deletions yelp_beans/matching/match.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

from yelp_beans.matching.pair_match import generate_pair_meetings


def generate_meetings(users, spec, prev_meeting_tuples=None, group_size=2):
if group_size == 2:
return generate_pair_meetings(users, spec, prev_meeting_tuples)
else:
raise NotImplementedError("Group matching not implemented yet.")
8 changes: 5 additions & 3 deletions yelp_beans/matching/pair_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from datetime import timedelta

import networkx as nx
from google.appengine.ext import ndb

from yelp_beans.logic.config import get_config
from yelp_beans.logic.user import user_preference
Expand Down Expand Up @@ -52,7 +53,7 @@ def save_pair_meetings(matches, spec):
))


def get_previous_pair_meetings(cooldown=None):
def get_previous_pair_meetings(subscription, cooldown=None):

if cooldown is None:
cooldown = get_config()['meeting_cooldown_weeks']
Expand All @@ -64,7 +65,8 @@ def get_previous_pair_meetings(cooldown=None):

meeting_spec_keys = [
spec.key for spec in MeetingSpec.query(
MeetingSpec.datetime > time_threshold_for_meetings
ndb.AND(MeetingSpec.datetime > time_threshold_for_meetings,
MeetingSpec.meeting_subscription == subscription)
).fetch()
]

Expand Down Expand Up @@ -112,7 +114,7 @@ def generate_pair_meetings(users, spec, prev_meeting_tuples=None):
- unmatched_user_ids: users with no matches.
"""
if prev_meeting_tuples is None:
prev_meeting_tuples = get_previous_pair_meetings()
prev_meeting_tuples = get_previous_pair_meetings(spec.meeting_subscription)

uid_to_users = {user.key.id(): user for user in users}
user_ids = sorted(uid_to_users.keys())
Expand Down