Пример #1
0
    def test_validate_bad_field(self):
        badch = tuple(c for c in string.punctuation + string.digits)
        notnames = (
                '1a',
                'a.b',
                'a-b',
                '&a',
                'a++',
                ) + badch
        tests = [
            ('filename', ()),  # Any non-empty str is okay.
            ('funcname', notnames),
            ('name', notnames),
            ]
        seen = set()
        for field, invalid in tests:
            for value in invalid:
                seen.add(value)
                with self.subTest(f'{field}={value!r}'):
                    id = ID(**self.VALID_KWARGS)
                    id = id._replace(**{field: value})

                    with self.assertRaises(ValueError):
                        id.validate()

        for field, invalid in tests:
            valid = seen - set(invalid)
            for value in valid:
                with self.subTest(f'{field}={value!r}'):
                    id = ID(**self.VALID_KWARGS)
                    id = id._replace(**{field: value})

                    id.validate()  # This does not fail.
Пример #2
0
    def test_validate_missing_field(self):
        for field in ID._fields:
            with self.subTest(field):
                id = ID(**self.VALID_KWARGS)
                id = id._replace(**{field: None})

                if field == 'funcname':
                    id.validate()  # The field can be missing (not set).
                    id = id._replace(filename=None)
                    id.validate()  # Both fields can be missing (not set).
                    continue

                with self.assertRaises(TypeError):
                    id.validate()