Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. import mongoengine
  2. from django.test import TestCase
  3. from django.conf import settings
  4.  
  5.  
  6. class MongoTestCase(TestCase):
  7. """Instead of using django 'TestCase' class. Use this in tests.
  8. It will overwrite the 'setUp' and 'tearDown' functions of django 'TestCase'.
  9.  
  10. For example->
  11. In tests.py :
  12.  
  13. from models import App
  14.  
  15. class AppCreationTest(MongoTestCase):
  16. def test(self):
  17. app = App(name="first_app")
  18. app.save()
  19. assert App.objects.first().name == app.name
  20.  
  21. In terminal :
  22.  
  23. python manage.py test
  24.  
  25. Note: Dont forget to specify test db details in settings.
  26. """
  27. def setUp(self):
  28. mongoengine.connection.disconnect()
  29. mongoengine.connect(
  30. host=settings.MONGO['host'],
  31. port=settings.MONGO['port'],
  32. db=settings.MONGO['db'],
  33. username=settings.MONGO['username'],
  34. password=settings.MONGO['password']
  35. )
  36. super().setUpClass()
  37.  
  38. def tearDown(self):
  39. from mongoengine.connection import get_connection, disconnect
  40. connection = get_connection()
  41. connection.drop_database(settings.MONGO['db'])
  42. disconnect()
  43. super().tearDownClass()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement