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 setUp(self): self.BaseSetUp() self.repo = VendorRatingRepo() self.location = LocationFactory() self.vendor = VendorFactory() self.engagement = VendorEngagementFactory(location_id=self.location.id, vendor_id=self.vendor.id)
def test_new_location_method_returns_new_location_object(self): location = LocationFactory.build() new_location = self.repo.new_location(location.name, location.zone) self.assertIsInstance(new_location, Location) self.assertEqual(new_location.name, location.name) self.assertEqual(new_location.zone, location.zone)
def setUp(self): self.BaseSetUp() location = LocationFactory() self.mock_vendor = Vendor(id=1, created_at=datetime.now(), updated_at=datetime.now(), name='Mock vendor', address='Mock address', tel='', contact_person='Mock person', is_active=True, location_id=location.id) self.mock_vendor_engagement = VendorEngagement( id=1, created_at=datetime.now(), updated_at=datetime.now(), location_id=location.id, start_date=datetime.now(), end_date=datetime.now(), status=1, termination_reason='Mock reason', vendor=self.mock_vendor) self.mock_menu = Menu(is_deleted=False, date=datetime.now(), meal_period='', location_id=location.id, main_meal_id=1, allowed_side=1, allowed_protein=1, side_items='', protein_items='', vendor_engagement_id=1, created_at=datetime.now(), updated_at=datetime.now()) self.mock_vendor_engagement = VendorEngagement( id=1, created_at=datetime.now(), updated_at=datetime.now(), location_id=location.id, start_date=datetime.now(), end_date=datetime.now(), status=1, termination_reason='Mock reason', vendor=self.mock_vendor, menus=[ self.mock_menu, ]) self.mock_vendor_engagement_no_child = VendorEngagement( id=1, created_at=datetime.now(), updated_at=datetime.now(), location_id=location.id, start_date=datetime.now(), end_date=datetime.now(), status=1, termination_reason='Mock reason', vendor=self.mock_vendor)
def setUp(self): self.BaseSetUp() self.repo = OrderRepo() self.location = LocationFactory() self.meal_item = MealItemFactory(location_id=self.location.id) self.vendor_engagement = VendorEngagementFactory( location_id=self.location.id) self.menu = MenuFactory(location_id=self.location.id, main_meal_id=self.meal_item.id, vendor_engagement_id=self.vendor_engagement.id)
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_hard_delete_on_vendor_model(self): vendor = Vendor( name='Your name', address='Your Address', tel='12345678', contact_person='Contact Person', location=LocationFactory(), ) vendor.save() vendor_id = vendor.id vendor.delete() self.assertEquals(Vendor.query.get(vendor_id), None)
def test_creating_vendor_gets_logged(self): vendor = Vendor( name='Your name', address='Your Address', tel='12345678', contact_person='Contact Person', location=LocationFactory(), ) vendor.save() activity = [ activity.action_details for activity in Activity.query.all() ] self.assertTrue("created" in activity[0])
def test_new_meal_session_method_returns_new_meal_session_object(self): location = LocationFactory() meal_session = MealSessionFactory(location=location) new_meal_session = self.repo.new_meal_session( name=meal_session.name, start_time=meal_session.start_time, stop_time=meal_session.stop_time, date=meal_session.date, location_id=meal_session.location.id) self.assertIsInstance(new_meal_session, MealSession) self.assertEqual(new_meal_session.location_id, meal_session.location_id) self.assertEqual(new_meal_session.name, meal_session.name) self.assertEqual(new_meal_session.start_time, meal_session.start_time) self.assertEqual(new_meal_session.stop_time, meal_session.stop_time)
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_create_user_succeeds(self, mock_request_params, mock_get_location): location = LocationFactory() role = RoleFactory(name='test_role') with self.app.app_context(): mock_get_location.return_value = location.id mock_request_params.return_value = [ "Joseph", "Serunjogi", None, None, "-LXTuXlk2W4Gskt8KTte", role.id ] user_controller = UserController(self.request_context) # Act result = user_controller.create_user() # Assert assert result.status_code == 201 assert result.get_json()['msg'] == 'OK'
def test_create_user_fails_for_existing_user( self, mock_user_role_repo_new_user_role, # mock_user_repo_new_user, mock_user_repo_exists, mock_role_repo_find_first, mock_request_params, mock_get_location, ): location = LocationFactory() role = RoleFactory(name="test_role") with self.app.app_context(): mock_get_location.return_value = location.id mock_role_repo_find_first.return_value = self.mock_role mock_user_repo_exists.return_value = self.mock_user2 # mock_user_repo_new_user.return_value = None mock_user_role_repo_new_user_role.return_value = self.mock_user_role mock_request_params.return_value = [ "Joseph", "Serunjogi", self.mock_user2.email, role.id, "male", str(datetime.now()), 1, "password", ] user_controller = UserController(self.request_context) # Act result = user_controller.create_user() print(result) print(result.get_json()) # Assert assert result.status_code == 400 assert ( result.get_json()["msg"] == f"User with email '{self.mock_user2.email}' already exists")
def test_new_menu_method_returns_new_menu_object(self): location = LocationFactory() meal_item = MealItemFactory.create(location=location) vendor_engagement = VendorEngagementFactory() menu = MenuFactory(location=location, main_meal_id=meal_item.id, vendor_engagement=vendor_engagement) new_menu = self.repo.new_menu(menu.date.strftime('%Y-%m-%d'), menu.meal_period, menu.main_meal_id, menu.allowed_side, menu.allowed_protein, [1, 2], [3, 4], vendor_engagement.id, location.id) self.assertIsInstance(new_menu, Menu) self.assertEqual(new_menu.allowed_protein, menu.allowed_protein) self.assertEqual(new_menu.allowed_side, menu.allowed_protein) self.assertEqual(new_menu.meal_period, menu.meal_period) self.assertIsInstance(new_menu, Menu) self.assertEqual(new_menu.allowed_protein, menu.allowed_protein) self.assertEqual(new_menu.allowed_side, menu.allowed_protein) self.assertEqual(new_menu.meal_period, menu.meal_period)
def test_create_user_succeeds( self, mock_user_role_repo_new_user_role, mock_user_repo_new_user, mock_user_repo_exists, mock_role_repo_find_first, mock_request_params, mock_get_location, ): location = LocationFactory() role = RoleFactory(name="test_role") with self.app.app_context(): mock_get_location.return_value = location.id mock_role_repo_find_first.return_value = self.mock_role mock_user_repo_exists.return_value = None mock_user_repo_new_user.return_value = self.mock_user2 mock_user_role_repo_new_user_role.return_value = self.mock_user_role mock_request_params.return_value = [ "Joseph", "Serunjogi", "*****@*****.**", role.id, "male", str(datetime.now()), 1, "password", ] user_controller = UserController(self.request_context) # Act result = user_controller.create_user() # Assert assert result.status_code == 201 assert result.get_json()["msg"] == "OK"
def setUp(self): self.BaseSetUp() self.fake = Faker() vendor = VendorFactory() location = LocationFactory() self.mock_vendor_engagement = VendorEngagement( id=1, created_at=datetime.now(), updated_at=datetime.now(), vendor_id=vendor.id, location_id=location.id, start_date=datetime.now(), end_date=datetime.now(), status=1, termination_reason=self.fake.text()) self.mock_rating = VendorRating(id=1, created_at=datetime.now(), updated_at=datetime.now(), vendor_id=vendor.id, user_id=1, comment=self.fake.text(), service_date=datetime.now(), rating=1.2, channel='web', type_id=1, engagement_id=1, main_meal_id=1) self.mock_vendor_with_dependants = Vendor( id=1, created_at=datetime.now(), updated_at=datetime.now(), is_deleted=False, name=self.fake.name(), address=self.fake.address(), tel=self.fake.phone_number(), contact_person=self.fake.name(), is_active=True, location_id=location.id, ratings=[ self.mock_rating, ], engagements=[ self.mock_vendor_engagement, ]) self.mock_vendor = Vendor(id=1, created_at=datetime.now(), updated_at=datetime.now(), is_deleted=False, name=self.fake.name(), address=self.fake.address(), tel=self.fake.phone_number(), contact_person=self.fake.name(), is_active=True, location_id=location.id) self.mock_deleted_vendor = Vendor(id=1, created_at=datetime.now(), updated_at=datetime.now(), is_deleted=True, name=self.fake.name(), address=self.fake.address(), tel=self.fake.phone_number(), contact_person=self.fake.name(), is_active=True, location_id=location.id)
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() self.repo = VendorRepo() self.rating_repo = VendorRatingRepo() self.location = LocationFactory()
def setUp(self): self.BaseSetUp() self.repo = VendorEngagementRepo() self.location = LocationFactory() self.vendor = VendorFactory()
def test_create_order_rating_ok_response( self, mock_vendor_engagement_repo_get, mock_meal_item_repo_get, mock_auth_user, mock_vendor_rating_controller_request_params, mock_order_repo_get, mock_vendor_repo_update_average_rating ): '''Test create_order_rating when order has already been rated. ''' # Arrange vendor = VendorFactory() location = LocationFactory() vendor_engagement = VendorEngagementFactory() meal_item = MealItemFactory() menu = MenuFactory() with self.app.app_context(): mock_meal_item = MealItem( id=1, created_at=datetime.now(), updated_at=datetime.now(), meal_type='main', name='Mock meal', image='', location_id=location.id ) mock_vendor_engagement = VendorEngagement( id=1, created_at=datetime.now(), updated_at=datetime.now(), vendor_id=vendor.id, location_id=location.id, start_date=datetime.now(), end_date=(datetime.now() + timedelta(days=5)), status=1, termination_reason='Mock reason' ) mock_order = Order( id=1, created_at=datetime.now(), updated_at=datetime.now(), user_id='user_id', date_booked_for=datetime.now(), date_booked=datetime.now(), channel='web', meal_period='lunch', order_status='booked', has_rated=False, menu_id=menu.id, location_id=location.id ) mock_vendor_rating_controller_request_params.return_value = ( 2, meal_item.id, vendor_engagement.id, 'Mock comment', 3, '2019-02-06', 'web' ) mock_auth_user.return_value = 1 mock_meal_item_repo_get.return_value = mock_meal_item mock_vendor_engagement_repo_get.return_value = \ mock_vendor_engagement mock_order_repo_get.return_value = mock_order vendor_rating_controller = VendorRatingController( self.request_context ) mock_vendor_repo_update_average_rating.return_value = None # Act result = vendor_rating_controller.create_order_rating() # Assert assert result.status_code == 201 assert result.get_json()['msg'] == 'Rating successful'
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')