I'm writing a test to retrieve the top n posts from a community.
Before getting those values, I want to post data from a JSON file, instead of posting them to the endpoint individually.
I've tried slight variations of test_retrieve_recent_posts_existing_community, but keep getting a TypeError.
Can anyone suggest how I can fix the test case?
Route:
@app.route('/api/v2/post/retrieve/<community>/<top>', methods=['GET'])
def recent_community_posts(community=None, top=0):
community = request.args.get('community')
top = request.args.get('top')
filtered_posts = []
if community:
for post in posts:
if post['community'] == community:
filtered_posts.append(post)
if len(filtered_posts) == 0:
return {'message': 'Community does not exist ...', 'status': 402}
return {'data': jsonify(filtered_posts[:top]), 'message': "Filtered data based on community", 'status': 203}
Test:
def test_retrieve_recent_posts_existing_community(client):
with open('posts.json') as posts_data:
posts_json = json.dumps(posts_data)
client.post('/api/v2/post/create', data=json.dump(posts_json), content_type='application/json')
response = client.get('/api/v2/post/retrieve/tech/2')
data = json.load(response.get_data(as_text=True))
assert "Filtered data" in data["message"] and data["status"] == 203
Error:
TypeError: Object of type TextIOWrapper is not JSON serializable
File (posts.json):
[
{
"postid": 2,
"title": "Heading 2",
"text": "Content of post 2",
"published": "YYYY/MM/DD hh:mm:ss",
"community": "tech"
},
{
"postid": 3,
"title": "Heading 3",
"text": "Content of post 3",
"published": "YYYY/MM/DD hh:mm:ss",
"community": "tech"
},
{
"postid": 4,
"title": "Heading 4",
"text": "Content of post 4",
"published": "YYYY/MM/DD hh:mm:ss",
"community": "art"
},
{
"postid": 5,
"title": "Heading 5",
"text": "Content of post 5",
"published": "YYYY/MM/DD hh:mm:ss",
"community": "art"
},
{
"postid": 6,
"title": "Heading 6",
"text": "Content of post 6",
"published": "YYYY/MM/DD hh:mm:ss",
"community": "science"
},
{
"postid": 7,
"title": "Heading 7",
"text": "Content of post 7",
"published": "YYYY/MM/DD hh:mm:ss",
"community": "tech"
}
]
Aucun commentaire:
Enregistrer un commentaire