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.

334 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_with_correct_description(self):
  116. desc = 'This is a fake description'
  117. err_msgs = check_description(0, desc)
  118. self.assertIsInstance(err_msgs, list)
  119. self.assertEqual(len(err_msgs), 0)
  120. self.assertEqual(err_msgs, [])
  121. def test_check_description_with_first_char_is_not_capitalized(self):
  122. desc = 'this is a fake description'
  123. err_msgs = check_description(0, desc)
  124. self.assertIsInstance(err_msgs, list)
  125. self.assertEqual(len(err_msgs), 1)
  126. err_msg = err_msgs[0]
  127. expected_err_msg = '(L001) first character of description is not capitalized'
  128. self.assertIsInstance(err_msg, str)
  129. self.assertEqual(err_msg, expected_err_msg)
  130. def test_check_description_with_punctuation_in_the_end(self):
  131. base_desc = 'This is a fake description'
  132. punctuation = r"""!"#$%&'*+,-./:;<=>?@[\]^_`{|}~"""
  133. desc_with_punc = [base_desc + punc for punc in punctuation]
  134. for desc in desc_with_punc:
  135. with self.subTest():
  136. err_msgs = check_description(0, desc)
  137. self.assertIsInstance(err_msgs, list)
  138. self.assertEqual(len(err_msgs), 1)
  139. err_msg = err_msgs[0]
  140. expected_err_msg = f'(L001) description should not end with {desc[-1]}'
  141. self.assertIsInstance(err_msg, str)
  142. self.assertEqual(err_msg, expected_err_msg)
  143. def test_check_description_that_exceeds_the_character_limit(self):
  144. long_desc = 'Desc' * max_description_length
  145. long_desc_length = len(long_desc)
  146. err_msgs = check_description(0, long_desc)
  147. self.assertIsInstance(err_msgs, list)
  148. self.assertEqual(len(err_msgs), 1)
  149. err_msg = err_msgs[0]
  150. expected_err_msg = f'(L001) description should not exceed {max_description_length} characters (currently {long_desc_length})'
  151. self.assertIsInstance(err_msg, str)
  152. self.assertEqual(err_msg, expected_err_msg)
  153. def test_check_auth_return_type(self):
  154. auth_with_backtick = [f'`{auth}`' for auth in auth_keys if auth != 'No']
  155. auth_with_backtick.append('No')
  156. auth_without_backtick = [auth for auth in auth_keys if auth != 'No']
  157. auth_invalid = ['Yes', 'yes', 'no', 'random', 'Unknown']
  158. for auth in auth_with_backtick:
  159. with self.subTest():
  160. result = check_auth(0, auth)
  161. self.assertIsInstance(result, list)
  162. for auth in auth_without_backtick:
  163. with self.subTest():
  164. err_msgs = check_auth(0, auth)
  165. self.assertIsInstance(result, list)
  166. err_msg = err_msgs[0]
  167. self.assertIsInstance(err_msg, str)
  168. for auth in auth_invalid:
  169. with self.subTest():
  170. err_msgs = check_auth(0, auth)
  171. self.assertIsInstance(result, list)
  172. err_msg = err_msgs[0]
  173. self.assertIsInstance(err_msg, str)
  174. def test_check_auth_with_correct_auth(self):
  175. auth_valid = [f'`{auth}`' for auth in auth_keys if auth != 'No']
  176. auth_valid.append('No')
  177. for auth in auth_valid:
  178. with self.subTest():
  179. err_msgs = check_auth(0, auth)
  180. self.assertIsInstance(err_msgs, list)
  181. self.assertEqual(len(err_msgs), 0)
  182. self.assertEqual(err_msgs, [])
  183. def test_check_auth_without_backtick(self):
  184. auth_without_backtick = [auth for auth in auth_keys if auth != 'No']
  185. for auth in auth_without_backtick:
  186. with self.subTest():
  187. err_msgs = check_auth(0, auth)
  188. self.assertIsInstance(err_msgs, list)
  189. self.assertEqual(len(err_msgs), 1)
  190. err_msg = err_msgs[0]
  191. expected_err_msg = '(L001) auth value is not enclosed with `backticks`'
  192. self.assertEqual(err_msg, expected_err_msg)
  193. def test_check_auth_with_invalid_auth(self):
  194. auth_invalid = ['Yes', 'yes', 'no', 'random', 'Unknown']
  195. for auth in auth_invalid:
  196. with self.subTest():
  197. err_msgs = check_auth(0, auth)
  198. self.assertIsInstance(err_msgs, list)
  199. self.assertEqual(len(err_msgs), 2)
  200. err_msg_1 = err_msgs[0]
  201. err_msg_2 = err_msgs[1]
  202. expected_err_msg_1 = f'(L001) auth value is not enclosed with `backticks`'
  203. expected_err_msg_2 = f'(L001) {auth} is not a valid Auth option'
  204. self.assertEqual(err_msg_1, expected_err_msg_1)
  205. self.assertEqual(err_msg_2, expected_err_msg_2)
  206. def test_check_https_with_valid_https(self):
  207. for https in https_keys:
  208. with self.subTest():
  209. err_msgs = check_https(0, https)
  210. self.assertIsInstance(err_msgs, list)
  211. self.assertEqual(len(err_msgs), 0)
  212. self.assertEqual(err_msgs, [])
  213. def test_check_https_with_invalid_https(self):
  214. invalid_https_keys = ['yes', 'no', 'Unknown', 'https', 'http']
  215. for https in invalid_https_keys:
  216. with self.subTest():
  217. err_msgs = check_https(0, https)
  218. self.assertIsInstance(err_msgs, list)
  219. self.assertEqual(len(err_msgs), 1)
  220. err_msg = err_msgs[0]
  221. expected_err_msg = f'(L001) {https} is not a valid HTTPS option'
  222. self.assertIsInstance(err_msg, str)
  223. self.assertEqual(err_msg, expected_err_msg)
  224. def test_check_cors_with_valid_cors(self):
  225. for cors in cors_keys:
  226. with self.subTest():
  227. err_msgs = check_cors(0, cors)
  228. self.assertIsInstance(err_msgs, list)
  229. self.assertEqual(len(err_msgs), 0)
  230. self.assertEqual(err_msgs, [])
  231. def test_check_cors_with_invalid_cors(self):
  232. invalid_cors_keys = ['yes', 'no', 'unknown', 'cors']
  233. for cors in invalid_cors_keys:
  234. with self.subTest():
  235. err_msgs = check_cors(0, cors)
  236. self.assertIsInstance(err_msgs, list)
  237. self.assertEqual(len(err_msgs), 1)
  238. err_msg = err_msgs[0]
  239. expected_err_msg = f'(L001) {cors} is not a valid CORS option'
  240. self.assertIsInstance(err_msg, str)
  241. self.assertEqual(err_msg, expected_err_msg)