-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_main.py
More file actions
53 lines (40 loc) · 1.35 KB
/
test_main.py
File metadata and controls
53 lines (40 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from main import *
import json
import pytest
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///"
@pytest.fixture(scope="function")
def session(request):
"""Creates a new database session for a test."""
with app.app_context():
db.create_all()
def teardown():
with app.app_context():
db.drop_all()
request.addfinalizer(teardown)
return db.session
@pytest.fixture
def client():
return app.test_client()
def test_articles(session, client):
jane = Author(firstname="Jane", lastname="Doe")
brief = Article(title="A brief history", author=jane)
with app.app_context():
session.add(brief)
session.commit()
response = client.get("/articles.json")
assert json.loads(response.data) == [
{"author": {"firstname": "Jane", "lastname": "Doe"}, "title": "A brief history"}
]
# Uncomment the following line to skip this test
@pytest.mark.skip()
def test_article_by_id(session, client):
jane = Author(firstname="Jane", lastname="Doe")
brief = Article(title="A brief history", author=jane)
with app.app_context():
session.add(brief)
session.commit()
response = client.get("/article.json?id=%i" % (brief.id))
assert json.loads(response.data) == {
"author": {"firstname": "Jane", "lastname": "Doe"},
"title": "A brief history",
}