generated from prof-rossetti/flask-sheets-template-2023
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconftest.py
51 lines (37 loc) · 1.38 KB
/
conftest.py
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
import pytest
import os
from time import sleep
from dotenv import load_dotenv
from gspread_models.service import SpreadsheetService
from gspread_models.base import BaseModel
from app.db import GOOGLE_CREDENTIALS_FILEPATH
from web_app import create_app
load_dotenv()
# an example sheet that is being used for testing purposes:
GOOGLE_SHEETS_TEST_DOCUMENT_ID= os.getenv("GOOGLE_SHEETS_TEST_DOCUMENT_ID")
TEST_SLEEP = int(os.getenv("TEST_SLEEP", default="3"))
@pytest.fixture()
def service():
"""Spreadsheet service connected to the test document. Sleeps to avoid rate limits."""
ss = SpreadsheetService(
credentials_filepath=GOOGLE_CREDENTIALS_FILEPATH,
document_id=GOOGLE_SHEETS_TEST_DOCUMENT_ID
)
assert ss.document_id == GOOGLE_SHEETS_TEST_DOCUMENT_ID
yield ss
print("SLEEPING...")
sleep(TEST_SLEEP)
@pytest.fixture()
def model_context(service):
"""Use this fixture and subsequent model calls will be made against the test database."""
BaseModel.service = service
assert BaseModel.service.document_id == GOOGLE_SHEETS_TEST_DOCUMENT_ID
yield "Using test document!"
@pytest.fixture()
def test_client(model_context):
"""Test client for the flask web application.
Uses the model context and therefore runs against the test database.
"""
app = create_app()
app.config.update({"TESTING": True})
return app.test_client()