def setUp(self):
        INPUT_CSV: str = "https://storage.googleapis.com/juntossomosmais-code-challenge/input-backend.csv"

        self.helpers = LocatorHelpers()
        self.iterator = DataLoadIterator(
            redis=LocatorFactory().create_redis_instance(),
            helper=LocatorHelpers())
        self.df = self.iterator._create_dataframe_from_csv(INPUT_CSV)
class LocatorHelpersTestCase(TestCase):
    """
    Tests of LocatorHelpers in locator.presenters.helpers.py
    """
    def setUp(self):
        INPUT_CSV: str = "https://storage.googleapis.com/juntossomosmais-code-challenge/input-backend.csv"

        self.helpers = LocatorHelpers()
        self.iterator = DataLoadIterator(
            redis=LocatorFactory().create_redis_instance(),
            helper=LocatorHelpers())
        self.df = self.iterator._create_dataframe_from_csv(INPUT_CSV)

    def test_get_region(self):
        region: str = self.helpers.get_region(state="ceará")
        self.assertIsInstance(region, str)
        self.assertEquals(region, "NORDESTE")

    def test_get_region_fail(self):
        region: str = self.helpers.get_region(state="new york")
        self.assertIsInstance(region, str)
        self.assertEquals(region, "DESCONHECIDO")

    def test_transform_to_numeric(self):
        df = self.helpers._trasform_to_numeric(
            df=self.df, col="location__coordinates__latitude")
        self.assertIsInstance(df.iloc[0]["location__coordinates__latitude"],
                              float)

    def test_change_type_coordinates(self):
        df = self.helpers._change_type_coordinates(df=self.df)
        self.assertIsInstance(df.iloc[0]["location__coordinates__latitude"],
                              float)
        self.assertIsInstance(df.iloc[0]["location__coordinates__longitude"],
                              float)

    def test_set_col_phonenumber(self):
        df = self.helpers._set_col_phonenumber(df=self.df, col="phone")
        self.assertEquals(df.iloc[0]["phone"], "+55154155648")

    def test_change_phones_number(self):
        df = self.helpers._set_col_phonenumber(df=self.df, col="phone")
        df = self.helpers._set_col_phonenumber(df=df, col="cell")
        self.assertEquals(df.iloc[0]["phone"], "+55154155648")
        self.assertEquals(df.iloc[0]["cell"], "+551082645550")

    def test_remove_cols_dataframe(self):
        df = self.helpers._remove_cols_dataframe(df=self.df)
        self.assertEquals("dob__age" not in df.columns, True)
        self.assertEquals("registered__age" not in df.columns, True)

    def test_change_col_gender(self):
        df = self.helpers._change_col_gender(df=self.df)
        self.assertEquals(df.iloc[0]["gender"], "F")
    def setUp(self):
        INPUT_CSV: str = "https://storage.googleapis.com/juntossomosmais-code-challenge/input-backend.csv"

        self.helpers = LocatorHelpers()
        self.iterator = DataLoadIterator(
            redis=LocatorFactory().create_redis_instance(),
            helper=LocatorHelpers())
        self.df = self.helpers.format_dataframe(
            df=self.iterator._create_dataframe_from_csv(INPUT_CSV))
        self.classification = cl.Classification(lat=-38.9614, lon=-10.766959)
        self.classification.get_classification()
        self.list_users = lu.ListUsers(users=self.df,
                                       classification=self.classification)

        self.serializer = DefaultSerializer
class DefaultSerializerTestCase(TestCase):
    """
    Tests of DefaultSerializer in locator.infra.serializer.py
    """
    def setUp(self):
        INPUT_CSV: str = "https://storage.googleapis.com/juntossomosmais-code-challenge/input-backend.csv"

        self.helpers = LocatorHelpers()
        self.iterator = DataLoadIterator(
            redis=LocatorFactory().create_redis_instance(),
            helper=LocatorHelpers())
        self.df = self.helpers.format_dataframe(
            df=self.iterator._create_dataframe_from_csv(INPUT_CSV))
        self.classification = cl.Classification(lat=-38.9614, lon=-10.766959)
        self.classification.get_classification()
        self.list_users = lu.ListUsers(users=self.df,
                                       classification=self.classification)

        self.serializer = DefaultSerializer

    def test_init(self):
        serializer = self.serializer(list_users=self.list_users,
                                     page_number=1,
                                     page_size=12)
        self.assertIsInstance(serializer.list_users, lu.ListUsers)
        self.assertIsInstance(serializer.page_number, int)
        self.assertIsInstance(serializer.page_size, int)

    def test_mount_user_payload(self):
        serializer = self.serializer(list_users=self.list_users,
                                     page_number=1,
                                     page_size=12)
        result = serializer._mount_user_payload(user=self.df.iloc[0],
                                                classification="NORMAL")
        self.assertIsInstance(result, dict)
        self.assertEquals(result["type"], "NORMAL")
        self.assertEquals(result["gender"], "F")
        self.assertEquals(result["telephoneNumbers"][0], "+55154155648")
        self.assertEquals(result["mobileNumbers"][0], "+551082645550")

    def test_mount_list_users(self):
        serializer = self.serializer(list_users=self.list_users,
                                     page_number=1,
                                     page_size=12)
        list_users = serializer._mount_list_users()
        self.assertIsInstance(list_users, object)

    def test_create_message(self):
        serializer = self.serializer(list_users=self.list_users,
                                     page_number=1,
                                     page_size=12)
        message = serializer.create_message()
        self.assertIsInstance(message, dict)
Beispiel #5
0
 def __init__(self, list_users: object, page_number: int, page_size: int):
     self.list_users = list_users
     self.page_number = page_number
     self.page_size = page_size
     self.helper = LocatorHelpers()
Beispiel #6
0
class DefaultSerializer(ISerializer):
    def __init__(self, list_users: object, page_number: int, page_size: int):
        self.list_users = list_users
        self.page_number = page_number
        self.page_size = page_size
        self.helper = LocatorHelpers()

    def _mount_user_payload(self, user: Type[pd.DataFrame],
                            classification: str) -> dict:
        payload: dict = {
            "type":
            classification,
            "gender":
            user["gender"],
            "name": {
                "title": user["name__title"],
                "first": user["name__first"],
                "last": user["name__last"],
            },
            "location": {
                "region":
                self.helper.get_region(state=user["location__state"]),
                "street": user["location__street"],
                "city": user["location__city"],
                "state": user["location__state"],
                "postcode": user["location__postcode"],
                "coordinates": {
                    "latitude": user["location__coordinates__latitude"],
                    "longitude": user["location__coordinates__longitude"],
                },
                "timezone": {
                    "offset": user["location__timezone__offset"],
                    "description": user["location__timezone__description"],
                },
            },
            "email":
            user["email"],
            "registered":
            user["registered__date"],
            "birthday":
            user["dob__date"],
            "telephoneNumbers": [
                user["phone"],
            ],
            "mobileNumbers": [user["cell"]],
            "picture": {
                "large": user["picture__large"],
                "medium": user["picture__medium"],
                "thumbnail": user["picture__thumbnail"],
            },
            "nationality":
            "BR" if user["phone"].split("+")[1][0:2] == "55" else "OTHER",
        }

        return payload

    def _paginate_list_users(self, users: list) -> object:
        paginated_users = Paginator(users, self.page_size)

        if len(users) < self.page_size:
            self.page_number = 1

        page_users = paginated_users.page(self.page_number)
        return page_users

    def _mount_list_users(self) -> list:
        users: list = list()
        classification: str = self.list_users.classification.name

        for index, user in self.list_users.users.iterrows():
            usr: dict = self._mount_user_payload(user=user,
                                                 classification=classification)
            users.append(usr)

        users: Type[Paginator] = self._paginate_list_users(users)

        return users

    def mount_payload(self) -> dict:
        users: Type[Paginator] = self._mount_list_users()

        return {
            "pageNumber": self.page_number,
            "pageSize": self.page_size,
            "totalCount": len(users),
            "users": users.object_list,
        }

    def create_message(self) -> dict:
        message: dict = self.mount_payload()
        return message
 def setUp(self):
     self.iterator = DataLoadIterator(
         redis=LocatorFactory().create_redis_instance(),
         requests=requests,
         helper=LocatorHelpers(),
     )
class LocatorRepo:
    """
    This layer is responsible for interacting with models and entities
    """

    helper: Type[LocatorHelpers] = LocatorHelpers()

    def __init__(self, redis_instance):
        self.redis = redis_instance

    def get_clients_by_position(self, lat: float, lon: float) -> (dict, str):
        """
        Get a list of Clients by coordinates
        """
        try:
            classification: Type[
                clsn.Classification] = self.create_classification(lat=lat,
                                                                  lon=lon)
            classification.get_classification()

            df: Type[pd.DataFrame] = self.get_all_data()

            df = df[df["location__coordinates__latitude"].between(
                classification.minlat, classification.maxlat)
                    & df["location__coordinates__longitude"].between(
                        classification.maxlon, classification.minlon)]

            if df.empty:
                return (f"No clients found with coordinates: "
                        f"Lattitude;{lat}, Longitude:{lon}")

            list_users: Type[lu.ListUsers] = self.create_list_users(
                classification=classification, users=df)

            return list_users

        except ConflictException as err:
            raise ConflictException(
                source="repository",
                code="conflit_get_all_clients",
                message=f"Error in load clients by position: {err}",
            )

    def get_all_data(self) -> Type[pd.DataFrame]:
        """
        Get all data in Redis
        """
        try:
            context = pa.default_serialization_context()
            json_df = context.deserialize(self.redis.get("json-populate"))
            csv_df = context.deserialize(self.redis.get("csv-populate"))
            dfs = [json_df, csv_df]
            return pd.concat(dfs)
        except ConflictException as err:
            raise ConflictException(
                source="repository",
                code="conflit_get_all_data",
                message=f"Error in get all data in Redis: {err}",
            )

    def create_classification(self, lat: float, lon: float) -> Type[dataclass]:
        """
        Create a Classification Entity
        """
        try:
            return clsn.Classification(lat=lat, lon=lon)
        except ConflictException as err:
            raise ConflictException(
                source="repository",
                code="conflit_in_create",
                message=f"Error in create a Classification Entity: {err}",
            )

    def create_list_users(self, classification: Type[clsn.Classification],
                          users: Type[pd.DataFrame]) -> Type[dataclass]:
        """
        Create a ListUsers Entity
        """
        try:
            return lu.ListUsers(classification=classification, users=users)
        except ConflictException as err:
            raise ConflictException(
                source="repository",
                code="conflit_in_create",
                message=f"Error in create a ListUsers Entity: {err}",
            )