From f322699c39031ac349e2fb5628f37829841a4584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quim=20Pic=C3=B3=20Mora?= Date: Tue, 15 Jun 2021 17:56:54 +0200 Subject: [PATCH] finished tree tests and deleted trash --- .env | 1 - .gitignore | 3 +- .quota.pickle | Bin 135 -> 128 bytes youtube_discussion_tree_api/.idea/.gitignore | 3 - .../inspectionProfiles/Project_Default.xml | 12 ---- .../inspectionProfiles/profiles_settings.xml | 6 -- youtube_discussion_tree_api/.idea/misc.xml | 4 -- youtube_discussion_tree_api/.idea/modules.xml | 8 --- youtube_discussion_tree_api/.idea/vcs.xml | 6 -- .../.idea/youtube_discussion_tree_api.iml | 8 --- youtube_discussion_tree_api/_tree.py | 58 +++++------------- youtube_discussion_tree_api/tests/__init__.py | 0 youtube_discussion_tree_api/tests/test_api.py | 12 +--- .../tests/test_http.py | 33 +++++----- .../tests/test_tree.py | 34 +++++++++- 15 files changed, 67 insertions(+), 121 deletions(-) delete mode 100644 .env delete mode 100644 youtube_discussion_tree_api/.idea/.gitignore delete mode 100644 youtube_discussion_tree_api/.idea/inspectionProfiles/Project_Default.xml delete mode 100644 youtube_discussion_tree_api/.idea/inspectionProfiles/profiles_settings.xml delete mode 100644 youtube_discussion_tree_api/.idea/misc.xml delete mode 100644 youtube_discussion_tree_api/.idea/modules.xml delete mode 100644 youtube_discussion_tree_api/.idea/vcs.xml delete mode 100644 youtube_discussion_tree_api/.idea/youtube_discussion_tree_api.iml create mode 100644 youtube_discussion_tree_api/tests/__init__.py diff --git a/.env b/.env deleted file mode 100644 index 91df508..0000000 --- a/.env +++ /dev/null @@ -1 +0,0 @@ -export API_KEY="AIzaSyD-UjlHhqsZkhKKrDFp5PNaHyS6JHjLSUg" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 66d8c19..9014243 100644 --- a/.gitignore +++ b/.gitignore @@ -165,4 +165,5 @@ Thumbs.db .ionide # End of https://www.toptal.com/developers/gitignore/api/python,visualstudiocode -.idea \ No newline at end of file +.idea/ +.env \ No newline at end of file diff --git a/.quota.pickle b/.quota.pickle index 4e1958a54a9bebfd48e8bbea3124e3997ebf6c03..737ef34486baa33d158ada9e100e37bb376a850b 100644 GIT binary patch delta 33 ocmZo?Y+z(*V3}Gvk;PxouZJtSw5TY)ur$9Uaf - - - \ No newline at end of file diff --git a/youtube_discussion_tree_api/.idea/inspectionProfiles/profiles_settings.xml b/youtube_discussion_tree_api/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 105ce2d..0000000 --- a/youtube_discussion_tree_api/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/youtube_discussion_tree_api/.idea/misc.xml b/youtube_discussion_tree_api/.idea/misc.xml deleted file mode 100644 index d1e22ec..0000000 --- a/youtube_discussion_tree_api/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/youtube_discussion_tree_api/.idea/modules.xml b/youtube_discussion_tree_api/.idea/modules.xml deleted file mode 100644 index f41cdec..0000000 --- a/youtube_discussion_tree_api/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/youtube_discussion_tree_api/.idea/vcs.xml b/youtube_discussion_tree_api/.idea/vcs.xml deleted file mode 100644 index 6c0b863..0000000 --- a/youtube_discussion_tree_api/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/youtube_discussion_tree_api/.idea/youtube_discussion_tree_api.iml b/youtube_discussion_tree_api/.idea/youtube_discussion_tree_api.iml deleted file mode 100644 index 5f2a065..0000000 --- a/youtube_discussion_tree_api/.idea/youtube_discussion_tree_api.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/youtube_discussion_tree_api/_tree.py b/youtube_discussion_tree_api/_tree.py index 287665d..0f45545 100644 --- a/youtube_discussion_tree_api/_tree.py +++ b/youtube_discussion_tree_api/_tree.py @@ -33,7 +33,7 @@ def _create_replies_nodes(self, replies, top_level_comment_id): match = re.match('(@.{0,50} )', reply["snippet"]["textOriginal"]) if match: possible_names = self._get_possible_names(match[0].split(" ")) - name = self._find_name_in_thread(possible_names, self.contributions) + name = self._find_name_in_thread(possible_names) if not name: curr_node = self._new_node(reply, top_level_comment_id) else: @@ -45,34 +45,10 @@ def _create_replies_nodes(self, replies, top_level_comment_id): def _create_deep_replie_node(self, name, reply): if len(self.contributions[name]) == 1: - return Node( - id=reply["id"], - author_id=reply["snippet"]["authorChannelId"]["value"], - author_name=reply["snippet"]["authorDisplayName"], - text=reply["snippet"]["textOriginal"], - like_count=reply["snippet"]["likeCount"], - parent_id=self.contributions[name][0].id, - published_at = reply["snippet"]["publishedAt"] - ) + return self._new_node(reply, self.contributions[name][0].id) else: - parent_id = self.conflict_solving_algorithm(Node( - id=reply["id"], - author_id=reply["snippet"]["authorChannelId"]["value"], - author_name=reply["snippet"]["authorDisplayName"], - text=reply["snippet"]["textOriginal"], - like_count=reply["snippet"]["likeCount"], - parent_id=None, - published_at = reply["snippet"]["publishedAt"] - ), self.contributions[name]) - return Node( - id=reply["id"], - author_id=reply["snippet"]["authorChannelId"]["value"], - author_name=reply["snippet"]["authorDisplayName"], - text=reply["snippet"]["textOriginal"], - like_count=reply["snippet"]["likeCount"], - parent_id=parent_id, - published_at = reply["snippet"]["publishedAt"] - ) + parent_id = self.conflict_solving_algorithm(self._new_node(reply, None), self.contributions[name]) + return self._new_node(reply, parent_id) def _actualize_contributions(self, curr_node): if curr_node.author_name in self.contributions.keys(): @@ -80,26 +56,26 @@ def _actualize_contributions(self, curr_node): else: self.contributions[curr_node.author_name] = [curr_node] - def _new_node(self, top_level_comment, parent_id): + def _new_node(self, comment, parent_id): return Node( - id=top_level_comment["id"], - author_id=top_level_comment["snippet"]["authorChannelId"]["value"], - author_name=top_level_comment["snippet"]["authorDisplayName"], - text=top_level_comment["snippet"]["textOriginal"], - like_count=top_level_comment["snippet"]["likeCount"], + id=comment["id"], + author_id=comment["snippet"]["authorChannelId"]["value"], + author_name=comment["snippet"]["authorDisplayName"], + text=comment["snippet"]["textOriginal"], + like_count=comment["snippet"]["likeCount"], parent_id=parent_id, - published_at = top_level_comment["snippet"]["publishedAt"] + published_at = comment["snippet"]["publishedAt"] ) - def _get_possible_names(self, match_string): - if not match_string: - return [" "] + def _get_possible_names(self, tokenized_match_string): + if not tokenized_match_string: + return [] else: - return [' '.join(match_string)[1:]] + self._get_possible_names(match_string[:-1]) + return [' '.join(tokenized_match_string)[1:]] + self._get_possible_names(tokenized_match_string[:-1]) - def _find_name_in_thread(self, possible_names, contributions): + def _find_name_in_thread(self, possible_names): for name in possible_names: - if name in contributions.keys(): + if name in self.contributions.keys(): return name return [] diff --git a/youtube_discussion_tree_api/tests/__init__.py b/youtube_discussion_tree_api/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/youtube_discussion_tree_api/tests/test_api.py b/youtube_discussion_tree_api/tests/test_api.py index 93e05bd..22a3481 100644 --- a/youtube_discussion_tree_api/tests/test_api.py +++ b/youtube_discussion_tree_api/tests/test_api.py @@ -44,14 +44,4 @@ def test_search_videos(self): def test_search_video_rise_exception(self): with self.assertRaises(SearchBoundsExceded): self.api.search_videos("Functional Programming", 60) - - def test_create_quota_controller(self): - if os.path.exists(".quota.pickle"): - os.remove(".quota.pickle") - self.api._create_quota_controller() - self.assertTrue(os.path.exists(".quota.pickle")) - with open(".quota.pickle", "rb") as f: - quota_controller = pickle.load(f) - self.assertEqual(quota_controller.api_key, self.api_key) - self.assertEqual(quota_controller.curr_date, datetime.now().strftime("%Y-%m-%d")) - self.assertEqual(quota_controller.curr_quota, 0) \ No newline at end of file + \ No newline at end of file diff --git a/youtube_discussion_tree_api/tests/test_http.py b/youtube_discussion_tree_api/tests/test_http.py index b120c43..7768dc5 100644 --- a/youtube_discussion_tree_api/tests/test_http.py +++ b/youtube_discussion_tree_api/tests/test_http.py @@ -1,75 +1,74 @@ from youtube_discussion_tree_api._http import _get_list_search_videos, _get_video_comments, _get_video_transcription, _get_video_info from unittest import TestCase import os -from youtube_discussion_tree_api.utils import QuotaController -import pickle +from youtube_discussion_tree_api.utils import QuotaInfo +from youtube_discussion_tree_api._quota import QuotaManager class TestHttpMethods(TestCase): def setUp(self): self.API_KEY = os.getenv("API_KEY") - self.quota_controller = QuotaController("apikey", 0, "12-12-2012") - with open(".quota.pickle", "wb") as f: - pickle.dump(self.quota_controller, f) + self.quota_controller = QuotaInfo("apikey", 0, "12-12-2012") + self.quota_manger = QuotaManager("./youtube_discussion_tree_api/tests/.quota.pickle") def test_get_video_comments(self): - result = _get_video_comments("9GHmfg54gg8", self.API_KEY) + result = _get_video_comments("9GHmfg54gg8", self.API_KEY, self.quota_manger) self.assertIn("kind", result) self.assertEqual("youtube#commentThreadListResponse", result["kind"]) def test_get_video_comments_bad_api_key(self): - result = _get_video_comments("9GHmfg54gg8", "Google, Explode") + result = _get_video_comments("9GHmfg54gg8", "Google, Explode", self.quota_manger) self.assertIn("error", result) self.assertEqual(400, result["error"]["code"]) def test_get_video_comments_no_api_key(self): - result = _get_video_comments("9GHmfg54gg8", "") + result = _get_video_comments("9GHmfg54gg8", "", self.quota_manger) self.assertIn("error", result) self.assertEqual(403, result["error"]["code"]) def test_get_video_comments_unexisting_video(self): - result = _get_video_comments("Google, Explode", self.API_KEY) + result = _get_video_comments("Google, Explode", self.API_KEY, self.quota_manger) self.assertIn("error", result) self.assertEqual(404, result["error"]["code"]) def test_get_video_info(self): - result = _get_video_info("9GHmfg54gg8", self.API_KEY) + result = _get_video_info("9GHmfg54gg8", self.API_KEY, self.quota_manger) self.assertIn("kind", result) self.assertEqual("youtube#videoListResponse", result["kind"]) def test_get_video_info_bad_api_key(self): - result = _get_video_info("9GHmfg54gg8", "Google, Explode") + result = _get_video_info("9GHmfg54gg8", "Google, Explode", self.quota_manger) self.assertIn("error", result) self.assertEqual(400, result["error"]["code"]) def test_get_video_info_no_api_key(self): - result = _get_video_info("9GHmfg54gg8", "") + result = _get_video_info("9GHmfg54gg8", "", self.quota_manger) self.assertIn("error", result) self.assertEqual(403, result["error"]["code"]) def test_get_video_info_unexisting_video(self): - result = _get_video_info("Google, Explode", self.API_KEY) + result = _get_video_info("Google, Explode", self.API_KEY, self.quota_manger) self.assertIn("kind", result) self.assertEqual("youtube#videoListResponse", result["kind"]) self.assertEqual(0, len(result["items"])) def test_get_list_search_videos(self): - result = _get_list_search_videos("Functional Programming", 30, self.API_KEY) + result = _get_list_search_videos("Functional Programming", 30, self.API_KEY, self.quota_manger) self.assertIn("kind", result) self.assertEqual("youtube#searchListResponse", result["kind"]) self.assertEqual(30, len(result["items"])) def test_get_list_search_videos_bad_pk(self): - result = _get_list_search_videos("Functional Programming", 30, "Google, Explode") + result = _get_list_search_videos("Functional Programming", 30, "Google, Explode", self.quota_manger) self.assertIn("error", result) self.assertEqual(400, result["error"]["code"]) def test_get_list_search_videos_bad_pk(self): - result = _get_list_search_videos("Functional Programming", 30, "") + result = _get_list_search_videos("Functional Programming", 30, "", self.quota_manger) self.assertIn("error", result) self.assertEqual(403, result["error"]["code"]) def test_get_list_search_videos_bad_maxresults(self): - result = _get_list_search_videos("Functional Programming", -1, self.API_KEY) + result = _get_list_search_videos("Functional Programming", -1, self.API_KEY, self.quota_manger) self.assertIn("error", result) self.assertEqual(400, result["error"]["code"]) \ No newline at end of file diff --git a/youtube_discussion_tree_api/tests/test_tree.py b/youtube_discussion_tree_api/tests/test_tree.py index 8950742..a99d844 100644 --- a/youtube_discussion_tree_api/tests/test_tree.py +++ b/youtube_discussion_tree_api/tests/test_tree.py @@ -188,9 +188,37 @@ def test_actualize_contributions_various_contributions(self): )) self.assertEqual(2, len(self.tree.contributions["Quim Picó Mora"])) self.assertEqual("comment1", self.tree.contributions["Quim Picó Mora"][1].id) - - - + def test_new_node(self): + jsonComment = { + "id" : "comment2", + "snippet" : { + "authorChannelId" : { + "value" : "author2" + }, + "authorDisplayName" : "Miquel Farré", + "textOriginal" : "I like Turtles, i found them really interesting and cute", + "likeCount" : 100000, + "publishedAt" : "12-12-2020" + } + } + node = self.tree._new_node(jsonComment, "root") + self.assertEqual(jsonComment["id"],node.id) + self.assertEqual(jsonComment["snippet"]["authorDisplayName"], node.author_name) + self.assertEqual(jsonComment["snippet"]["authorChannelId"]["value"], node.author_id) + self.assertEqual(jsonComment["snippet"]["textOriginal"], node.text) + self.assertEqual(jsonComment["snippet"]["likeCount"], node.like_count) + self.assertEqual(jsonComment["snippet"]["publishedAt"], node.published_at) + self.assertEqual("root", node.parent_id) + def test_get_possible_names(self): + self.assertEqual(list(reversed(["Quim", "Quim Picó", "Quim Picó Mora"])), self.tree._get_possible_names("@Quim Picó Mora".split())) + self.assertEqual(list(reversed(["Quim", "Quim Picó", "Quim Picó Mora", "Quim Picó Mora aaaaaa", "Quim Picó Mora aaaaaa aaaaaa"])), self.tree._get_possible_names("@Quim Picó Mora aaaaaa aaaaaa".split())) + self.assertEqual(["Quim"], self.tree._get_possible_names("@Quim".split())) + + def test_find_name_in_thread(self): + self.tree.contributions = { + "Quim Picó Mora" : "Tha one and only" + } + self.assertEqual("Quim Picó Mora", self.tree._find_name_in_thread(["Quim", "Quim Picó", "Quim Picó Mora"])) \ No newline at end of file