def test_should_get_all_work_centers(self): WorkCentersUseCases(self._data_source).create( WorkCentersEntity(region="SP - São Paulo")) WorkCentersUseCases(self._data_source).create( WorkCentersEntity(region="RJ - Rio de Janeiro")) WorkCentersUseCases(self._data_source).create( WorkCentersEntity(region="BH - Belo Horizonte")) work_centers = WorkCentersUseCases(self._data_source).get_all() self.assertEqual(len(work_centers), 3)
def get_qty_of_terminals_available(self, work_center: WorkCentersEntity): """ (BR02) A quantidade de terminais disponível é resultado das entregas das espedições menos a quantidade usada em atendimentos Args: work_center (WorkCentersEntity): Polo Returns: [type]: Quantidade recebida em espedições - Quantidade usada em atendimentos """ return work_center.calcule_qty_of_terminals_received() - work_center.calcule_qty_of_terminals_used()
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_should_get_all_work_centers_in_db(self): repository = WorkCentersRepository(self._data_source) entity_SP = WorkCentersEntity(region="SP - São Paulo") entity_RJ = WorkCentersEntity(region="RJ - Rio de Janeiro") entity_BH = WorkCentersEntity(region="BH - Belo Horizonte") repository.persist(entity_SP) repository.persist(entity_RJ) repository.persist(entity_BH) repository.save_transaction() entities_on_db = repository.fetch() self.assertEqual(len(entities_on_db), 3)
def test_should_find_a_work_center(self): created_entity = WorkCentersUseCases(self._data_source).create( WorkCentersEntity(region="SP - São Paulo")) found_entity = WorkCentersUseCases(self._data_source).find( created_entity.id) self.assertEqual(found_entity.id, created_entity.id)
def _create_a_work_center_by_data_layer(self): work_center_repository = WorkCentersRepository(self._data_source) work_center = WorkCentersEntity(region="SP - Osasco") work_center_model = work_center_repository.persist(work_center) work_center_repository.save_transaction() return work_center_model
def test_should_delete_a_work_center(self): created_entity = WorkCentersUseCases(self._data_source).create( WorkCentersEntity(region="SP - São Paulo")) WorkCentersUseCases(self._data_source).delete(created_entity) found_entity = WorkCentersUseCases(self._data_source).find( created_entity.id) self.assertIsNone(found_entity)
def to_entity(self) -> WorkCentersEntity: expeditions = [exp.to_entity() for exp in self.expeditions] attendance = [attdc.to_entity() for attdc in self.attendance] return WorkCentersEntity(self.region, self.id, expeditions, attendance, self.days_qty_ideal_for_coverage, self.coverage_classification, self.days_of_coverage, self.avg_of_attendence, self.qty_of_terminals_available, self.qty_of_terminals_used, self.qty_of_terminals_received)
def on_post(self, req: Request, resp: Response): result = {} try: result = self._resource_use_cases.create( WorkCentersEntity(**req.media)) except Exception as ex: raise falcon.HTTPError(falcon.HTTP_500, str(ex)) resp.status = falcon.HTTP_CREATED resp.body = serialize(result)
def test_should_get_one_work_center_in_db(self): repository = WorkCentersRepository(self._data_source) entity_SP = WorkCentersEntity(region="SP - São Paulo") entity = repository.persist(entity_SP) repository.save_transaction() all_entities = repository.fetch() last_entity_added = all_entities[len(all_entities) - 1] self.assertEqual(entity.region, last_entity_added.region) self.assertIsNotNone(last_entity_added.id)
def test_should_persist_a_work_center_in_db(self): repository = WorkCentersRepository(self._data_source) qty_of_entities_in_db_before = len(repository.fetch()) entity = WorkCentersEntity(region="SP - São Paulo") repository.persist(entity) repository.save_transaction() qty_of_entities_in_db_after = len(repository.fetch()) self.assertNotEqual(qty_of_entities_in_db_before, qty_of_entities_in_db_after)
def test_should_update_a_work_center(self): new_region = "RJ - Rio de Janeiro - Madureira" created_entity = WorkCentersUseCases(self._data_source).create( WorkCentersEntity(region="SP - São Paulo")) created_entity.region = new_region WorkCentersUseCases(self._data_source).update(created_entity) found_entity = WorkCentersUseCases(self._data_source).find( created_entity.id) self.assertEqual(found_entity.region, new_region)
def test_should_delete_one_work_center_in_db(self): entity_SP = WorkCentersEntity(region="SP - São Paulo") repository = WorkCentersRepository(self._data_source) entity = repository.persist(entity_SP) repository.save_transaction() repository.delete(entity) repository.save_transaction() found_entity = repository.find(entity.id) self.assertIsNone(found_entity)
def test_should_create_a_work_center_and_return_data_from_db(self): repository = WorkCentersRepository(self._data_source) qty_of_register_in_db_before = len(repository.fetch()) created_entity = WorkCentersUseCases(self._data_source).create( WorkCentersEntity(region="SP - São Paulo")) qty_of_register_in_db_after = len(repository.fetch()) self.assertIsNotNone(created_entity.id) self.assertEqual(created_entity.region, "SP - São Paulo") self.assertEqual(qty_of_register_in_db_after, qty_of_register_in_db_before + 1)
def test_should_update_one_work_center_in_db(self): entity_SP = WorkCentersEntity(region="SP - São Paulo") repository = WorkCentersRepository(self._data_source) new_name = "SP - São Paulo 2" entity = repository.persist(entity_SP) repository.save_transaction() entity.region = new_name repository.update(entity) repository.save_transaction() self.assertEqual(entity.region, new_name)
def is_not_a_valid_work_center_data_to_register(self, work_center: WorkCentersEntity = WorkCentersEntity()) -> bool: """ (BRO1) Um polo sempre deve ter uma região Args: work_center (WorkCentersEntity, optional): Polo. Defaults to WorkCentersEntity(). Returns: bool: Se a entidade tiver uma região irá retornar verdadeiro se não falso """ if work_center.region == None or len(work_center.region) == 0: return True return False
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()
class WorkCenterBusinessCoverage(ResetDatabaseEachTestCase): _fake_wc_entity = WorkCentersEntity(region="SP - São Paulo") def _get_date_days_ago(self, days_ago = 14): param_date = datetime.datetime.now() days_to_discount = datetime.timedelta(days_ago) new_date = param_date - days_to_discount return new_date def test_should_get_right_average_of_attendence(self): days_used_in_avg = 14 use_cases = WorkCentersUseCases(self._data_source) work_center = use_cases.create(self._fake_wc_entity) avg_before = use_cases.get_average_of_attendences_in_wc( work_center, days_used_in_avg) list_of_attendences = [ AttendanceEntity(qty_of_terminals=1, work_center=work_center), AttendanceEntity(qty_of_terminals=1, work_center=work_center, attendance_date=self._get_date_days_ago(7)), AttendanceEntity(qty_of_terminals=1, work_center=work_center, attendance_date=self._get_date_days_ago(10)), AttendanceEntity(qty_of_terminals=1, work_center=work_center, was_canceled=True), ] for attdc in list_of_attendences: attdc = AttendanceUseCases(self._data_source).create(attdc) avg_after = use_cases.get_average_of_attendences_in_wc( work_center, days_used_in_avg) self.assertEqual(avg_before, 0) self.assertEqual(avg_after, 3) 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 test_is_getting_right_red_with_high_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, 30, 10) time.sleep(1) 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, 300) self.assertGreater(days_coverage, 23) self.assertEqual(classfication, CoverageClassifications.RED) def test_is_getting_right_yellow_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, 13, 10) time.sleep(1) 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, 130) self.assertGreaterEqual(days_coverage, 10) self.assertLessEqual(days_coverage, 13) self.assertEqual(classfication, CoverageClassifications.YELLOW) def test_is_getting_right_yellow_with_high_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, 19, 10) time.sleep(1) 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, 190) self.assertGreaterEqual(days_coverage, 19) self.assertLessEqual(days_coverage, 23) self.assertEqual(classfication, CoverageClassifications.YELLOW) 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) def _register_fake_attendences_in(self, work_center: WorkCentersEntity, qty_of_attendence: int = 5, days_limit: int = 14): while len(AttendanceUseCases(self._data_source).get_all()) < qty_of_attendence: AttendanceUseCases(self._data_source).create(AttendanceEntity( qty_of_terminals=1, work_center=work_center, attendance_date=self._get_date_days_ago(int(days_limit) / 2), )) def _register_fake_expeditions_in(self, work_center: WorkCentersEntity, qty_of_expeditions: int = 1, qty_of_terminals_per_expedition: int = 100): while len(ExpeditionsUseCases(self._data_source).get_all()) < qty_of_expeditions: ExpeditionsUseCases(self._data_source).create(ExpeditionsEntity( qty_of_terminals=qty_of_terminals_per_expedition, work_center=work_center ))
def _remove_big_attributes(self, work_center: WorkCentersEntity): work_center.attendance = [] work_center.expeditions = [] return work_center
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)
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)