Skip to content

Commit

Permalink
finished tree tests and deleted trash
Browse files Browse the repository at this point in the history
  • Loading branch information
quimpm committed Jun 15, 2021
1 parent 737c1ff commit f322699
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 121 deletions.
1 change: 0 additions & 1 deletion .env

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,5 @@ Thumbs.db
.ionide

# End of https://www.toptal.com/developers/gitignore/api/python,visualstudiocode
.idea
.idea/
.env
Binary file modified .quota.pickle
Binary file not shown.
3 changes: 0 additions & 3 deletions youtube_discussion_tree_api/.idea/.gitignore

This file was deleted.

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions youtube_discussion_tree_api/.idea/misc.xml

This file was deleted.

8 changes: 0 additions & 8 deletions youtube_discussion_tree_api/.idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions youtube_discussion_tree_api/.idea/vcs.xml

This file was deleted.

This file was deleted.

58 changes: 17 additions & 41 deletions youtube_discussion_tree_api/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -45,61 +45,37 @@ 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():
self.contributions[curr_node.author_name].append(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 []

Expand Down
Empty file.
12 changes: 1 addition & 11 deletions youtube_discussion_tree_api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

33 changes: 16 additions & 17 deletions youtube_discussion_tree_api/tests/test_http.py
Original file line number Diff line number Diff line change
@@ -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"])
34 changes: 31 additions & 3 deletions youtube_discussion_tree_api/tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]))

0 comments on commit f322699

Please sign in to comment.