Skip to content

Commit 9cff4cb

Browse files
committed
Python: ORM: Add a few more tests
There were a few methods I had overlooked
1 parent ae057c7 commit 9cff4cb

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

python/ql/test/library-tests/frameworks/django-orm/testapp/orm_tests.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,45 @@ def test_save11_load():
248248
obj = save11_Comment.objects.first()
249249
SINK(obj.text) # $ MISSING: flow
250250

251+
# --------------------------------------
252+
# <Model>.objects.bulk_create()
253+
# see https://docs.djangoproject.com/en/4.0/ref/models/querysets/#bulk-create
254+
# --------------------------------------
255+
class TestSave12(models.Model):
256+
text = models.CharField(max_length=512)
257+
258+
def test_save12_store():
259+
objs = TestSave12.objects.bulk_create([
260+
TestSave12(text=SOURCE),
261+
TestSave12(text="foo"),
262+
])
263+
SINK(objs[0].text) # $ MISSING: flow
264+
265+
def test_save12_load():
266+
obj = TestSave12.objects.first()
267+
SINK(obj.text) # $ MISSING: flow
268+
269+
# --------------------------------------
270+
# <Model>.objects.bulk_update()
271+
# see https://docs.djangoproject.com/en/4.0/ref/models/querysets/#bulk-update
272+
# --------------------------------------
273+
class TestSave13(models.Model):
274+
text = models.CharField(max_length=512)
275+
276+
def test_save13_init():
277+
obj = TestSave13(text="foo")
278+
obj.save()
279+
280+
def test_save13_store():
281+
objs = TestSave13.objects.all()
282+
for obj in objs:
283+
obj.text = SOURCE
284+
TestSave13.objects.bulk_update(objs, ["text"])
285+
286+
def test_save13_load():
287+
obj = TestSave13.objects.first()
288+
SINK(obj.text) # $ MISSING: flow
289+
251290
# ------------------------------------------------------------------------------
252291
# Different ways to load data from the DB through the ORM
253292
# ------------------------------------------------------------------------------
@@ -319,6 +358,14 @@ def test_load_values_list():
319358
SINK(text) # $ MISSING: flow
320359
SINK(vals[0]) # $ MISSING: flow
321360

361+
def test_load_in_bulk():
362+
# see https://docs.djangoproject.com/en/4.0/ref/models/querysets/#in-bulk
363+
d = TestLoad.objects.in_bulk([1])
364+
for val in d.values():
365+
SINK(val.text) # $ MISSING: flow
366+
SINK(d[1].text) # $ MISSING: flow
367+
368+
322369
# Good resources:
323370
# - https://docs.djangoproject.com/en/4.0/topics/db/queries/#making-queries
324371
# - https://docs.djangoproject.com/en/4.0/ref/models/querysets/

python/ql/test/library-tests/frameworks/django-orm/testapp/tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,12 @@ def test_mymodel_form_save():
7474

7575
obj = MyModel.objects.last()
7676
assert obj.text == text
77+
78+
@pytest.mark.django_db
79+
def test_none_all():
80+
from .orm_form_test import MyModel
81+
MyModel.objects.create(text="foo")
82+
83+
assert len(MyModel.objects.all()) == 1
84+
assert len(MyModel.objects.none().all()) == 0
85+
assert len(MyModel.objects.all().none()) == 0

0 commit comments

Comments
 (0)