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.

test_validate_format.py 18 KiB

6 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. from validate.format import check_file_format, min_entries_per_category, num_segments
  13. class TestValidadeFormat(unittest.TestCase):
  14. def test_error_message_return_and_return_type(self):
  15. line_num_unity = 1
  16. line_num_ten = 10
  17. line_num_hundred = 100
  18. line_num_thousand = 1000
  19. msg = 'This is a unit test'
  20. msg2 ="this is unew message"
  21. err_msg_unity = error_message(line_num_unity, msg)
  22. err_msg_ten = error_message(line_num_ten, msg)
  23. err_msg_hundred = error_message(line_num_hundred, msg)
  24. err_msg_thousand = error_message(line_num_thousand, msg)
  25. self.assertIsInstance(err_msg_unity, str)
  26. self.assertIsInstance(err_msg_ten, str)
  27. self.assertIsInstance(err_msg_hundred, str)
  28. self.assertIsInstance(err_msg_thousand, str)
  29. self.assertEqual(err_msg_unity, '(L002) This is a unit test')
  30. self.assertEqual(err_msg_ten, '(L011) This is a unit test')
  31. self.assertEqual(err_msg_hundred, '(L101) This is a unit test')
  32. self.assertEqual(err_msg_thousand, '(L1001) This is a unit test')
  33. def test_if_get_categories_content_return_correct_data_of_categories(self):
  34. fake_contents = [
  35. '### A',
  36. 'API | Description | Auth | HTTPS | CORS |',
  37. '|---|---|---|---|---|',
  38. '| [AA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  39. '| [AB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  40. '',
  41. '### B',
  42. 'API | Description | Auth | HTTPS | CORS |',
  43. '|---|---|---|---|---|',
  44. '| [BA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  45. '| [BB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |'
  46. ]
  47. result = get_categories_content(fake_contents)
  48. self.assertIsInstance(result, tuple)
  49. categories, category_line_num = result
  50. self.assertIsInstance(categories, dict)
  51. self.assertIsInstance(category_line_num, dict)
  52. expected_result = ({'A': ['AA', 'AB'], 'B': ['BA', 'BB']}, {'A': 0, 'B': 6})
  53. for res, ex_res in zip(result, expected_result):
  54. with self.subTest():
  55. self.assertEqual(res, ex_res)
  56. def test_if_check_alphabetical_order_return_correct_msg_error(self):
  57. correct_lines = [
  58. '### A',
  59. 'API | Description | Auth | HTTPS | CORS |',
  60. '|---|---|---|---|---|',
  61. '| [AA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  62. '| [AB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  63. '',
  64. '### B',
  65. 'API | Description | Auth | HTTPS | CORS |',
  66. '|---|---|---|---|---|',
  67. '| [BA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  68. '| [BB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |'
  69. ]
  70. incorrect_lines = [
  71. '### A',
  72. 'API | Description | Auth | HTTPS | CORS |',
  73. '|---|---|---|---|---|',
  74. '| [AB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  75. '| [AA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  76. '',
  77. '### B',
  78. 'API | Description | Auth | HTTPS | CORS |',
  79. '|---|---|---|---|---|',
  80. '| [BB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  81. '| [BA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |'
  82. ]
  83. err_msgs_1 = check_alphabetical_order(correct_lines)
  84. err_msgs_2 = check_alphabetical_order(incorrect_lines)
  85. self.assertIsInstance(err_msgs_1, list)
  86. self.assertIsInstance(err_msgs_2, list)
  87. self.assertEqual(len(err_msgs_1), 0)
  88. self.assertEqual(len(err_msgs_2), 2)
  89. expected_err_msgs = [
  90. '(L001) A category is not alphabetical order',
  91. '(L007) B category is not alphabetical order'
  92. ]
  93. for err_msg, ex_err_msg in zip(err_msgs_2, expected_err_msgs):
  94. with self.subTest():
  95. self.assertEqual(err_msg, ex_err_msg)
  96. def test_check_title_with_correct_title(self):
  97. raw_title = '[A](https://www.ex.com)'
  98. err_msgs = check_title(0, raw_title)
  99. self.assertIsInstance(err_msgs, list)
  100. self.assertEqual(len(err_msgs), 0)
  101. self.assertEqual(err_msgs, [])
  102. def test_check_title_with_markdown_syntax_incorrect(self):
  103. raw_title = '[A(https://www.ex.com)'
  104. err_msgs = check_title(0, raw_title)
  105. self.assertIsInstance(err_msgs, list)
  106. self.assertEqual(len(err_msgs), 1)
  107. err_msg = err_msgs[0]
  108. expected_err_msg = '(L001) Title syntax should be "[TITLE](LINK)"'
  109. self.assertEqual(err_msg, expected_err_msg)
  110. def test_check_title_with_api_at_the_end_of_the_title(self):
  111. raw_title = '[A API](https://www.ex.com)'
  112. err_msgs = check_title(0, raw_title)
  113. self.assertIsInstance(err_msgs, list)
  114. self.assertEqual(len(err_msgs), 1)
  115. err_msg = err_msgs[0]
  116. expected_err_msg = '(L001) Title should not end with "... API". Every entry is an API here!'
  117. self.assertEqual(err_msg, expected_err_msg)
  118. def test_check_description_with_correct_description(self):
  119. desc = 'This is a fake description'
  120. err_msgs = check_description(0, desc)
  121. self.assertIsInstance(err_msgs, list)
  122. self.assertEqual(len(err_msgs), 0)
  123. self.assertEqual(err_msgs, [])
  124. def test_check_description_with_first_char_is_not_capitalized(self):
  125. desc = 'this is a fake description'
  126. err_msgs = check_description(0, desc)
  127. self.assertIsInstance(err_msgs, list)
  128. self.assertEqual(len(err_msgs), 1)
  129. err_msg = err_msgs[0]
  130. expected_err_msg = '(L001) first character of description is not capitalized'
  131. self.assertIsInstance(err_msg, str)
  132. self.assertEqual(err_msg, expected_err_msg)
  133. def test_check_description_with_punctuation_in_the_end(self):
  134. base_desc = 'This is a fake description'
  135. punctuation = r"""!"#$%&'*+,-./:;<=>?@[\]^_`{|}~"""
  136. desc_with_punc = [base_desc + punc for punc in punctuation]
  137. for desc in desc_with_punc:
  138. with self.subTest():
  139. err_msgs = check_description(0, desc)
  140. self.assertIsInstance(err_msgs, list)
  141. self.assertEqual(len(err_msgs), 1)
  142. err_msg = err_msgs[0]
  143. expected_err_msg = f'(L001) description should not end with {desc[-1]}'
  144. self.assertIsInstance(err_msg, str)
  145. self.assertEqual(err_msg, expected_err_msg)
  146. def test_check_description_that_exceeds_the_character_limit(self):
  147. long_desc = 'Desc' * max_description_length
  148. long_desc_length = len(long_desc)
  149. err_msgs = check_description(0, long_desc)
  150. self.assertIsInstance(err_msgs, list)
  151. self.assertEqual(len(err_msgs), 1)
  152. err_msg = err_msgs[0]
  153. expected_err_msg = f'(L001) description should not exceed {max_description_length} characters (currently {long_desc_length})'
  154. self.assertIsInstance(err_msg, str)
  155. self.assertEqual(err_msg, expected_err_msg)
  156. def test_check_auth_with_valid_auth(self):
  157. auth_valid = [f'`{auth}`' for auth in auth_keys if auth != 'No']
  158. auth_valid.append('No')
  159. for auth in auth_valid:
  160. with self.subTest():
  161. err_msgs = check_auth(0, auth)
  162. self.assertIsInstance(err_msgs, list)
  163. self.assertEqual(len(err_msgs), 0)
  164. self.assertEqual(err_msgs, [])
  165. def test_check_auth_without_backtick(self):
  166. auth_without_backtick = [auth for auth in auth_keys if auth != 'No']
  167. for auth in auth_without_backtick:
  168. with self.subTest():
  169. err_msgs = check_auth(0, auth)
  170. self.assertIsInstance(err_msgs, list)
  171. self.assertEqual(len(err_msgs), 1)
  172. err_msg = err_msgs[0]
  173. expected_err_msg = '(L001) auth value is not enclosed with `backticks`'
  174. self.assertIsInstance(err_msg, str)
  175. self.assertEqual(err_msg, expected_err_msg)
  176. def test_check_auth_with_invalid_auth(self):
  177. auth_invalid_without_backtick = ['Yes', 'yes', 'no', 'random', 'Unknown']
  178. auth_invalid_with_backtick = ['`Yes`', '`yes`', '`no`', '`random`', '`Unknown`']
  179. for auth in auth_invalid_without_backtick:
  180. with self.subTest():
  181. err_msgs = check_auth(0, auth)
  182. self.assertIsInstance(err_msgs, list)
  183. self.assertEqual(len(err_msgs), 2)
  184. err_msg_1 = err_msgs[0]
  185. err_msg_2 = err_msgs[1]
  186. expected_err_msg_1 = f'(L001) auth value is not enclosed with `backticks`'
  187. expected_err_msg_2 = f'(L001) {auth} is not a valid Auth option'
  188. self.assertIsInstance(err_msg_1, str)
  189. self.assertIsInstance(err_msg_2, str)
  190. self.assertEqual(err_msg_1, expected_err_msg_1)
  191. self.assertEqual(err_msg_2, expected_err_msg_2)
  192. for auth in auth_invalid_with_backtick:
  193. with self.subTest():
  194. err_msgs = check_auth(0, auth)
  195. self.assertIsInstance(err_msgs, list)
  196. self.assertEqual(len(err_msgs), 1)
  197. err_msg = err_msgs[0]
  198. expected_err_msg = f'(L001) {auth} is not a valid Auth option'
  199. self.assertIsInstance(err_msg, str)
  200. self.assertEqual(err_msg, expected_err_msg)
  201. def test_check_https_with_valid_https(self):
  202. for https in https_keys:
  203. with self.subTest():
  204. err_msgs = check_https(0, https)
  205. self.assertIsInstance(err_msgs, list)
  206. self.assertEqual(len(err_msgs), 0)
  207. self.assertEqual(err_msgs, [])
  208. def test_check_https_with_invalid_https(self):
  209. invalid_https_keys = ['yes', 'no', 'Unknown', 'https', 'http']
  210. for https in invalid_https_keys:
  211. with self.subTest():
  212. err_msgs = check_https(0, https)
  213. self.assertIsInstance(err_msgs, list)
  214. self.assertEqual(len(err_msgs), 1)
  215. err_msg = err_msgs[0]
  216. expected_err_msg = f'(L001) {https} is not a valid HTTPS option'
  217. self.assertIsInstance(err_msg, str)
  218. self.assertEqual(err_msg, expected_err_msg)
  219. def test_check_cors_with_valid_cors(self):
  220. for cors in cors_keys:
  221. with self.subTest():
  222. err_msgs = check_cors(0, cors)
  223. self.assertIsInstance(err_msgs, list)
  224. self.assertEqual(len(err_msgs), 0)
  225. self.assertEqual(err_msgs, [])
  226. def test_check_cors_with_invalid_cors(self):
  227. invalid_cors_keys = ['yes', 'no', 'unknown', 'cors']
  228. for cors in invalid_cors_keys:
  229. with self.subTest():
  230. err_msgs = check_cors(0, cors)
  231. self.assertIsInstance(err_msgs, list)
  232. self.assertEqual(len(err_msgs), 1)
  233. err_msg = err_msgs[0]
  234. expected_err_msg = f'(L001) {cors} is not a valid CORS option'
  235. self.assertIsInstance(err_msg, str)
  236. self.assertEqual(err_msg, expected_err_msg)
  237. def test_check_entry_with_correct_segments(self):
  238. correct_segments = ['[A](https://www.ex.com)', 'Desc', '`apiKey`', 'Yes', 'Yes']
  239. err_msgs = check_entry(0, correct_segments)
  240. self.assertIsInstance(err_msgs, list)
  241. self.assertEqual(len(err_msgs), 0)
  242. self.assertEqual(err_msgs, [])
  243. def test_check_entry_with_incorrect_segments(self):
  244. incorrect_segments = ['[A API](https://www.ex.com)', 'desc.', 'yes', 'yes', 'yes']
  245. err_msgs = check_entry(0, incorrect_segments)
  246. expected_err_msgs = [
  247. '(L001) Title should not end with "... API". Every entry is an API here!',
  248. '(L001) first character of description is not capitalized',
  249. '(L001) description should not end with .',
  250. '(L001) auth value is not enclosed with `backticks`',
  251. '(L001) yes is not a valid Auth option',
  252. '(L001) yes is not a valid HTTPS option',
  253. '(L001) yes is not a valid CORS option'
  254. ]
  255. self.assertIsInstance(err_msgs, list)
  256. self.assertEqual(len(err_msgs), 7)
  257. for err_msg in err_msgs:
  258. with self.subTest():
  259. self.assertIsInstance(err_msg, str)
  260. self.assertEqual(err_msgs, expected_err_msgs)
  261. def test_check_file_format_with_correct_format(self):
  262. correct_format = [
  263. '## Index',
  264. '* [A](#a)',
  265. '* [B](#b)',
  266. '',
  267. '### A',
  268. 'API | Description | Auth | HTTPS | CORS |',
  269. '|---|---|---|---|---|',
  270. '| [AA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  271. '| [AB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  272. '| [AC](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  273. '',
  274. '### B',
  275. 'API | Description | Auth | HTTPS | CORS |',
  276. '|---|---|---|---|---|',
  277. '| [BA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  278. '| [BB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  279. '| [BC](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |'
  280. ]
  281. err_msgs = check_file_format(lines=correct_format)
  282. self.assertIsInstance(err_msgs, list)
  283. self.assertEqual(len(err_msgs), 0)
  284. self.assertEqual(err_msgs, [])
  285. def test_check_file_format_with_category_header_not_added_to_index(self):
  286. incorrect_format = [
  287. '## Index',
  288. '',
  289. '### A',
  290. 'API | Description | Auth | HTTPS | CORS |',
  291. '|---|---|---|---|---|',
  292. '| [AA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  293. '| [AB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  294. '| [AC](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  295. ]
  296. err_msgs = check_file_format(lines=incorrect_format)
  297. expected_err_msg = '(L003) category header (A) not added to Index section'
  298. self.assertIsInstance(err_msgs, list)
  299. self.assertEqual(len(err_msgs), 1)
  300. err_msg = err_msgs[0]
  301. self.assertEqual(err_msg, expected_err_msg)
  302. def test_check_file_format_with_category_without_min_entries(self):
  303. incorrect_format = [
  304. '## Index',
  305. '* [A](#a)',
  306. '* [B](#b)',
  307. '',
  308. '### A',
  309. 'API | Description | Auth | HTTPS | CORS |',
  310. '|---|---|---|---|---|',
  311. '| [AA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  312. '',
  313. '### B',
  314. 'API | Description | Auth | HTTPS | CORS |',
  315. '|---|---|---|---|---|',
  316. '| [BA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  317. '| [BB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  318. '| [BC](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |'
  319. ]
  320. category_with_err = 'A'
  321. num_in_category = 1
  322. err_msgs = check_file_format(lines=incorrect_format)
  323. expected_err_msg = f'(L005) {category_with_err} category does not have the minimum {min_entries_per_category} entries (only has {num_in_category})'
  324. self.assertIsInstance(err_msgs, list)
  325. self.assertEqual(len(err_msgs), 1)
  326. err_msg = err_msgs[0]
  327. self.assertEqual(err_msg, expected_err_msg)
  328. def test_check_file_format_entry_without_all_necessary_columns(self):
  329. incorrect_format = [
  330. '## Index',
  331. '* [A](#a)',
  332. '',
  333. '### A',
  334. 'API | Description | Auth | HTTPS | CORS |',
  335. '|---|---|---|---|---|',
  336. '| [AA](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  337. '| [AB](https://www.ex.com) | Desc | `apiKey` |', # missing https and cors
  338. '| [AC](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  339. ]
  340. current_segments_num = 3
  341. err_msgs = check_file_format(lines=incorrect_format)
  342. expected_err_msg = f'(L008) entry does not have all the required columns (have {current_segments_num}, need {num_segments})'
  343. self.assertIsInstance(err_msgs, list)
  344. self.assertEqual(len(err_msgs), 1)
  345. err_msg = err_msgs[0]
  346. self.assertEqual(err_msg, expected_err_msg)
  347. def test_check_file_format_without_1_space_between_the_segments(self):
  348. incorrect_format = [
  349. '## Index',
  350. '* [A](#a)',
  351. '',
  352. '### A',
  353. 'API | Description | Auth | HTTPS | CORS |',
  354. '|---|---|---|---|---|',
  355. '| [AA](https://www.ex.com) | Desc |`apiKey`| Yes | Yes |', # space between segment of auth column missing
  356. '| [AB](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  357. '| [AC](https://www.ex.com) | Desc | `apiKey` | Yes | Yes |',
  358. ]
  359. err_msgs = check_file_format(lines=incorrect_format)
  360. expected_err_msg = f'(L007) each segment must start and end with exactly 1 space'
  361. self.assertIsInstance(err_msgs, list)
  362. self.assertEqual(len(err_msgs), 1)
  363. err_msg = err_msgs[0]
  364. self.assertEqual(err_msg, expected_err_msg)