예제 #1
0
    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 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)
예제 #4
0
    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)
class ExpeditionsResource(object):
    _data_source = None
    _resource_use_cases = None
    _work_centers_use_case = None

    def __init__(self, data_source: DBDataSource):
        self._data_source = data_source
        self._resource_use_cases = ExpeditionsUseCases(self._data_source)
        self._work_centers_use_case = WorkCentersUseCases(self._data_source)

    def on_post(self, req: Request, resp: Response):
        expedition = {}
        try:
            expedition_data = req.media
            destiny_of_expedition = None
            work_center_id = expedition_data.get('work_center_id')

            if work_center_id != None:
                destiny_of_expedition = self._work_centers_use_case.find(
                    work_center_id)

            if destiny_of_expedition == None:
                falcon.HTTPError(
                    "Error", ExpeditionOperationsRejectionMessages.
                    WORK_CENTER_IS_REQUIRED)

            expedition = ExpeditionsEntity()
            expedition.fill(
                qty_of_terminals=expedition_data.get('qty_of_terminals'),
                work_center=destiny_of_expedition,
                auto_predict_qty_needed=expedition_data.get(
                    'auto_predict_qty_needed'),
            )

            expedition = self._resource_use_cases.create(expedition)
            self._work_centers_use_case.update_calculated_values(
                destiny_of_expedition)
            expedition_updated = self._resource_use_cases.find(expedition.id)

        except UseCaseException as ex:
            raise falcon.HTTPBadRequest(falcon.HTTP_400, str(ex))
        except Exception as ex:
            raise falcon.HTTPError(falcon.HTTP_500, str(ex))

        resp.status = falcon.HTTP_CREATED
        resp.body = falcon.media.JSONHandler().serialize(
            expedition_updated.to_dict(), falcon.MEDIA_JSON)

    def on_get(self, req: Request, resp: Response):
        try:
            resp.media = prepare_list_to_json(
                self._resource_use_cases.get_all())
            resp.content_type = falcon.MEDIA_JSON
            resp.status = falcon.HTTP_OK
        except Exception as ex:
            raise falcon.HTTPError(str(ex))
예제 #6
0
    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)
class ExpeditionResource(object):
    _data_source = None
    _resource_use_cases = None
    _work_centers_use_case = None

    def __init__(self, data_source: DBDataSource):
        self._data_source = data_source
        self._resource_use_cases = ExpeditionsUseCases(self._data_source)
        self._work_centers_use_case = WorkCentersUseCases(self._data_source)

    def on_get(self, req: Request, resp: Response, primary_key: int):
        found_entity = self._resource_use_cases.find(primary_key=primary_key)

        if found_entity == None:
            raise falcon.HTTPNotFound()

        resp.media = found_entity.to_dict()
        resp.content_type = falcon.MEDIA_JSON
        resp.status = falcon.HTTP_OK

    def on_delete(self, req: Request, resp: Response, primary_key: int):
        found_entity = self._resource_use_cases.find(primary_key=primary_key)

        if found_entity == None:
            raise falcon.HTTPNotFound()

        try:
            resp.body = json.dumps(
                self._resource_use_cases.delete(found_entity))
            resp.status = falcon.HTTP_OK
        except Exception as ex:
            raise falcon.HTTPError(falcon.HTTP_500, str(ex))

    def on_put(self, req: Request, resp: Response, primary_key: int):
        found_entity = self._resource_use_cases.find(primary_key=primary_key)
        if found_entity == None:
            raise falcon.HTTPNotFound()

        try:
            found_entity.fill(
                qty_of_terminals=req.media.get('qty_of_terminals'),
                was_canceled=req.media.get('was_canceled'))

            expedition = self._resource_use_cases.update(found_entity)
            self._work_centers_use_case.update_calculated_values(
                expedition.work_center)
            expedition_updated = self._resource_use_cases.find(expedition.id)

            resp.media = expedition_updated.to_dict()
            resp.content_type = falcon.MEDIA_JSON
            resp.status = falcon.HTTP_OK
        except UseCaseException as ex:
            raise falcon.HTTPBadRequest(falcon.HTTP_400, str(ex))
        except Exception as ex:
            raise falcon.HTTPError(falcon.HTTP_500, str(ex))
예제 #8
0
    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)
class WorkCenterResource(object):
    _data_source = None
    _resource_use_cases = None

    def __init__(self, data_source: DBDataSource):
        self._data_source = data_source
        self._resource_use_cases = WorkCentersUseCases(self._data_source)

    def on_get(self, req: Request, resp: Response, primary_key: int):
        found_entity = self._resource_use_cases.find(primary_key=primary_key)

        if found_entity == None:
            raise falcon.HTTPNotFound()

        resp.media = found_entity.to_dict()
        resp.content_type = falcon.MEDIA_JSON
        resp.status = falcon.HTTP_OK

    def on_delete(self, req: Request, resp: Response, primary_key: int):
        found_entity = self._resource_use_cases.find(primary_key=primary_key)

        if found_entity == None:
            raise falcon.HTTPNotFound()

        try:
            resp.body = json.dumps(
                self._resource_use_cases.delete(found_entity))
            resp.status = falcon.HTTP_OK
        except Exception as ex:
            raise falcon.HTTPError(falcon.HTTP_500, str(ex))

    def on_put(self, req: Request, resp: Response, primary_key: int):
        found_entity = self._resource_use_cases.find(primary_key=primary_key)
        if found_entity == None:
            raise falcon.HTTPNotFound()

        found_entity.fill(region=req.media.get('region'))

        try:
            entity_updated = self._resource_use_cases.update(found_entity)
            resp.media = entity_updated.to_dict()
            resp.content_type = falcon.MEDIA_JSON
            resp.status = falcon.HTTP_OK
        except Exception as ex:
            print(ex)
            raise falcon.HTTPError(falcon.HTTP_500, str(ex))
class WorkCentersResource(object):
    _data_source = None
    _resource_use_cases = None

    def __init__(self, data_source: DBDataSource):
        self._data_source = data_source
        self._resource_use_cases = WorkCentersUseCases(self._data_source)

    @falcon.before(verify_if_is_a_valid_work_center_to_register)
    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 _prepare_to_parse(self, work_centers: list):
        return [
            self._remove_big_attributes(work_center)
            for work_center in work_centers
        ]

    def _remove_big_attributes(self, work_center: WorkCentersEntity):
        work_center.attendance = []
        work_center.expeditions = []

        return work_center

    def on_get(self, req: Request, resp: Response):
        try:
            all_work_centers = self._resource_use_cases.get_all()
            all_work_centers = self._prepare_to_parse(all_work_centers)

            resp.media = prepare_list_to_json(all_work_centers)
            resp.content_type = falcon.MEDIA_JSON
            resp.status = falcon.HTTP_OK
        except Exception as ex:
            raise falcon.HTTPError(str(ex))
예제 #11
0
    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_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_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 __init__(self, data_source: DBDataSource):
     self._data_source = data_source
     self._resource_use_cases = WorkCentersUseCases(self._data_source)
from data.models import (WorkCentersModel, ExpeditionsModel, AttendanceModel)
from datetime import datetime
import multiprocessing as mp
from use_cases.work_centers import WorkCentersUseCases

ds = DBDataSource()

db_conn = ds.connect_to_source('sqlite:///db.sqlite3')
db_session = db_conn.create_a_session()
wc_table = db_session.query(WorkCentersModel)
expedition_table = db_session.query(ExpeditionsModel)
attendance_table = db_session.query(AttendanceModel)
wc_region_model_dict = {}
wc_id_n_attendance_dict = {}
wc_expeditions_increment = {}
work_center_use_case = WorkCentersUseCases(ds)


def open_csv_file(filename: str):
    input_file = csv.DictReader(open(filename))
    return list(input_file)


def save_a_wc_by_dict(wc: dict):
    wc_model = WorkCentersModel(region=wc.get('region'))
    db_session.add(wc_model)

    return wc_model


print("Clear DB -> ")
 def __init__(self, data_source: DBDataSource):
     self._data_source = data_source
     self._resource_use_cases = ExpeditionsUseCases(self._data_source)
     self._work_centers_use_case = WorkCentersUseCases(self._data_source)
예제 #17
0
 def __init__(self, data_source: DBDataSource):
     self._data_source = data_source
     self._resource_use_cases = AttendanceUseCases(self._data_source)
     self._work_centers_use_case = WorkCentersUseCases(self._data_source)