Best Python code snippet using lisa_python
test_state.py
Source:test_state.py  
...5from reproenv.state import _TemplateRegistry, _validate_template6from reproenv import types7def test_validate_template_invalid_templates():8    with pytest.raises(exceptions.TemplateError, match="'name' is a required property"):9        _validate_template({})10    with pytest.raises(11        exceptions.TemplateError, match="'binaries' is a required property"12    ):13        _validate_template({"name": "bar"})14    # missing 'name' top-level key15    with pytest.raises(exceptions.TemplateError, match="'name' is a required property"):16        _validate_template(17            {18                # "name": "foobar",19                "binaries": {20                    "urls": {"1.0.0": "foobar.com"},21                    "instructions": "foobar",22                },23            }24        )25    # 'name' top-level value is not a string26    with pytest.raises(exceptions.TemplateError, match="1234 is not of type 'string'"):27        _validate_template(28            {29                "name": 1234,30                "binaries": {31                    "urls": {"1.0.0": "foobar.com"},32                    "instructions": "foobar",33                },34            }35        )36    #37    # test of 'binaries' templates38    #39    # no instructions40    with pytest.raises(41        exceptions.TemplateError, match="'instructions' is a required property"42    ):43        _validate_template(44            {45                "name": "foobar",46                "binaries": {47                    "urls": {"1.0.0": "foobar.com"},48                    # "instructions": "foobar",49                },50            }51        )52    # malformed env53    with pytest.raises(54        exceptions.TemplateError,55        match="Invalid template: \\['foo'\\] is not of type 'string'.",56    ):57        _validate_template(58            {59                "name": "foobar",60                "binaries": {61                    "urls": {"1.0.0": "foobar.com"},62                    "env": {"foo": ["foo"]},63                    "instructions": "foobar",64                },65            }66        )67    # binaries but no urls68    with pytest.raises(exceptions.TemplateError, match="'urls' is a required property"):69        _validate_template(70            {71                "name": "foobar",72                "binaries": {73                    # "urls": {"1.0.0": "foobar.com"},74                    "instructions": "foobar",75                },76            }77        )78    # extra keys79    with pytest.raises(80        exceptions.TemplateError,81        match=(82            "Invalid template: Additional properties are not allowed \\('extra' was"83            " unexpected\\)"84        ),85    ):86        _validate_template(87            {88                "name": "foobar",89                "binaries": {90                    "urls": {"1.0.0": "foobar.com"},91                    "instructions": "foobar",92                    "extra": "",93                },94            }95        )96    # extra keys in dependencies97    with pytest.raises(98        exceptions.TemplateError,99        match=(100            "Invalid template: Additional properties are not allowed \\('fakemngr'"101            " was unexpected\\)."102        ),103    ):104        _validate_template(105            {106                "name": "foobar",107                "binaries": {108                    "urls": {"1.0.0": "foobar.com"},109                    "instructions": "foobar",110                    "dependencies": {"apt": [], "fakemngr": []},111                },112            }113        )114    # defines variable but does not indicate if optional or required115    # TODO116    #117    # test of 'source' templates118    #119    # urls should not be in source120    with pytest.raises(121        exceptions.TemplateError,122        match=(123            "Invalid template: Additional properties are not allowed \\('urls' was"124            " unexpected\\)."125        ),126    ):127        _validate_template(128            {129                "name": "foobar",130                "source": {131                    "urls": {"1.0.0": "foobar.com"},132                    "instructions": "foobar",133                },134            }135        )136    # no instructions137    with pytest.raises(138        exceptions.TemplateError, match="'instructions' is a required property"139    ):140        _validate_template(141            {142                "name": "foobar",143                "source": {144                    # "instructions": "foobar",145                },146            }147        )148    # malformed env149    with pytest.raises(150        exceptions.TemplateError,151        match="Invalid template: \\['foo'\\] is not of type 'string'.",152    ):153        _validate_template(154            {155                "name": "foobar",156                "source": {157                    "env": {"foo": ["foo"]},158                    "instructions": "foobar",159                },160            }161        )162    # extra keys163    with pytest.raises(164        exceptions.TemplateError,165        match=(166            "Invalid template: Additional properties are not allowed \\('extra' was"167            " unexpected\\)"168        ),169    ):170        _validate_template(171            {172                "name": "foobar",173                "source": {174                    "instructions": "foobar",175                    "extra": "",176                },177            }178        )179    # extra keys in dependencies180    with pytest.raises(181        exceptions.TemplateError,182        match=(183            "Invalid template: Additional properties are not allowed \\('fakemngr'"184            " was unexpected\\)."185        ),186    ):187        _validate_template(188            {189                "name": "foobar",190                "source": {191                    "instructions": "foobar",192                    "dependencies": {"apt": [], "fakemngr": []},193                },194            }195        )196    # defines variable but does not indicate if optional or required197    # TODO198def test_validate_template_valid_templates():199    # minimal templates200    _validate_template(201        {202            "name": "foobar",203            "binaries": {204                "urls": {"v1": "foo"},205                "instructions": "foobar",206            },207        }208    )209    _validate_template(210        {211            "name": "foobar",212            "source": {213                "instructions": "foobar",214            },215        }216    )217    # bigger templates218    _validate_template(219        {220            "name": "foobar",221            "binaries": {222                "urls": {"v1": "foo"},223                "env": {"baz": "cat", "boo": "123"},224                "instructions": "echo hi there\n{{ self.install_dependencies() }}",225                "arguments": {"required": [], "optional": []},226                "dependencies": {"apt": ["curl"], "debs": ["foo"], "yum": ["curl"]},227            },228            "source": {229                "env": {"foo": "bar"},230                "instructions": "echo foo\n{{ self.install_dependencies() }}",231                "arguments": {232                    "required": [],...forms.py
Source:forms.py  
...12class CourseEmailTemplateForm(forms.ModelForm):  # pylint: disable=R092413    """Form providing validation of CourseEmail templates."""14    class Meta:  # pylint: disable=C011115        model = CourseEmailTemplate16    def _validate_template(self, template):17        """Check the template for required tags."""18        index = template.find(COURSE_EMAIL_MESSAGE_BODY_TAG)19        if index < 0:20            msg = 'Missing tag: "{}"'.format(COURSE_EMAIL_MESSAGE_BODY_TAG)21            log.warning(msg)22            raise ValidationError(msg)23        if template.find(COURSE_EMAIL_MESSAGE_BODY_TAG, index + 1) >= 0:24            msg = 'Multiple instances of tag: "{}"'.format(COURSE_EMAIL_MESSAGE_BODY_TAG)25            log.warning(msg)26            raise ValidationError(msg)27        # TODO: add more validation here, including the set of known tags28        # for which values will be supplied.  (Email will fail if the template29        # uses tags for which values are not supplied.)30    def clean_html_template(self):31        """Validate the HTML template."""32        template = self.cleaned_data["html_template"]33        self._validate_template(template)34        return template35    def clean_plain_template(self):36        """Validate the plaintext template."""37        template = self.cleaned_data["plain_template"]38        self._validate_template(template)39        return template40class CourseAuthorizationAdminForm(forms.ModelForm):  # pylint: disable=R092441    """Input form for email enabling, allowing us to verify data."""42    class Meta:  # pylint: disable=C011143        model = CourseAuthorization44    def clean_course_id(self):45        """Validate the course id"""46        course_id = self.cleaned_data["course_id"]47        try:48            # Just try to get the course descriptor.49            # If we can do that, it's a real course.50            get_course_by_id(course_id, depth=1)51        except Exception as exc:52            msg = 'Error encountered ({0})'.format(str(exc).capitalize())...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
