You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.7 KiB

  1. # -*- coding: utf-8 -*-
  2. import unittest
  3. from validate.format import error_message
  4. from validate.format import get_categories_content
  5. from validate.format import check_alphabetical_order
  6. from validate.format import check_title
  7. class TestValidadeFormat(unittest.TestCase):
  8. def test_error_message_return_and_return_type(self):
  9. line_num_unity = 1
  10. line_num_ten = 10
  11. line_num_hundred = 100
  12. line_num_thousand = 1000
  13. msg = 'This is a unit test'
  14. err_msg_unity = error_message(line_num_unity, msg)
  15. err_msg_ten = error_message(line_num_ten, msg)
  16. err_msg_hundred = error_message(line_num_hundred, msg)
  17. err_msg_thousand = error_message(line_num_thousand, msg)
  18. self.assertIsInstance(err_msg_unity, str)
  19. self.assertIsInstance(err_msg_ten, str)
  20. self.assertIsInstance(err_msg_hundred, str)
  21. self.assertIsInstance(err_msg_thousand, str)
  22. self.assertEqual(err_msg_unity, '(L002) This is a unit test')
  23. self.assertEqual(err_msg_ten, '(L011) This is a unit test')
  24. self.assertEqual(err_msg_hundred, '(L101) This is a unit test')
  25. self.assertEqual(err_msg_thousand, '(L1001) This is a unit test')
  26. def test_if_get_categories_content_return_correct_data_of_categories(self):
  27. fake_contents = [
  28. '### A',
  29. 'API | Description | Auth | HTTPS | CORS |',
  30. '|---|---|---|---|---|',
  31. '| [AA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  32. '| [AB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  33. '',
  34. '### B',
  35. 'API | Description | Auth | HTTPS | CORS |',
  36. '|---|---|---|---|---|',
  37. '| [BA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  38. '| [BB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |'
  39. ]
  40. result = get_categories_content(fake_contents)
  41. self.assertIsInstance(result, tuple)
  42. categories, category_line_num = result
  43. self.assertIsInstance(categories, dict)
  44. self.assertIsInstance(category_line_num, dict)
  45. expected_result = ({'A': ['AA', 'AB'], 'B': ['BA', 'BB']}, {'A': 0, 'B': 6})
  46. for res, ex_res in zip(result, expected_result):
  47. with self.subTest():
  48. self.assertEqual(res, ex_res)
  49. def test_if_check_alphabetical_order_return_correct_msg_error(self):
  50. correct_lines = [
  51. '### A',
  52. 'API | Description | Auth | HTTPS | CORS |',
  53. '|---|---|---|---|---|',
  54. '| [AA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  55. '| [AB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  56. '',
  57. '### B',
  58. 'API | Description | Auth | HTTPS | CORS |',
  59. '|---|---|---|---|---|',
  60. '| [BA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  61. '| [BB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |'
  62. ]
  63. incorrect_lines = [
  64. '### A',
  65. 'API | Description | Auth | HTTPS | CORS |',
  66. '|---|---|---|---|---|',
  67. '| [AB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  68. '| [AA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  69. '',
  70. '### B',
  71. 'API | Description | Auth | HTTPS | CORS |',
  72. '|---|---|---|---|---|',
  73. '| [BB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  74. '| [BA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |'
  75. ]
  76. err_msgs_1 = check_alphabetical_order(correct_lines)
  77. err_msgs_2 = check_alphabetical_order(incorrect_lines)
  78. self.assertIsInstance(err_msgs_1, list)
  79. self.assertIsInstance(err_msgs_2, list)
  80. self.assertEqual(len(err_msgs_1), 0)
  81. self.assertEqual(len(err_msgs_2), 2)
  82. expected_err_msgs = [
  83. '(L001) A category is not alphabetical order',
  84. '(L007) B category is not alphabetical order'
  85. ]
  86. for err_msg, ex_err_msg in zip(err_msgs_2, expected_err_msgs):
  87. with self.subTest():
  88. self.assertEqual(err_msg, ex_err_msg)
  89. def test_check_title_return_type(self):
  90. raw_title_1 = '[A](https://www.ex.com)'
  91. raw_title_2 = '[A(https://www.ex.com)'
  92. raw_title_3 = '[A API](https://www.ex.com)'
  93. result_1 = check_title(0, raw_title_1)
  94. result_2 = check_title(0, raw_title_2)
  95. result_3 = check_title(0, raw_title_3)
  96. self.assertIsInstance(result_1, list)
  97. self.assertIsInstance(result_2, list)
  98. self.assertIsInstance(result_3, list)
  99. err_msg_1 = result_2[0]
  100. err_msg_2 = result_3[0]
  101. self.assertIsInstance(err_msg_1, str)
  102. self.assertIsInstance(err_msg_2, str)
  103. def test_check_title_with_correct_title(self):
  104. raw_title = '[A](https://www.ex.com)'
  105. err_msgs = check_title(0, raw_title)
  106. self.assertEqual(len(err_msgs), 0)
  107. self.assertEqual(err_msgs, [])
  108. def test_check_title_with_markdown_syntax_incorrect(self):
  109. raw_title = '[A(https://www.ex.com)'
  110. err_msgs = check_title(0, raw_title)
  111. self.assertEqual(len(err_msgs), 1)
  112. err_msg = err_msgs[0]
  113. expected_err_msg = '(L001) Title syntax should be "[TITLE](LINK)"'
  114. self.assertEqual(err_msg, expected_err_msg)
  115. def test_check_title_with_api_at_the_end_of_the_title(self):
  116. raw_title = '[A API](https://www.ex.com)'
  117. err_msgs = check_title(0, raw_title)
  118. self.assertEqual(len(err_msgs), 1)
  119. err_msg = err_msgs[0]
  120. expected_err_msg = '(L001) Title should not end with "... API". Every entry is an API here!'
  121. self.assertEqual(err_msg, expected_err_msg)