ran unittest2pytest
This commit is contained in:
@@ -7,6 +7,7 @@ from bson import ObjectId
|
||||
from mongoengine import *
|
||||
from mongoengine.errors import InvalidQueryError
|
||||
from mongoengine.queryset import Q
|
||||
import pytest
|
||||
|
||||
|
||||
class TestQ(unittest.TestCase):
|
||||
@@ -35,10 +36,10 @@ class TestQ(unittest.TestCase):
|
||||
age = IntField()
|
||||
|
||||
query = {"$or": [{"age": {"$gte": 18}}, {"name": "test"}]}
|
||||
self.assertEqual((q1 | q2 | q3 | q4 | q5).to_query(Person), query)
|
||||
assert (q1 | q2 | q3 | q4 | q5).to_query(Person) == query
|
||||
|
||||
query = {"age": {"$gte": 18}, "name": "test"}
|
||||
self.assertEqual((q1 & q2 & q3 & q4 & q5).to_query(Person), query)
|
||||
assert (q1 & q2 & q3 & q4 & q5).to_query(Person) == query
|
||||
|
||||
def test_q_with_dbref(self):
|
||||
"""Ensure Q objects handle DBRefs correctly"""
|
||||
@@ -53,8 +54,8 @@ class TestQ(unittest.TestCase):
|
||||
user = User.objects.create()
|
||||
Post.objects.create(created_user=user)
|
||||
|
||||
self.assertEqual(Post.objects.filter(created_user=user).count(), 1)
|
||||
self.assertEqual(Post.objects.filter(Q(created_user=user)).count(), 1)
|
||||
assert Post.objects.filter(created_user=user).count() == 1
|
||||
assert Post.objects.filter(Q(created_user=user)).count() == 1
|
||||
|
||||
def test_and_combination(self):
|
||||
"""Ensure that Q-objects correctly AND together.
|
||||
@@ -65,12 +66,10 @@ class TestQ(unittest.TestCase):
|
||||
y = StringField()
|
||||
|
||||
query = (Q(x__lt=7) & Q(x__lt=3)).to_query(TestDoc)
|
||||
self.assertEqual(query, {"$and": [{"x": {"$lt": 7}}, {"x": {"$lt": 3}}]})
|
||||
assert query == {"$and": [{"x": {"$lt": 7}}, {"x": {"$lt": 3}}]}
|
||||
|
||||
query = (Q(y="a") & Q(x__lt=7) & Q(x__lt=3)).to_query(TestDoc)
|
||||
self.assertEqual(
|
||||
query, {"$and": [{"y": "a"}, {"x": {"$lt": 7}}, {"x": {"$lt": 3}}]}
|
||||
)
|
||||
assert query == {"$and": [{"y": "a"}, {"x": {"$lt": 7}}, {"x": {"$lt": 3}}]}
|
||||
|
||||
# Check normal cases work without an error
|
||||
query = Q(x__lt=7) & Q(x__gt=3)
|
||||
@@ -78,7 +77,7 @@ class TestQ(unittest.TestCase):
|
||||
q1 = Q(x__lt=7)
|
||||
q2 = Q(x__gt=3)
|
||||
query = (q1 & q2).to_query(TestDoc)
|
||||
self.assertEqual(query, {"x": {"$lt": 7, "$gt": 3}})
|
||||
assert query == {"x": {"$lt": 7, "$gt": 3}}
|
||||
|
||||
# More complex nested example
|
||||
query = Q(x__lt=100) & Q(y__ne="NotMyString")
|
||||
@@ -87,7 +86,7 @@ class TestQ(unittest.TestCase):
|
||||
"x": {"$lt": 100, "$gt": -100},
|
||||
"y": {"$ne": "NotMyString", "$in": ["a", "b", "c"]},
|
||||
}
|
||||
self.assertEqual(query.to_query(TestDoc), mongo_query)
|
||||
assert query.to_query(TestDoc) == mongo_query
|
||||
|
||||
def test_or_combination(self):
|
||||
"""Ensure that Q-objects correctly OR together.
|
||||
@@ -99,7 +98,7 @@ class TestQ(unittest.TestCase):
|
||||
q1 = Q(x__lt=3)
|
||||
q2 = Q(x__gt=7)
|
||||
query = (q1 | q2).to_query(TestDoc)
|
||||
self.assertEqual(query, {"$or": [{"x": {"$lt": 3}}, {"x": {"$gt": 7}}]})
|
||||
assert query == {"$or": [{"x": {"$lt": 3}}, {"x": {"$gt": 7}}]}
|
||||
|
||||
def test_and_or_combination(self):
|
||||
"""Ensure that Q-objects handle ANDing ORed components.
|
||||
@@ -113,15 +112,12 @@ class TestQ(unittest.TestCase):
|
||||
|
||||
query = Q(x__gt=0) | Q(x__exists=False)
|
||||
query &= Q(x__lt=100)
|
||||
self.assertEqual(
|
||||
query.to_query(TestDoc),
|
||||
{
|
||||
"$and": [
|
||||
{"$or": [{"x": {"$gt": 0}}, {"x": {"$exists": False}}]},
|
||||
{"x": {"$lt": 100}},
|
||||
]
|
||||
},
|
||||
)
|
||||
assert query.to_query(TestDoc) == {
|
||||
"$and": [
|
||||
{"$or": [{"x": {"$gt": 0}}, {"x": {"$exists": False}}]},
|
||||
{"x": {"$lt": 100}},
|
||||
]
|
||||
}
|
||||
|
||||
q1 = Q(x__gt=0) | Q(x__exists=False)
|
||||
q2 = Q(x__lt=100) | Q(y=True)
|
||||
@@ -131,16 +127,13 @@ class TestQ(unittest.TestCase):
|
||||
TestDoc(x=10).save()
|
||||
TestDoc(y=True).save()
|
||||
|
||||
self.assertEqual(
|
||||
query,
|
||||
{
|
||||
"$and": [
|
||||
{"$or": [{"x": {"$gt": 0}}, {"x": {"$exists": False}}]},
|
||||
{"$or": [{"x": {"$lt": 100}}, {"y": True}]},
|
||||
]
|
||||
},
|
||||
)
|
||||
self.assertEqual(2, TestDoc.objects(q1 & q2).count())
|
||||
assert query == {
|
||||
"$and": [
|
||||
{"$or": [{"x": {"$gt": 0}}, {"x": {"$exists": False}}]},
|
||||
{"$or": [{"x": {"$lt": 100}}, {"y": True}]},
|
||||
]
|
||||
}
|
||||
assert 2 == TestDoc.objects(q1 & q2).count()
|
||||
|
||||
def test_or_and_or_combination(self):
|
||||
"""Ensure that Q-objects handle ORing ANDed ORed components. :)
|
||||
@@ -160,26 +153,23 @@ class TestQ(unittest.TestCase):
|
||||
q2 = Q(x__lt=100) & (Q(y=False) | Q(y__exists=False))
|
||||
query = (q1 | q2).to_query(TestDoc)
|
||||
|
||||
self.assertEqual(
|
||||
query,
|
||||
{
|
||||
"$or": [
|
||||
{
|
||||
"$and": [
|
||||
{"x": {"$gt": 0}},
|
||||
{"$or": [{"y": True}, {"y": {"$exists": False}}]},
|
||||
]
|
||||
},
|
||||
{
|
||||
"$and": [
|
||||
{"x": {"$lt": 100}},
|
||||
{"$or": [{"y": False}, {"y": {"$exists": False}}]},
|
||||
]
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
self.assertEqual(2, TestDoc.objects(q1 | q2).count())
|
||||
assert query == {
|
||||
"$or": [
|
||||
{
|
||||
"$and": [
|
||||
{"x": {"$gt": 0}},
|
||||
{"$or": [{"y": True}, {"y": {"$exists": False}}]},
|
||||
]
|
||||
},
|
||||
{
|
||||
"$and": [
|
||||
{"x": {"$lt": 100}},
|
||||
{"$or": [{"y": False}, {"y": {"$exists": False}}]},
|
||||
]
|
||||
},
|
||||
]
|
||||
}
|
||||
assert 2 == TestDoc.objects(q1 | q2).count()
|
||||
|
||||
def test_multiple_occurence_in_field(self):
|
||||
class Test(Document):
|
||||
@@ -192,8 +182,8 @@ class TestQ(unittest.TestCase):
|
||||
q3 = q1 & q2
|
||||
|
||||
query = q3.to_query(Test)
|
||||
self.assertEqual(query["$and"][0], q1.to_query(Test))
|
||||
self.assertEqual(query["$and"][1], q2.to_query(Test))
|
||||
assert query["$and"][0] == q1.to_query(Test)
|
||||
assert query["$and"][1] == q2.to_query(Test)
|
||||
|
||||
def test_q_clone(self):
|
||||
class TestDoc(Document):
|
||||
@@ -207,15 +197,15 @@ class TestQ(unittest.TestCase):
|
||||
# Check normal cases work without an error
|
||||
test = TestDoc.objects(Q(x__lt=7) & Q(x__gt=3))
|
||||
|
||||
self.assertEqual(test.count(), 3)
|
||||
assert test.count() == 3
|
||||
|
||||
test2 = test.clone()
|
||||
self.assertEqual(test2.count(), 3)
|
||||
self.assertNotEqual(test2, test)
|
||||
assert test2.count() == 3
|
||||
assert test2 != test
|
||||
|
||||
test3 = test2.filter(x=6)
|
||||
self.assertEqual(test3.count(), 1)
|
||||
self.assertEqual(test.count(), 3)
|
||||
assert test3.count() == 1
|
||||
assert test.count() == 3
|
||||
|
||||
def test_q(self):
|
||||
"""Ensure that Q objects may be used to query for documents.
|
||||
@@ -252,19 +242,19 @@ class TestQ(unittest.TestCase):
|
||||
|
||||
# Check ObjectId lookup works
|
||||
obj = BlogPost.objects(id=post1.id).first()
|
||||
self.assertEqual(obj, post1)
|
||||
assert obj == post1
|
||||
|
||||
# Check Q object combination with one does not exist
|
||||
q = BlogPost.objects(Q(title="Test 5") | Q(published=True))
|
||||
posts = [post.id for post in q]
|
||||
|
||||
published_posts = (post2, post3)
|
||||
self.assertTrue(all(obj.id in posts for obj in published_posts))
|
||||
assert all(obj.id in posts for obj in published_posts)
|
||||
|
||||
q = BlogPost.objects(Q(title="Test 1") | Q(published=True))
|
||||
posts = [post.id for post in q]
|
||||
published_posts = (post1, post2, post3, post5, post6)
|
||||
self.assertTrue(all(obj.id in posts for obj in published_posts))
|
||||
assert all(obj.id in posts for obj in published_posts)
|
||||
|
||||
# Check Q object combination
|
||||
date = datetime.datetime(2010, 1, 10)
|
||||
@@ -272,9 +262,9 @@ class TestQ(unittest.TestCase):
|
||||
posts = [post.id for post in q]
|
||||
|
||||
published_posts = (post1, post2, post3, post4)
|
||||
self.assertTrue(all(obj.id in posts for obj in published_posts))
|
||||
assert all(obj.id in posts for obj in published_posts)
|
||||
|
||||
self.assertFalse(any(obj.id in posts for obj in [post5, post6]))
|
||||
assert not any(obj.id in posts for obj in [post5, post6])
|
||||
|
||||
BlogPost.drop_collection()
|
||||
|
||||
@@ -284,15 +274,15 @@ class TestQ(unittest.TestCase):
|
||||
self.Person(name="user3", age=30).save()
|
||||
self.Person(name="user4", age=40).save()
|
||||
|
||||
self.assertEqual(self.Person.objects(Q(age__in=[20])).count(), 2)
|
||||
self.assertEqual(self.Person.objects(Q(age__in=[20, 30])).count(), 3)
|
||||
assert self.Person.objects(Q(age__in=[20])).count() == 2
|
||||
assert self.Person.objects(Q(age__in=[20, 30])).count() == 3
|
||||
|
||||
# Test invalid query objs
|
||||
with self.assertRaises(InvalidQueryError):
|
||||
with pytest.raises(InvalidQueryError):
|
||||
self.Person.objects("user1")
|
||||
|
||||
# filter should fail, too
|
||||
with self.assertRaises(InvalidQueryError):
|
||||
with pytest.raises(InvalidQueryError):
|
||||
self.Person.objects.filter("user1")
|
||||
|
||||
def test_q_regex(self):
|
||||
@@ -302,31 +292,31 @@ class TestQ(unittest.TestCase):
|
||||
person.save()
|
||||
|
||||
obj = self.Person.objects(Q(name=re.compile("^Gui"))).first()
|
||||
self.assertEqual(obj, person)
|
||||
assert obj == person
|
||||
obj = self.Person.objects(Q(name=re.compile("^gui"))).first()
|
||||
self.assertEqual(obj, None)
|
||||
assert obj == None
|
||||
|
||||
obj = self.Person.objects(Q(name=re.compile("^gui", re.I))).first()
|
||||
self.assertEqual(obj, person)
|
||||
assert obj == person
|
||||
|
||||
obj = self.Person.objects(Q(name__not=re.compile("^bob"))).first()
|
||||
self.assertEqual(obj, person)
|
||||
assert obj == person
|
||||
|
||||
obj = self.Person.objects(Q(name__not=re.compile("^Gui"))).first()
|
||||
self.assertEqual(obj, None)
|
||||
assert obj == None
|
||||
|
||||
def test_q_repr(self):
|
||||
self.assertEqual(repr(Q()), "Q(**{})")
|
||||
self.assertEqual(repr(Q(name="test")), "Q(**{'name': 'test'})")
|
||||
assert repr(Q()) == "Q(**{})"
|
||||
assert repr(Q(name="test")) == "Q(**{'name': 'test'})"
|
||||
|
||||
self.assertEqual(
|
||||
repr(Q(name="test") & Q(age__gte=18)),
|
||||
"(Q(**{'name': 'test'}) & Q(**{'age__gte': 18}))",
|
||||
assert (
|
||||
repr(Q(name="test") & Q(age__gte=18))
|
||||
== "(Q(**{'name': 'test'}) & Q(**{'age__gte': 18}))"
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
repr(Q(name="test") | Q(age__gte=18)),
|
||||
"(Q(**{'name': 'test'}) | Q(**{'age__gte': 18}))",
|
||||
assert (
|
||||
repr(Q(name="test") | Q(age__gte=18))
|
||||
== "(Q(**{'name': 'test'}) | Q(**{'age__gte': 18}))"
|
||||
)
|
||||
|
||||
def test_q_lists(self):
|
||||
@@ -341,8 +331,8 @@ class TestQ(unittest.TestCase):
|
||||
BlogPost(tags=["python", "mongo"]).save()
|
||||
BlogPost(tags=["python"]).save()
|
||||
|
||||
self.assertEqual(BlogPost.objects(Q(tags="mongo")).count(), 1)
|
||||
self.assertEqual(BlogPost.objects(Q(tags="python")).count(), 2)
|
||||
assert BlogPost.objects(Q(tags="mongo")).count() == 1
|
||||
assert BlogPost.objects(Q(tags="python")).count() == 2
|
||||
|
||||
BlogPost.drop_collection()
|
||||
|
||||
@@ -355,12 +345,12 @@ class TestQ(unittest.TestCase):
|
||||
pk = ObjectId()
|
||||
User(email="example@example.com", pk=pk).save()
|
||||
|
||||
self.assertEqual(
|
||||
1,
|
||||
User.objects.filter(Q(email="example@example.com") | Q(name="John Doe"))
|
||||
assert (
|
||||
1
|
||||
== User.objects.filter(Q(email="example@example.com") | Q(name="John Doe"))
|
||||
.limit(2)
|
||||
.filter(pk=pk)
|
||||
.count(),
|
||||
.count()
|
||||
)
|
||||
|
||||
def test_chained_q_or_filtering(self):
|
||||
@@ -376,14 +366,12 @@ class TestQ(unittest.TestCase):
|
||||
Item(postables=[Post(name="a"), Post(name="c")]).save()
|
||||
Item(postables=[Post(name="a"), Post(name="b"), Post(name="c")]).save()
|
||||
|
||||
self.assertEqual(
|
||||
Item.objects(Q(postables__name="a") & Q(postables__name="b")).count(), 2
|
||||
assert (
|
||||
Item.objects(Q(postables__name="a") & Q(postables__name="b")).count() == 2
|
||||
)
|
||||
self.assertEqual(
|
||||
Item.objects.filter(postables__name="a")
|
||||
.filter(postables__name="b")
|
||||
.count(),
|
||||
2,
|
||||
assert (
|
||||
Item.objects.filter(postables__name="a").filter(postables__name="b").count()
|
||||
== 2
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user