コード例 #1
0
def test_import_class_invalid_module_name():
    with pytest.raises(ImportError) as excinfo:
        utils.import_class('wrong.module.Document')

    expected = "No module named 'wrong'"
    assert str(excinfo.value) == expected

    assert 'wrong.module.Document' not in utils.CLASSES_CACHE
コード例 #2
0
def test_import_class_invalid_class_name():
    with pytest.raises(ImportError) as excinfo:
        utils.import_class('aioodm.WrongNameClass')

    expected = "No class named 'aioodm.WrongNameClass'"
    assert str(excinfo.value) == expected

    assert 'aioodm.WrongNameClass' not in utils.CLASSES_CACHE
コード例 #3
0
def test_import_class():
    utils.CLASSES_CACHE = {}

    Document = utils.import_class('aioodm.document.Document')
    assert Document is aioodm.Document
    assert 'aioodm.document.Document' in utils.CLASSES_CACHE

    OtherDocument = utils.import_class('aioodm.document.Document')
    assert OtherDocument is Document

    utils.CLASSES_CACHE = {}
コード例 #4
0
def test_import_class_invalid_value():
    with pytest.raises(ImportError) as excinfo:
        utils.import_class('Xxx')

    expected = "Import path should be absolute string, not 'Xxx'"
    assert str(excinfo.value) == expected

    with pytest.raises(ImportError) as excinfo:
        utils.import_class(1)

    expected = "Import path should be absolute string, not '1'"
    assert str(excinfo.value) == expected
コード例 #5
0
    def document_class(self):
        if isinstance(self._document_class, str):
            self._document_class = import_class(self._document_class)
            if not issubclass(self._document_class, self._base_document_class):
                raise TypeError(("document_class should be a "
                                 "subclass of '{0}', not a '{1}'").format(
                                     self._base_document_class,
                                     self._document_class))

        return self._document_class
コード例 #6
0
    def __init__(self, document_class, **kwargs):
        """Create Reference field.

        Args:
            document_class: A subclass of the ``aioodm.Document`` class
                or string with absolute path to such class.
            **kwargs: Other arguments from ``Field``.
        """
        Document = import_class('aioodm.Document')
        super().__init__(document_class, Document, **kwargs)
        self.validators = [self._validate_none, self._validate_ref]
コード例 #7
0
    def __init__(self, document_class, **kwargs):
        """Create Embedded Document field.

        Args:
            document_class: A subclass of the
                ``aioodm.EmbeddedDocument`` class or string with
                absolute path to such class.
            **kwargs: Other arguments from ``Field``.
        """
        EmbeddedDocument = import_class('aioodm.EmbeddedDocument')
        super().__init__(document_class, EmbeddedDocument, **kwargs)
        self.validators.append(lambda value: value.validate())
コード例 #8
0
    def __init__(self,
                 item_field,
                 *,
                 min_length=None,
                 max_length=None,
                 **kwargs):
        """Create List field.

        Args:
            item_field (Field): Instance of the field to reflect list
                items' type.
            min_length (int): Minimum length of the list. Defaults to ``None``.
            max_length (int): Maximum length of the list. Defaults to ``None``.
            **kwargs: Other arguments from ``Field``.

        Raises:
            TypeError: If item_field is not instance of the ``Field`` subclass.
        """
        if not isinstance(item_field, Field):
            raise TypeError(
                ('item_field should be an instance of the `Field` '
                 'subclass, not of the `{0}`').format(type(item_field)))

        EmbeddedDocument = import_class('aioodm.EmbeddedDocument')
        document_class, base_document_class = ((
            item_field._document_class,
            EmbeddedDocument) if isinstance(item_field, EmbDocField) else
                                               (None, None))
        super().__init__(document_class,
                         base_document_class,
                         field_type=list,
                         **kwargs)

        self.item_field = item_field
        self.min_length = min_length
        self.max_length = max_length

        if min_length is not None:
            self.validators.append(self._validate_min_length)
        if max_length is not None:
            self.validators.append(self._validate_max_length)
        self.validators.append(self._validate_items)