def test_basics(mongodb_backend): attributes = {'foo': 'bar', 'baz': 1243, 'd': {1: 3, 4: 5}, 'l': [1, 2, 3, 4]} doc = Document(attributes) mongodb_backend.save(doc) mongodb_backend.commit() doc.foobar = 'baz' mongodb_backend.update(doc,['foobar']) mongodb_backend.commit() assert mongodb_backend.get(Document,{'foobar' : 'baz'}) == doc
def test_non_existing_key(mongodb_backend): attributes = {'foo': 'bar', 'baz': 1243, 'd': {1: 3, 4: 5}, 'l': [1, 2, 3, 4]} doc = Document(attributes) mongodb_backend.save(doc) mongodb_backend.commit() mongodb_backend.update(doc,['non_existing_key']) mongodb_backend.commit() assert mongodb_backend.get(Document,{'pk' : doc.pk}) == doc
def test_container_operations(): attributes = {"foo": "bar", "baz": 1243, "d": {1: 3, 4: 5}, "l": [1, 2, 3, 4]} doc = Document(attributes) with pytest.raises(KeyError): doc["fooz"] assert ("foo" in doc) == True assert ("fooz" in doc) == False assert list(doc.keys()) == list(attributes.keys()) assert list(doc.values()) == list(attributes.values()) assert doc.items() == attributes.items()
def test_container_operations(): attributes = {'foo': 'bar', 'baz': 1243, 'd': {1: 3, 4: 5}, 'l': [1, 2, 3, 4]} doc = Document(attributes) with pytest.raises(KeyError): doc['fooz'] assert ('foo' in doc) == True assert ('fooz' in doc) == False assert list(doc.keys()) == list(attributes.keys()) assert list(doc.values()) == list(attributes.values()) assert doc.items() == attributes.items()
def test_basic_attributes(): attributes = {"foo": "bar", "baz": 1243, "d": {1: 3, 4: 5}, "l": [1, 2, 3, 4]} doc = Document(attributes) assert doc.foo == "bar" assert doc.baz == 1243 assert doc.d == {1: 3, 4: 5} assert doc.l == [1, 2, 3, 4] assert doc.foo == doc["foo"] assert doc.baz == doc["baz"] assert doc.d == doc["d"] assert doc.attributes == attributes
def test_iteration(): attributes = {'foo': 'bar', 'baz': 1243, 'd': {1: 3, 4: 5}, 'l': [1, 2, 3, 4]} doc = Document(attributes) for key in doc: assert key in attributes for key,value in doc.items(): assert key in attributes assert attributes[key] == value for value in doc.values(): assert value in attributes.values()
def test_basic_attributes(): attributes = {'foo': 'bar', 'baz': 1243, 'd': {1: 3, 4: 5}, 'l': [1, 2, 3, 4]} doc = Document(attributes) assert doc.foo == 'bar' assert doc.baz == 1243 assert doc.d == {1: 3, 4: 5} assert doc.l == [1, 2, 3, 4] assert doc.foo == doc['foo'] assert doc.baz == doc['baz'] assert doc.d == doc['d'] assert doc.attributes == attributes
def test_iteration(): attributes = {"foo": "bar", "baz": 1243, "d": {1: 3, 4: 5}, "l": [1, 2, 3, 4]} doc = Document(attributes) for key in doc: assert key in attributes for key, value in doc.items(): assert key in attributes assert attributes[key] == value for value in doc.values(): assert value in attributes.values()
def test_complex(mongodb_backend): c = 1j+4 attributes = {'foo': c,} doc = Document(attributes) mongodb_backend.save(doc) mongodb_backend.commit() assert mongodb_backend.get(Document,{'pk' : doc.pk}) == doc with pytest.raises(ValueError): assert mongodb_backend.get(Document,{'foo' : c}) assert mongodb_backend.get(Document,{'foo.r' : c.real,'foo.i' : c.imag}).foo == c
def test_attribute_deletion(): attributes = {'foo': 'bar', 'baz': 1243, 'd': {1: 3, 4: 5}, 'l': [1, 2, 3, 4]} doc = Document(attributes) del doc.foo with pytest.raises(AttributeError): doc.foo with pytest.raises(KeyError): doc['foo'] with pytest.raises(KeyError): del doc['foo'] with pytest.raises(AttributeError): del doc.foo
def test_attribute_deletion(): attributes = {"foo": "bar", "baz": 1243, "d": {1: 3, 4: 5}, "l": [1, 2, 3, 4]} doc = Document(attributes) del doc.foo with pytest.raises(AttributeError): doc.foo with pytest.raises(KeyError): doc["foo"] with pytest.raises(KeyError): del doc["foo"] with pytest.raises(AttributeError): del doc.foo
def test_deep_update(mongodb_backend): attributes = {'foo': {'bar' : 'baz'}, 'baz': 1243, 'd': {1: 3, 4: 5}, 'l': [1, 2, 3, 4]} doc = Document(attributes) mongodb_backend.save(doc) mongodb_backend.commit() mongodb_backend.update(doc,{'foo.bar' : 'bam'}) mongodb_backend.commit() assert mongodb_backend.get(Document,{'foo.bar' : 'bam'}) == doc doc.foo['bar'] = 'squirrel' #we update using a list rather than a dict mongodb_backend.update(doc,['foo.bar']) mongodb_backend.commit() assert mongodb_backend.get(Document,{'foo.bar' : 'squirrel'}) == doc
def test_update_non_existing_document(mongodb_backend): attributes = { "foo": "bar", "baz": 1243, "d": { 1: 3, 4: 5 }, "l": [1, 2, 3, 4] } doc = Document(attributes) doc.foobar = "baz" with pytest.raises(Document.DoesNotExist): mongodb_backend.update(doc, ["foobar"]) mongodb_backend.commit() with pytest.raises(Document.DoesNotExist): assert mongodb_backend.get(Document, {"foobar": "baz"})
def test_update_non_existing_document(mongodb_backend): attributes = { 'foo': 'bar', 'baz': 1243, 'd': { 1: 3, 4: 5 }, 'l': [1, 2, 3, 4] } doc = Document(attributes) doc.foobar = 'baz' with pytest.raises(Document.DoesNotExist): mongodb_backend.update(doc, ['foobar']) mongodb_backend.commit() with pytest.raises(Document.DoesNotExist): assert mongodb_backend.get(Document, {'foobar': 'baz'})
def test_basics(mongodb_backend): attributes = { "foo": "bar", "baz": 1243, "d": { 1: 3, 4: 5 }, "l": [1, 2, 3, 4] } doc = Document(attributes) mongodb_backend.save(doc) mongodb_backend.commit() doc.foobar = "baz" mongodb_backend.update(doc, ["foobar"]) mongodb_backend.commit() assert mongodb_backend.get(Document, {"foobar": "baz"}) == doc
def test_complex(mongodb_backend): c = 1j + 4 attributes = {"foo": c} doc = Document(attributes) mongodb_backend.save(doc) mongodb_backend.commit() assert mongodb_backend.get(Document, {"pk": doc.pk}) == doc with pytest.raises(ValueError): assert mongodb_backend.get(Document, {"foo": c}) assert (mongodb_backend.get(Document, { "foo.r": c.real, "foo.i": c.imag }).foo == c)
def test_non_existing_key(mongodb_backend): attributes = { "foo": "bar", "baz": 1243, "d": { 1: 3, 4: 5 }, "l": [1, 2, 3, 4] } doc = Document(attributes) mongodb_backend.save(doc) mongodb_backend.commit() mongodb_backend.update(doc, ["non_existing_key"]) mongodb_backend.commit() assert mongodb_backend.get(Document, {"pk": doc.pk}) == doc
def test_complex(mongodb_backend): c = 1j + 4 attributes = { 'foo': c, } doc = Document(attributes) mongodb_backend.save(doc) mongodb_backend.commit() assert mongodb_backend.get(Document, {'pk': doc.pk}) == doc assert mongodb_backend.get(Document, {'foo': c}) == doc assert len(mongodb_backend.filter(Document, {'foo': c})) == 1 with pytest.raises(Document.DoesNotExist): mongodb_backend.get(Document, {'foo': 1j + 5}) assert mongodb_backend.get(Document, {'foo': c}).foo == c
def test_dots(mongodb_backend): attributes = {'foo.baz.bam': 'blub', 'foo' : {'baz' : {'bam' : 'bar'}} } doc = Document(attributes) mongodb_backend.save(doc) mongodb_backend.commit() assert mongodb_backend.get(Document,{'pk' : doc.pk}) == doc #Dotted queries work as one would expect assert mongodb_backend.get(Document,{'foo.baz.bam' : 'bar'}) == doc #Filter queries too assert len(mongodb_backend.filter(Document,{'foo.baz.bam' : 'bar'})) == 1 #When using dots in queries, we will NOT match the dotted field with pytest.raises(Document.DoesNotExist): mongodb_backend.get(Document,{'foo.baz.bam' : 'blub'}) #If we escape the dots, the query should work as we expect assert mongodb_backend.get(Document,DotEncoder.encode({'foo.baz.bam' : 'blub'},[])) == doc
def get_lazy_doc(): return Document({'pk': 1}, lazy=True, backend=mockup_backend)
def test_unicode(): doc = Document({"pk": "foo"}) assert doc.__unicode__ == doc.__str__