Beispiel #1
0
    def __init__(self, filename=None):

        if not os.path.exists(HOME):
            initialize_home_directory()

        if not filename:
            filename = os.path.join(HOME, "settings.json")
            if not os.path.exists(filename):
                initialize_default_settings(filename)

        try:
            with open(filename) as fp:
                data = json.load(fp)
        except Exception as e:
            raise

        if 'daemon' in data:

            data = data['daemon']
            for key in data:

                if key == 'fetcher':
                    self.fetcher = imgurfetcher()
                    continue

                setattr(self, key, data[key])

            if self.backup == "yes":
                self.backup = True
            else:
                self.backup = False
    def __init__(self, filename=None):

        if not os.path.exists(HOME):
            initialize_home_directory()

        if not filename:
            filename = os.path.join(HOME, "settings.json")
            if not os.path.exists(filename):
                initialize_default_settings(filename)

        try:
            with open(filename) as fp:
                data = json.load(fp)
        except Exception as e:
            raise

        if 'daemon' in data:

            data = data['daemon']
            for key in data:

                if key == 'fetcher':
                    self.fetcher = imgurfetcher()
                    continue

                setattr(self, key, data[key])

            if self.backup == "yes":
                self.backup = True
            else:
                self.backup = False
    def test_constructor(self):
        """
        tests that the constructor works properly:

        Tests included here are:

            * Wrong typed arguments to the constructor
            * Wrong/corrupted version of the settings file
            * That the default method is "recent"
        """
        # test for wrong argument for settings file
        with patch("bg_daemon.fetchers.imgurfetcher.os.path.join") as \
                mock_method:

            mock_method.return_value = None

            with self.assertRaises(TypeError):
                dummy_fetcher = imgurfetcher.imgurfetcher(None)

        # test for corrupted json file
        with patch("bg_daemon.fetchers.imgurfetcher.json.load") as mock_method:

            # we write a json file that tries to overwrite the save method
            corrupted_json = {"fetcher": {"query": None}}
            mock_method.return_value = corrupted_json

            with self.assertRaises(ValueError):
                dummy_fetcher = imgurfetcher.imgurfetcher(self.settings_path)

        # test for a "recent" mode fallback when initializing
        with patch("bg_daemon.fetchers.imgurfetcher.json.load") as mock_method:

            corrupted_json = {"fetcher": {"mode": "nonexistent"}}
            mock_method.return_value = corrupted_json

            dummy_fetcher = imgurfetcher.imgurfetcher(self.settings_path)
            self.assertTrue(dummy_fetcher.mode is "recent")

            corrupted_json = {"fetcher": {"mode": None}}
            mock_method.return_value = corrupted_json

            dummy_fetcher = imgurfetcher.imgurfetcher(self.settings_path)
            self.assertTrue(dummy_fetcher.mode is "recent")
    def test_constructor(self):
        """
        tests that the constructor works properly:

        Tests included here are:

            * Wrong typed arguments to the constructor
            * Wrong/corrupted version of the settings file
            * That the default method is "recent"
        """
        # test for wrong argument for settings file
        with patch("bg_daemon.fetchers.imgurfetcher.os.path.join") as \
                mock_method:

            mock_method.return_value = None

            with self.assertRaises(TypeError):
                dummy_fetcher = imgurfetcher.imgurfetcher(None)

        # test for corrupted json file
        with patch("bg_daemon.fetchers.imgurfetcher.json.load") as mock_method:

            # we write a json file that tries to overwrite the save method
            corrupted_json = {"fetcher": {"query": None}}
            mock_method.return_value = corrupted_json

            with self.assertRaises(ValueError):
                dummy_fetcher = imgurfetcher.imgurfetcher(self.settings_path)

        # test for a "recent" mode fallback when initializing
        with patch("bg_daemon.fetchers.imgurfetcher.json.load") as mock_method:

            corrupted_json = {"fetcher": {"mode": "nonexistent"}}
            mock_method.return_value = corrupted_json

            dummy_fetcher = imgurfetcher.imgurfetcher(self.settings_path)
            self.assertTrue(dummy_fetcher.mode is "recent")

            corrupted_json = {"fetcher": {"mode": None}}
            mock_method.return_value = corrupted_json

            dummy_fetcher = imgurfetcher.imgurfetcher(self.settings_path)
            self.assertTrue(dummy_fetcher.mode is "recent")
    def setUp(self):

        self.settings_path = join(dirname(abspath(__file__)), "settings.json")
        self.fetcher = imgurfetcher.imgurfetcher(self.settings_path)
        self.gallery = []

        for i in range(NUMBER_OF_IMAGES):
            self.gallery.append(imgurpython.helpers.GalleryImage(
                link=self._generate_title(),
                title=self._generate_title(),
                description=self._generate_title(),
                width=random.randint(100, 10000),
                height=random.randint(100, 10000),
                nsfw = False))

        # we create a proper image that passes all tests
        self.gallery[-1].title = " ".join(self.fetcher.keywords)
        self.gallery[-1].description = " ".join(self.fetcher.keywords)

        # create some template bad/good images for testing
        self.bad_image_height = imgurpython.helpers.GalleryImage(link=None,
                title=self.fetcher.keywords[0],
                description=self.fetcher.keywords[0],
                width=10000, height=1, nsfw=False)

        self.bad_image_width = imgurpython.helpers.GalleryImage(link=None,
                title=self.fetcher.keywords[0],
                description=self.fetcher.keywords[0],
                width=1, height=10000, nsfw=False)

        self.bad_image_title = imgurpython.helpers.GalleryImage(link=None,
                title=self.fetcher.blacklist_words[0],
                description=self.fetcher.keywords[0],
                width=1000, height=10000, nsfw=False)

        self.bad_image_description = imgurpython.helpers.GalleryImage(
                link=None,
                title=self.fetcher.keywords[0],
                description=self.fetcher.blacklist_words[0],
                width=1000, height=10000, nsfw=False)

        self.good_image = self.gallery[-1]

        self.nsfw_image = imgurpython.helpers.GalleryImage(link=None,
                title=self.fetcher.keywords[0],
                description=self.fetcher.keywords[0],
                width=10000, height=10000, nsfw=True)

        # we populate a dummy album for testing
        self.album = imgurpython.helpers.GalleryAlbum()
        self.album.id = 1

        # we monkeypatch the iter content method
        self.fake_response = Mock(spec=requests.Response)
        self.fake_response.iter_content = fake_iter_content
    def setUp(self):

        settings_path = os.path.join(os.getcwd(), "tests",  "settings.json")
        self.fetcher = imgurfetcher.imgurfetcher(settings_path)
        self.gallery = []

        for i in range(NUMBER_OF_IMAGES):
            self.gallery.append(imgurpython.helpers.GalleryImage(
                link=self._generate_title(),
                title=self._generate_title(),
                width=random.randint(100, 10000),
                height=random.randint(100, 10000)))
    def setUp(self):

        self.settings_path = join(dirname(abspath(__file__)), "settings.json")
        self.fetcher = imgurfetcher.imgurfetcher(self.settings_path)
        self.gallery = []

        for i in range(NUMBER_OF_IMAGES):
            self.gallery.append(
                imgurpython.helpers.GalleryImage(
                    link=self._generate_title(),
                    title=self._generate_title(),
                    description=self._generate_title(),
                    width=random.randint(100, 10000),
                    height=random.randint(100, 10000),
                    nsfw=False))

        # we create a proper image that passes all tests
        self.gallery[-1].title = " ".join(self.fetcher.keywords)
        self.gallery[-1].description = " ".join(self.fetcher.keywords)

        # create some template bad/good images for testing
        self.bad_image_height = imgurpython.helpers.GalleryImage(
            link=None,
            title=self.fetcher.keywords[0],
            description=self.fetcher.keywords[0],
            width=10000,
            height=1,
            nsfw=False)

        self.bad_image_width = imgurpython.helpers.GalleryImage(
            link=None,
            title=self.fetcher.keywords[0],
            description=self.fetcher.keywords[0],
            width=1,
            height=10000,
            nsfw=False)

        self.bad_image_title = imgurpython.helpers.GalleryImage(
            link=None,
            title=self.fetcher.blacklist_words[0],
            description=self.fetcher.keywords[0],
            width=1000,
            height=10000,
            nsfw=False)

        self.bad_image_description = imgurpython.helpers.GalleryImage(
            link=None,
            title=self.fetcher.keywords[0],
            description=self.fetcher.blacklist_words[0],
            width=1000,
            height=10000,
            nsfw=False)

        self.good_image = self.gallery[-1]

        self.nsfw_image = imgurpython.helpers.GalleryImage(
            link=None,
            title=self.fetcher.keywords[0],
            description=self.fetcher.keywords[0],
            width=10000,
            height=10000,
            nsfw=True)

        # we populate a dummy album for testing
        self.album = imgurpython.helpers.GalleryAlbum()
        self.album.id = 1

        # we monkeypatch the iter content method
        self.fake_response = Mock(spec=requests.Response)
        self.fake_response.iter_content = fake_iter_content