ran unittest2pytest

This commit is contained in:
Bastien Gérard
2019-08-30 16:13:30 +03:00
parent aa6ff8c84a
commit ac25f4b98b
46 changed files with 4247 additions and 4428 deletions

View File

@@ -5,13 +5,15 @@ from mongoengine import *
from mongoengine.base import LazyReference
from tests.utils import MongoDBTestCase
import pytest
class TestLazyReferenceField(MongoDBTestCase):
def test_lazy_reference_config(self):
# Make sure ReferenceField only accepts a document class or a string
# with a document class name.
self.assertRaises(ValidationError, LazyReferenceField, EmbeddedDocument)
with pytest.raises(ValidationError):
LazyReferenceField(EmbeddedDocument)
def test___repr__(self):
class Animal(Document):
@@ -25,7 +27,7 @@ class TestLazyReferenceField(MongoDBTestCase):
animal = Animal()
oc = Ocurrence(animal=animal)
self.assertIn("LazyReference", repr(oc.animal))
assert "LazyReference" in repr(oc.animal)
def test___getattr___unknown_attr_raises_attribute_error(self):
class Animal(Document):
@@ -39,7 +41,7 @@ class TestLazyReferenceField(MongoDBTestCase):
animal = Animal().save()
oc = Ocurrence(animal=animal)
with self.assertRaises(AttributeError):
with pytest.raises(AttributeError):
oc.animal.not_exist
def test_lazy_reference_simple(self):
@@ -57,19 +59,19 @@ class TestLazyReferenceField(MongoDBTestCase):
animal = Animal(name="Leopard", tag="heavy").save()
Ocurrence(person="test", animal=animal).save()
p = Ocurrence.objects.get()
self.assertIsInstance(p.animal, LazyReference)
assert isinstance(p.animal, LazyReference)
fetched_animal = p.animal.fetch()
self.assertEqual(fetched_animal, animal)
assert fetched_animal == animal
# `fetch` keep cache on referenced document by default...
animal.tag = "not so heavy"
animal.save()
double_fetch = p.animal.fetch()
self.assertIs(fetched_animal, double_fetch)
self.assertEqual(double_fetch.tag, "heavy")
assert fetched_animal is double_fetch
assert double_fetch.tag == "heavy"
# ...unless specified otherwise
fetch_force = p.animal.fetch(force=True)
self.assertIsNot(fetch_force, fetched_animal)
self.assertEqual(fetch_force.tag, "not so heavy")
assert fetch_force is not fetched_animal
assert fetch_force.tag == "not so heavy"
def test_lazy_reference_fetch_invalid_ref(self):
class Animal(Document):
@@ -87,8 +89,8 @@ class TestLazyReferenceField(MongoDBTestCase):
Ocurrence(person="test", animal=animal).save()
animal.delete()
p = Ocurrence.objects.get()
self.assertIsInstance(p.animal, LazyReference)
with self.assertRaises(DoesNotExist):
assert isinstance(p.animal, LazyReference)
with pytest.raises(DoesNotExist):
p.animal.fetch()
def test_lazy_reference_set(self):
@@ -122,7 +124,7 @@ class TestLazyReferenceField(MongoDBTestCase):
):
p = Ocurrence(person="test", animal=ref).save()
p.reload()
self.assertIsInstance(p.animal, LazyReference)
assert isinstance(p.animal, LazyReference)
p.animal.fetch()
def test_lazy_reference_bad_set(self):
@@ -149,7 +151,7 @@ class TestLazyReferenceField(MongoDBTestCase):
DBRef(baddoc._get_collection_name(), animal.pk),
LazyReference(BadDoc, animal.pk),
):
with self.assertRaises(ValidationError):
with pytest.raises(ValidationError):
p = Ocurrence(person="test", animal=bad).save()
def test_lazy_reference_query_conversion(self):
@@ -179,14 +181,14 @@ class TestLazyReferenceField(MongoDBTestCase):
post2.save()
post = BlogPost.objects(author=m1).first()
self.assertEqual(post.id, post1.id)
assert post.id == post1.id
post = BlogPost.objects(author=m2).first()
self.assertEqual(post.id, post2.id)
assert post.id == post2.id
# Same thing by passing a LazyReference instance
post = BlogPost.objects(author=LazyReference(Member, m2.pk)).first()
self.assertEqual(post.id, post2.id)
assert post.id == post2.id
def test_lazy_reference_query_conversion_dbref(self):
"""Ensure that LazyReferenceFields can be queried using objects and values
@@ -215,14 +217,14 @@ class TestLazyReferenceField(MongoDBTestCase):
post2.save()
post = BlogPost.objects(author=m1).first()
self.assertEqual(post.id, post1.id)
assert post.id == post1.id
post = BlogPost.objects(author=m2).first()
self.assertEqual(post.id, post2.id)
assert post.id == post2.id
# Same thing by passing a LazyReference instance
post = BlogPost.objects(author=LazyReference(Member, m2.pk)).first()
self.assertEqual(post.id, post2.id)
assert post.id == post2.id
def test_lazy_reference_passthrough(self):
class Animal(Document):
@@ -239,20 +241,20 @@ class TestLazyReferenceField(MongoDBTestCase):
animal = Animal(name="Leopard", tag="heavy").save()
Ocurrence(animal=animal, animal_passthrough=animal).save()
p = Ocurrence.objects.get()
self.assertIsInstance(p.animal, LazyReference)
with self.assertRaises(KeyError):
assert isinstance(p.animal, LazyReference)
with pytest.raises(KeyError):
p.animal["name"]
with self.assertRaises(AttributeError):
with pytest.raises(AttributeError):
p.animal.name
self.assertEqual(p.animal.pk, animal.pk)
assert p.animal.pk == animal.pk
self.assertEqual(p.animal_passthrough.name, "Leopard")
self.assertEqual(p.animal_passthrough["name"], "Leopard")
assert p.animal_passthrough.name == "Leopard"
assert p.animal_passthrough["name"] == "Leopard"
# Should not be able to access referenced document's methods
with self.assertRaises(AttributeError):
with pytest.raises(AttributeError):
p.animal.save
with self.assertRaises(KeyError):
with pytest.raises(KeyError):
p.animal["save"]
def test_lazy_reference_not_set(self):
@@ -269,7 +271,7 @@ class TestLazyReferenceField(MongoDBTestCase):
Ocurrence(person="foo").save()
p = Ocurrence.objects.get()
self.assertIs(p.animal, None)
assert p.animal is None
def test_lazy_reference_equality(self):
class Animal(Document):
@@ -280,12 +282,12 @@ class TestLazyReferenceField(MongoDBTestCase):
animal = Animal(name="Leopard", tag="heavy").save()
animalref = LazyReference(Animal, animal.pk)
self.assertEqual(animal, animalref)
self.assertEqual(animalref, animal)
assert animal == animalref
assert animalref == animal
other_animalref = LazyReference(Animal, ObjectId("54495ad94c934721ede76f90"))
self.assertNotEqual(animal, other_animalref)
self.assertNotEqual(other_animalref, animal)
assert animal != other_animalref
assert other_animalref != animal
def test_lazy_reference_embedded(self):
class Animal(Document):
@@ -308,12 +310,12 @@ class TestLazyReferenceField(MongoDBTestCase):
animal2 = Animal(name="cheeta").save()
def check_fields_type(occ):
self.assertIsInstance(occ.direct, LazyReference)
assert isinstance(occ.direct, LazyReference)
for elem in occ.in_list:
self.assertIsInstance(elem, LazyReference)
self.assertIsInstance(occ.in_embedded.direct, LazyReference)
assert isinstance(elem, LazyReference)
assert isinstance(occ.in_embedded.direct, LazyReference)
for elem in occ.in_embedded.in_list:
self.assertIsInstance(elem, LazyReference)
assert isinstance(elem, LazyReference)
occ = Ocurrence(
in_list=[animal1, animal2],
@@ -346,19 +348,19 @@ class TestGenericLazyReferenceField(MongoDBTestCase):
animal = Animal(name="Leopard", tag="heavy").save()
Ocurrence(person="test", animal=animal).save()
p = Ocurrence.objects.get()
self.assertIsInstance(p.animal, LazyReference)
assert isinstance(p.animal, LazyReference)
fetched_animal = p.animal.fetch()
self.assertEqual(fetched_animal, animal)
assert fetched_animal == animal
# `fetch` keep cache on referenced document by default...
animal.tag = "not so heavy"
animal.save()
double_fetch = p.animal.fetch()
self.assertIs(fetched_animal, double_fetch)
self.assertEqual(double_fetch.tag, "heavy")
assert fetched_animal is double_fetch
assert double_fetch.tag == "heavy"
# ...unless specified otherwise
fetch_force = p.animal.fetch(force=True)
self.assertIsNot(fetch_force, fetched_animal)
self.assertEqual(fetch_force.tag, "not so heavy")
assert fetch_force is not fetched_animal
assert fetch_force.tag == "not so heavy"
def test_generic_lazy_reference_choices(self):
class Animal(Document):
@@ -385,13 +387,13 @@ class TestGenericLazyReferenceField(MongoDBTestCase):
occ_animal = Ocurrence(living_thing=animal, thing=animal).save()
occ_vegetal = Ocurrence(living_thing=vegetal, thing=vegetal).save()
with self.assertRaises(ValidationError):
with pytest.raises(ValidationError):
Ocurrence(living_thing=mineral).save()
occ = Ocurrence.objects.get(living_thing=animal)
self.assertEqual(occ, occ_animal)
self.assertIsInstance(occ.thing, LazyReference)
self.assertIsInstance(occ.living_thing, LazyReference)
assert occ == occ_animal
assert isinstance(occ.thing, LazyReference)
assert isinstance(occ.living_thing, LazyReference)
occ.thing = vegetal
occ.living_thing = vegetal
@@ -399,7 +401,7 @@ class TestGenericLazyReferenceField(MongoDBTestCase):
occ.thing = mineral
occ.living_thing = mineral
with self.assertRaises(ValidationError):
with pytest.raises(ValidationError):
occ.save()
def test_generic_lazy_reference_set(self):
@@ -434,7 +436,7 @@ class TestGenericLazyReferenceField(MongoDBTestCase):
):
p = Ocurrence(person="test", animal=ref).save()
p.reload()
self.assertIsInstance(p.animal, (LazyReference, Document))
assert isinstance(p.animal, (LazyReference, Document))
p.animal.fetch()
def test_generic_lazy_reference_bad_set(self):
@@ -455,7 +457,7 @@ class TestGenericLazyReferenceField(MongoDBTestCase):
animal = Animal(name="Leopard", tag="heavy").save()
baddoc = BadDoc().save()
for bad in (42, "foo", baddoc, LazyReference(BadDoc, animal.pk)):
with self.assertRaises(ValidationError):
with pytest.raises(ValidationError):
p = Ocurrence(person="test", animal=bad).save()
def test_generic_lazy_reference_query_conversion(self):
@@ -481,14 +483,14 @@ class TestGenericLazyReferenceField(MongoDBTestCase):
post2.save()
post = BlogPost.objects(author=m1).first()
self.assertEqual(post.id, post1.id)
assert post.id == post1.id
post = BlogPost.objects(author=m2).first()
self.assertEqual(post.id, post2.id)
assert post.id == post2.id
# Same thing by passing a LazyReference instance
post = BlogPost.objects(author=LazyReference(Member, m2.pk)).first()
self.assertEqual(post.id, post2.id)
assert post.id == post2.id
def test_generic_lazy_reference_not_set(self):
class Animal(Document):
@@ -504,7 +506,7 @@ class TestGenericLazyReferenceField(MongoDBTestCase):
Ocurrence(person="foo").save()
p = Ocurrence.objects.get()
self.assertIs(p.animal, None)
assert p.animal is None
def test_generic_lazy_reference_accepts_string_instead_of_class(self):
class Animal(Document):
@@ -521,7 +523,7 @@ class TestGenericLazyReferenceField(MongoDBTestCase):
animal = Animal().save()
Ocurrence(animal=animal).save()
p = Ocurrence.objects.get()
self.assertEqual(p.animal, animal)
assert p.animal == animal
def test_generic_lazy_reference_embedded(self):
class Animal(Document):
@@ -544,12 +546,12 @@ class TestGenericLazyReferenceField(MongoDBTestCase):
animal2 = Animal(name="cheeta").save()
def check_fields_type(occ):
self.assertIsInstance(occ.direct, LazyReference)
assert isinstance(occ.direct, LazyReference)
for elem in occ.in_list:
self.assertIsInstance(elem, LazyReference)
self.assertIsInstance(occ.in_embedded.direct, LazyReference)
assert isinstance(elem, LazyReference)
assert isinstance(occ.in_embedded.direct, LazyReference)
for elem in occ.in_embedded.in_list:
self.assertIsInstance(elem, LazyReference)
assert isinstance(elem, LazyReference)
occ = Ocurrence(
in_list=[animal1, animal2],