Skip to content

Commit 7c838ee

Browse files
authored
tests: check JSON feed validity (#239)
Using https://pypi.org/project/jsonfeed-util/ during tests.
2 parents 71e87a0 + 09b0a31 commit 7c838ee

File tree

4 files changed

+754
-1
lines changed

4 files changed

+754
-1
lines changed

requirements/testing.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# Testing
44
# -------
55

6-
feedparser>=6.0,<6.1
6+
feedparser>=6.0.11,<6.1
7+
jsonfeed-util>=1.1.2,<2
78
mkdocs-material[imaging]>=9
89
pytest-cov>=4,<4.2
910
validator-collection>=1.5,<1.6

tests/dev/dev_json_feed_validation.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import json
2+
from pathlib import Path
3+
4+
import jsonfeed as jf
5+
import requests
6+
from jsonfeedvalidator import ErrorTree, format_errors, validate_feed
7+
8+
# read
9+
10+
# Requesting a valid JSON feed!
11+
r = requests.get("https://arxiv-feeds.appspot.com/json/test")
12+
# Parse from raw text...
13+
feed_from_text = jf.Feed.parse_string(r.text)
14+
# ...or parse JSON separately.
15+
r_json = r.json()
16+
feed_from_json = jf.Feed.parse(r_json)
17+
18+
print(feed_from_text.title)
19+
20+
# # write
21+
# me = jf.Author(name="Lukas Schwab", url="https://github.com/lukasschwab")
22+
# feed = jf.Feed("My Feed Title", authors=[me])
23+
# item = jf.Item("some_item_id")
24+
# feed.items.append(item)
25+
26+
# print(feed.toJSON())
27+
28+
# read from file
29+
path_json_feed = Path(__file__).parent.joinpath("json_feed_sample.json")
30+
31+
with path_json_feed.open("r", encoding="UTF-8") as in_json:
32+
feed_data = json.load(in_json)
33+
34+
35+
feed_from_file = jf.Feed.parse(feed_data)
36+
print(feed_from_file.title)
37+
38+
errors = validate_feed(feed_from_file)
39+
format_errors(feed_from_file, ErrorTree(errors))

0 commit comments

Comments
 (0)