def test_happy_path_without_start_time( self, ctx, mock_membership_repository: MagicMock, mock_member_repository: MagicMock, member_manager: MemberManager): # Given that now == TEST_DATE (monkey patch datetime.now()) # See here: http://blog.xelnor.net/python-mocking-datetime/ with mock.patch.object(datetime, 'datetime', mock.Mock(wraps=datetime.datetime)) as patched: patched.now.return_value = TEST_DATE1 # When... member_manager.new_membership(ctx, TEST_USERNAME, 1, 'card') # Expect... expected_start_date = TEST_DATE1 expected_end_date = TEST_DATE1 + datetime.timedelta(days=1) # Expect to create a new membership record... self._assert_membership_record_was_created(ctx, TEST_USERNAME, mock_membership_repository, expected_start_date, expected_end_date) # And update the member object. mock_member_repository.update_member.assert_called_once_with( ctx, TEST_USERNAME, departure_date=expected_end_date.isoformat())
def test_member_not_found(self, ctx, mock_member_repository: MemberRepository, member_manager: MemberManager): # Given that the database cannot find the specified member. mock_member_repository.update_member = MagicMock( side_effect=MemberNotFoundError) with raises(MemberNotFoundError): member_manager.new_membership(ctx, TEST_USERNAME, 1, 'card')
def test_invalid_duration(self, ctx, mock_member_repository: MagicMock, mock_membership_repository: MagicMock, member_manager: MemberManager): # When... with raises(IntMustBePositive): member_manager.new_membership(ctx, TEST_USERNAME, -1, 'bank_cheque') # Expect that the database has not been touched. mock_member_repository.update_member.assert_not_called() mock_membership_repository.create_membership.assert_not_called()
def test_no_price_for_duration(self, ctx, mock_member_repository: MagicMock, mock_membership_repository: MagicMock, member_manager: MemberManager): # When... with raises(NoPriceAssignedToThatDuration): member_manager.new_membership(ctx, TEST_USERNAME, 123456789, 'cash') # Expect that the database has not been touched. mock_member_repository.update_member.assert_not_called() mock_membership_repository.create_membership.assert_not_called()
def test_fail_to_create_transaction( self, ctx, mock_membership_repository: MembershipRepository, mock_member_repository: MemberRepository, mock_money_repository: MoneyRepository, exception: Exception, member_manager: MemberManager): mock_member_repository.update_member = MagicMock() mock_money_repository.add_member_payment_record = MagicMock( side_effect=exception) # When... with raises(exception): member_manager.new_membership(ctx, TEST_USERNAME, 1, 'cash', start_str=TEST_DATE1.isoformat()) # Expect... mock_member_repository.update_member.assert_not_called()
def test_happy_path(self, ctx, mock_membership_repository: MagicMock, mock_member_repository: MagicMock, member_manager: MemberManager): # When... member_manager.new_membership(ctx, TEST_USERNAME, 1, 'cash', start_str=TEST_DATE1.isoformat()) # Expect... expected_start_date = TEST_DATE1 expected_end_date = TEST_DATE1 + datetime.timedelta(days=1) # Expect to create a new membership record... self._assert_membership_record_was_created(ctx, TEST_USERNAME, mock_membership_repository, expected_start_date, expected_end_date) # And update the member object. mock_member_repository.update_member.assert_called_once_with( ctx, TEST_USERNAME, departure_date=expected_end_date.isoformat())