Ejemplo n.º 1
0
    def test_anonymizer_with_NO_params(self):
        a = FakerAnonymizer("{fake:random_uppercase_letter}")

        assert a("Value") == a("Value")
        assert a("boo") in uppercase

        b = FakerAnonymizer("{fake:military_apo}")
        assert b("Value") == b("Value")
        assert b("boo") == b("boo")
        assert b("Value") != b("boo")
Ejemplo n.º 2
0
    def test_anonymizer_with_postprocessor(self):
        # FakerAnonymizer always returns string unless converted
        a = FakerAnonymizer("{fake:ean8}")

        assert isinstance(a("Value"), str)

        b = FakerAnonymizer("{fake:ean8}", postprocessor=lambda x: int(x))

        assert isinstance(b("Value"), int)

        assert int(a("Value")) == b("Value")
Ejemplo n.º 3
0
    def test_anonymizer_with_stringprovider(self):
        """Register a string provider that is dynamically imported"""
        a = FakerAnonymizer("{fake:foo}",
                            providers=["recipe.utils.TestProvider"])

        assert isinstance(a("Value"), str)
        assert a("Value") == "foo"
Ejemplo n.º 4
0
    def test_anonymizer_with_multipleproviders(self):
        """Register multiple providers"""
        a = FakerAnonymizer(
            "{fake:foo} {fake:moo}",
            providers=["recipe.utils.TestProvider", CowProvider],
        )

        assert isinstance(a("Value"), str)
        assert a("Value") == "foo moo"
Ejemplo n.º 5
0
    def test_anonymizer_with_params(self):
        a = FakerAnonymizer("{fake:numerify|text=###}")
        assert a("Value") == a("Value")

        b = FakerAnonymizer(
            "{fake:lexify|text=???,letters=abcdefghijklmnopqrstuvwxyz"
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ}")
        assert len(b("value"))

        # Show we handle booleans
        before_today = FakerAnonymizer(
            "{fake:date_this_century|before_today=True,after_today=False}")
        after_today = FakerAnonymizer(
            "{fake:date_this_century|before_today=False,after_today=True}")

        # FakerAnonymizer always returns a string
        today = str(date.today())
        for let in "abcdefghijklmnopq":
            assert before_today(let) < today
            assert after_today(let) > today
Ejemplo n.º 6
0
    def add_ingredients(self):
        """Put the anonymizers in the last position of formatters"""
        for ingredient in self.recipe._cauldron.values():
            if hasattr(ingredient.meta, "anonymizer"):
                anonymizer = ingredient.meta.anonymizer

                # Build a FakerAnonymizer if we have a string
                if isinstance(anonymizer, str):

                    # Check for extra parameters
                    kwargs = {}
                    anonymizer_locale = getattr(ingredient.meta,
                                                "anonymizer_locale", None)
                    anonymizer_postprocessor = getattr(
                        ingredient.meta, "anonymizer_postprocessor", None)
                    anonymizer_providers = getattr(ingredient.meta,
                                                   "anonymizer_providers",
                                                   None)
                    if anonymizer_postprocessor is not None:
                        kwargs["postprocessor"] = anonymizer_postprocessor
                    if anonymizer_locale is not None:
                        kwargs["locale"] = anonymizer_locale
                    if anonymizer_providers is not None:
                        kwargs["providers"] = anonymizer_providers

                    anonymizer = FakerAnonymizer(anonymizer, **kwargs)

                # Strip out all FakerAnonymizers
                ingredient.formatters = [
                    f for f in ingredient.formatters
                    if not isinstance(f, FakerAnonymizer)
                ]

                if self._anonymize:
                    if ingredient.meta.anonymizer not in ingredient.formatters:
                        ingredient.formatters.append(anonymizer)
                else:
                    if ingredient.meta.anonymizer in ingredient.formatters:
                        ingredient.formatters.remove(anonymizer)
Ejemplo n.º 7
0
    def test_anonymizer_with_bad_providers(self):
        """Register a provider"""
        a = FakerAnonymizer("{fake:moo}", providers=[None, 4, CowProvider])

        assert isinstance(a("Value"), str)
        assert a("Value") == "moo"