|
2 | 2 | from django.db.models import Count, Sum, F, Q
|
3 | 3 | from django.test import TestCase
|
4 | 4 |
|
5 |
| -from models import Artist, Group |
| 5 | +from models import Artist, Group, Post |
6 | 6 |
|
7 | 7 |
|
8 | 8 | class MongoTestCase(TestCase):
|
| 9 | + def assert_unsupported(self, obj): |
| 10 | + if callable(obj): |
| 11 | + # Queryset wrapped in a function (for aggregates and such) |
| 12 | + self.assertRaises(UnsupportedDatabaseOperation, obj) |
| 13 | + else: |
| 14 | + # Just a queryset that blows up on evaluation |
| 15 | + self.assertRaises(UnsupportedDatabaseOperation, list, obj) |
| 16 | + |
9 | 17 | def test_create(self):
|
10 | 18 | b = Artist.objects.create(name="Bruce Springsteen", good=True)
|
11 | 19 | self.assertTrue(b.pk is not None)
|
@@ -359,15 +367,7 @@ def test_close(self):
|
359 | 367 | # Ensure that closing a connection that was never established doesn't
|
360 | 368 | # blow up.
|
361 | 369 | connection.close()
|
362 |
| - |
363 |
| - def assert_unsupported(self, obj): |
364 |
| - if callable(obj): |
365 |
| - # Queryset wrapped in a function (for aggregates and such) |
366 |
| - self.assertRaises(UnsupportedDatabaseOperation, obj) |
367 |
| - else: |
368 |
| - # Just a queryset that blows up on evaluation |
369 |
| - self.assertRaises(UnsupportedDatabaseOperation, list, obj) |
370 |
| - |
| 370 | + |
371 | 371 | def test_unsupported_ops(self):
|
372 | 372 | self.assert_unsupported(
|
373 | 373 | Artist.objects.filter(current_group__name="The Beatles")
|
@@ -396,3 +396,55 @@ def test_unsupported_ops(self):
|
396 | 396 | self.assert_unsupported(
|
397 | 397 | Artist.objects.filter(Q(pk=0) | Q(pk=1))
|
398 | 398 | )
|
| 399 | + |
| 400 | + def test_list_field(self): |
| 401 | + p = Post.objects.create( |
| 402 | + title="Django ORM grows MongoDB support", |
| 403 | + tags=["python", "django", "mongodb", "web"] |
| 404 | + ) |
| 405 | + |
| 406 | + self.assertEqual(p.tags, ["python", "django", "mongodb", "web"]) |
| 407 | + |
| 408 | + p = Post.objects.get(pk=p.pk) |
| 409 | + self.assertEqual(p.tags, ["python", "django", "mongodb", "web"]) |
| 410 | + |
| 411 | + p = Post.objects.create( |
| 412 | + title="Rails 3.0 Released", |
| 413 | + tags=["ruby", "rails", "release", "web"], |
| 414 | + ) |
| 415 | + |
| 416 | + self.assertQuerysetEqual( |
| 417 | + Post.objects.filter(tags="web"), [ |
| 418 | + "Django ORM grows MongoDB support", |
| 419 | + "Rails 3.0 Released", |
| 420 | + ], |
| 421 | + lambda p: p.title, |
| 422 | + ) |
| 423 | + |
| 424 | + self.assertQuerysetEqual( |
| 425 | + Post.objects.filter(tags="python"), [ |
| 426 | + "Django ORM grows MongoDB support", |
| 427 | + ], |
| 428 | + lambda p: p.title |
| 429 | + ) |
| 430 | + |
| 431 | + self.assertRaises(ValueError, |
| 432 | + lambda: Post.objects.create(magic_numbers=["a"]) |
| 433 | + ) |
| 434 | + |
| 435 | + p = Post.objects.create( |
| 436 | + title="Simon the Wizard", |
| 437 | + magic_numbers=["42"] |
| 438 | + ) |
| 439 | + self.assertQuerysetEqual( |
| 440 | + Post.objects.filter(magic_numbers=42), [ |
| 441 | + "Simon the Wizard", |
| 442 | + ], |
| 443 | + lambda p: p.title, |
| 444 | + ) |
| 445 | + self.assertQuerysetEqual( |
| 446 | + Post.objects.filter(magic_numbers="42"), [ |
| 447 | + "Simon the Wizard", |
| 448 | + ], |
| 449 | + lambda p: p.title, |
| 450 | + ) |
0 commit comments