def test_get_all_guidelines(app, db): title_ng = "Not a guideline" title_g = "Guideline" summary = "A short summary" content = "A few paragraphs of content..." with app.app_context(): db.session.add(Post(title=title_ng, summary=summary, content=content)) db.session.add( Post(title=title_g, summary=summary, content=content, is_guideline=True)) db.session.commit() with app.test_client() as client: response = client.get("/api/posts?guidelines_only=true") assert "200" in response.status data = json.loads(response.data.decode("utf-8")) assert len(data) == 1 assert data[0]["title"] == title_g
def test_timezone_utc(app, db): """ Ensure that api returns datetimes in utc. """ title = "A title" summary = "A short summary" content = "A few paragraphs of content..." with app.app_context(): post = Post(title=title, summary=summary, content=content) db.session.add(post) db.session.commit() id = post.id with app.test_client() as client: response = client.get(f"/api/posts/{id}") assert "200" in response.status post = json.loads(response.data.decode("utf-8"))[0] from datetime import datetime, timedelta utc_now = datetime.utcnow() created_at = datetime.fromisoformat( post["created_at"]).replace(tzinfo=None) assert utc_now - created_at < timedelta(milliseconds=5000)
def test_get_single_post(app, db): title = "A title" summary = "A short summary" content = "A few paragraphs of content..." with app.app_context(): post = Post(title=title, summary=summary, content=content) db.session.add(post) db.session.commit() id = post.id with app.test_client() as client: response = client.get(f"/api/posts/{id}") assert "200" in response.status posts = json.loads(response.data.decode("utf-8")) assert len(posts) == 1 post = posts[0] assert post["title"] == title assert post["summary"] == summary assert post["content"] == content
def add_test_posts(app, db): posts = [ { "title": "Every thing must have a beginning", "summary": "Text by Marry Shelley", "content": """Every thing must have a beginning, to \ speak in Sanchean phrase; and that beginning must be \ linked to something that went before. The Hindoos give \ the world an elephant to support it, but they make the \ elephant stand upon a tortoise. Invention, it must be humbly \ admitted, does not consist in creating out of void, but out \ of chaos; the materials must, in the first place, be afforded: \ it can give form to dark, shapeless substances, but cannot \ bring into being the substance itself. In all matters of \ discovery and invention, even of those that appertain to \ the imagination, we are continually reminded of the story of \ Columbus and his egg. Invention consists in the capacity of \ seizing on the capabilities of a subject, and in the power of \ moulding and fashioning ideas suggested to it.""" }, { "title": "World Turtle", "summary": "Text from Wikipedia, CC-BY-SA", "content": """The World Turtle (also referred to as \ the Cosmic Turtle or the World-bearing Turtle) is a mytheme of a \ giant turtle (or tortoise) supporting or containing the world. \ The mytheme, which is similar to that of the World Elephant and \ World Serpent, occurs in Hindu mythology, Chinese mythology and the \ mythologies of the indigenous peoples of the Americas. The "World-Tortoise" \ mytheme was discussed comparatively by Edward Burnett Tylor (1878:341).""" }, { "title": "Test 1", "summary": "Alpha is a letter.", "content": "So is beta." }, { "title": "Test 2", "summary": "Text from Wikipedia, CC-BY-SA", "content": """Alpha /ˈælfə/[1] (uppercase Α, lowercase α; \ Ancient Greek: ἄλφα, álpha, modern pronunciation álfa) is the first letter of \ the Greek alphabet. In the system of Greek numerals, it has a value of 1. \ Beta (UK: /ˈbiːtə/, US: /ˈbeɪtə/; uppercase Β, lowercase β, or cursive ϐ; \ Ancient Greek: βῆτα, romanized: bē̂ta or Greek: βήτα, romanized: víta) is \ the second letter of the Greek alphabet.""", }, { "title": "Test 3", "summary": "Test summary", "content": "Alpha beta" } ] with app.app_context(): for post in posts: post = Post(title=post["title"], summary=post["summary"], content=post["content"]) db.session.add(post) db.session.commit()
def test_delete_last_revision(app, db): title_old = "A title" title_new = "A new title" summary = "A short summary" content = "A few paragraphs of content..." with app.app_context(): old = Post(title=title_old, summary=summary, content=content, is_guideline=True) db.session.add(old) db.session.commit() id = old.post_id new = Post(title=title_new, summary=summary, content=content, is_guideline=True, post_id=id) old.is_current = False db.session.add(new) db.session.commit() revision_id = new.id with app.test_client() as client: response = client.delete(f"/api/revisions/{revision_id}") assert "204" in response.status response = client.get(f"/api/posts/{id}") assert "200" in response.status data = json.loads(response.data.decode("utf-8")) assert len(data) == 1 post = data[0] assert post["is_current"] assert post["title"] == title_old assert post["id"] == id
def test_delete_single_post_that_doesnt_exist(app, db): title = "A title" summary = "A short summary" content = "A few paragraphs of content..." with app.app_context(): post = Post(title=title, summary=summary, content=content) db.session.add(post) db.session.commit() id = post.id with app.test_client() as client: response = client.delete(f"/api/posts/{id + 1}") assert "404" in response.status with app.app_context(): assert Post.query.count() == 1
def test_get_all_posts(app, db): title = "A title" summary = "A short summary" content = "A few paragraphs of content..." count = 3 create_posts(app, db, [ Post(title=title, summary=summary, content=content) for i in range(0, count) ]) with app.test_client() as client: response = client.get("/api/posts") assert "200" in response.status data = json.loads(response.data.decode("utf-8")) assert len(data) == count for post in data: assert post["title"] == title assert post["summary"] == summary assert post["content"] == content