Beispiel #1
0
def test_decorated_collection_find(mongo_client, monkeypatch):
    collection = MappedCollection(mongo_client.db,
                                  mongo_client.collection,
                                  impl=None)
    result = collection.find({"query": "value"})
    assert isinstance(result, MappedCursor)
    assert result.collection is collection
    assert result.__dict__["_Cursor__spec"] == {"query": "value"}
Beispiel #2
0
def test_decorated_collection_find_skip_validation(mongo_client, monkeypatch):
    with monkeypatch.context() as m:
        _result = object()
        m.setattr(Collection, "find", MagicMock(return_value=_result))
        collection = MappedCollection(mongo_client.db,
                                      mongo_client.collection,
                                      impl=None)
        assert collection.find({"query": "value"},
                               _skip_validation=True) is _result
        Collection.find.assert_called_with({"query": "value"})
Beispiel #3
0
def test_decorated_collection_insert_one_instance(mongo_client, insert_mock):
    collection = MappedCollection(mongo_client.db,
                                  mongo_client.collection,
                                  impl=Cop)
    document = {"the_name": "Ray", "the_surname": "Holt", "skill_level": 10}
    obj = Cop()
    obj._document = document
    assert isinstance(collection.insert_one(obj), InsertOneResult)
    assert document["_id"] == insert_mock
    Collection.insert_one.assert_called_with(document)
Beispiel #4
0
def test_decorated_collection_insert_one_skip_validation(
        monkeypatch, mongo_client, insert_mock):
    collection = MappedCollection(mongo_client.db,
                                  mongo_client.collection,
                                  impl=Cop)
    document = {"the_name": "Ray", "the_surname": "Holt", "skill_level": 10}
    monkeypatch.setattr(collection._impl, "validate", MagicMock())
    assert isinstance(collection.insert_one(document, _skip_validation=True),
                      InsertOneResult)
    assert document["_id"] == insert_mock
    Collection.insert_one.assert_called_with(document)
    collection._impl.validate.assert_not_called()
Beispiel #5
0
def test_float_type(mongo_client, value, exception):
    class DocWithFloat(MappedClass):
        value = Field("value", type_=FloatType)

    if exception:
        with pytest.raises(exception):
            DocWithFloat(value=value)
    else:
        doc = DocWithFloat(value=value)
        assert doc.value == value
        assert doc._document["value"] == value
        collection = MappedCollection(mongo_client.db,
                                      mongo_client.collection,
                                      impl=DocWithFloat)
        collection.insert_one(doc)
Beispiel #6
0
def test_decimal_type(mongo_client, amount):
    class DocWithDecimal(MappedClass):
        amount = Field("amount", type_=DecimalType)

    doc = DocWithDecimal(amount=amount)
    assert isinstance(doc.amount, Decimal)
    assert isinstance(doc._document["amount"], Decimal128)

    collection = MappedCollection(mongo_client.db,
                                  mongo_client.collection,
                                  impl=DocWithDecimal)
    doc_id = collection.insert_one(doc).inserted_id
    fetched_doc = collection.find_one({"_id": doc_id})
    assert isinstance(fetched_doc.amount, Decimal)
    assert isinstance(fetched_doc._document["amount"], Decimal128)

    assert doc.amount == fetched_doc.amount
Beispiel #7
0
def test_decimal_type_if_missing(mongo_client):
    class DocWithDecimalRequired(MappedClass):
        amount = Field("amount",
                       type_=DecimalType,
                       required=True,
                       if_missing=Decimal(5))

    doc = DocWithDecimalRequired()
    assert isinstance(doc.amount, Decimal)
    assert isinstance(doc._document["amount"], Decimal128)

    collection = MappedCollection(mongo_client.db,
                                  mongo_client.collection,
                                  impl=DocWithDecimalRequired)
    doc_id = collection.insert_one(doc).inserted_id
    fetched_doc = collection.find_one({"_id": doc_id})
    assert isinstance(fetched_doc.amount, Decimal)
    assert isinstance(fetched_doc._document["amount"], Decimal128)

    assert doc.amount == fetched_doc.amount