No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

test_validate_format.py 13 KiB

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