def test_is_getting_right_green_classification_of_coverage(self): days_used_in_avg = 14 use_cases = WorkCentersUseCases(self._data_source) work_center = use_cases.create(self._fake_wc_entity) business_rules = WorkCenterBusinessRules() self._register_fake_attendences_in(work_center, 250, days_used_in_avg) self._register_fake_expeditions_in(work_center, 20, 25) work_center_updated = use_cases.find(work_center.id) terminals_used = work_center_updated.calcule_qty_of_terminals_used() terminals_received = work_center_updated.calcule_qty_of_terminals_received() terminails_available = business_rules.get_qty_of_terminals_available( work_center_updated) avg_attendance = use_cases.get_average_of_attendences_in_wc( work_center_updated, days_used_in_avg) classfication = business_rules.get_coverage_classification( terminails_available, avg_attendance, days_used_in_avg) self.assertEqual(terminals_used, 250) self.assertEqual(terminals_received, 500) self.assertEqual(avg_attendance, 17) self.assertEqual(classfication, CoverageClassifications.GREEN)
def test_is_getting_right_red_with_low_stock_classification_of_coverage(self): days_used_in_avg = 14 use_cases = WorkCentersUseCases(self._data_source) work_center = use_cases.create(self._fake_wc_entity) business_rules = WorkCenterBusinessRules() self._register_fake_attendences_in(work_center, 75, days_used_in_avg) self._register_fake_expeditions_in(work_center, 9, 10) work_center_updated = use_cases.find(work_center.id) terminals_used = work_center_updated.calcule_qty_of_terminals_used() terminals_received = work_center_updated.calcule_qty_of_terminals_received() terminails_available = business_rules.get_qty_of_terminals_available( work_center_updated) avg_attendance = use_cases.get_average_of_attendences_in_wc( work_center_updated, days_used_in_avg) classfication = business_rules.get_coverage_classification( terminails_available, avg_attendance, days_used_in_avg) days_coverage = business_rules.get_days_coverage( terminails_available, avg_attendance) self.assertEqual(terminals_used, 75) self.assertEqual(terminals_received, 90) self.assertEqual(days_coverage, 3) self.assertEqual(classfication, CoverageClassifications.RED)
def __init__(self, data_source: DBDataSource): self._data_source = data_source self._expeditions_repository: ExpeditionsRepository = ExpeditionsRepository( db_data_source=self._data_source) self._work_center_repository: WorkCentersRepository = WorkCentersRepository( db_data_source=self._data_source) self._business_rules: WorkCenterBusinessRules = WorkCenterBusinessRules( )
class WorkCenterBusinessRulesTest(unittest.TestCase): rules = WorkCenterBusinessRules() def test_should_invalid_a_work_center_without_region(self): wc_empty = WorkCentersEntity() wc_without_region = WorkCentersEntity(**{'region': ''}) res_of_wc_empty = self.rules.is_not_a_valid_work_center_data_to_register( wc_empty) res_of_wc_without_region = self.rules.is_not_a_valid_work_center_data_to_register( wc_without_region) self.assertEqual(res_of_wc_empty, True) self.assertEqual(res_of_wc_without_region, True)
def test_is_getting_right_qty_of_terminals_in_send_auto_predict_expeditions(self): use_cases = WorkCentersUseCases(self._data_source) work_center = use_cases.create(self._fake_wc_entity) business_rules = WorkCenterBusinessRules() expedition_use_case = ExpeditionsUseCases(self._data_source) self._register_fake_attendences_in(work_center, 75, work_center.days_qty_ideal_for_coverage) self._register_fake_expeditions_in(work_center, 9, 10) work_center_updated = use_cases.find(work_center.id) terminails_available_before = business_rules.get_qty_of_terminals_available( work_center_updated) avg_attendance = use_cases.get_average_of_attendences_in_wc( work_center_updated, work_center.days_qty_ideal_for_coverage) classfication_before = business_rules.get_coverage_classification( terminails_available_before, avg_attendance, work_center.days_qty_ideal_for_coverage) expedition = expedition_use_case.create(ExpeditionsEntity( work_center=work_center, auto_predict_qty_needed=True )) # Atualizando dados do work_center work_center_updated = use_cases.find(work_center.id) terminails_available = business_rules.get_qty_of_terminals_available( work_center_updated) avg_attendance = use_cases.get_average_of_attendences_in_wc( work_center_updated, work_center.days_qty_ideal_for_coverage) classfication_after = business_rules.get_coverage_classification( terminails_available, avg_attendance, work_center.days_qty_ideal_for_coverage) self.assertEqual(classfication_before, CoverageClassifications.RED) self.assertEqual(classfication_after, CoverageClassifications.GREEN)
class WorkCentersUseCases(): _work_centers_repository: WorkCentersRepository = None _business_rules: WorkCenterBusinessRules = None _data_source = None def __init__(self, data_source: DBDataSource): self._data_source = data_source self._work_centers_repository = WorkCentersRepository( db_data_source=self._data_source) self._business_rules = WorkCenterBusinessRules() def get_average_of_attendences_in_wc( self, work_center: WorkCentersEntity = WorkCentersEntity(), days: int = 14) -> int: return self._work_centers_repository.get_average_of_attendence_by_days_period( work_center, 14) def create( self, work_center: WorkCentersEntity = WorkCentersEntity() ) -> WorkCentersEntity: if work_center == {}: raise UseCaseException( WorkCenterOperationsRejectionMessages.INVALID_REGION_NAME) if self._business_rules.is_not_a_valid_work_center_data_to_register( work_center): raise UseCaseException( WorkCenterOperationsRejectionMessages.INVALID_DATA_TO_REGISTER) try: model_created = self._work_centers_repository.persist(work_center) self._work_centers_repository.save_transaction() except Exception as ex: self._work_centers_repository.revert_transaction() raise ex return model_created.to_entity() def update_calculated_values( self, entity: WorkCentersEntity) -> WorkCentersEntity: if entity is not None: entity_updated = self._work_centers_repository.find(entity.id) entity_updated.days_qty_ideal_for_coverage = 14 entity_updated.calcule_qty_of_terminals_used() entity_updated.calcule_qty_of_terminals_received() entity_updated.calcule_qty_of_terminals_available() entity_updated.avg_of_attendence = self.get_average_of_attendences_in_wc( entity_updated, entity_updated.days_qty_ideal_for_coverage) entity_updated.days_of_coverage = self._business_rules.get_days_coverage( entity_updated.qty_of_terminals_available, entity_updated.avg_of_attendence) entity_updated.coverage_classification = self._business_rules.get_coverage_classification( entity_updated.qty_of_terminals_available, entity_updated.avg_of_attendence, entity_updated.days_qty_ideal_for_coverage) model = self._work_centers_repository.update(entity_updated) self._work_centers_repository.save_transaction() self._work_centers_repository.refresh_data(model) return model.to_entity() return entity def get_all(self) -> list: work_centers_list = [] work_centers_in_DS = self._work_centers_repository.fetch() for model in work_centers_in_DS: self._work_centers_repository.refresh_data(model) model.to_entity() work_centers_list.append(model) return work_centers_list def find(self, primary_key: int) -> WorkCentersEntity: if primary_key == None or primary_key == 0: raise UseCaseException( DefaultOperationsRejectionsMessages.NEED_A_ID_TO_FIND) return self._work_centers_repository.find(primary_key) def delete(self, entity: WorkCentersEntity) -> bool: try: self._work_centers_repository.delete(entity) self._work_centers_repository.save_transaction() except Exception as ex: self._work_centers_repository.revert_transaction() raise ex def update(self, entity: WorkCentersEntity) -> WorkCentersEntity: try: model_updated = self._work_centers_repository.update(entity) self._work_centers_repository.save_transaction() return model_updated.to_entity() except Exception as ex: self._work_centers_repository.revert_transaction() raise ex
def verify_if_is_a_valid_work_center_to_register(req, resp, resource, params): if req.media is None or WorkCenterBusinessRules().is_not_a_valid_work_center_data_to_register(WorkCentersEntity(**req.media)): raise falcon.HTTPBadRequest("Bad Request", WorkCenterOperationsRejectionMessages.INVALID_REGION_NAME)