Source

django-mongodb / testproject / testapp / tests.py

Full commit
#-*- coding:utf-8 -*-

from django.db import models, connection, DatabaseError
from django.test import TestCase

from testapp.models import CRUDTestModel, NoAutoPK_CRUDTestModel, Author, Article, LargeArticle

class TestCRUD(TestCase):

    def setUp(self):
        self.db = connection.cursor()
        self.crud_col_name = CRUDTestModel._meta.db_table
        self.nopk_crud_col_name = NoAutoPK_CRUDTestModel._meta.db_table

    def tearDown(self):
        self.db.drop_collection(self.crud_col_name)
        self.db.drop_collection(self.nopk_crud_col_name)
        self.db.logout()

    def test_insert(self):
        # Normal insert
        for i in xrange(10):
            obj = CRUDTestModel(text="text%d" % i)
            obj.save()
            self.assertTrue(isinstance(obj.pk, str))
            query = {'text':"text%d" % i}
            self.assertTrue(self.db[self.crud_col_name].find_one(query) is not None)
        self.assertEqual(self.db[self.crud_col_name].find().count(), 10)
        
        # Insert with skipped primary key in model without AutoField
        self.assertRaises(DatabaseError, lambda: NoAutoPK_CRUDTestModel(text="text").save())
        self.assertEqual(self.db[self.nopk_crud_col_name].find().count(), 0)
        # Insert with predefined primary key
        NoAutoPK_CRUDTestModel(text="text", name=12).save()
        self.assertEqual(self.db[self.nopk_crud_col_name].find_one({'_id':12})['text'], "text")
        self.assertEqual(NoAutoPK_CRUDTestModel.objects.get(name=12).pk, 12)


class TestQueries(TestCase):
    def setUp(self):
        self.db = connection.cursor()
        self.author_col = self.db[Author._meta.db_table]

    def tearDown(self):
        self.db.logout()
        Author.objects.all().delete()

    def test_queries(self):
        author = Author.objects.create(name="simple name")
        self.assertEqual(Author.objects.get(name__iexact="Simple Name"), author)


class TestRelations(TestCase):
    
    def setUp(self):
        self.db = connection.cursor()
        self.author_col = self.db[Author._meta.db_table]
        self.arts_col = self.db[Article._meta.db_table]
        self.large_arts_col = self.db[LargeArticle._meta.db_table]

    def tearDown(self):
        Author.objects.all().delete()
        self.db.logout()

    def test_foreign_keys(self):
        author1 = Author.objects.create(name="name1")
        author2 = Author.objects.create(name="name2")
        art1 = Article.objects.create(title="title1", author=author1)
        art2 = Article.objects.create(title="title2", author=author1)
        self.assertEqual(Article.objects.get(title="title1").author.pk, author1.pk)
        self.assertEqual(Article.objects.get(title="title2").author.pk, author1.pk)
        self.assertEqual(list(author1.article_set.all().order_by("title")), [art1, art2])
        self.assertEqual(author1.article_set.all().count(), 2)
        self.assertEqual(author2.article_set.all().count(), 0)
        author1.delete()
        self.assertEqual(list(Article.objects.filter(title__in=["title1", "title2"])), [])

    def test_many_to_many(self):
        author1 = Author.objects.create(name="name1")
        author2 = Author.objects.create(name="name2")
        art1 = LargeArticle.objects.create(title="title1")
        art1.authors = [author1, author2]
        art2 = LargeArticle.objects.create(title="title2")
        self.assertEqual(list(art1.authors.all().order_by('name')), [author1, author2])
        self.assertEqual(art1.authors.count(), 2)
        author1.delete()
        self.assertEqual(art1.authors.count(), 1)
        self.assertEqual(art2.authors.count(), 0)