PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test_extensions/examples/examples.py

https://github.com/towerjoo/django-test-extensions
Python | 112 lines | 106 code | 1 blank | 5 comment | 7 complexity | 5a4d170e3a22bcf51f2496974161536d MD5 | raw file
  1. from test_extensions.common import Common
  2. class Examples(Common):
  3. """
  4. This class contains a number of example tests using the common custom assertions.
  5. Note that these tests won't run as they refer to code that does not exist. Note also
  6. that all tests begin with test_. This ensures the test runner picks them up.
  7. """
  8. def test_always_pass(self):
  9. "Demonstration of how to pass a test based on logic"
  10. self.assertTrue(True)
  11. def test_always_fail(self):
  12. "Demonstration of how to fail a test based on logic"
  13. self.assertFalse(False, "Something went wrong")
  14. def test_presense_of_management_command(self):
  15. "Check to see whether a given management commands is available"
  16. try:
  17. call_command('management_command_name')
  18. except Exception:
  19. self.assert_("management_command_name management command missing")
  20. def test_object_type(self):
  21. "Simple test to check a given object type"
  22. example = Object()
  23. self.assert_is_instance(Object,example)
  24. def test_assert_raises(self):
  25. "Test whether a given function raises a given exception"
  26. path = os.path.join('/example_directory', 'invalid_file_name')
  27. self.assert_raises(ExampleException, example_function, path)
  28. def test_assert_attrs(self):
  29. "Demonstration of assert_attrs, checks that a given object has a given set of attributes"
  30. event = Event(title="Title",description="Description")
  31. self.assert_attrs(event,
  32. title = 'Title',
  33. description = 'Description'
  34. )
  35. def test_assert_counts(self):
  36. "Demonstration of assert_counts using a set of fictional objects"
  37. self.assert_counts([1, 1, 1, 1], [Object1, Object2, Object3, Object4])
  38. def test_assert_code(self):
  39. "Use the HTTP client to make a request and then check the HTTP status code in the response"
  40. response = self.client.get('/')
  41. self.assert_code(response, 200)
  42. def test_creation_of_objects_in_admin(self):
  43. "Demonstration of an admin test to check successful object creation"
  44. form = {
  45. 'title': 'title',
  46. 'description': 'description',
  47. }
  48. self.login_as_admin()
  49. self.assert_counts([0], [Object])
  50. response = self.client.post('/admin/objects/object/add/', form)
  51. self.assert_code(response, 302)
  52. self.assert_counts([1], [Object])
  53. def test_you_can_delete_objects_you_created(self):
  54. "Test object deletion via the admin"
  55. self.login_as_admin()
  56. form = {
  57. 'title': 'title',
  58. 'description': 'description',
  59. }
  60. self.assert_counts([0], [Object])
  61. self.client.post('/admin/objects/object/add/', form)
  62. self.assert_counts([1], [Object])
  63. response = self.client.post('/admin/objects/object/%d/delete/' % Object.objects.get().id, {'post':'yes'})
  64. self.assert_counts([0], [Object])
  65. def test_assert_renders(self):
  66. "Example template tag test to check correct rendering"
  67. expected = 'output of template tag'
  68. self.assert_renders('{% load templatetag %}{% templatetag %}', expected)
  69. def test_assert_render_matches(self):
  70. "Example of testing template tags using regex match"
  71. self.assert_render_matches(
  72. r'^/assets/a/common/blah.js\?cachebust=[\d\.]+$',
  73. '{% load static %}{% static "a/common/blah.js" %}',
  74. )
  75. def test_simple_addition(self):
  76. "Pattern for testing input/output of a function"
  77. for (augend, addend, result, msg) in [
  78. (1, 2, 3, "1+2=3"),
  79. (1, 3, 4, "1+3=4"),
  80. ]:
  81. self.assert_equal(result,augend.plus(addend))
  82. def test_response_contains(self):
  83. "Example of a test for content on a given view"
  84. response = self.client.get('/example/')
  85. self.assert_response_contains('<h1>', response)
  86. self.assert_response_doesnt_contain("Not on page", response)
  87. def test_using_beautiful_soup(self):
  88. "Example test for content on a given view, this time using the BeautifulSoup parser"
  89. response = self.client.get('/example/')
  90. soup = BeautifulSoup(response.content)
  91. self.assert_equal("Page Title", soup.find("title").string.strip())
  92. def test_get_tables_that_should_exist(self):
  93. "Useful pattern for checking for the existence of all required tables"
  94. tables_that_exist = [row[0] for row in _execute("SHOW TABLES").fetchall()]
  95. self.assert_equal(True, 'objects_object' in tables_that_exist)