示例#1
0
    def test___init__(self):
        """Test JSONFeedMetadata initialization."""
        meta_temp_dir = tempfile.mkdtemp(dir=_TEMP_DATA_DIR)

        # existing, update=False
        feed_name = 'sample'
        meta = JSONFeedMetadata(feed_name=feed_name, data_dir=meta_temp_dir)

        # meta object created
        self.assertIsInstance(meta, JSONFeedMetadata)

        # ---
        # non-existing, update=False

        feed_name = 'modified'
        meta = JSONFeedMetadata(feed_name=feed_name, data_dir=meta_temp_dir)

        # meta object created
        self.assertIsNotNone(meta)
        # should not be ready
        self.assertFalse(meta.is_downloaded())
        self.assertFalse(meta.is_parsed())
        self.assertFalse(meta.is_ready())

        # ---
        # non-existing, update=True

        meta = JSONFeedMetadata(
            feed_name=feed_name,
            data_dir=meta_temp_dir,
        )
        _EVENT_LOOP.run_until_complete(meta.update())

        # should be ready
        self.assertTrue(meta.is_ready())

        # file should exist
        self.assertTrue(os.path.exists(meta.path))

        # file content should not be empty
        with open(meta.path, 'r') as f:
            self.assertTrue(f.readline())
示例#2
0
    def test_update(self):
        """Test TestJSONFeedMetadata `update` method."""
        feed_name = 'modified'
        meta_temp_dir = tempfile.mkdtemp(dir=_TEMP_DATA_DIR)

        meta = JSONFeedMetadata(feed_name=feed_name, data_dir=meta_temp_dir)

        future = asyncio.ensure_future(meta.fetch())
        metadata: JSONFeedMetadata = _EVENT_LOOP.run_until_complete(future)
        data: str = metadata.data

        self.assertIsInstance(data, str)
        self.assertTrue(data)

        # should not be ready
        self.assertFalse(meta.is_ready())

        _EVENT_LOOP.run_until_complete(meta.update())

        # should be ready now
        self.assertTrue(meta.is_ready())

        # metadata present in the directory
        self.assertTrue(meta.filename in os.listdir(meta_temp_dir))