def test_list_deleted_vendors(self): vendors = VendorFactory.create_batch(4, is_deleted=True) vendors = VendorFactory.create_batch(3, is_deleted=False) response = self.client().get(self.make_url('/vendors/deleted/'), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) payload = response_json['payload'] self.assert200(response) self.assertEqual(len(payload['vendors']), 4) self.assertEqual(payload['vendors'][0]['isDeleted'], True)
def test_create_vendor_rating_endpoint_invalid_token(self): rating = VendorRatingFactory.build() order_id = OrderFactory.create().id vendor_id = VendorFactory.create().id engagement_id = VendorEngagementFactory.create().id main_meal_id = MealItemFactory.create().id data = { 'mainMealId': main_meal_id, 'vendorId': vendor_id, 'engagementId': engagement_id, 'serviceDate': datetime.strftime(rating.service_date, '%Y-%m-%d'), 'ratingType': rating.rating_type, 'orderId': order_id, 'user_id': rating.user_id, 'rating': rating.rating, 'comment': rating.comment, 'channel': rating.channel } response = self.client().post( self.make_url('/ratings/order/'), data=self.encode_to_json_string(data), headers={ 'Content-Type': 'application/json', 'X-Location': '1', 'Authorization': 'Bearer vnvhnv.hhbhjvjvcbcgff.cggnncbnnf' }) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assertEqual(response.status_code, 400) self.assertEqual(response_json['msg'], 'Error Decoding')
def test_order_allocations(app): org = OrganizationFactory.create() u1 = UserFactory.create(organization=org) u2 = UserFactory.create(organization=org) u3 = UserFactory.create(organization=org) vendor = VendorFactory.create() order = Order( ordered_by=u1, vendor=vendor, for_date=date.today(), placed_at=datetime.now(), ) db.session.add(order) db.session.commit() assert order.total_amount == Decimal("0.00") oc1 = OrderContribution(order=order, user=u1, amount=Decimal("10.80")) db.session.add(oc1) db.session.commit() assert order.total_amount == Decimal("10.80") oc2 = OrderContribution(order=order, user=u2, amount=Decimal("2.50")) db.session.add(oc2) db.session.commit() assert order.total_amount == Decimal("13.30") oc3 = OrderContribution(order=order, user=u3, amount=Decimal("4.75")) db.session.add(oc3) db.session.commit() assert order.total_amount == Decimal("18.05")
def test_create_vendor_rating_endpoint_no_token(self): rating = VendorRatingFactory.build() order_id = OrderFactory.create().id vendor_id = VendorFactory.create().id engagement_id = VendorEngagementFactory.create().id main_meal_id = MealItemFactory.create().id data = { 'mainMealId': main_meal_id, 'vendorId': vendor_id, 'engagementId': engagement_id, 'serviceDate': datetime.strftime(rating.service_date, '%Y-%m-%d'), 'ratingType': rating.rating_type, 'orderId': order_id, 'user_id': rating.user_id, 'rating': rating.rating, 'comment': rating.comment, 'channel': rating.channel } response = self.client().post(self.make_url('/ratings/order/'), data=self.encode_to_json_string(data), headers=self.headers_without_token()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assertEqual(response.status_code, 400) self.assertEqual(response_json['msg'], 'Authorization Header is Expected')
def test_create_vendor_rating_endpoint(self): rating = VendorRatingFactory.build() vendor_id = VendorFactory.create().id engagement_id = VendorEngagementFactory.create().id data = { 'vendorId': vendor_id, 'engagementId': engagement_id, 'user_id': rating.user_id, 'rating': rating.rating, 'comment': rating.comment, 'channel': rating.channel } response = self.client().post(self.make_url('/ratings/'), data=self.encode_to_json_string(data), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assertEqual(response.status_code, 201) self.assertJSONKeyPresent(response_json, 'payload') self.assertEqual(payload['rating']['vendorId'], vendor_id) self.assertEqual(payload['rating']['userId'], rating.user_id) self.assertEqual(payload['rating']['rating'], rating.rating) self.assertEqual(payload['rating']['comment'], rating.comment) self.assertEqual(payload['rating']['channel'], rating.channel)
def test_delete_engagement_with_associated_menus(self): datetime.now().date() location = LocationFactory() location.save() meal_item = MealItemFactory(location=location) vendor = VendorFactory(location=location) engagement = VendorEngagementFactory.create(location=location, vendor=vendor) menu_repo = MenuRepo() menu_repo.new_menu( date='2018-10-15', meal_period='lunch', main_meal_id=meal_item.id, allowed_side=1, allowed_protein=1, side_items=[2], protein_items=[3], vendor_engagement_id=engagement.id, location_id=location.id ) role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='delete_engagement', role=role) UserRoleFactory.create(user_id=user_id, role=role, location_id=location.id) response = self.client().delete(self.make_url(f'/engagements/{engagement.id}'), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) self.assert400(response) self.assertEqual(response_json['msg'], 'This engagement cannot be deleted because it has a child object')
def test_order_allocations(app): org = OrganizationFactory.create() u1 = UserFactory.create(organization=org) u2 = UserFactory.create(organization=org) u3 = UserFactory.create(organization=org) vendor = VendorFactory.create() order = Order( ordered_by=u1, vendor=vendor, for_date=date.today(), placed_at=datetime.now(), ) db.session.add(order) db.session.commit() assert order.total_amount == Decimal("0.00") oc1 = OrderContribution(order=order, user=u1, amount=Decimal("10.80")) db.session.add(oc1) db.session.commit() assert order.total_amount == Decimal("10.80") oc2 = OrderContribution(order=order, user=u2, amount=Decimal("2.50")) db.session.add(oc2) db.session.commit() assert order.total_amount == Decimal("13.30") oc3 = OrderContribution(order=order, user=u3, amount=Decimal("4.75")) db.session.add(oc3) db.session.commit() assert order.total_amount == Decimal("18.05")
def test_create_vendor_endpoint(self): location = LocationFactory(id=self.headers()['X-Location']) vendor = VendorFactory.build() data = { 'name': vendor.name, 'address': vendor.address, 'tel': vendor.tel, 'isActive': vendor.is_active, 'contactPerson': vendor.contact_person } response = self.client().post(self.make_url('/vendors/'), data=self.encode_to_json_string(data), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assertEqual(response.status_code, 201) self.assertJSONKeyPresent(response_json, 'payload') self.assertEqual(payload['vendor']['name'], vendor.name) self.assertEqual(payload['vendor']['tel'], vendor.tel) self.assertEqual(payload['vendor']['contactPerson'], vendor.contact_person) self.assertEqual(payload['vendor']['address'], vendor.address)
def test_create_vendor_engagement_endpoint(self): vendor = VendorFactory() engagement = VendorEngagementFactory.build() start_date = str(engagement.start_date) end_date = str(engagement.end_date) data = {'vendorId': vendor.id, 'startDate': start_date, 'endDate': end_date, 'status': 1, 'terminationReason': engagement.termination_reason, 'location_id': engagement.location_id} response = self.client().post(self.make_url('/engagements/'), data=self.encode_to_json_string(data), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) payload = response_json['payload'] self.assertEqual(response.status_code, 201) self.assertJSONKeysPresent(payload, 'engagement') self.assertJSONKeysPresent(payload['engagement'], 'endDate', 'startDate', 'vendor', 'terminationReason', 'timestamps') self.assertEqual(payload['engagement']['vendor']['id'], vendor.id) self.assertEqual(payload['engagement']['vendor']['name'], vendor.name) self.assertEqual(payload['engagement']['vendor']['tel'], vendor.tel) # Assert The Year in engagement.start_date exists in the returned start_date response self.assertTrue(payload['engagement']['startDate'].find(start_date.split('-')[0]) > -1) self.assertTrue(payload['engagement']['startDate'].find(start_date.split('-')[2]) > -1) # Assert The Year in engagement.end_date exists in the returned end_date response self.assertTrue(payload['engagement']['endDate'].find(end_date.split('-')[0]) > -1) self.assertTrue(payload['engagement']['endDate'].find(end_date.split('-')[2]) > -1)
def test_create_order_rating_endpoint(self): rating = VendorRatingFactory.build() order_id = OrderFactory.create().id vendor_id = VendorFactory.create().id engagement_id = VendorEngagementFactory.create().id main_meal_id = MealItemFactory.create().id data = { 'mainMealId': main_meal_id, 'vendorId': vendor_id, 'engagementId': engagement_id, 'serviceDate': datetime.strftime(rating.service_date, '%Y-%m-%d'), 'ratingType': rating.rating_type, 'orderId': order_id, 'user_id': rating.user_id, 'rating': rating.rating, 'comment': rating.comment, 'channel': rating.channel } response = self.client().post(self.make_url('/ratings/order/'), data=self.encode_to_json_string(data), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assertEqual(response.status_code, 201) self.assertJSONKeyPresent(response_json, 'payload') self.assertEqual(payload['rating']['userId'], rating.user_id) self.assertEqual(payload['rating']['rating'], rating.rating) self.assertEqual(payload['rating']['comment'], rating.comment) self.assertEqual(payload['rating']['channel'], rating.channel)
def test_list_vendor_ratings_endpoint(self): '''Test that users with the right permission can view list of vendor ratings''' role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() vendor_id = VendorFactory.create().id permission = PermissionFactory.create(keyword='view_ratings', role_id=role.id) user_role = UserRoleFactory.create(user_id=user_id, role_id=role.id) for i in range(5): vendor_rating = VendorRating(vendor_id=vendor_id, user_id=user_id, comment=f'comment-{i}', rating=f'{i}', channel='slack') vendor_rating.save() response = self.client().get( self.make_url(f'/ratings/vendor/{vendor_id}'), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assert200(response) self.assertEqual(len(payload['ratings']), 5) self.assertJSONKeysPresent(payload['ratings'][0], 'vendorId', 'userId', 'id', 'comment', 'rating', 'channel')
def test_create_menu_endpoint_with_wrong_permission(self): """ Test for creation of a new menu without permmission """ role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='delete_menu', role=role) UserRoleFactory.create(user_id=user_id, role=role) menu = MenuFactory.build() main_meal_item = MealItemFactory.build() side_meal_item = MealItemFactory.build() protein_meal_item = MealItemFactory.build() vendor = VendorFactory.build() vendor_engagement = VendorEngagementFactory.build(vendor_id=vendor.id) data = { 'date': menu.date.strftime('%Y-%m-%d'), 'mealPeriod': menu.meal_period, 'mainMealId': main_meal_item.id, 'allowedSide': menu.allowed_side, 'allowedProtein': menu.allowed_protein, 'sideItems': [side_meal_item.id], 'proteinItems': [protein_meal_item.id], 'vendorEngagementId': vendor_engagement.id } response = self.client().post(self.make_url('/admin/menus/'), \ data=self.encode_to_json_string(data), headers=self.headers()) self.assert400(response)
def test_update_vendors_endpoint(self): vendor = VendorFactory.create() data = { 'name': 'Jays Place', 'address': '123 Awesome Ave', 'tel': '10101010101', 'isActive': True, 'contactPerson': 'Joseph Cobhams' } response = self.client().put(self.make_url('/vendors/{}'.format( vendor.id)), data=self.encode_to_json_string(data), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assert200(response) self.assertEqual(payload['vendor']['name'], data['name']) self.assertEqual(payload['vendor']['address'], data['address']) self.assertEqual(payload['vendor']['tel'], data['tel']) self.assertEqual(payload['vendor']['contactPerson'], data['contactPerson']) '''Test invalid update request''' # User arbitrary value of 100 as the Vendor ID response = self.client().put(self.make_url('/vendors/100'), data=self.encode_to_json_string(data), headers=self.headers()) self.assert400(response)
def test_list_suspended_vendors(self): location = LocationFactory(id=self.headers()['X-Location']) vendors = VendorFactory.create_batch(3, is_active=True, location=location) vendors = VendorFactory.create_batch(4, is_active=False, location=location) response = self.client().get(self.make_url('/vendors/suspended/'), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assert200(response) self.assertEqual(len(payload['vendors']), 4) self.assertEqual(payload['vendors'][0]['isActive'], False)
def test_un_suspend_vendor(self): vendor = VendorFactory.create(is_active=False) response = self.client().put(self.make_url(f'/vendors/un_suspend/{vendor.id}'), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) payload = response_json['payload'] self.assert200(response) self.assertEqual(payload['vendor']['isActive'], True)
def test_update_menu_endpoint_with_wrong_protein_item_values(self): location = LocationFactory.create() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='update_menu', role=role) UserRoleFactory.create(user_id=user_id, role=role) meal_item_repo = MealItemRepo() main_meal_item = meal_item_repo.new_meal_item(name="main1", image="image1", meal_type="main", location_id=location.id) side_meal_item = meal_item_repo.new_meal_item(name="side1", image="image11", meal_type="side", location_id=location.id) protein_meal_item = meal_item_repo.new_meal_item( name="protein1", image="image12", meal_type="protein", location_id=location.id) vendor = VendorFactory.build() vendor_engagement = VendorEngagementFactory.build(vendor_id=vendor.id) vendor_engagement.save() menu = MenuFactory.create(main_meal_id=main_meal_item.id, side_items=side_meal_item.id, protein_items=protein_meal_item.id) menu.vendor_engagement_id = vendor_engagement.id menu.save() data = { 'date': menu.date.strftime('%Y-%m-%d'), 'mealPeriod': menu.meal_period, 'mainMealId': main_meal_item.id, 'allowedSide': 2, 'allowedProtein': 2, 'sideItems': [side_meal_item.id], 'proteinItems': [1000], 'vendorEngagementId': vendor_engagement.id } response = self.client().put(self.make_url('/admin/menus/{}'.format( menu.id)), data=self.encode_to_json_string(data), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assert400(response) self.assertEqual( response_json['msg'], 'Bad Request - proteinItems contains invalid id(s) for meal_item table ' )
def test_list_engagements_by_vendor_endpoint(self): vendor = VendorFactory.create() VendorEngagementFactory.create(vendor=vendor) response = self.client().get(self.make_url(f'/engagements/vendor/{vendor.id}'), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) self.assert200(response) self.assertEqual(response_json['msg'], 'OK') self.assertEqual(response_json['payload']['engagements'][0]['vendor']['id'], vendor.id)
def test_delete_vendor_endpoint_with_wrong_vendor_id(self): vendor = VendorFactory.create() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() permission = PermissionFactory.create(keyword='delete_vendor', role_id=role.id) user_role = UserRoleFactory.create(user_id=user_id, role_id=role.id) response = self.client().delete(self.make_url(f'/vendors/-576A'), headers=self.headers()) self.assert404(response)
def test_update_menu_endpoint(self): """Test update of a menu""" location = LocationFactory() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='update_menu', role=role) UserRoleFactory.create(user_id=user_id, role=role) meal_item_repo = MealItemRepo() main_meal_item = meal_item_repo.new_meal_item(name="main1", image="image1", meal_type="main", location_id=location.id) side_meal_item = meal_item_repo.new_meal_item(name="side1", image="image11", meal_type="side", location_id=location.id) protein_meal_item = meal_item_repo.new_meal_item( name="protein1", image="image12", meal_type="protein", location_id=location.id) vendor = VendorFactory.build() vendor_engagement = VendorEngagementFactory.build(vendor_id=vendor.id) vendor_engagement.save() menu = MenuFactory.create(main_meal_id=main_meal_item.id, side_items=side_meal_item.id, protein_items=protein_meal_item.id, location=location) menu.save() data = { 'date': menu.date.strftime('%Y-%m-%d'), 'mealPeriod': menu.meal_period, 'mainMealId': main_meal_item.id, 'allowedSide': 2, 'allowedProtein': 2, 'sideItems': [side_meal_item.id], 'proteinItems': [protein_meal_item.id], 'vendorEngagementId': vendor_engagement.id } response = self.client().put(self.make_url('/admin/menus/{}'.format( menu.id)), data=self.encode_to_json_string(data), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assert200(response) self.assertEqual(payload['menu']['allowedProtein'], data['allowedProtein']) self.assertEqual(payload['menu']['allowedSide'], data['allowedSide'])
def test_update_menu_endpoint_with_wrong_permission(self): role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='wrong_permission', role_id=role.id) UserRoleFactory.create(user_id=user_id, role_id=role.id) meal_item_repo = MealItemRepo() main_meal_item = meal_item_repo.new_meal_item(name="main1", description="descr1", image="image1", meal_type="main", location_id=1) side_meal_item = meal_item_repo.new_meal_item(name="side1", description="descr11", image="image11", meal_type="side", location_id=1) protein_meal_item = meal_item_repo.new_meal_item(name="protein1", description="descr11", image="image12", meal_type="protein", location_id=1) vendor = VendorFactory.build() vendor_engagement = VendorEngagementFactory.build(vendor_id=vendor.id) menu = MenuFactory.create(main_meal_id=main_meal_item.id, side_items=side_meal_item.id, protein_items=protein_meal_item.id) menu.vendor_engagement_id = vendor_engagement.id data = { 'date': menu.date.strftime('%Y-%m-%d'), 'mealPeriod': menu.meal_period, 'mainMealId': 10000, 'allowedSide': 2, 'allowedProtein': 2, 'sideItems': [side_meal_item.id], 'proteinItems': [protein_meal_item.id], 'vendorEngagementId': vendor_engagement.id } response = self.client().put(self.make_url('/admin/menus/{}'.format( menu.id)), data=self.encode_to_json_string(data), headers=self.headers()) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assert400(response) self.assertEqual(response_json['msg'], 'Access Error - Permission Denied')
def test_new_vendor_method_returns_new_vendor_object(self): vendor = VendorFactory.build() new_vendor = self.repo.new_vendor(vendor.name, vendor.address, vendor.tel, vendor.is_active, vendor.contact_person, vendor.location_id) self.assertIsInstance(new_vendor, Vendor) self.assertEqual(vendor.name, new_vendor.name) self.assertEqual(vendor.tel, new_vendor.tel) self.assertEqual(vendor.address, new_vendor.address) self.assertEqual(vendor.contact_person, new_vendor.contact_person) self.assertIsNot(new_vendor.id, 0)
def test_rating_today_meal_fails(self): vendor = VendorFactory.create() engagement = VendorEngagementFactory.create(vendor=vendor) engagement_rating = VendorRatingFactory.build(engagement_id=engagement.id) rating_data = {'rating': engagement_rating.rating, 'engagementId': engagement_rating.engagement_id, 'serviceDate': datetime.strftime(datetime.now().date(), '%Y-%m-%d'), 'mainMealId': MealItemFactory.create().id} response = self.client().post(self.make_url(f'/ratings/order/'), data=self.encode_to_json_string(rating_data), headers=self.headers()) self.assertEqual(response.status_code, 400)
def test_list_vendors_endpoint(self): location = LocationFactory(id=self.headers()['X-Location']) # Create Three Dummy Vendors vendors = VendorFactory.create_batch(3, location_id=location.id) page_id = 1 response = self.client().get(self.make_url('/vendors/'), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) payload = response_json['payload'] self.assert200(response) self.assertEqual(len(payload['vendors']), 3) self.assertJSONKeysPresent(payload['vendors'][0], 'name', 'tel', 'id', 'address', 'contactPerson','timestamps')
def test_delete_vendor_endpoint_without_right_permission(self): vendor = VendorFactory.create() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() permission = PermissionFactory.create(keyword='delete_vendor', role_id=100) user_role = UserRoleFactory.create(user_id=user_id, role_id=role.id) response = self.client().delete(self.make_url(f'/vendors/{vendor.id}'), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) self.assert400(response) self.assertEqual(response_json['msg'], 'Access Error - No Permission Granted')
def test_list_rating_endpoint(self): vendor = VendorFactory.create() engagement = VendorEngagementFactory.create(vendor=vendor) rating = VendorRatingFactory.create(engagement=engagement, vendor=vendor) create_user_role('view_ratings') now = datetime.now() response = self.client().get(self.make_url(f'/ratings/{now.strftime("%Y-%m-%d")}'), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) self.assert404(response) self.assertEqual(response_json['msg'], 'No ratings for this date')
def test_create_menu_endpoint_with_right_permission_and_input(self): """Test for creation of new menu""" location = LocationFactory() menu = MenuFactory.build() main_meal_item = MealItemFactory.create() side_meal_item = MealItemFactory.create() protein_meal_item = MealItemFactory.create() vendor = VendorFactory.build() vendor_engagement = VendorEngagementFactory.build(vendor=vendor) vendor_engagement.save() role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='create_menu', role=role) UserRoleFactory.create(user_id=user_id, role=role) data = { 'date': menu.date.strftime('%Y-%m-%d'), 'mealPeriod': menu.meal_period, 'mainMealId': main_meal_item.id, 'allowedSide': menu.allowed_side, 'allowedProtein': menu.allowed_protein, 'sideItems': [side_meal_item.id], 'proteinItems': [protein_meal_item.id], 'vendorEngagementId': vendor_engagement.id } headers = self.headers() headers.update({'X-Location': location.id}) response = self.client().post(self.make_url('/admin/menus/'), \ data=self.encode_to_json_string(data), headers=headers) response_json = self.decode_from_json_string( response.data.decode('utf-8')) payload = response_json['payload'] self.assertEqual(response.status_code, 201) self.assertJSONKeysPresent(payload, 'menu') self.assertJSONKeysPresent(payload['menu'], 'mainMeal', 'proteinItems', 'sideItems', 'allowedProtein', 'allowedSide', 'date', 'id', 'mealPeriod', 'timestamps', 'vendorEngagementId') self.assertEqual(payload['menu']['vendorEngagementId'], vendor_engagement.id) self.assertEqual(payload['menu']['mealPeriod'], menu.meal_period) self.assertEqual(payload['menu']['mainMealId'], main_meal_item.id) self.assertEqual(payload['menu']['allowedSide'], menu.allowed_side) self.assertEqual(payload['menu']['allowedProtein'], menu.allowed_protein)
def test_create_menu_endpoint_with_existing_main_meal_item(self): """Multiple menus with same same main meal id should not exist on the same day""" location = LocationFactory.create() main_meal_item = MealItemFactory.create(location=location) side_meal_item = MealItemFactory.create(location=location) protein_meal_item = MealItemFactory.create(location=location) headers = self.headers() headers.update({'X-Location': location.id}) menu = MenuFactory.build(location=location) menu.save() vendor = VendorFactory.build(location=location) vendor_engagement = VendorEngagementFactory.build(vendor=vendor, location=location) vendor_engagement.save() data = { 'date': menu.date.strftime('%Y-%m-%d'), 'mealPeriod': menu.meal_period, 'mainMealId': main_meal_item.id, 'allowedSide': menu.allowed_side, 'allowedProtein': menu.allowed_protein, 'sideItems': [side_meal_item.id], 'proteinItems': [protein_meal_item.id], 'vendorEngagementId': vendor_engagement.id, 'location_id': location.id } existing_menu = MenuRepo().new_menu( menu.date.strftime('%Y-%m-%d'), menu.meal_period, main_meal_item.id, menu.allowed_side, menu.allowed_protein, [side_meal_item.id], [protein_meal_item.id], vendor_engagement.id, location.id) role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() PermissionFactory.create(keyword='create_menu', role=role) UserRoleFactory.create(user_id=user_id, role=role) response = self.client().post(self.make_url('/admin/menus/'), \ data=self.encode_to_json_string(data), headers=headers) response_json = self.decode_from_json_string( response.data.decode('utf-8')) self.assertEqual(response.status_code, 400) self.assertEqual( response_json['msg'], 'You can\'t create multiple menus with same main item on the same day' )
def test_delete_vendor_with_associated_engagement(self): current_date = datetime.datetime.now().date() vendor = VendorFactory.create() vendor_engagement_repo = VendorEngagementRepo() vendor_engagement = vendor_engagement_repo.new_vendor_engagement(vendor_id=vendor.id, start_date=current_date, location_id=self.headers()['X-Location']) role = RoleFactory.create(name='admin') user_id = BaseTestCase.user_id() permission = PermissionFactory.create(keyword='delete_vendor', role_id=role.id) user_role = UserRoleFactory.create(user_id=user_id, role_id=role.id) response = self.client().delete(self.make_url(f'/vendors/{vendor.id}'), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) self.assert400(response) self.assertEqual(response_json['msg'], 'Vendor cannot be deleted because it has a child object')
def test_create_vendor_rating_endpoint(self): vendor = VendorFactory.create() engagement = VendorEngagementFactory.create(vendor=vendor) engagement_rating = VendorRatingFactory.build(engagement_id=engagement.id) rating_data = {'rating': engagement_rating.rating, 'engagementId': engagement_rating.engagement_id, 'serviceDate': engagement_rating.service_date.strftime('%Y-%m-%d')} response = self.client().post(self.make_url(f'/ratings/'), data=self.encode_to_json_string(rating_data), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) self.assertEqual(response.status_code, 201) self.assertEqual(response_json['msg'], 'Rating created') self.assertEqual(response_json['payload']['rating']['id'], 1)
def test_immediate_past_engagement_no_past_engagement(self): location = LocationFactory() location.save() vendor = VendorFactory.create(location=location) vendor.save() engagement1 = VendorEngagementFactory.create(vendor_id=vendor.id, end_date=datetime.now()+timedelta(10), location=location) engagement2 = VendorEngagementFactory.create(vendor_id=vendor.id, end_date=datetime.now()+timedelta(14), location=location) engagement1.save() engagement2.save() response = self.client().get(self.make_url(f'/engagements/past/{location.id}'), headers=self.headers()) response_json = self.decode_from_json_string(response.data.decode('utf-8')) self.assert404(response) self.assertEqual(response_json['msg'], 'No past engagement for this location')
def test_update_vendor_rating(self): vendor = VendorFactory.build() new_vendor = self.repo.new_vendor(vendor.name, vendor.address, vendor.tel, vendor.is_active, vendor.contact_person, vendor.location_id) vendor_rating = VendorRatingFactory.build() self.rating_repo.new_rating( new_vendor.id, vendor_rating.user_id, 4, vendor_rating.service_date, vendor_rating.rating_type, vendor_rating.type_id, vendor_rating.engagement_id, vendor_rating.channel, vendor_rating.comment) self.repo.update_vendor_average_rating(new_vendor.id) self.assertEqual(new_vendor.average_rating, 4)
def test_create(client): user = UserFactory.create() vendor = VendorFactory.create() db.session.commit() response = client.post('/api/orders', data={ "contributed_by": user.id, "contributed_amount": "8.50", "ordered_by_id": user.id, "vendor_id": vendor.id, }) assert response.status_code == 201 assert response.headers["Access-Control-Allow-Origin"] == "*" assert "Location" in response.headers obj = json.loads(response.get_data(as_text=True)) assert "id" in obj url = response.headers["Location"] path = urlparse(url).path resp2 = client.get(path) assert resp2.status_code == 200 created = json.loads(resp2.get_data(as_text=True)) assert created["contributions"][0]["amount"] == "8.50"
def vendors(app): v1 = VendorFactory.create() v2 = VendorFactory.create() db.session.commit() return [v1, v2]