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.

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