Exemple #1
0
class JsonParserTests(MainTestCase):
    def setUp(self):
        db.create_all()
        populate_db()

        self.parser = JsonParser()
        self.data = self.parser.parse_raw_data(jsondata)

    def test_parse_raw_data(self):
        data = self.parser.parse_raw_data(jsondata)

        self.assertIsNotNone(data)
        self.assertEqual(data['status']['velocity'], 50)
        self.assertEqual(len(data.get('items')), 2)

    def test_parse_raw_data_unicode(self):
        f = open(TEST_FILES_DIR + 'notification.json').read()
        data = self.parser.parse_raw_data(f)

        self.assertIsNotNone(data)
        self.assertEqual(data['status']['title'], "A Test Feed")
        self.assertEqual(len(data['items']), 2)

    def test_update_feed_details(self):
        feed = FeedFactory()

        feed = self.parser.update_feed_details(self.data, feed)

        self.assertEqual(feed.title, self.data['title'])
        self.assertEqual(feed.description, self.data['subtitle'])
        self.assertEqual(feed.feed_format, 'json')

    def test_get_items(self):
        items = self.parser.get_items(self.data)

        self.assertEqual(len(items), 2)

        data = self.data
        data['items'] = None

        self.assertIsNone(self.parser.get_items(data))
Exemple #2
0
class JsonItemParserTests(MainTestCase):
    def setUp(self):
        db.create_all()
        populate_db()

        self.json_parser = JsonParser()
        # self.data = self.json_parser.parse_raw_data(jsondata)

        self.feed = FeedFactory()
        db.session.add(self.feed)
        db.session.commit()

    def test_item_guid(self):
        item = dict(id='abcde')
        parser = JsonItemParser(item, self.feed.id)
        self.assertEqual(parser.item_guid(item), 'abcde')

    def test_item_guid_from_link_and_none_id(self):
        item = dict(id=None, permalinkUrl='abcde')
        parser = JsonItemParser(item, self.feed.id)
        self.assertEqual(parser.item_guid(item), 'abcde')

    def test_item_guid_from_none(self):
        item = dict(id=None)
        parser = JsonItemParser(item, self.feed.id)
        item_id = parser.item_guid(item)
        self.assertIsNotNone(item_id)
        self.assertTrue(valid_uuid(item_id))

    def test_item_guid_from_link(self):
        item = dict(permalinkUrl='abcde')
        parser = JsonItemParser(item, self.feed.id)
        self.assertEqual(parser.item_guid(item), 'abcde')

    def test_item_guid_from_empty_dict(self):
        item = dict()
        parser = JsonItemParser(item, self.feed.id)
        item_id = parser.item_guid(item)
        self.assertIsNotNone(item_id)
        self.assertTrue(valid_uuid(item_id))

    def test_get_entry(self):
        item = dict()
        entry = Entry(guid='abcde', feed=self.feed)

        parser = JsonItemParser(item, self.feed)

        out, update = parser.get_entry('abcde', self.feed.id)
        self.assertEqual(out, entry)
        self.assertEqual(out.feed, self.feed)
        self.assertTrue(update)

    def test_get_entry_new_entry(self):
        item = dict(id='12345')
        parser = JsonItemParser(item, self.feed)

        out, update = parser.get_entry('12345', self.feed)
        self.assertEqual(out.guid, '12345')
        self.assertEqual(out.feed, self.feed)
        self.assertFalse(update)

    def test_published(self):
        dt_unix = '1448928000'
        item = dict(published=dt_unix)
        parser = JsonItemParser(item, self.feed)

        entry = Entry()
        entry.published = parser.published(item)

        self.assertIsNotNone(entry.published)
        dt = datetime(2015, 12, 1)
        self.assertAlmostEqual(entry.published, dt, delta=timedelta(seconds=1))

    def test_published_updated(self):
        dt_unix = '1448928000'
        item = dict(updated=dt_unix)
        parser = JsonItemParser(item, self.feed)

        entry = Entry()
        entry.published = parser.published(item)

        self.assertIsNotNone(entry.published)
        dt = datetime(2015, 12, 1)
        self.assertAlmostEqual(entry.published, dt, delta=timedelta(seconds=1))

    def test_published_no_date(self):
        item = dict()
        parser = JsonItemParser(item, self.feed)

        entry = Entry()
        entry.published = parser.published(item)

        self.assertIsNotNone(entry.published)

        dt = datetime.utcnow()
        self.assertAlmostEqual(entry.published, dt, delta=timedelta(seconds=1))

    def test_updated(self):
        dt_unix = '1448928000'
        item = dict(updated=dt_unix)
        parser = JsonItemParser(item, self.feed)

        entry = Entry()
        entry.updated = parser.updated(item)

        self.assertIsNotNone(entry.updated)
        dt = datetime(2015, 12, 1)
        self.assertAlmostEqual(entry.updated, dt, delta=timedelta(seconds=1))

    def test_updated_no_date(self):
        item = dict()
        parser = JsonItemParser(item, self.feed)

        entry = Entry()
        entry.updated = parser.updated(item)

        self.assertIsNone(entry.updated)

    def test_content(self):
        content = 'Test Content'
        item = dict(content=content)
        parser = JsonItemParser(item, self.feed)

        parsed_content = parser.content(item)

        self.assertEqual(parsed_content, content)

    def test_content_returns_summary(self):
        summary = 'Test Summary'
        item = dict(summary=summary)
        parser = JsonItemParser(item, self.feed)

        parsed_content = parser.content(item)

        self.assertEqual(parsed_content, summary)

    def test_content_returns_none(self):
        item = dict()
        parser = JsonItemParser(item, self.feed)

        parsed_content = parser.content(item)

        self.assertIsNone(parsed_content)

    def test_link_from_permalink(self):
        link = 'http://test.com/entry1'
        item = dict(permalinkUrl=link)
        parser = JsonItemParser(item, self.feed)

        parsed_link = parser.link(item)

        self.assertEqual(parsed_link, link)

    def test_link_from_link(self):
        link = 'http://test.com/entry1'
        item = dict(link=link)
        parser = JsonItemParser(item, self.feed)

        parsed_link = parser.link(item)

        self.assertEqual(parsed_link, link)

    def test_link_with_permalink_and_link(self):
        link = 'http://test.com/entry1'
        permalink = 'http://test.com/permalink/entry1'
        item = dict(permalinkUrl=permalink, link=link)
        parser = JsonItemParser(item, self.feed)

        parsed_link = parser.link(item)

        self.assertEqual(parsed_link, permalink)

    def test_link_returns_none(self):
        item = dict()
        parser = JsonItemParser(item, self.feed)

        parsed_link = parser.link(item)

        self.assertIsNone(parsed_link)

    def test_author_name(self):
        name = 'Test Author'
        item = dict(actor=dict(displayName=name))
        parser = JsonItemParser(item, self.feed)

        parsed_name = parser.author_name(item)

        self.assertEqual(parsed_name, name)

    def test_author_name_no_displayname(self):
        item = dict(actor={})
        parser = JsonItemParser(item, self.feed)

        parsed_name = parser.author_name(item)
        self.assertIsNone(parsed_name)

    def test_title(self):
        title = 'Test title'
        item = dict(title=title)
        parser = JsonItemParser(item)

        parsed_title = parser.title(item)

        self.assertEqual(parsed_title, title)

    def test_title_long(self):
        title = "<p>x</p>" * 1100
        item = dict(title=title)
        result = JsonItemParser().title(item)
        self.assertEqual(len(result), 1023)
        self.assertEqual(result[-5:], 'xx...')
        self.assertEqual(result[:5], 'xxxxx')

    def test_language(self):
        language = 'EN'
        item = dict(language=language)
        parser = JsonItemParser(item, self.feed)

        parsed_language = parser.language(item)

        self.assertEqual(parsed_language, language)

    def test_parse(self):
        data = self.json_parser.parse_raw_data(jsondata)
        item = self.json_parser.get_items(data)[0]

        entry = JsonItemParser(item, self.feed).parse()[0]

        self.assertIsNotNone(entry)
        self.assertEqual(entry.title, 'Entry published on hour ago')
        self.assertEqual(entry.link, 'http://domain.tld/entry/1')
        self.assertEqual(entry.guid, 'domain.tld:09/05/03-1')
        self.assertEqual(entry.content, "Entry published on hour ago when it was shinny outside, but now it's raining")
        self.assertIsNone(entry.summary)
        self.assertEqual(entry.published, datetime(2010, 4, 21, 12, 0, 40))
        self.assertEqual(entry.updated, datetime(2010, 4, 21, 12, 0, 40))
        self.assertEqual(entry.wordcount, 14)
        self.assertEqual(entry.original_authorstring, 'First')
        self.assertEqual(entry.authors[0].name, 'First')
        self.assertEqual(entry.authorstring, 'First')
        self.assertEqual(entry.feed_id, self.feed.id)
        self.assertEqual(entry.language, 'en-gb')