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

Jenny master #199

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions api/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ packaging==23.2
# pyproject-api
# pytest
# tox
pandas==1.5.3
platformdirs==3.11.0
# via
# tox
Expand Down
1 change: 1 addition & 0 deletions api/requirements-minimal.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ flask-api-utils
Flask-SQLAlchemy
httplib2
networkx
pandas
psycopg2-binary
pydantic
pytz
Expand Down
1 change: 1 addition & 0 deletions api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ markupsafe==2.1.3
# werkzeug
networkx==3.1
# via -r requirements-minimal.txt
pandas==1.5.3
psycopg2-binary==2.9.9
# via -r requirements-minimal.txt
pydantic==2.4.2
Expand Down
141 changes: 117 additions & 24 deletions api/tests/matching/match_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import itertools
import json
import os
from datetime import datetime
from datetime import timedelta
from unittest import mock

import pytest
from yelp_beans.logic.subscription import get_specs_from_subscription
from yelp_beans.logic.subscription import store_specs_from_subscription
from yelp_beans.matching.match import generate_meetings
from yelp_beans.models import Employee
from yelp_beans.models import Meeting
from yelp_beans.models import MeetingParticipant
from yelp_beans.models import MeetingRequest
Expand All @@ -17,19 +22,62 @@

MEETING_COOLDOWN_WEEKS = 10

base_dir = os.path.dirname(__file__)
mock_json_location = os.path.join(base_dir, "mock_employee_data")

def test_generate_meetings_same_department(session, subscription):
rule = Rule(name="department", value="")

@pytest.fixture
def mock_requests_get():
with mock.patch("requests.get", autospec=True) as mock_requests_get:
yield mock_requests_get


class MockResponse:
def __init__(self):
self.status_code = 200
self.text = "3"

def json(self):
with open(os.path.join(mock_json_location, "general_mock.json")) as f:
result = json.load(f)
return result


def test_generate_meetings_same_department(session, subscription, mock_requests_get):
mock_requests_get.return_value = MockResponse()
rule = Rule(name="cost_center_name", value="")
session.add(rule)
subscription.dept_rules = [rule]
preference = subscription.datetime[0]
user_pref = UserSubscriptionPreferences(preference=preference, subscription=subscription)
session.add(user_pref)
user1 = User(email="[email protected]", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
session.add(user1)
user2 = User(email="[email protected]", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
session.add(user2)
user_list = [user1, user2]
# user1 = User(email="[email protected]", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
# session.add(user1)
test_employee_1 = Employee(
cost_center_name="cost_center_1",
days_since_start=123,
employee_id="id_1",
location="Earth",
manager_id="id_2",
pronoun="they",
work_email="[email protected]",
languages="test",
)
session.add(test_employee_1)
# user2 = User(email="[email protected]", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
# session.add(user2)
test_employee_2 = Employee(
cost_center_name="cost_center_1",
days_since_start=123,
employee_id="id_2",
location="Earth",
manager_id="id_3",
pronoun="they",
work_email="[email protected]",
languages="test",
)
session.add(test_employee_2)
user_list = [test_employee_1, test_employee_2]
session.commit()

_, specs = get_specs_from_subscription(subscription)
Expand All @@ -38,22 +86,64 @@ def test_generate_meetings_same_department(session, subscription):
assert len(matches) == 0


def test_generate_meetings_with_history(session, subscription):
rule = Rule(name="department", value="")
def test_generate_meetings_with_history(session, subscription, mock_requests_get):
mock_requests_get.return_value = MockResponse()
rule = Rule(name="cost_center_name", value="")
session.add(rule)
subscription.dept_rules = [rule]

preference = subscription.datetime[0]
user_pref = UserSubscriptionPreferences(preference=preference, subscription=subscription)
session.add(user_pref)

user1 = User(email="[email protected]", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
user1 = Employee(
cost_center_name="cost_center_1",
days_since_start=123,
employee_id="id_1",
location="Earth",
manager_id="id_2",
pronoun="they",
work_email="[email protected]",
languages="test",
)
user2 = Employee(
cost_center_name="cost_center_2",
days_since_start=13,
employee_id="id_2",
location="Mars",
manager_id="id_4",
pronoun="they",
work_email="[email protected]",
languages="test",
)
user3 = Employee(
cost_center_name="cost_center_1",
days_since_start=143,
employee_id="id_3",
location="Earth",
manager_id="id_2",
pronoun="they",
work_email="[email protected]",
languages="test",
)
user4 = Employee(
cost_center_name="cost_center_2",
days_since_start=1,
employee_id="id_4",
location="Mercury",
manager_id="id_5",
pronoun="they",
work_email="[email protected]",
languages="test",
)

# user1 = User(email="[email protected]", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
session.add(user1)
user2 = User(email="b@yelp.com", meta_data={"department": "dept2"}, subscription_preferences=[user_pref])
# user2 = User(email="2@yelp.com", meta_data={"department": "dept2"}, subscription_preferences=[user_pref])
session.add(user2)
user3 = User(email="c@yelp.com", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
# user3 = User(email="3@yelp.com", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
session.add(user3)
user4 = User(email="d@yelp.com", meta_data={"department": "dept2"}, subscription_preferences=[user_pref])
# user4 = User(email="4@yelp.com", meta_data={"department": "dept2"}, subscription_preferences=[user_pref])
session.add(user4)

user_list = [user1, user2, user3, user4]
Expand All @@ -67,10 +157,10 @@ def test_generate_meetings_with_history(session, subscription):

meeting_history = set(
[
(user1.id, user2.id),
(user3.id, user4.id),
(user2.id, user3.id),
(user1.id, user4.id),
(user1.work_email, user2.work_email),
(user3.work_email, user4.work_email),
(user2.work_email, user3.work_email),
(user1.work_email, user4.work_email),
]
)
matches, unmatched = generate_meetings(user_list, specs[0], meeting_history)
Expand All @@ -79,17 +169,18 @@ def test_generate_meetings_with_history(session, subscription):

meeting_history = set(
[
(user1.id, user2.id),
(user3.id, user4.id),
(user2.id, user3.id),
(user1.work_email, user2.work_email),
(user3.work_email, user4.work_email),
(user2.work_email, user3.work_email),
]
)
matches, unmatched = generate_meetings(user_list, specs[0], meeting_history)
assert len(matches) == 1
assert len(unmatched) == 2


def test_no_re_matches(session):
def test_no_re_matches(session, mock_requests_get):
mock_requests_get.return_value = MockResponse()
pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1))
subscription = MeetingSubscription(title="all engineering weekly", datetime=[pref_1])
user_pref = UserSubscriptionPreferences(preference=pref_1, subscription=subscription)
Expand All @@ -103,17 +194,18 @@ def test_no_re_matches(session):
num_users = 20
for i in range(0, num_users):
user = User(email=f"{i}@yelp.com", meta_data={"department": f"dept{i}"}, subscription_preferences=[user_pref])
# user = Employee(cost_center_name=f"cost_center_{i}", days_since_start=100+i, employee_id=f"id_{i}", location="Earth", manager_id="id_2", pronoun="they", work_email=f"{i}@yelp.com",languages="test",)
session.add(user)
mr = MeetingRequest(user=user, meeting_spec=meeting_spec)
session.add(mr)
users.append(user)
session.commit()

previous_meetings = {pair for pair in itertools.combinations([user.id for user in users], 2)}
previous_meetings = previous_meetings - {(users[0].id, users[1].id)}
previous_meetings = {pair for pair in itertools.combinations([user.email for user in users], 2)}
previous_meetings = previous_meetings - {(users[0].email, users[1].email)}
matches, unmatched = generate_meetings(users, meeting_spec, previous_meetings)
assert len(unmatched) == num_users - 2
assert [(match[0].id, match[1].id) for match in matches] == [(users[0].id, users[1].id)]
assert [(match[0].email, match[1].email) for match in matches] == [(users[0].email, users[1].email)]


def test_generate_group_meeting(session):
Expand All @@ -130,6 +222,7 @@ def test_generate_group_meeting(session):
num_users = 21
for i in range(0, num_users):
user = User(email=f"{i}@yelp.com", meta_data={"department": f"dept{i}"}, subscription_preferences=[user_pref])
# user = Employee(cost_center_name=f"cost_center_{i}", days_since_start=100+i, employee_id=f"id_{i}", location="Earth", manager_id="id_2", pronoun="they", work_email=f"{i}@yelp.com",languages="test",)
session.add(user)
mr = MeetingRequest(user=user, meeting_spec=meeting_spec)
session.add(mr)
Expand Down
47 changes: 36 additions & 11 deletions api/tests/matching/match_utils_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import json
import os
from datetime import datetime
from datetime import timedelta
from unittest import mock

import pytest
from yelp_beans.matching.match import generate_meetings
from yelp_beans.matching.match_utils import get_counts_for_pairs
from yelp_beans.matching.match_utils import get_previous_meetings
Expand All @@ -16,6 +20,26 @@

MEETING_COOLDOWN_WEEKS = 10

base_dir = os.path.dirname(__file__)
mock_json_location = os.path.join(base_dir, "mock_employee_data")


@pytest.fixture
def mock_requests_get():
with mock.patch("requests.get", autospec=True) as mock_requests_get:
yield mock_requests_get


class MockResponse:
def __init__(self):
self.status_code = 200
self.text = "3"

def json(self):
with open(os.path.join(mock_json_location, "general_mock.json")) as f:
result = json.load(f)
return result


def test_pair_to_counts():
pairs = [("user1", "user2"), ("user1", "user2"), ("user2", "user3")]
Expand All @@ -24,12 +48,13 @@ def test_pair_to_counts():
assert counts[("user1", "user2")] == 2


def test_generate_save_meetings(session, subscription):
def test_generate_save_meetings(session, subscription, mock_requests_get):
mock_requests_get.return_value = MockResponse()
pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1))
subscription = MeetingSubscription(title="all engineering weekly", datetime=[pref_1])
user_pref = UserSubscriptionPreferences(preference=pref_1, subscription=subscription)
user1 = User(email="a@yelp.com", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
user2 = User(email="b@yelp.com", meta_data={"department": "dept2"}, subscription_preferences=[user_pref])
user1 = User(email="1@yelp.com", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
user2 = User(email="2@yelp.com", meta_data={"department": "dept2"}, subscription_preferences=[user_pref])
meeting_spec = MeetingSpec(meeting_subscription=subscription, datetime=pref_1.datetime)
mr1 = MeetingRequest(user=user1, meeting_spec=meeting_spec)
mr2 = MeetingRequest(user=user2, meeting_spec=meeting_spec)
Expand Down Expand Up @@ -58,8 +83,8 @@ def test_get_previous_meetings(session):
pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1))
subscription = MeetingSubscription(title="all engineering weekly", datetime=[pref_1])
user_pref = UserSubscriptionPreferences(preference=pref_1, subscription=subscription)
user1 = User(email="a@yelp.com", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
user2 = User(email="a@yelp.com", meta_data={"department": "dept2"}, subscription_preferences=[user_pref])
user1 = User(email="1@yelp.com", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
user2 = User(email="1@yelp.com", meta_data={"department": "dept2"}, subscription_preferences=[user_pref])
meeting_spec = MeetingSpec(meeting_subscription=subscription, datetime=pref_1.datetime)
meeting = Meeting(meeting_spec=meeting_spec, cancelled=False)
mp1 = MeetingParticipant(meeting=meeting, user=user2)
Expand All @@ -85,8 +110,8 @@ def test_get_previous_meetings_multi_subscription(session):
subscription2 = MeetingSubscription(title="all sales weekly", datetime=[pref_1])
user_pref1 = UserSubscriptionPreferences(preference=pref_1, subscription=subscription1)
user_pref2 = UserSubscriptionPreferences(preference=pref_1, subscription=subscription2)
user1 = User(email="a@yelp.com", meta_data={"department": "dept"}, subscription_preferences=[user_pref1, user_pref2])
user2 = User(email="a@yelp.com", meta_data={"department": "dept2"}, subscription_preferences=[user_pref1, user_pref2])
user1 = User(email="1@yelp.com", meta_data={"department": "dept"}, subscription_preferences=[user_pref1, user_pref2])
user2 = User(email="1@yelp.com", meta_data={"department": "dept2"}, subscription_preferences=[user_pref1, user_pref2])
meeting_spec1 = MeetingSpec(meeting_subscription=subscription1, datetime=pref_1.datetime)
meeting = Meeting(meeting_spec=meeting_spec1, cancelled=False)
mp1 = MeetingParticipant(meeting=meeting, user=user2)
Expand All @@ -113,8 +138,8 @@ def test_get_previous_multi_meetings(session):
pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS - 1))
subscription = MeetingSubscription(title="all engineering weekly", datetime=[pref_1])
user_pref = UserSubscriptionPreferences(preference=pref_1, subscription=subscription)
user1 = User(email="a@yelp.com", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
user2 = User(email="a@yelp.com", meta_data={"department": "dept2"}, subscription_preferences=[user_pref])
user1 = User(email="1@yelp.com", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
user2 = User(email="2@yelp.com", meta_data={"department": "dept2"}, subscription_preferences=[user_pref])
meeting_spec = MeetingSpec(meeting_subscription=subscription, datetime=pref_1.datetime)
meeting1 = Meeting(meeting_spec=meeting_spec, cancelled=False)
meeting2 = Meeting(meeting_spec=meeting_spec, cancelled=False)
Expand Down Expand Up @@ -144,8 +169,8 @@ def test_get_previous_meetings_no_specs(database_no_specs, session):
pref_1 = SubscriptionDateTime(datetime=datetime.now() - timedelta(weeks=MEETING_COOLDOWN_WEEKS + 1))
subscription = MeetingSubscription(title="all engineering weekly", datetime=[pref_1])
user_pref = UserSubscriptionPreferences(preference=pref_1, subscription=subscription)
user1 = User(email="a@yelp.com", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
user2 = User(email="a@yelp.com", meta_data={"department": "dept2"}, subscription_preferences=[user_pref])
user1 = User(email="1@yelp.com", meta_data={"department": "dept"}, subscription_preferences=[user_pref])
user2 = User(email="2@yelp.com", meta_data={"department": "dept2"}, subscription_preferences=[user_pref])
meeting_spec = MeetingSpec(meeting_subscription=subscription, datetime=pref_1.datetime)
meeting = Meeting(meeting_spec=meeting_spec, cancelled=False)
mp1 = MeetingParticipant(meeting=meeting, user=user2)
Expand Down
Loading
Loading