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.

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