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.

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