def test_one_survey_needs_updates_no_homes_valid(self, mock_os, mock_alog): """ Tests that if a survey needs updating but there isn't enough homes that meet the criteria then the email is not called """ # Arrange user = MyUser.objects.create(email="*****@*****.**") home_type = HomeTypeModel.objects.get_or_create( home_type=HomeTypeModel.APARTMENT)[0] survey = RentingSurveyModel.create_survey(user.userProfile, home_type=home_type, wants_update=True, update_frequency=0, score_threshold=100, num_home_threshold=3) home = RentDatabaseModel.create_house_database() home1 = RentDatabaseModel.create_house_database() # Create homes that will return from the algorithm home = HomeScore(home) home.accumulated_points = 100 home.total_possible_points = 100 home1 = HomeScore(home1) home1.accumulated_points = 50 home1.total_possible_points = 100 mc = mock_alog.return_value mc.homes = [home, home1] # Act notify_user_survey_updates() # Assert mock_os.assert_not_called()
def test_one_survey_needs_updates(self, mock_os, mock_alog): """ Tests that if at least one home has past the score threshold and the survey update timestamp is valid, then the survey updates and calls the email function """ # Arrange user = MyUser.objects.create(email="*****@*****.**") home_type = HomeTypeModel.objects.get_or_create( home_type=HomeTypeModel.APARTMENT)[0] survey = RentingSurveyModel.create_survey(user.userProfile, home_type=home_type, wants_update=True, update_frequency=0, score_threshold=100, num_home_threshold=1) home = RentDatabaseModel.create_house_database() home1 = RentDatabaseModel.create_house_database() # Create homes that will return from the algorithm home = HomeScore(home) home.accumulated_points = 100 home.total_possible_points = 100 home1 = HomeScore(home1) home1.accumulated_points = 50 home1.total_possible_points = 100 mc = mock_alog.return_value mc.homes = [home, home1] # Act notify_user_survey_updates() # Assert mock_os.assert_called_once_with(survey, 1)
def test_survey_marked_for_no_update(self, mock_os, mock_alog): """ Tests that if the user marks to not update the survey then the survey is not updated """ # Arrange user = MyUser.objects.create(email="*****@*****.**") home_type = HomeTypeModel.objects.get_or_create( home_type=HomeTypeModel.APARTMENT)[0] survey = RentingSurveyModel.create_survey(user.userProfile, home_type=home_type, wants_update=False, update_frequency=0, score_threshold=100, num_home_threshold=1) home = RentDatabaseModel.create_house_database() home1 = RentDatabaseModel.create_house_database() # Create homes that will return from the algorithm home = HomeScore(home) home.accumulated_points = 100 home.total_possible_points = 100 home1 = HomeScore(home1) home1.accumulated_points = 50 home1.total_possible_points = 100 mc = mock_alog.return_value mc.homes = [home, home1] # Act notify_user_survey_updates() # Assert mock_os.assert_not_called()
def test_determine_threshold_trigger_is_triggered_all_homes(self): """ Tests if an email should be trigger based on the user criteria """ # Arrange user = MyUser.objects.create(email="*****@*****.**") survey = RentingSurveyModel.create_survey(user.userProfile, num_home_threshold=3, score_threshold=70) home = HomeScore(RentDatabaseModel.create_house_database()) home1 = HomeScore(RentDatabaseModel.create_house_database()) home2 = HomeScore(RentDatabaseModel.create_house_database()) # Give each home a score of 100 home.accumulated_points = 50 home.total_possible_points = 50 home1.accumulated_points = 50 home1.total_possible_points = 50 home2.accumulated_points = 50 home2.total_possible_points = 50 # Act result = survey.determine_threshold_trigger([home, home1, home2]) # Assert self.assertEqual(result, [home, home1, home2])
def test_home_setter_later(self): # Arrange // Note really following methodology home_score = HomeScore() self.home = self.create_home(self.home_type) self.assertIsNone(home_score.home) home_score.home = self.home self.assertIsNotNone(home_score.home)
def test_home_setter_constructor(self): # Arrange self.home = self.create_home(self.home_type) home_score = HomeScore(self.home) # Assert self.assertIsNotNone(home_score.home)
def test_eliminate_home(self): # Arrange // not really following methodology home_score = HomeScore() self.home = self.create_home(self.home_type) self.assertFalse(home_score.eliminated) home_score.eliminate_home() self.assertTrue(home_score.eliminated)
def test_multiple_surveys_one_wants_updating(self, mock_os, mock_alog): """ Tests that if there are multiple surveys but one is marked as want update, then only that survey gets updated """ # Arrange user = MyUser.objects.create(email="*****@*****.**") home_type = HomeTypeModel.objects.get_or_create( home_type=HomeTypeModel.APARTMENT)[0] survey = RentingSurveyModel.create_survey(user.userProfile, home_type=home_type, wants_update=False, update_frequency=0, score_threshold=100, num_home_threshold=1) survey1 = RentingSurveyModel.create_survey(user.userProfile, home_type=home_type, wants_update=True, update_frequency=0, score_threshold=100, num_home_threshold=1) home = RentDatabaseModel.create_house_database() home1 = RentDatabaseModel.create_house_database() # Create homes that will return from the algorithm home = HomeScore(home) home.accumulated_points = 100 home.total_possible_points = 100 home1 = HomeScore(home1) home1.accumulated_points = 50 home1.total_possible_points = 100 mc = mock_alog.return_value mc.homes = [home, home1] # Act notify_user_survey_updates() # Assert mock_os.assert_has_calls([call(survey1, 1)])
def test_determine_threshold_trigger_homes_in_blacklist_do_not_trigger( self): """ Tests that if homes are in the blacklist then they are not counted as part of the trigger criteria. This specifically tests that if there was 5 homes that fit but 3 are in the blacklist then the criteria is not hit and thus returns empty list """ # Arrange user = MyUser.objects.create(email="*****@*****.**") survey = RentingSurveyModel.create_survey(user.userProfile, num_home_threshold=3, score_threshold=70) home = HomeScore(RentDatabaseModel.create_house_database()) home1 = HomeScore(RentDatabaseModel.create_house_database()) home2 = HomeScore(RentDatabaseModel.create_house_database()) home3 = HomeScore(RentDatabaseModel.create_house_database()) home4 = HomeScore(RentDatabaseModel.create_house_database()) survey.blacklist_home(home.home) survey.blacklist_home(home2.home) survey.blacklist_home(home3.home) # Give each home a score of 100 home.accumulated_points = 50 home.total_possible_points = 50 home1.accumulated_points = 50 home1.total_possible_points = 50 home2.accumulated_points = 50 home2.total_possible_points = 50 home3.accumulated_points = 50 home3.total_possible_points = 50 home4.accumulated_points = 50 home4.total_possible_points = 50 # Act result = survey.determine_threshold_trigger( [home, home1, home2, home3, home4]) # Assert self.assertEqual(result, [])
def create_home(home_type, price=1500, currently_available=True, num_bedrooms=2, num_bathrooms=2, zip_code="02476", state="MA"): return HomeScore(RentDatabaseModel.objects.create( home_type=home_type, price=price, currently_available=currently_available, num_bedrooms=num_bedrooms, num_bathrooms=num_bathrooms, zip_code=zip_code, state=state, listing_provider=HomeProviderModel.objects.get(provider="MLSPIN"), ))
def test_approx_commute_times_setter_basic(self): # Arrange home_score = HomeScore() self.home = self.create_home(self.home_type) self.assertEqual(home_score.approx_commute_times, {}) # Act home_score.approx_commute_times = {"12345": 10} home_score.approx_commute_times = {"01234": 20} # Assert self.assertEqual(home_score.approx_commute_times, {"12345": 10, "01234": 20})
def test_determine_threshold_trigger_homes_in_blacklist_not_returned(self): """ Tests that if there are still enough homes after the blacklist homes are removed then homes not in the blacklist are returned """ # Arrange user = MyUser.objects.create(email="*****@*****.**") survey = RentingSurveyModel.create_survey(user.userProfile, num_home_threshold=3, score_threshold=70) home = HomeScore(RentDatabaseModel.create_house_database()) home1 = HomeScore(RentDatabaseModel.create_house_database()) home2 = HomeScore(RentDatabaseModel.create_house_database()) home3 = HomeScore(RentDatabaseModel.create_house_database()) home4 = HomeScore(RentDatabaseModel.create_house_database()) survey.blacklist_home(home.home) survey.blacklist_home(home3.home) # Give each home a score of 100 home.accumulated_points = 50 home.total_possible_points = 50 home1.accumulated_points = 50 home1.total_possible_points = 50 home2.accumulated_points = 50 home2.total_possible_points = 50 home3.accumulated_points = 50 home3.total_possible_points = 50 home4.accumulated_points = 50 home4.total_possible_points = 50 # Act result = survey.determine_threshold_trigger( [home, home1, home2, home3, home4]) # Assert self.assertEqual(result, [home1, home2, home4])
def test_percent_score_total_possible_points_negative(self): # Arrange home_score = HomeScore() self.home = self.create_home(self.home_type) accumulated_points = 20 total_possible_points = -30 # Act home_score._accumulated_points = accumulated_points home_score._total_possible_points = total_possible_points # Assert self.assertEqual(-1, home_score.percent_score())
def test_user_friendly_score(self): # Arrange home_score = HomeScore() self.home = self.create_home(self.home_type) accumulated_points = 20 total_possible_points = 50 # Act home_score._accumulated_points = accumulated_points home_score._total_possible_points = total_possible_points # Assert self.assertEqual(round((accumulated_points/total_possible_points)*100), home_score.user_friendly_score())
def test_percent_score_accumulated_points_zero_equals_zero(self): # Arrange home_score = HomeScore() self.home = self.create_home(self.home_type) accumulated_points = 0 total_possible_points = 30 # Act home_score._accumulated_points = accumulated_points home_score._total_possible_points = total_possible_points home_score.eliminate_home() # Assert self.assertEqual(0, home_score.percent_score())
def populate_survey_homes(self, user_survey): """ Populates the homes variable based off a user survey :param user_survey: (RentingSurveyModel): The survey filled out by the user """ # Find all the possible homes that fit the static filter filtered_home_list = self.generate_static_filter_home_list(user_survey) # Add homes to rent_algorithm, homes should be stored as a HomeScore polygons = self.generate_polygons(user_survey) for home in filtered_home_list: # Only add homes that fall within the users polygon if self.polygon_filter(home, polygons, user_survey.polygon_filter_type): self.homes = HomeScore(home)
def create_home(home_type, listing_provider, price=1500, currently_available=True, num_bedrooms=2, num_bathrooms=2, zip_code="02476", state="MA", last_updated=timezone.now(), date_available=timezone.now()): return HomeScore(RentDatabaseModel.objects.create( home_type=home_type, price=price, currently_available=currently_available, num_bedrooms=num_bedrooms, num_bathrooms=num_bathrooms, zip_code=zip_code, state=state, listing_provider=listing_provider, last_updated=last_updated, date_available=date_available, ))
def create_home(home_type, price=1500, currently_available=True, num_bedrooms=2, num_bathrooms=2, zip_code="02476", state="MA", latitude=0.0, longitude=0.0, laundry_in_building=False, laundry_in_unit=False): return HomeScore(RentDatabaseModel.objects.create( home_type=home_type, price=price, currently_available=currently_available, num_bedrooms=num_bedrooms, num_bathrooms=num_bathrooms, zip_code=zip_code, state=state, latitude=latitude, longitude=longitude, listing_provider=HomeProviderModel.objects.get(provider="MLSPIN"), laundry_in_unit=laundry_in_unit, laundry_in_building=laundry_in_building, ))
def setUp(self): self.home = HomeScore() self.home1 = HomeScore() self.home2 = HomeScore() self.home_list = [self.home, self.home1, self.home2]