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.

312 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. 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_with_correct_auth(self):
  154. auth_valid = [f'`{auth}`' for auth in auth_keys if auth != 'No']
  155. auth_valid.append('No')
  156. for auth in auth_valid:
  157. with self.subTest():
  158. err_msgs = check_auth(0, auth)
  159. self.assertIsInstance(err_msgs, list)
  160. self.assertEqual(len(err_msgs), 0)
  161. self.assertEqual(err_msgs, [])
  162. def test_check_auth_without_backtick(self):
  163. auth_without_backtick = [auth for auth in auth_keys if auth != 'No']
  164. for auth in auth_without_backtick:
  165. with self.subTest():
  166. err_msgs = check_auth(0, auth)
  167. self.assertIsInstance(err_msgs, list)
  168. self.assertEqual(len(err_msgs), 1)
  169. err_msg = err_msgs[0]
  170. expected_err_msg = '(L001) auth value is not enclosed with `backticks`'
  171. self.assertIsInstance(err_msg, str)
  172. self.assertEqual(err_msg, expected_err_msg)
  173. def test_check_auth_with_invalid_auth(self):
  174. auth_invalid = ['Yes', 'yes', 'no', 'random', 'Unknown']
  175. for auth in auth_invalid:
  176. with self.subTest():
  177. err_msgs = check_auth(0, auth)
  178. self.assertIsInstance(err_msgs, list)
  179. self.assertEqual(len(err_msgs), 2)
  180. err_msg_1 = err_msgs[0]
  181. err_msg_2 = err_msgs[1]
  182. expected_err_msg_1 = f'(L001) auth value is not enclosed with `backticks`'
  183. expected_err_msg_2 = f'(L001) {auth} is not a valid Auth option'
  184. self.assertIsInstance(err_msg_1, str)
  185. self.assertIsInstance(err_msg_2, str)
  186. self.assertEqual(err_msg_1, expected_err_msg_1)
  187. self.assertEqual(err_msg_2, expected_err_msg_2)
  188. def test_check_https_with_valid_https(self):
  189. for https in https_keys:
  190. with self.subTest():
  191. err_msgs = check_https(0, https)
  192. self.assertIsInstance(err_msgs, list)
  193. self.assertEqual(len(err_msgs), 0)
  194. self.assertEqual(err_msgs, [])
  195. def test_check_https_with_invalid_https(self):
  196. invalid_https_keys = ['yes', 'no', 'Unknown', 'https', 'http']
  197. for https in invalid_https_keys:
  198. with self.subTest():
  199. err_msgs = check_https(0, https)
  200. self.assertIsInstance(err_msgs, list)
  201. self.assertEqual(len(err_msgs), 1)
  202. err_msg = err_msgs[0]
  203. expected_err_msg = f'(L001) {https} is not a valid HTTPS option'
  204. self.assertIsInstance(err_msg, str)
  205. self.assertEqual(err_msg, expected_err_msg)
  206. def test_check_cors_with_valid_cors(self):
  207. for cors in cors_keys:
  208. with self.subTest():
  209. err_msgs = check_cors(0, cors)
  210. self.assertIsInstance(err_msgs, list)
  211. self.assertEqual(len(err_msgs), 0)
  212. self.assertEqual(err_msgs, [])
  213. def test_check_cors_with_invalid_cors(self):
  214. invalid_cors_keys = ['yes', 'no', 'unknown', 'cors']
  215. for cors in invalid_cors_keys:
  216. with self.subTest():
  217. err_msgs = check_cors(0, cors)
  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) {cors} is not a valid CORS option'
  222. self.assertIsInstance(err_msg, str)
  223. self.assertEqual(err_msg, expected_err_msg)