'DB Alias tests.'

def test_db_alias_tests(self):

    register_connection('testdb-1', 'mongoenginetest2')
    register_connection('testdb-2', 'mongoenginetest3')
    register_connection('testdb-3', 'mongoenginetest4')
    class User(Document, ):
        name = StringField()
        meta = {'db_alias': 'testdb-1'}
    class Book(Document, ):
        name = StringField()
        meta = {'db_alias': 'testdb-2'}
    User.drop_collection()
    Book.drop_collection()
    bob = User.objects.create(name='Bob')
    hp = Book.objects.create(name='Harry    Potter')
    self.assertEqual(User.objects.first(), bob)
    self.assertEqual(Book.objects.first(), hp)
    class AuthorBooks(Document, ):
        author = ReferenceField(User)
        book = ReferenceField(Book)
        meta = {'db_alias': 'testdb-3'}
    AuthorBooks.drop_collection()
    ab = AuthorBooks.objects.create(author=bob, book=hp)
    self.assertEqual(AuthorBooks.objects.first(), ab)
    self.assertEqual(AuthorBooks.objects.first().book, hp)
    self.assertEqual(AuthorBooks.objects.first().author, bob)
    self.assertEqual(AuthorBooks.objects.filter(author=bob).first(), ab)
    self.assertEqual(AuthorBooks.objects.filter(book=hp).first(), ab)
    self.assertEqual(User._get_db(), get_db('testdb-1'))
    self.assertEqual(Book._get_db(), get_db('testdb-2'))
    self.assertEqual(AuthorBooks._get_db(), get_db('testdb-3'))
    self.assertEqual(User._get_collection(), get_db('testdb-1')[User._get_collection_name()])
    self.assertEqual(Book._get_collection(), get_db('testdb-2')[Book._get_collection_name()])
    self.assertEqual(AuthorBooks._get_collection(), get_db('testdb-3')[AuthorBooks._get_collection_name()])
