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.

368 lines
13 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_return_type(self):
  94. raw_title_1 = '[A](https://www.ex.com)'
  95. raw_title_2 = '[A(https://www.ex.com)'
  96. raw_title_3 = '[A API](https://www.ex.com)'
  97. result_1 = check_title(0, raw_title_1)
  98. result_2 = check_title(0, raw_title_2)
  99. result_3 = check_title(0, raw_title_3)
  100. self.assertIsInstance(result_1, list)
  101. self.assertIsInstance(result_2, list)
  102. self.assertIsInstance(result_3, list)
  103. err_msg_1 = result_2[0]
  104. err_msg_2 = result_3[0]
  105. self.assertIsInstance(err_msg_1, str)
  106. self.assertIsInstance(err_msg_2, str)
  107. def test_check_title_with_correct_title(self):
  108. raw_title = '[A](https://www.ex.com)'
  109. err_msgs = check_title(0, raw_title)
  110. self.assertEqual(len(err_msgs), 0)
  111. self.assertEqual(err_msgs, [])
  112. def test_check_title_with_markdown_syntax_incorrect(self):
  113. raw_title = '[A(https://www.ex.com)'
  114. err_msgs = check_title(0, raw_title)
  115. self.assertEqual(len(err_msgs), 1)
  116. err_msg = err_msgs[0]
  117. expected_err_msg = '(L001) Title syntax should be "[TITLE](LINK)"'
  118. self.assertEqual(err_msg, expected_err_msg)
  119. def test_check_title_with_api_at_the_end_of_the_title(self):
  120. raw_title = '[A API](https://www.ex.com)'
  121. err_msgs = check_title(0, raw_title)
  122. self.assertEqual(len(err_msgs), 1)
  123. err_msg = err_msgs[0]
  124. expected_err_msg = '(L001) Title should not end with "... API". Every entry is an API here!'
  125. self.assertEqual(err_msg, expected_err_msg)
  126. def test_checK_description_return_type(self):
  127. desc_1 = 'This is a fake description'
  128. desc_2 = 'this is a fake description'
  129. desc_3 = 'This is a fake description!'
  130. desc_4 = 'This is a fake descriptionThis is a fake descriptionThis is a fake descriptionThis is a fake description'
  131. result_1 = check_title(0, desc_1)
  132. result_2 = check_title(0, desc_2)
  133. result_3 = check_title(0, desc_3)
  134. result_4 = check_title(0, desc_4)
  135. self.assertIsInstance(result_1, list)
  136. self.assertIsInstance(result_2, list)
  137. self.assertIsInstance(result_3, list)
  138. self.assertIsInstance(result_4, list)
  139. err_msg_1 = result_2[0]
  140. err_msg_2 = result_3[0]
  141. err_msg_3 = result_4[0]
  142. self.assertIsInstance(err_msg_1, str)
  143. self.assertIsInstance(err_msg_2, str)
  144. self.assertIsInstance(err_msg_3, str)
  145. def test_check_description_with_correct_description(self):
  146. desc = 'This is a fake description'
  147. err_msgs = check_description(0, desc)
  148. self.assertEqual(len(err_msgs), 0)
  149. self.assertEqual(err_msgs, [])
  150. def test_check_description_with_first_char_is_not_capitalized(self):
  151. desc = 'this is a fake description'
  152. err_msgs = check_description(0, desc)
  153. self.assertEqual(len(err_msgs), 1)
  154. err_msg = err_msgs[0]
  155. expected_err_msg = '(L001) first character of description is not capitalized'
  156. self.assertEqual(err_msg, expected_err_msg)
  157. def test_check_description_with_punctuation_in_the_end(self):
  158. base_desc = 'This is a fake description'
  159. punctuation = r"""!"#$%&'*+,-./:;<=>?@[\]^_`{|}~"""
  160. desc_with_punc = [base_desc + punc for punc in punctuation]
  161. for desc in desc_with_punc:
  162. with self.subTest():
  163. err_msgs = check_description(0, desc)
  164. self.assertEqual(len(err_msgs), 1)
  165. err_msg = err_msgs[0]
  166. expected_err_msg = f'(L001) description should not end with {desc[-1]}'
  167. self.assertEqual(err_msg, expected_err_msg)
  168. def test_check_description_that_exceeds_the_character_limit(self):
  169. long_desc = 'Desc' * max_description_length
  170. long_desc_length = len(long_desc)
  171. err_msgs = check_description(0, long_desc)
  172. self.assertEqual(len(err_msgs), 1)
  173. err_msg = err_msgs[0]
  174. expected_err_msg = f'(L001) description should not exceed {max_description_length} characters (currently {long_desc_length})'
  175. self.assertEqual(err_msg, expected_err_msg)
  176. def test_check_auth_return_type(self):
  177. auth_with_backtick = [f'`{auth}`' for auth in auth_keys if auth != 'No']
  178. auth_with_backtick.append('No')
  179. auth_without_backtick = [auth for auth in auth_keys if auth != 'No']
  180. auth_invalid = ['Yes', 'yes', 'no', 'random', 'Unknown']
  181. for auth in auth_with_backtick:
  182. with self.subTest():
  183. result = check_auth(0, auth)
  184. self.assertIsInstance(result, list)
  185. for auth in auth_without_backtick:
  186. with self.subTest():
  187. err_msgs = check_auth(0, auth)
  188. self.assertIsInstance(result, list)
  189. err_msg = err_msgs[0]
  190. self.assertIsInstance(err_msg, str)
  191. for auth in auth_invalid:
  192. with self.subTest():
  193. err_msgs = check_auth(0, auth)
  194. self.assertIsInstance(result, list)
  195. err_msg = err_msgs[0]
  196. self.assertIsInstance(err_msg, str)
  197. def test_check_auth_with_correct_auth(self):
  198. auth_valid = [f'`{auth}`' for auth in auth_keys if auth != 'No']
  199. auth_valid.append('No')
  200. for auth in auth_valid:
  201. with self.subTest():
  202. err_msgs = check_auth(0, auth)
  203. self.assertIsInstance(err_msgs, list)
  204. self.assertEqual(len(err_msgs), 0)
  205. self.assertEqual(err_msgs, [])
  206. def test_check_auth_without_backtick(self):
  207. auth_without_backtick = [auth for auth in auth_keys if auth != 'No']
  208. for auth in auth_without_backtick:
  209. with self.subTest():
  210. err_msgs = check_auth(0, auth)
  211. self.assertIsInstance(err_msgs, list)
  212. self.assertEqual(len(err_msgs), 1)
  213. err_msg = err_msgs[0]
  214. expected_err_msg = '(L001) auth value is not enclosed with `backticks`'
  215. self.assertEqual(err_msg, expected_err_msg)
  216. def test_check_auth_with_invalid_auth(self):
  217. auth_invalid = ['Yes', 'yes', 'no', 'random', 'Unknown']
  218. for auth in auth_invalid:
  219. with self.subTest():
  220. err_msgs = check_auth(0, auth)
  221. self.assertIsInstance(err_msgs, list)
  222. self.assertEqual(len(err_msgs), 2)
  223. err_msg_1 = err_msgs[0]
  224. err_msg_2 = err_msgs[1]
  225. expected_err_msg_1 = f'(L001) auth value is not enclosed with `backticks`'
  226. expected_err_msg_2 = f'(L001) {auth} is not a valid Auth option'
  227. self.assertEqual(err_msg_1, expected_err_msg_1)
  228. self.assertEqual(err_msg_2, expected_err_msg_2)
  229. def test_check_https_with_valid_https(self):
  230. for https in https_keys:
  231. with self.subTest():
  232. err_msgs = check_https(0, https)
  233. self.assertIsInstance(err_msgs, list)
  234. self.assertEqual(len(err_msgs), 0)
  235. self.assertEqual(err_msgs, [])
  236. def test_check_https_with_invalid_https(self):
  237. invalid_https_keys = ['yes', 'no', 'Unknown', 'https', 'http']
  238. for https in invalid_https_keys:
  239. with self.subTest():
  240. err_msgs = check_https(0, https)
  241. self.assertIsInstance(err_msgs, list)
  242. self.assertEqual(len(err_msgs), 1)
  243. err_msg = err_msgs[0]
  244. expected_err_msg = f'(L001) {https} is not a valid HTTPS option'
  245. self.assertIsInstance(err_msg, str)
  246. self.assertEqual(err_msg, expected_err_msg)
  247. def test_check_cors_with_valid_cors(self):
  248. for cors in cors_keys:
  249. with self.subTest():
  250. err_msgs = check_cors(0, cors)
  251. self.assertIsInstance(err_msgs, list)
  252. self.assertEqual(len(err_msgs), 0)
  253. self.assertEqual(err_msgs, [])
  254. def test_check_cors_with_invalid_cors(self):
  255. invalid_cors_keys = ['yes', 'no', 'unknown', 'cors']
  256. for cors in invalid_cors_keys:
  257. with self.subTest():
  258. err_msgs = check_cors(0, cors)
  259. self.assertIsInstance(err_msgs, list)
  260. self.assertEqual(len(err_msgs), 1)
  261. err_msg = err_msgs[0]
  262. expected_err_msg = f'(L001) {cors} is not a valid CORS option'
  263. self.assertIsInstance(err_msg, str)
  264. self.assertEqual(err_msg, expected_err_msg)