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.

241 lines
8.4 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. from validate.format import check_description, max_description_length
  8. class TestValidadeFormat(unittest.TestCase):
  9. def test_error_message_return_and_return_type(self):
  10. line_num_unity = 1
  11. line_num_ten = 10
  12. line_num_hundred = 100
  13. line_num_thousand = 1000
  14. msg = 'This is a unit test'
  15. err_msg_unity = error_message(line_num_unity, msg)
  16. err_msg_ten = error_message(line_num_ten, msg)
  17. err_msg_hundred = error_message(line_num_hundred, msg)
  18. err_msg_thousand = error_message(line_num_thousand, msg)
  19. self.assertIsInstance(err_msg_unity, str)
  20. self.assertIsInstance(err_msg_ten, str)
  21. self.assertIsInstance(err_msg_hundred, str)
  22. self.assertIsInstance(err_msg_thousand, str)
  23. self.assertEqual(err_msg_unity, '(L002) This is a unit test')
  24. self.assertEqual(err_msg_ten, '(L011) This is a unit test')
  25. self.assertEqual(err_msg_hundred, '(L101) This is a unit test')
  26. self.assertEqual(err_msg_thousand, '(L1001) This is a unit test')
  27. def test_if_get_categories_content_return_correct_data_of_categories(self):
  28. fake_contents = [
  29. '### A',
  30. 'API | Description | Auth | HTTPS | CORS |',
  31. '|---|---|---|---|---|',
  32. '| [AA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  33. '| [AB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  34. '',
  35. '### B',
  36. 'API | Description | Auth | HTTPS | CORS |',
  37. '|---|---|---|---|---|',
  38. '| [BA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  39. '| [BB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |'
  40. ]
  41. result = get_categories_content(fake_contents)
  42. self.assertIsInstance(result, tuple)
  43. categories, category_line_num = result
  44. self.assertIsInstance(categories, dict)
  45. self.assertIsInstance(category_line_num, dict)
  46. expected_result = ({'A': ['AA', 'AB'], 'B': ['BA', 'BB']}, {'A': 0, 'B': 6})
  47. for res, ex_res in zip(result, expected_result):
  48. with self.subTest():
  49. self.assertEqual(res, ex_res)
  50. def test_if_check_alphabetical_order_return_correct_msg_error(self):
  51. correct_lines = [
  52. '### A',
  53. 'API | Description | Auth | HTTPS | CORS |',
  54. '|---|---|---|---|---|',
  55. '| [AA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  56. '| [AB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  57. '',
  58. '### B',
  59. 'API | Description | Auth | HTTPS | CORS |',
  60. '|---|---|---|---|---|',
  61. '| [BA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  62. '| [BB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |'
  63. ]
  64. incorrect_lines = [
  65. '### A',
  66. 'API | Description | Auth | HTTPS | CORS |',
  67. '|---|---|---|---|---|',
  68. '| [AB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  69. '| [AA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  70. '',
  71. '### B',
  72. 'API | Description | Auth | HTTPS | CORS |',
  73. '|---|---|---|---|---|',
  74. '| [BB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  75. '| [BA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |'
  76. ]
  77. err_msgs_1 = check_alphabetical_order(correct_lines)
  78. err_msgs_2 = check_alphabetical_order(incorrect_lines)
  79. self.assertIsInstance(err_msgs_1, list)
  80. self.assertIsInstance(err_msgs_2, list)
  81. self.assertEqual(len(err_msgs_1), 0)
  82. self.assertEqual(len(err_msgs_2), 2)
  83. expected_err_msgs = [
  84. '(L001) A category is not alphabetical order',
  85. '(L007) B category is not alphabetical order'
  86. ]
  87. for err_msg, ex_err_msg in zip(err_msgs_2, expected_err_msgs):
  88. with self.subTest():
  89. self.assertEqual(err_msg, ex_err_msg)
  90. def test_check_title_return_type(self):
  91. raw_title_1 = '[A](https://www.ex.com)'
  92. raw_title_2 = '[A(https://www.ex.com)'
  93. raw_title_3 = '[A API](https://www.ex.com)'
  94. result_1 = check_title(0, raw_title_1)
  95. result_2 = check_title(0, raw_title_2)
  96. result_3 = check_title(0, raw_title_3)
  97. self.assertIsInstance(result_1, list)
  98. self.assertIsInstance(result_2, list)
  99. self.assertIsInstance(result_3, list)
  100. err_msg_1 = result_2[0]
  101. err_msg_2 = result_3[0]
  102. self.assertIsInstance(err_msg_1, str)
  103. self.assertIsInstance(err_msg_2, str)
  104. def test_check_title_with_correct_title(self):
  105. raw_title = '[A](https://www.ex.com)'
  106. err_msgs = check_title(0, raw_title)
  107. self.assertEqual(len(err_msgs), 0)
  108. self.assertEqual(err_msgs, [])
  109. def test_check_title_with_markdown_syntax_incorrect(self):
  110. raw_title = '[A(https://www.ex.com)'
  111. err_msgs = check_title(0, raw_title)
  112. self.assertEqual(len(err_msgs), 1)
  113. err_msg = err_msgs[0]
  114. expected_err_msg = '(L001) Title syntax should be "[TITLE](LINK)"'
  115. self.assertEqual(err_msg, expected_err_msg)
  116. def test_check_title_with_api_at_the_end_of_the_title(self):
  117. raw_title = '[A API](https://www.ex.com)'
  118. err_msgs = check_title(0, raw_title)
  119. self.assertEqual(len(err_msgs), 1)
  120. err_msg = err_msgs[0]
  121. expected_err_msg = '(L001) Title should not end with "... API". Every entry is an API here!'
  122. self.assertEqual(err_msg, expected_err_msg)
  123. def test_checK_description_return_type(self):
  124. desc_1 = 'This is a fake description'
  125. desc_2 = 'this is a fake description'
  126. desc_3 = 'This is a fake description!'
  127. desc_4 = 'This is a fake descriptionThis is a fake descriptionThis is a fake descriptionThis is a fake description'
  128. result_1 = check_title(0, desc_1)
  129. result_2 = check_title(0, desc_2)
  130. result_3 = check_title(0, desc_3)
  131. result_4 = check_title(0, desc_4)
  132. self.assertIsInstance(result_1, list)
  133. self.assertIsInstance(result_2, list)
  134. self.assertIsInstance(result_3, list)
  135. self.assertIsInstance(result_4, list)
  136. err_msg_1 = result_2[0]
  137. err_msg_2 = result_3[0]
  138. err_msg_3 = result_4[0]
  139. self.assertIsInstance(err_msg_1, str)
  140. self.assertIsInstance(err_msg_2, str)
  141. self.assertIsInstance(err_msg_3, str)
  142. def test_check_description_with_correct_description(self):
  143. desc = 'This is a fake description'
  144. err_msgs = check_description(0, desc)
  145. self.assertEqual(len(err_msgs), 0)
  146. self.assertEqual(err_msgs, [])
  147. def test_check_description_with_first_char_is_not_capitalized(self):
  148. desc = 'this is a fake description'
  149. err_msgs = check_description(0, desc)
  150. self.assertEqual(len(err_msgs), 1)
  151. err_msg = err_msgs[0]
  152. expected_err_msg = '(L001) first character of description is not capitalized'
  153. self.assertEqual(err_msg, expected_err_msg)
  154. def test_check_description_with_punctuation_in_the_end(self):
  155. base_desc = 'This is a fake description'
  156. punctuation = r"""!"#$%&'*+,-./:;<=>?@[\]^_`{|}~"""
  157. desc_with_punc = [base_desc + punc for punc in punctuation]
  158. for desc in desc_with_punc:
  159. with self.subTest():
  160. err_msgs = check_description(0, desc)
  161. self.assertEqual(len(err_msgs), 1)
  162. err_msg = err_msgs[0]
  163. expected_err_msg = f'(L001) description should not end with {desc[-1]}'
  164. self.assertEqual(err_msg, expected_err_msg)
  165. def test_check_description_that_exceeds_the_character_limit(self):
  166. long_desc = 'Desc' * max_description_length
  167. long_desc_length = len(long_desc)
  168. err_msgs = check_description(0, long_desc)
  169. self.assertEqual(len(err_msgs), 1)
  170. err_msg = err_msgs[0]
  171. expected_err_msg = f'(L001) description should not exceed {max_description_length} characters (currently {long_desc_length})'
  172. self.assertEqual(err_msg, expected_err_msg)