Ejemplo n.º 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
Ejemplo n.º 2
0
def test_import_class_invalid_class_name():
    with pytest.raises(ImportError) as excinfo:
        utils.import_class('aiomongodel.WrongNameClass')

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

    assert 'aiomongodel.WrongNameClass' not in utils.CLASSES_CACHE
Ejemplo n.º 3
0
def test_import_class():
    utils.CLASSES_CACHE = {}

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

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

    utils.CLASSES_CACHE = {}
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 6
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
Ejemplo n.º 7
0
    def __init__(self, document_class, **kwargs):
        """Create Reference field.

        Args:
            document_class: A subclass of the ``aiomongodel.Document`` class
                or string with absolute path to such class.
            **kwargs: Other arguments from ``Field``.
        """
        Document = import_class('aiomongodel.Document')
        super().__init__(document_class, Document, **kwargs)
        self.validators = [self._validate_none, self._validate_ref]
Ejemplo n.º 8
0
    def __init__(self, document_class, **kwargs):
        """Create Reference field.

        Args:
            document_class: A subclass of the ``aiomongodel.Document`` class
                or string with absolute path to such class.
            **kwargs: Other arguments from ``Field``.
        """
        Document = import_class('aiomongodel.Document')
        super().__init__(document_class, Document, **kwargs)
        self.validators = [self._validate_none, self._validate_ref]
Ejemplo n.º 9
0
    def __init__(self, document_class, **kwargs):
        """Create Embedded Document field.

        Args:
            document_class: A subclass of the
                ``aiomongodel.EmbeddedDocument`` class or string with
                absolute path to such class.
            **kwargs: Other arguments from ``Field``.
        """
        EmbeddedDocument = import_class('aiomongodel.EmbeddedDocument')
        super().__init__(document_class, EmbeddedDocument, **kwargs)
        self.validators.append(lambda value: value.validate())
Ejemplo n.º 10
0
    def __init__(self, document_class, **kwargs):
        """Create Embedded Document field.

        Args:
            document_class: A subclass of the
                ``aiomongodel.EmbeddedDocument`` class or string with
                absolute path to such class.
            **kwargs: Other arguments from ``Field``.
        """
        EmbeddedDocument = import_class('aiomongodel.EmbeddedDocument')
        super().__init__(document_class, EmbeddedDocument, **kwargs)
        self.validators.append(lambda value: value.validate())
Ejemplo n.º 11
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('aiomongodel.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)
Ejemplo n.º 12
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('aiomongodel.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)