Exemple #1
0
    def generate_test_data(self, obj_name, number_to_create, **fields):
        """Generate bulk test data

        This returns an array of dictionaries with template-formatted
        arguments which can be passed to the *Salesforce Collection Insert*
        keyword.

        You can use ``{{number}}`` to represent the unique index of
        the row in the list of rows.  If the entire string consists of
        a number, Salesforce API will treat the value as a number.

        Example:

        The following example creates three new Contacts:

            | @{objects} =  Generate Test Data  Contact  3
            | ...  Name=User {{number}}
            | ...  Age={{number}}

        The example code will generate Contact objects with these fields:

            | [{'Name': 'User 0', 'Age': '0'},
            |  {'Name': 'User 1', 'Age': '1'},
            |  {'Name': 'User 2', 'Age': '2'}]

        Python Expression Syntax is allowed so computed templates like this are also allowed: ``{{1000 + number}}``

        Python operators can be used, but no functions or variables are provided, so mostly you just
        have access to mathematical and logical operators. The Python operators are described here:

        https://www.digitalocean.com/community/tutorials/how-to-do-math-in-python-3-with-operators

        Contact the CCI team if you have a use-case that
        could benefit from more expression language power.

        Templates can also be based on faker patterns like those described here:

        https://faker.readthedocs.io/en/master/providers.html

        Most examples can be pasted into templates verbatim:

            | @{objects}=  Generate Test Data  Contact  200
            | ...  Name={{fake.first_name}} {{fake.last_name}}
            | ...  MailingStreet={{fake.street_address}}
            | ...  MailingCity=New York
            | ...  MailingState=NY
            | ...  MailingPostalCode=12345
            | ...  Email={{fake.email(domain="salesforce.com")}}

        """
        objs = []

        for i in range(int(number_to_create)):
            formatted_fields = {
                name: format_str(value, {"number": i}) for name, value in fields.items()
            }
            newobj = self._salesforce_generate_object(obj_name, **formatted_fields)
            objs.append(newobj)

        return objs
    def test_format_str_languages(self):
        norwegian_faker = template_utils.FakerTemplateLibrary("no_NO")

        val = template_utils.format_str(
            "{{vikingfake.first_name}} {{abc}}",
            {
                "abc": 5,
                "vikingfake": norwegian_faker
            },
        )
        assert "5" in val

        def cosmopolitan_faker(language):
            return template_utils.FakerTemplateLibrary(language)

        val = template_utils.format_str(
            "{{fakei18n('ne_NP').first_name}} {{abc}}",
            {
                "abc": 5,
                "fakei18n": cosmopolitan_faker,
                "type": type
            },
        )
        assert "5" in val
 def test_format_str(self):
     assert template_utils.format_str("abc") == "abc"
     assert template_utils.format_str("{{abc}}", {"abc": 5}) == "5"
     assert len(template_utils.format_str("{{fake.first_name}}"))
     assert "15" in template_utils.format_str(
         "{{fake.first_name}} {{count}}", {"count": 15})
     assert "15" in template_utils.format_str(
         "{{fake.first_name}} {{count}}", {"count": "15"})
     assert (template_utils.format_str("{% raw %}{}{% endraw %}",
                                       {"count": "15"}) == "{}")