# Path: logic/office.py
# REMOTE_OFFICE = 'Remote'
# 
# Path: logic/office.py
# def get_all_offices():
#     from models import Employee
# 
#     """
#     Retrieve all the offices in the database
#     Returns: List[str]
#     """
#     return [
#         row.office
#         for row in Employee.query(projection=['office'], distinct=True).fetch() if row.office
#     ]
# 
# Path: logic/office.py
# class OfficeParser:
#     def __init__(self, employee_dicts=None):
#         """
# 
#         Args:
#             employee_dicts ([Dict], optional): Defaults to None.
#             If this dict exists the location of the employee will be determined from
#             the team location in case we don't have a location assigned to the employee
#         """
#         self.offices = yaml.safe_load(open('offices.yaml'))
#         self.__team_country_map = None
#         if employee_dicts:
#             self.__team_country_map = self.__create_team_country_map(employee_dicts)
# 
#     def __create_team_country_map(self, employee_dicts):
#         """
# 
#         Args:
#             employee_dicts ([Dict]): employee info
# 
#         Returns:
#             [DICT]: Map of each team and the country assigned to it.
#             The team country is the country with more employees.
#         """
#         teams_locations = defaultdict(lambda: Counter())
#         for employee in employee_dicts:
#             teams_locations[employee['department']][
#                 self.__get_office_name_match(employee['office'])
#             ] += 1
# 
#         team_country_map = {}
#         for team, countries in teams_locations.items():
#             team_country_map[team] = countries.most_common(1)[0][0]
# 
#         return team_country_map
# 
#     def get_office_name(self, employee_office_location, employee_department=None):
#         """
#         Given the employee office location retrieve the office name
#         The matching is done by basic string matching
#         Args:
#             employee_office_location: str
#             employee_department: Optional[str].
#                 This is the team of the employee. If it exists the office will be guessed from the
#                 office location in case we don't have an office for the employee
#             Returns: str
# 
#         Examples in: out
#             if we have yaml file with the follwong content: {Hamburg office, SF office}
#             NY Remote: Remote
#             SF office: SF office
#             SF office Remote: SF office
#             Germany Hamburg office: Hamburg office
#             CA SF New Montgomery Office: Sf office
#         """
#         employee_office_location = employee_office_location.lower()
# 
#         matched_office_name = self.__get_office_name_match(employee_office_location)
#         if matched_office_name != employee_office_location:
#             return matched_office_name
# 
#         if self.__team_country_map and employee_department:
#             return self.get_office_name(self.__team_country_map[employee_department])
# 
#         if REMOTE_OFFICE.lower() in employee_office_location:
#             return REMOTE_OFFICE
# 
#         return OTHER_OFFICE
# 
#     def __get_office_name_match(self, office_name):
#         """
#         Get the office name in the saved yaml file that matches this office name
#         Args:
#             office_name [str]
# 
#         Returns:
#             [str]: The matched office name if exists otherwise the same name in the input
#         """
#         for office in self.offices:
#             if office.lower() in office_name.lower():
#                 return office
#         return office_name
# 
# Path: testing/factories/employee.py
# def create_employee(
#     username='johndoe',
#     department='Engineering',
#     first_name='John',
#     last_name='Doe',
#     photo_url=None,
#     office=None,
# ):
# 
#     if photo_url is None:
#         photo_url = 'http://example.com/photos/{0}.jpg'.format(username)
# 
#     return Employee.create_from_dict({
#         'username': username,
#         'department': department,
#         'first_name': first_name,
#         'last_name': last_name,
#         'photo_url': photo_url,
#         'office': office,
#     })


import unittest
import mock
from logic.office import REMOTE_OFFICE
from logic.office import get_all_offices
from logic.office import OfficeParser
from testing.factories import create_employee

    def test_get_all_offices(self):
        self._create_employees()
        self.assertEqual(OFFICES, set(get_all_offices()))

    @mock.patch('logic.office.yaml.safe_load', return_value=OFFICES)
    def test_employee_parser_no_team_match(self, mock_offices):
        office_parser = OfficeParser()
        self.assertEqual(
            office_parser.get_office_name(
                self.employee_dicts[0]['office'],
            ),
            'Germany',
        )
        self.assertEqual(
            office_parser.get_office_name(
                self.employee_dicts[1]['office'],
            ),
            'Germany',
        )
        self.assertEqual(
            office_parser.get_office_name(
                self.employee_dicts[2]['office'],
            ),
            REMOTE_OFFICE,
        )
        mock_offices.assert_called_once()

    @mock.patch('logic.office.yaml.safe_load', return_value=OFFICES)
    def test_employee_parser_with_team_match(self, mock_offices):

# Path: logic/office.py
# REMOTE_OFFICE = 'Remote'
# 
# Path: logic/office.py
# def get_all_offices():
#     from models import Employee
# 
#     """
#     Retrieve all the offices in the database
#     Returns: List[str]
#     """
#     return [
#         row.office
#         for row in Employee.query(projection=['office'], distinct=True).fetch() if row.office
#     ]
# 
# Path: logic/office.py
# class OfficeParser:
#     def __init__(self, employee_dicts=None):
#         """
# 
#         Args:
#             employee_dicts ([Dict], optional): Defaults to None.
#             If this dict exists the location of the employee will be determined from
#             the team location in case we don't have a location assigned to the employee
#         """
#         self.offices = yaml.safe_load(open('offices.yaml'))
#         self.__team_country_map = None
#         if employee_dicts:
#             self.__team_country_map = self.__create_team_country_map(employee_dicts)
# 
#     def __create_team_country_map(self, employee_dicts):
#         """
# 
#         Args:
#             employee_dicts ([Dict]): employee info
# 
#         Returns:
#             [DICT]: Map of each team and the country assigned to it.
#             The team country is the country with more employees.
#         """
#         teams_locations = defaultdict(lambda: Counter())
#         for employee in employee_dicts:
#             teams_locations[employee['department']][
#                 self.__get_office_name_match(employee['office'])
#             ] += 1
# 
#         team_country_map = {}
#         for team, countries in teams_locations.items():
#             team_country_map[team] = countries.most_common(1)[0][0]
# 
#         return team_country_map
# 
#     def get_office_name(self, employee_office_location, employee_department=None):
#         """
#         Given the employee office location retrieve the office name
#         The matching is done by basic string matching
#         Args:
#             employee_office_location: str
#             employee_department: Optional[str].
#                 This is the team of the employee. If it exists the office will be guessed from the
#                 office location in case we don't have an office for the employee
#             Returns: str
# 
#         Examples in: out
#             if we have yaml file with the follwong content: {Hamburg office, SF office}
#             NY Remote: Remote
#             SF office: SF office
#             SF office Remote: SF office
#             Germany Hamburg office: Hamburg office
#             CA SF New Montgomery Office: Sf office
#         """
#         employee_office_location = employee_office_location.lower()
# 
#         matched_office_name = self.__get_office_name_match(employee_office_location)
#         if matched_office_name != employee_office_location:
#             return matched_office_name
# 
#         if self.__team_country_map and employee_department:
#             return self.get_office_name(self.__team_country_map[employee_department])
# 
#         if REMOTE_OFFICE.lower() in employee_office_location:
#             return REMOTE_OFFICE
# 
#         return OTHER_OFFICE
# 
#     def __get_office_name_match(self, office_name):
#         """
#         Get the office name in the saved yaml file that matches this office name
#         Args:
#             office_name [str]
# 
#         Returns:
#             [str]: The matched office name if exists otherwise the same name in the input
#         """
#         for office in self.offices:
#             if office.lower() in office_name.lower():
#                 return office
#         return office_name
# 
# Path: testing/factories/employee.py
# def create_employee(
#     username='johndoe',
#     department='Engineering',
#     first_name='John',
#     last_name='Doe',
#     photo_url=None,
#     office=None,
# ):
# 
#     if photo_url is None:
#         photo_url = 'http://example.com/photos/{0}.jpg'.format(username)
# 
#     return Employee.create_from_dict({
#         'username': username,
#         'department': department,
#         'first_name': first_name,
#         'last_name': last_name,
#         'photo_url': photo_url,
#         'office': office,
#     })


# Path: tests/logic/office_test.py
import unittest
import mock
from logic.office import REMOTE_OFFICE
from logic.office import get_all_offices
from logic.office import OfficeParser
from testing.factories import create_employee


    def test_get_all_offices(self):
        self._create_employees()
        self.assertEqual(OFFICES, set(get_all_offices()))

    @mock.patch('logic.office.yaml.safe_load', return_value=OFFICES)
    def test_employee_parser_no_team_match(self, mock_offices):
        office_parser = OfficeParser()
        self.assertEqual(
            office_parser.get_office_name(
                self.employee_dicts[0]['office'],
            ),
            'Germany',
        )
        self.assertEqual(
            office_parser.get_office_name(
                self.employee_dicts[1]['office'],
            ),
            'Germany',
        )
        self.assertEqual(
            office_parser.get_office_name(
                self.employee_dicts[2]['office'],
            ),
            REMOTE_OFFICE,
        )
        mock_offices.assert_called_once()

    @mock.patch('logic.office.yaml.safe_load', return_value=OFFICES)
    def test_employee_parser_with_team_match(self, mock_offices):

        office_parser = OfficeParser(self.employee_dicts)