From d3bc7cb82b129424c1a4fb862c5fea2648de0a3e Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Sat, 24 Mar 2018 19:38:29 +0100 Subject: [PATCH] Add unit tests invoking all server requests --- instaloader.py | 7 ++ test/instaloader-unittests.py | 124 ++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 test/instaloader-unittests.py diff --git a/instaloader.py b/instaloader.py index 0757170..5aac9d2 100755 --- a/instaloader.py +++ b/instaloader.py @@ -268,6 +268,8 @@ class Post: return self._owner_profile.username.lower() return self._field('owner', 'username').lower() except (InstaloaderException, KeyError, TypeError) as err: + if self._instaloader.raise_all_errors: + raise err self._instaloader.error("Get owner name of {}: {} -- using \'UNKNOWN\'.".format(self, err)) return 'UNKNOWN' @@ -671,6 +673,9 @@ class Instaloader: # For the adaption of sleep intervals (rate control) self.previous_queries = dict() + # Can be set to True for testing, disables supression of Instaloader._error_catcher + self.raise_all_errors = False + @property def is_logged_in(self) -> bool: """True, if this Instaloader instance is logged in.""" @@ -717,6 +722,8 @@ class Instaloader: self.error('{}: {}'.format(extra_info, err)) else: self.error('{}'.format(err)) + if self.raise_all_errors: + raise err def _sleep(self): """Sleep a short time if self.sleep is set. Called before each request to instagram.com.""" diff --git a/test/instaloader-unittests.py b/test/instaloader-unittests.py new file mode 100644 index 0000000..9a5688c --- /dev/null +++ b/test/instaloader-unittests.py @@ -0,0 +1,124 @@ +"""Unit Tests for Instaloader""" + +import os +import shutil +import tempfile +import unittest + +import instaloader + +PUBLIC_PROFILE = "Thammus" +PUBLIC_PROFILE_ID = 1700252981 +HASHTAG = "kitten" +OWN_USERNAME = "aandergr" +NORMAL_MAX_COUNT = 2 +PAGING_MAX_COUNT = 15 +PRIVATE_PROFILE = "aandergr" + + +class TestInstaloader(unittest.TestCase): + + def setUp(self): + self.dir = tempfile.mkdtemp() + print("Testing in {}".format(self.dir)) + os.chdir(self.dir) + self.L = instaloader.Instaloader(download_geotags=instaloader.Tristate.always, + download_comments=instaloader.Tristate.always, + save_metadata=instaloader.Tristate.always) + self.L.raise_all_errors = True + + def tearDown(self): + self.L.session.close() + os.chdir('/') + print("Removing {}".format(self.dir)) + shutil.rmtree(self.dir) + + @unittest.SkipTest + def test_public_profile_download(self): + self.L.download_profile(PUBLIC_PROFILE, profile_pic=False, fast_update=True) + self.L.download_profile(PUBLIC_PROFILE, profile_pic=False, fast_update=True) + + @unittest.SkipTest + def test_stories_download(self): + if not self.L.is_logged_in: + self.L.load_session_from_file(OWN_USERNAME) + self.L.download_stories() + + @unittest.SkipTest + def test_private_profile_download(self): + if not self.L.is_logged_in: + self.L.load_session_from_file(OWN_USERNAME) + self.L.download_profile(PRIVATE_PROFILE, download_stories=True) + + def test_profile_pic_download(self): + self.L.download_profile(PUBLIC_PROFILE, profile_pic_only=True) + + def test_hashtag_download(self): + self.L.download_hashtag(HASHTAG, NORMAL_MAX_COUNT) + + def test_feed_download(self): + if not self.L.is_logged_in: + self.L.load_session_from_file(OWN_USERNAME) + self.L.download_feed_posts(NORMAL_MAX_COUNT) + + def test_feed_paging(self): + if not self.L.is_logged_in: + self.L.load_session_from_file(OWN_USERNAME) + for count, post in enumerate(self.L.get_feed_posts()): + print(post) + if count == PAGING_MAX_COUNT: + break + + def test_saved_download(self): + if not self.L.is_logged_in: + self.L.load_session_from_file(OWN_USERNAME) + self.L.download_saved_posts(NORMAL_MAX_COUNT) + + def test_saved_paging(self): + if not self.L.is_logged_in: + self.L.load_session_from_file(OWN_USERNAME) + for count, post in enumerate(instaloader.Profile(self.L, OWN_USERNAME).get_saved_posts()): + print(post) + if count == PAGING_MAX_COUNT: + break + + def test_test_login(self): + if not self.L.is_logged_in: + self.L.load_session_from_file(OWN_USERNAME) + self.assertEqual(OWN_USERNAME, self.L.test_login()) + + def test_get_followees(self): + if not self.L.is_logged_in: + self.L.load_session_from_file(OWN_USERNAME) + for f in self.L.get_followees(OWN_USERNAME): + print(f['username']) + + def test_get_followers(self): + if not self.L.is_logged_in: + self.L.load_session_from_file(OWN_USERNAME) + for f in self.L.get_followers(OWN_USERNAME): + print(f['username']) + + def test_get_username_by_id(self): + self.assertEqual(PUBLIC_PROFILE.lower(), self.L.get_username_by_id(PUBLIC_PROFILE_ID)) + + def test_get_id_by_username(self): + self.assertEqual(PUBLIC_PROFILE_ID, self.L.get_id_by_username(PUBLIC_PROFILE)) + + def test_get_likes(self): + if not self.L.is_logged_in: + self.L.load_session_from_file(OWN_USERNAME) + for post in instaloader.Profile(self.L, OWN_USERNAME).get_posts(): + for like in post.get_likes(): + print(like['username']) + break + + def test_post_from_mediaid(self): + for post in instaloader.Profile(self.L, PUBLIC_PROFILE).get_posts(): + post2 = instaloader.Post.from_mediaid(self.L, post.mediaid) + self.assertEqual(post, post2) + break + + +if __name__ == '__main__': + unittest.main()