class TestVendorEngagementRepo(BaseTestCase): def setUp(self): self.BaseSetUp() self.repo = VendorEngagementRepo() def test_new_vendor_engagement_method_returns_new_vendor_engagement_object( self): engagement = VendorEngagementFactory.build() new_engagement = self.repo.new_vendor_engagement( engagement.vendor.id, engagement.start_date, engagement.location_id, engagement.end_date, 1, 1) self.assertIsInstance(new_engagement, VendorEngagement) self.assertEqual(engagement.vendor_id, new_engagement.vendor_id) self.assertEqual(new_engagement.status, 1) self.assertEqual(engagement.start_date, new_engagement.start_date) self.assertEqual(engagement.end_date, new_engagement.end_date) def test_get_existing_engagement_when_engagment_exists(self): engagement = VendorEngagementFactory.build() self.repo.new_vendor_engagement(engagement.vendor.id, engagement.start_date, engagement.location_id, engagement.end_date, 1, 1) count = self.repo.get_existing_engagement(engagement.start_date) self.assertEqual(count, 1) def test_get_existing_engagement_when_engagment_does_not_exists(self): engagement = VendorEngagementFactory.build() count = self.repo.get_existing_engagement(engagement.start_date) self.assertEqual(count, 0) def test_get_engagement_by_date_when_engagment_does_exists(self): engagement = VendorEngagementFactory.build() new_engagement = self.repo.new_vendor_engagement( engagement.vendor.id, engagement.start_date, engagement.location_id, engagement.end_date, 1, 1) paginated_result = self.repo.get_engagement_by_date() self.assertIsInstance(paginated_result.items[0], VendorEngagement) self.assertEqual(paginated_result.items[0], new_engagement) def test_new_vendor_engagement_raises_exception_with_invalid_date_format( self): engagement = VendorEngagementFactory.build() invalid_start_date = 'invalid date format' with self.assertRaises(Exception) as e: self.repo.new_vendor_engagement(engagement.vendor.id, invalid_start_date, engagement.location_id, engagement.end_date, 1, 1)
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_run_24_hourly_method(self): end_date = (datetime.now() - timedelta(weeks=1)).date() new_engagement = VendorEngagementFactory.create(end_date=end_date) temp_engagement_id = new_engagement.id Cron(self.app).run_24_hourly() with self.app.app_context(): engagement = VendorEngagementRepo().get(temp_engagement_id) self.assertEqual(engagement.status, 0)
class VendorController(BaseController): def __init__(self, request): BaseController.__init__(self, request) self.vendor_repo = VendorRepo() self.vendor_engagement_repo = VendorEngagementRepo() self.vendor_rating_repo = VendorRatingRepo() def list_vendors(self): location = Auth.get_location() vendors = self.vendor_repo.filter_by(is_deleted=False, is_active=True, location_id=location) vendors_list = [vendor.serialize() for vendor in vendors.items] return self.handle_response('OK', payload={ 'vendors': vendors_list, 'meta': self.pagination_meta(vendors) }) def list_deleted_vendors(self): location = Auth.get_location() vendors = self.vendor_repo.filter_by(is_deleted=True, location_id=location) vendors_list = [vendor.serialize() for vendor in vendors.items] return self.handle_response('OK', payload={ 'vendors': vendors_list, 'meta': self.pagination_meta(vendors) }) def list_suspended_vendors(self): location = Auth.get_location() vendors = self.vendor_repo.filter_by(is_deleted=False, is_active=False, location_id=location) vendors_list = [vendor.serialize() for vendor in vendors.items] return self.handle_response('OK', payload={ 'vendors': vendors_list, 'meta': self.pagination_meta(vendors) }) def get_vendor(self, vendor_id): vendor = self.vendor_repo.get(vendor_id) if vendor: vendor = vendor.serialize() return self.handle_response('OK', payload={'vendor': vendor}) else: return self.handle_response( 'Bad Request - Invalid or Missing vendor_id', status_code=400) def create_vendor(self): location = Auth.get_location() name, tel, address, is_active, contact_person = self.request_params( 'name', 'tel', 'address', 'isActive', 'contactPerson') vendor = self.vendor_repo.new_vendor(name, address, tel, is_active, contact_person, location).serialize() return self.handle_response('OK', payload={'vendor': vendor}, status_code=201) def update_vendor(self, vendor_id): name, tel, address, is_active, contact_person = self.request_params( 'name', 'tel', 'address', 'isActive', 'contactPerson') vendor = self.vendor_repo.get(vendor_id) if vendor: updates = {} if name: updates['name'] = name if tel: updates['tel'] = tel if address: updates['address'] = address if is_active: updates['is_active'] = is_active if contact_person: updates['contact_person'] = contact_person self.vendor_repo.update(vendor, **updates) return self.handle_response('OK', payload={'vendor': vendor.serialize()}) return self.handle_response('Invalid or incorrect vendor_id provided', status_code=400) def suspend_vendor(self, vendor_id): vendor = self.vendor_repo.get(vendor_id) if vendor: updates = {} updates['is_active'] = 0 self.vendor_repo.update(vendor, **updates) return self.handle_response('OK', payload={'vendor': vendor.serialize()}) return self.handle_response('Invalid or incorrect vendor_id provided', status_code=400) def un_suspend_vendor(self, vendor_id): vendor = self.vendor_repo.get(vendor_id) if vendor: updates = {} updates['is_active'] = 1 self.vendor_repo.update(vendor, **updates) return self.handle_response('OK', payload={'vendor': vendor.serialize()}) return self.handle_response('Invalid or incorrect vendor_id provided', status_code=400) def delete_vendor(self, vendor_id): vendor = self.vendor_repo.get(vendor_id) if vendor: if vendor.is_deleted: return self.handle_response('Vendor has already been deleted', status_code=400) if any(not dependent.is_deleted for dependent in (vendor.engagements or vendor.ratings)): return self.handle_response( 'Vendor cannot be deleted because it has a child object', status_code=400) updates = {} updates['is_deleted'] = True self.vendor_repo.update(vendor, **updates) engagements = self.vendor_engagement_repo.filter_by( vendor_id=vendor_id) for e in engagements.items: updates_engagement = {} updates_engagement['is_deleted'] = True self.vendor_engagement_repo.update(e, **updates_engagement) return self.handle_response('Vendor deleted', payload={"status": "success"}) return self.handle_response('Invalid or incorrect vendor_id provided', status_code=400)
def __init__(self, request): BaseController.__init__(self, request) self.vendor_repo = VendorRepo() self.vendor_engagement_repo = VendorEngagementRepo() self.vendor_rating_repo = VendorRatingRepo()
class VendorEngagementController(BaseController): def __init__(self, request): BaseController.__init__(self, request) self.vendor_engagement_repo = VendorEngagementRepo() self.vendor_repo = VendorRepo() def list_vendor_engagements(self): location = Auth.get_location() engagements = self.vendor_engagement_repo.filter_by_desc( self.vendor_engagement_repo._model.start_date, is_deleted=False, location_id=location) engagements_list = [] for e in engagements.items: engagement = e.serialize() engagement['vendor'] = e.vendor.serialize() engagements_list.append(engagement) return self.handle_response('OK', payload={ 'engagements': engagements_list, 'meta': self.pagination_meta(engagements) }) def list_vendor_engagements_by_vendor(self, vendor_id): vendor = self.vendor_repo.get(vendor_id) if vendor.is_deleted is True: return self.handle_response('Invalid Vendor', status_code=400) if vendor.is_active is False: return self.handle_response('Vendor is disabled', status_code=400) engagements = self.vendor_engagement_repo.filter_by( vendor_id=vendor_id) engagements_list = [] for e in engagements.items: engagement = e.serialize() engagement['vendor'] = e.vendor.serialize() engagements_list.append(engagement) return self.handle_response('OK', payload={ 'engagements': engagements_list, 'meta': self.pagination_meta(engagements) }) def upcoming_vendor_engagements(self): location = Auth.get_location() engagements = self.vendor_engagement_repo.get_engagement_by_date() engagements_list = [] for e in engagements.items: if e.location_id == location: engagement = e.serialize() engagement['vendor'] = e.vendor.serialize() engagements_list.append(engagement) return self.handle_response('OK', payload={ 'engagements': engagements_list, 'meta': self.pagination_meta(engagements) }) def get_vendor_engagement(self, engagement_id): engagement = self.vendor_engagement_repo.get(engagement_id) if engagement: e = engagement.serialize() e['vendor'] = engagement.vendor.serialize() return self.handle_response('OK', payload={'engagement': e}) else: return self.handle_response('Bad Request', status_code=400) def create_vendor_engagement(self): vendor_id, start_date, end_date, status = self.request_params( 'vendorId', 'startDate', 'endDate', 'status') vendor = self.vendor_repo.get(vendor_id) start_date = datetime.strptime(start_date, '%Y-%m-%d').date() end_date = datetime.strptime(end_date, '%Y-%m-%d').date() existing_engagement = self.vendor_engagement_repo.get_existing_engagement( start_date=start_date, end_date=end_date) if existing_engagement > 0: return self.handle_response( 'An engagement already exists for this period. Kindly disable engagement first.', status_code=400) if vendor: engagement = self.vendor_engagement_repo.new_vendor_engagement( vendor_id, start_date, vendor.location_id, end_date, status) e = engagement.serialize() e['vendor'] = engagement.vendor.serialize() return self.handle_response('OK', payload={'engagement': e}, status_code=201) return self.handle_response('Invalid vendor_id provided', status_code=400) def update_vendor_engagement(self, engagement_id): vendor_id, start_date, end_date, status,\ termination_reason = self.request_params('vendorId', 'startDate', 'endDate', 'status', 'terminationReason') engagement = self.vendor_engagement_repo.get(engagement_id) if start_date: start_date = datetime.strptime(start_date, '%Y-%m-%d') if end_date: end_date = datetime.strptime(end_date, '%Y-%m-%d') if engagement: updates = {'vendor_id': vendor_id} if start_date: updates['start_date'] = start_date if end_date: updates['end_date'] = end_date if status is not None: updates['status'] = status if termination_reason: updates['termination_reason'] = termination_reason self.vendor_engagement_repo.update(engagement, **updates) e = engagement.serialize() e['vendor'] = engagement.vendor.serialize() return self.handle_response('OK', payload={'engagement': e}) return self.handle_response( 'Invalid or incorrect engagement_id provided', status_code=400) def delete_engagement(self, engagement_id): engagement = self.vendor_engagement_repo.get(engagement_id) if engagement: if engagement.is_deleted: return self.handle_response( 'This engagement has already been deleted', status_code=400) if any(not dependent.is_deleted for dependent in engagement.menus): return self.handle_response( 'This engagement cannot be deleted because it has a child object', status_code=400) updates = {} updates['is_deleted'] = True self.vendor_engagement_repo.update(engagement, **updates) return self.handle_response('Engagement deleted', payload={"status": "success"}) return self.handle_response( 'Invalid or incorrect engagement_id provided', status_code=400) def immediate_past_engagement(self, location_id): past_dates = self.vendor_engagement_repo.get_past_engagement_dates( location_id) if past_dates: latest_date = max(past_dates) immediate_past_engagment = VendorEngagement.query.filter_by( end_date=latest_date).first() e = immediate_past_engagment.serialize() engmnt = { k: v for k, v in e.items() if k in ['startDate', 'endDate', 'vendor'] } vendor_id = e['vendorId'] vendor = self.vendor_repo.get(vendor_id).serialize() vendor_info = { k: v for k, v in vendor.items() if k in ['id', 'name', 'tel', 'contactPerson'] } engmnt['vendor'] = vendor_info return self.handle_response('OK', payload={'engagement': engmnt}) else: return self.handle_response('No past engagement for this location', status_code=404)
def setUp(self): self.BaseSetUp() self.repo = VendorEngagementRepo()
def setUp(self): self.BaseSetUp() self.repo = VendorEngagementRepo() self.location = LocationFactory() self.vendor = VendorFactory()