diff --git a/cellphones/main.py b/cellphones/main.py index 791dd4c..8a47655 100644 --- a/cellphones/main.py +++ b/cellphones/main.py @@ -31,7 +31,7 @@ def main(): print(assignments.phone_info(employee3)) # None assignments.assign(phone3.id, employee3) # Assign phone 3 to employee 3 - assignments.assign(phone2.id, employee3) # Reassign phone 3 to employee3. TODO this should fail; employee3 should not be able to have two phones + # assignments.assign(phone2.id, employee3) # Reassign phone 3 to employee3. TODO this should fail; employee3 should not be able to have two phones diff --git a/cellphones/phone_manager.py b/cellphones/phone_manager.py index 93f4219..fb670a0 100644 --- a/cellphones/phone_manager.py +++ b/cellphones/phone_manager.py @@ -45,24 +45,37 @@ def __init__(self): def add_employee(self, employee): - # TODO raise exception if two employees with same ID are added + # Raises exception if two employees with same ID are added + for empl in self.employees: + if empl.id == employee.id: + raise PhoneError('There is already an employee with that ID.') self.employees.append(employee) def add_phone(self, phone): - # TODO raise exception if two phones with same ID are added + # Raises exception if two phones with same ID are added + for old_phone in self.phones: + if old_phone.id == phone.id: + raise PhoneError('There is already an phone with that ID.') self.phones.append(phone) - def assign(self, phone_id, employee): + def assign(self, assigning_phone_id, employee): # Find phone in phones list - # TODO if phone is already assigned to an employee, do not change list, raise exception - # TODO if employee already has a phone, do not change list, and raise exception - # TODO if employee already has this phone, don't make any changes. This should NOT raise an exception. + # Checks if phone is already assigned to the employee - if so exits out of method. + # Checks if phone is already assigned to a different employee - if so raises exception. + # Checks if given employee already has different phone assigned to them - if so raises exception + # If all of that ^ doesn't trigger any problems, assigns employee to phone. for phone in self.phones: - if phone.id == phone_id: - phone.assign(employee.id) - return + if phone.id == assigning_phone_id: + assigning_phone = phone + if phone.employee_id == employee.id: + return + if phone.employee_id is not None: + raise PhoneError('Phone is already assigned to an employee.') + if phone.employee_id == employee.id: + raise PhoneError('Employee already has phone assigned to them.') + assigning_phone.assign(employee.id) def un_assign(self, phone_id): @@ -73,17 +86,16 @@ def un_assign(self, phone_id): def phone_info(self, employee): - # find phone for employee in phones list - - # TODO should return None if the employee does not have a phone - # TODO the method should raise an exception if the employee does not exist - - for phone in self.phones: - if phone.employee_id == employee.id: - return phone - - - return None + # Find phone for employee in phones list + # Checks given ID against all valid IDs. If the ID is valid, method attempts to find a phone with that employee assigned to it. + #If the given ID is not found in the employees list, raises excepetion. + for existing_employee in self.employees: + if existing_employee.id == employee.id: + for phone in self.phones: + if phone.employee_id == employee.id: + return phone + return None + raise PhoneError("There is no employee with that ID.") class PhoneError(Exception): diff --git a/cellphones/test_phone_manager.py b/cellphones/test_phone_manager.py index 987d6fb..cd4549c 100644 --- a/cellphones/test_phone_manager.py +++ b/cellphones/test_phone_manager.py @@ -20,8 +20,7 @@ def test_create_and_add_new_phone(self): def test_create_and_add_phone_with_duplicate_id(self): - # TODO add a phone, add another phone with the same id, and verify an PhoneError exception is thrown - # TODO you'll need to modify PhoneAssignments.add_phone() to make this test pass + # Adds a phone, adds another phone with the same id, and verifies an PhoneError exception is thrown testPhone1 = Phone(1, 'Apple', 'iPhone 6') testPhone2 = Phone(1, 'Apple', 'iPhone 5') @@ -33,59 +32,125 @@ def test_create_and_add_phone_with_duplicate_id(self): def test_create_and_add_new_employee(self): - # TODO write this test and then remove the self.fail() statement # Add some employees and verify they are present in the PhoneAssignments.employees list - self.fail() + testEmployee = Employee(1, 'Test Name') + testAssignmentMgt = PhoneAssignments() + + testAssignmentMgt.add_employee(testEmployee) + + self.assertIn(testEmployee, testAssignmentMgt.employees) def test_create_and_add_employee_with_duplicate_id(self): - # TODO write this test and then remove the self.fail() statement - # TODO you'll need to fix the add_employee method in PhoneAssignments to make this test PhoneAssignments - # This method will be similar to test_create_and_add_phone_with_duplicate_id - self.fail() + #Tests that duplicate IDs raises error. + testEmployee1 = Employee(1, 'Test Name 1') + testEmployee2 = Employee(1, 'Test Name 2') + + testAssignmentMgr = PhoneAssignments() + testAssignmentMgr.add_employee(testEmployee1) + + with self.assertRaises(PhoneError): + testAssignmentMgr.add_employee(testEmployee2) def test_assign_phone_to_employee(self): - # TODO write this test and remove the self.fail() statement - # TODO you'll need to fix the assign method in PhoneAssignments + testEmployee = Employee(1, 'Test Name 1') + testPhone = Phone(1, 'Test Brand', 'Test Model') + + testAssignmentMgr = PhoneAssignments() + testAssignmentMgr.add_employee(testEmployee) + testAssignmentMgr.add_phone(testPhone) - self.fail() + testAssignmentMgr.assign(1, testEmployee) + + self.assertTrue(testPhone.employee_id == testEmployee.id) def test_assign_phone_that_has_already_been_assigned_to_employee(self): - # If a phone is already assigned to an employee, it is an error to assign it to a different employee. A PhoneError should be raised. - # TODO write this test and remove the self.fail() statement - # TODO you'll need to fix the assign method in PhoneAssignments so it throws an exception if the phone is alreaady assigned. + # Attempts to assign one phone to multiple employees and ensures it raises error. + testEmployee1 = Employee(1, 'Test Name 1') + testEmployee2 = Employee(2, 'Test Name 2') + testPhone = Phone(1, 'Test Brand', 'Test Model') + + testAssignmentMgr = PhoneAssignments() + testAssignmentMgr.add_employee(testEmployee1) + testAssignmentMgr.add_employee(testEmployee2) + testAssignmentMgr.add_phone(testPhone) - self.fail() + testAssignmentMgr.assign(1, testEmployee1) + + with self.assertRaises(PhoneError): + testAssignmentMgr.assign(1, testEmployee2) def test_assign_phone_to_employee_who_already_has_a_phone(self): - # TODO write this test and remove the self.fail() statement - # TODO you'll need to fix the assign method in PhoneAssignments so it raises a PhoneError if the phone is alreaady assigned. + # Assigns a phone to an employee who already has a phone and ensures an error is raised. + testEmployee = Employee(1, 'Test Name 1') + testPhone1 = Phone(1, 'Test Brand', 'Test Model') + testPhone2 = Phone(2, 'Test Brand', 'Test Model') + + testAssignmentMgr = PhoneAssignments() + testAssignmentMgr.add_employee(testEmployee) + testAssignmentMgr.add_phone(testPhone1) + testAssignmentMgr.add_phone(testPhone2) - self.fail() + testAssignmentMgr.assign(1, testEmployee) + + with self.assertRaises(PhoneError): + testAssignmentMgr.assign(2, testEmployee) def test_assign_phone_to_the_employee_who_already_has_this_phone(self): - # TODO The method should not make any changes but NOT raise a PhoneError if a phone + # The method should not make any changes but NOT raise a PhoneError if a phone # is assigned to the same user it is currenly assigned to. + try: + testEmployee = Employee(1, 'Test Name 1') + testPhone = Phone(1, 'Test Brand', 'Test Model') - self.fail() + testAssignmentMgr = PhoneAssignments() + testAssignmentMgr.add_employee(testEmployee) + testAssignmentMgr.add_phone(testPhone) + + testAssignmentMgr.assign(1, testEmployee) + except PhoneError: + self.fail('Phone error raised by assigning same phone to employee twice.') def test_un_assign_phone(self): - # TODO write this test and remove the self.fail() statement - # Assign a phone, unasign the phone, verify the employee_id is None - self.fail() + # Assigns a phone, unasigns the phone, verifies the employee_id is None + testEmployee = Employee(1, 'Test Name 1') + testPhone = Phone(1, 'Test Brand', 'Test Model') + + testAssignmentMgr = PhoneAssignments() + testAssignmentMgr.add_employee(testEmployee) + testAssignmentMgr.add_phone(testPhone) + + testAssignmentMgr.assign(1, testEmployee) + testAssignmentMgr.un_assign(1) + + self.assertTrue(testPhone.employee_id == None) def test_get_phone_info_for_employee(self): - # TODO write this test and remove the self.fail() statement - # Create some phones, and employees, assign a phone, - # call phone_info and verify correct phone info is returned + # Checks that the method returns None if the employee does not have a phone + # Checks that the method raises an PhoneError if the employee does not exist - # TODO check that the method returns None if the employee does not have a phone - # TODO check that the method raises an PhoneError if the employee does not exist + testEmployee1 = Employee(1, 'Test Name 1') + testEmployee2 = Employee(2, 'Test Name 2') + fakeEmployee = Employee(3, 'Fake Name') + testPhone1 = Phone(1, 'Test Brand 1', 'Test Model 1') + testPhone2 = Phone(2, 'Test Brand 2', 'Test Model 2') - self.fail() + testAssignmentMgr = PhoneAssignments() + testAssignmentMgr.add_employee(testEmployee1) + testAssignmentMgr.add_employee(testEmployee2) + testAssignmentMgr.add_phone(testPhone1) + testAssignmentMgr.add_phone(testPhone2) + + testAssignmentMgr.assign(1, testEmployee1) + + self.assertTrue(testAssignmentMgr.phone_info(testEmployee1) == testPhone1) + self.assertTrue(testAssignmentMgr.phone_info(testEmployee2) == None) + with self.assertRaises(PhoneError): + testAssignmentMgr.phone_info(fakeEmployee) + diff --git a/student_lists/studentlists.py b/student_lists/studentlists.py index 8df4f8a..d2eca14 100644 --- a/student_lists/studentlists.py +++ b/student_lists/studentlists.py @@ -48,8 +48,11 @@ def index_of_student(self, student): return None - ## TODO add a method called is_class_full. - # This should return True or False to indicate if the class is full. + def is_class_full(self): + if len(self.class_list) >= self.max_students: + return True + else: + return False def __str__(self): diff --git a/student_lists/test_studentlists.py b/student_lists/test_studentlists.py index 6e4e33f..b325327 100644 --- a/student_lists/test_studentlists.py +++ b/student_lists/test_studentlists.py @@ -24,22 +24,29 @@ def test_add_student_check_student_in_list(self): self.assertIn('Test Student', test_class.class_list) self.assertIn('Another Test Student', test_class.class_list) - def test_add_student_already_in_list(self): test_class = ClassList(2) test_class.add_student('Test Student') with self.assertRaises(StudentError): test_class.add_student('Test Student') + def test_add_remove_student(self): + test_class = ClassList(1) + test_class.add_student('Test Student') + test_class.remove_student('Test Student') + self.assertNotIn('Test Student', test_class.class_list) - ## TODO write a test that adds and removes a student, and asserts the student is removed. Use assertNotIn - - - ## TODO write a test that adds some example students, then removes a student not in the list, and asserts a StudentError is raised - - - ## TODO write a test that removes a student from an empty list, and asserts a StudentError is raised + def test_add_students_remove_student_not_in_list(self): + test_class = ClassList(3) + test_class.add_student('Test Student 1') + test_class.add_student('Test Student 2') + with self.assertRaises(StudentError): + test_class.remove_student('Test Student 3') + def test_remove_student_from_empty_list(self): + test_class = ClassList(0) + with self.assertRaises(StudentError): + test_class.remove_student('Test Student') def test_is_enrolled_when_student_present(self): @@ -54,10 +61,12 @@ def test_is_enrolled_empty_class_list(self): test_class = ClassList(2) self.assertFalse(test_class.is_enrolled('Snoop Dogg')) - - ## TODO write a test that adds some example students to a test class, - ## then, call is_enrolled for a student who is not enrolled. use assertFalse to verify is_enrolled returns False. - + def test_is_enrolled_populated_class_list_student_not_enrolled(self): + test_class = ClassList(3) + test_class.add_student('Test Student 1') + test_class.add_student('Test Student 2') + test_class.add_student('Test Student 3') + self.assertFalse(test_class.is_enrolled('Test Student 4')) def test_string_with_students_enrolled(self): test_class = ClassList(2) @@ -85,15 +94,30 @@ def test_index_of_student_student_present(self): # the method call returns None self.assertIsNotNone(test_class.index_of_student('Harry')) + def test_student_index_empty_class_list(self): + test_class = ClassList(0) + self.assertIsNone(test_class.index_of_student('Test Student')) - - ## TODO write a test for index_of_student when the class_list list is empty. - # Assert index_of_student returns None for a student if the list is empty. use assertIsNone. - - ## TODO write another test for index_of_student. In the case when the list is not empty but - # assert that searching for a student name that is not in the list, returns None. - + def test_student_index_populated_class_list_student_not_in_list(self): + test_class = ClassList(3) + test_class.add_student('Test Student 1') + test_class.add_student('Test Student 2') + test_class.add_student('Test Student 3') + self.assertIsNone(test_class.index_of_student('Test Student 4')) - ## TODO write a test for your new is_class_full method when the class is full. use assertTrue. - - ## TODO write a test for your new is_class_full method for when is empty, and when it is not full. Use assertFalse. + def test_is_class_full_class_is_full(self): + test_class = ClassList(3) + test_class.add_student('Test Student 1') + test_class.add_student('Test Student 2') + test_class.add_student('Test Student 3') + self.assertTrue(test_class.is_class_full()) + + def test_is_class_full_class_not_full(self): + test_class = ClassList(3) + test_class.add_student('Test Student 1') + test_class.add_student('Test Student 2') + self.assertFalse(test_class.is_class_full()) + + def test_is_class_full_class_empty(self): + test_class = ClassList(3) + self.assertFalse(test_class.is_class_full()) \ No newline at end of file