test_models.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from redmail.models import EmailAddress, Error
  2. import pytest
  3. @pytest.mark.parametrize("addr,expected",
  4. [
  5. pytest.param('info@company.com', {'first_name': None, 'last_name': None, 'full_name': 'Info', 'organization': 'Company'}, id="Not personal"),
  6. pytest.param('first.last@company.com', {'first_name': "First", 'last_name': "Last", 'full_name': 'First Last', 'organization': 'Company'}, id="Personal"),
  7. pytest.param('no-reply@en.company.com', {'first_name': None, 'last_name': None, 'full_name': 'No-reply', 'organization': 'Company'}, id="Multi-domain-part"),
  8. ]
  9. )
  10. def test_address(addr, expected):
  11. address = EmailAddress(addr)
  12. assert expected['first_name'] == address.first_name
  13. assert expected['last_name'] == address.last_name
  14. assert expected['full_name'] == address.full_name
  15. assert expected['organization'] == address.organization
  16. assert str(address) == addr
  17. @pytest.mark.parametrize("exc_as", ['current stack', 'passed exception'])
  18. @pytest.mark.parametrize("cont_type,starts,ends",
  19. [
  20. pytest.param(
  21. 'text',
  22. 'Traceback (most recent call last):\n File "',
  23. '\n raise RuntimeError("deliberate error")\n\nRuntimeError: deliberate error',
  24. id="text"
  25. ),
  26. pytest.param(
  27. 'html',
  28. '<div class="error">\n <h4 class="header">Traceback (most recent call last):</h4>\n <pre class="traceback"><code> File &quot;',
  29. '\n raise RuntimeError(&quot;deliberate error&quot;)</code></pre>\n <div class="exception">\n <span class="exception-type">RuntimeError</span>: <span class="exception-value">deliberate error</span>\n </div>\n </div>',
  30. id="html"
  31. ),
  32. pytest.param(
  33. 'html-inline',
  34. '\n <div>\n <h4>Traceback (most recent call last):</h4>\n <pre><code> File &quot;',
  35. '\nraise RuntimeError(&quot;deliberate error&quot;)</code></pre>\n <span style="color: red; font-weight: bold">deliberate error</span>: <span>RuntimeError</span>\n </div>',
  36. id="html-inline"
  37. ),
  38. ]
  39. )
  40. def test_error(cont_type, starts, ends, exc_as):
  41. try:
  42. raise RuntimeError("deliberate error")
  43. except RuntimeError as exc:
  44. if exc_as == 'current stack':
  45. err = Error(content_type=cont_type)
  46. exc_type = err.exception_type
  47. exc_value = err.exception_value
  48. tb = err.traceback
  49. string = str(err)
  50. assert bool(err)
  51. e = exc
  52. if exc_as == 'passed exception':
  53. err = Error(content_type=cont_type, exception=e)
  54. exc_type = err.exception_type
  55. exc_value = err.exception_value
  56. tb = err.traceback
  57. string = str(err)
  58. assert bool(err)
  59. assert exc_type == 'RuntimeError'
  60. assert exc_value == 'deliberate error'
  61. assert len(tb) == 1
  62. assert tb[0].startswith(' File "')
  63. assert tb[0].endswith(', in test_error\n raise RuntimeError("deliberate error")\n')
  64. assert string.startswith(starts)
  65. assert string.endswith(ends)
  66. def test_error_no_error():
  67. err = Error()
  68. assert err.exception_type is None
  69. assert err.exception_value is None
  70. assert err.traceback is None
  71. assert not bool(err)
  72. def test_error_invalid_type():
  73. err = Error("Not existing")
  74. with pytest.raises(ValueError):
  75. str(err)