def test_meal_session_cron_creates_meal_session_using_scheduler_date( self, mock_location_current_date, mock_scheduler_current_date, ): LocationFactory.create(id=1, name="Lagos") with self.app.app_context(): mock_scheduler_current_date.return_value = datetime( year=2019, month=4, day=10, hour=0, minute=0, tzinfo=pytz.timezone("Africa/Lagos")) mock_location_current_date.return_value = datetime( year=2019, month=3, day=10, hour=11, minute=0, tzinfo=pytz.timezone("Africa/Dakar")) Cron(self.app).run_meal_session_cron() meal_sessions = MealSessionRepo().fetch_all().items assert meal_sessions[0].name == "breakfast" assert meal_sessions[0].date.month == 4 assert meal_sessions[1].name == "lunch" assert meal_sessions[1].date.month == 4
def test_job_to_schedule_method_creates_meal_sessions(self): LocationFactory.create(id=1, name="Lagos") with self.app.app_context(): Cron(self.app).run_meal_session_cron() meal_sessions = MealSessionRepo().fetch_all().items assert meal_sessions[0].name == "breakfast" assert meal_sessions[1].name == "lunch"
def test_bot(self): with self.app.app_context(): # Arrange locations = [ ('Kampala', '+3'), ('Kigali', '+2'), ('Lagos', '+1'), ('Nairobi', '+3'), ] for location in locations: LocationFactory.create(name=location[0], zone=location[1]) bot_controller = BotController(self.request_context) # Act result = bot_controller.bot() # Assert result_json = result.get_json() if result.status_code != 200: raise AssertionError() if 'text' not in result_json: raise AssertionError() if result_json['text'] != 'Welcome To Andela Eats': raise AssertionError() if 'attachments' not in result_json: raise AssertionError() if 'callback_id' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['callback_id']\ != 'center_selector': raise AssertionError() if 'color' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['color'] != '#3AA3E3': raise AssertionError() if 'attachment_type' not in result_json['attachments'][0]: raise AssertionError() if result_json['attachments'][0]['attachment_type'] != 'default': raise AssertionError() if 'actions' not in result_json['attachments'][0]: raise AssertionError() for i in range(len(locations)): if not any( loc.get('text') == locations[i][0] for loc in result_json['attachments'][0]['actions']): raise AssertionError()
def test_check_meal_session_exists_in_specified_time_method_returns_false( self): new_location = LocationFactory.create(name="Lagos") tz = pytz.timezone("Africa/Lagos") current_date = datetime.now(tz) first_meal_session = { "name": "lunch", "start_time": time(hour=12, minute=10), "end_time": time(hour=12, minute=40), "date_sent": datetime(year=current_date.year, month=current_date.month, day=current_date.day), "location_id": new_location.id } MealSessionFactory.create(name="lunch", start_time=time(hour=13, minute=0), stop_time=time(hour=14, minute=0), date=datetime(year=current_date.year, month=current_date.month, day=current_date.day), location_id=new_location.id) return_value = self.logic.check_meal_session_exists_in_specified_time( **first_meal_session) self.assertEqual(return_value, False)
def test_meal_session_cron_does_not_create_meal_session_if_session_already_exists( self, mock_location_current_date, mock_scheduler_current_date, mock_validate_meal_session_times): LocationFactory.create(id=1, name="Kampala") with self.app.app_context(): mock_scheduler_current_date.return_value = datetime( year=2019, month=4, day=10, tzinfo=pytz.timezone("Africa/Lagos")) mock_location_current_date.return_value = datetime( year=2019, month=3, day=10, tzinfo=pytz.timezone("Africa/Dakar")) mock_validate_meal_session_times.return_value = "A meal session already exists" Cron(self.app).run_meal_session_cron() meal_sessions = MealSessionRepo().fetch_all().items assert len(meal_sessions) == 0
def test_new_user_role_method_returns_new_user_role_object(self): role = RoleFactory.create() location = LocationFactory.create() user_role = UserRoleFactory.build(role_id=role.id, location=location) new_user_role = self.repo.new_user_role(user_role.role_id, user_role.user_id, user_role.location_id, user_role.email) self.assertIsInstance(new_user_role, UserRole) self.assertEqual(str(new_user_role.user_id), str(user_role.user_id)) self.assertEqual(new_user_role.location.id, user_role.location.id)
def test_exclude_works_user_role_instance(self): role = RoleFactory.create() location = LocationFactory.create() user_role = UserRoleFactory.build(role_id=role.id, location_id=location.id) new_user_role = self.repo.new_user_role(user_role.role_id, user_role.user_id, user_role.location_id, user_role.email) excluded_response = new_user_role.to_dict(exclude=["user_id"]) self.assertFalse(excluded_response.get("user_id", False))
def test_run_5_minute_method(self): role = RoleFactory.create() location = LocationFactory.create() user_role = UserRoleFactory.build(role_id=role.id, location=location) UserRoleRepo().new_user_role(user_role.role_id, user_role.user_id, user_role.location_id, user_role.email) Cron(self.app).run_5_minute() results = self.redis_set.get(user_role.email[0]) self.assertEqual(user_role.email, results[0]) results = self.redis_set.get(user_role.email[0:1]) self.assertEqual(user_role.email, results[0]) results = self.redis_set.get(user_role.email[0:2]) self.assertEqual(user_role.email, results[0])
def test_create_session_method_succeeds(self, mock_request_params): with self.app.app_context(): new_location = LocationFactory.create(id=1, name="Lagos") tz = pytz.timezone("Africa/Lagos") self.current_date = datetime.now(tz) mock_request_params_return_value = [ "lunch", "13:10", "14:45", "".join([ MealSessionLogic.format_preceding(self.current_date.year), "-", MealSessionLogic.format_preceding(self.current_date.month), "-", MealSessionLogic.format_preceding(self.current_date.day) ]), new_location.id ] mock_request_params.return_value = mock_request_params_return_value meal_session_controller = MealSessionController( self.request_context) response = meal_session_controller.create_session() response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assertEqual(response.status_code, 201) self.assertEqual(response_json['msg'], 'OK') self.assertEqual(response_json['payload']['mealSession']['name'], mock_request_params_return_value[0]) self.assertEqual( (response_json['payload']['mealSession']['startTime'])[:-3], mock_request_params_return_value[1]) self.assertEqual( (response_json['payload']['mealSession']['stopTime'])[:-3], mock_request_params_return_value[2]) self.assertEqual(response_json['payload']['mealSession']['date'], mock_request_params_return_value[3]) self.assertEqual( response_json['payload']['mealSession']['locationId'], mock_request_params_return_value[4])
def test_new_user_role_updates_cache(self): role = RoleFactory.create() location = LocationFactory.create() user_role = UserRoleFactory.build(role_id=role.id, location=location) self.repo.new_user_role(user_role.role_id, user_role.user_id, user_role.location_id, user_role.email) results = self.redis_set.get(user_role.email[0:1]) self.assertTrue(user_role.email in results) results = self.redis_set.get(user_role.email[0:3]) self.assertTrue(user_role.email in results) results = self.redis_set.get(user_role.email[0:5]) self.assertTrue(user_role.email in results) results = self.redis_set.get(user_role.email[0:len(user_role.email) - 1]) self.assertTrue(user_role.email in results)
def test_get_location_time_zone_raises_unknownTimeZoneError(self): LocationFactory.create(id=2, name="Uganda") return_value = self.base_logic.get_location_time_zone(2) self.assertEqual(pytz.exceptions.UnknownTimeZoneError, return_value)
def setUp(self): self.BaseSetUp() LocationFactory.create(name='Lagos', zone='+1') LocationFactory.create(name='Kampala', zone='+3') LocationFactory.create(name='Kigali', zone='+2') LocationFactory.create(name='Nairobi', zone='+3')