Пример #1
0
    def run(self):
        parser = FeedParser(self.instance)
        defaults = parser.get_defaults()

        # update instance
        if parser.feed.get('status', False) == 304: return True
        self.instance.__dict__.update(**truncate(defaults))
        self.instance.save()

        # update entries
        for entry in parser.get_entries():
            if self._is_entry_expired(entry): continue
            e, new = self.instance.entry_set.get_or_create(**entry.get_defaults())
            e.tags.set(*entry.get_tags())
            e.save()

        # create parsing errors if necessary
        if parser.error['raised']:
            self.instance.parsingerror_set.create(error_message=parser.error['message'][:255])

        # validate and trash if necessary
        self.instance.valid = parser.is_valid()
        if not self.instance.valid and self.instance.trashed_at is None: self.instance.trashed_at = datetime.now()
        self.instance.save()
        return self.instance.valid
class FeedParserWithInvalidFeedTests(TestCase):

    def setUp(self):
        self.parser = FeedParser(InvalidFeedFactory.build())

    def teardown(self):
        pass

    def test_feed_parser_should_set_error_on_invalid_feeds(self):
        self.parser.get_defaults()
        assert_true(self.parser.error['raised'])

    def test_feed_parser_should_return_empty_entries_array(self):
        assert_equal(self.parser.get_entries(), [])

    def test_feed_parser_should_return_correct_validation_status(self):
        assert_true(not self.parser.is_valid())

    def test_feed_parser_assigns_no_defaults(self):
        defaults = self.parser.get_defaults()
        assert_true(all([val is None for key, val in defaults.iteritems()]))