Coverage for tests/conftest.py: 100%
40 statements
« prev ^ index » next coverage.py v7.0.0, created at 2022-12-20 14:31 +0000
« prev ^ index » next coverage.py v7.0.0, created at 2022-12-20 14:31 +0000
1from datetime import datetime
3import pytest
4from beanie import PydanticObjectId
5from fastapi.testclient import TestClient
6from mock import patch
7from pydantic import BaseModel, Field
9from microservice_websocket.app.blueprints.api.test import test_router
11# @pytest.fixture()
12# def isoTimestamp() -> str:
13# """
14# Time format received from chirpstack.
15# It does follow the ISO8601 standard.
16# """
17# return "2013-03-31T16:21:17.528002Z"
18#
19#
20# @pytest.fixture()
21# def node_data() -> dict:
22# return {
23# "sensorID": 1,
24# "sensorName": "irma-sensor",
25# "applicationID": "foo",
26# "organizationID": "bar",
27# "data": {
28# "state": 3,
29# "sensorData": 4.5,
30# "mobius_sensorId": "foo",
31# "mobius_sensorPath": "bar",
32# },
33# "publishedAt": "time",
34# "payloadType": 1,
35# }
38# @pytest.fixture()
39# def testing_socketio() -> Client:
40# return socketio
43@pytest.fixture
44def app_client() -> TestClient:
45 with patch("microservice_websocket.app.config.TESTING", True):
47 from microservice_websocket.app import app
49 app.include_router(test_router)
50 with TestClient(app) as client:
51 return client
54# @pytest.fixture()
55# def socketio_client(
56# testing_app: Flask, app_client: FlaskClient, testing_socketio: SocketIO
57# ) -> SocketIOTestClient:
58# return socketio.test_client(testing_app, flask_test_client=app_client)
61@pytest.fixture
62def token(app_client: TestClient) -> str:
63 response = app_client.post(
64 "/api/jwt/",
65 json={"email": "foo@bar.com", "password": "baz"},
66 )
68 decoded_json = response.json()
70 return decoded_json["access_token"]
73@pytest.fixture()
74def auth_header(token: str):
75 return {"Authorization": f"Bearer {token}"}
78# @pytest.fixture()
79# def refresh_header(tokens):
80# return {"Authorization": f"Bearer {tokens[1]}"}
83@pytest.fixture()
84def obj_id() -> str:
85 return "63186eab0ca2d54a5c258384"
88class MockReading(BaseModel):
89 id: PydanticObjectId
90 node: PydanticObjectId
91 canID: int = Field(default=..., lt=5, gt=0)
92 sensorNumber: int = Field(default=..., lt=3, gt=0)
93 readingID: int
94 sessionID: int
95 dangerLevel: int = 0
96 window1: int = 0
97 window2: int = 0
98 window3: int = 0
99 publishedAt: datetime
102@pytest.fixture()
103def reading(obj_id: str) -> MockReading:
104 return MockReading(
105 id=PydanticObjectId(obj_id),
106 node=PydanticObjectId(obj_id),
107 canID=2,
108 sensorNumber=1,
109 readingID=1_640_200,
110 sessionID=1_640_000,
111 dangerLevel=4,
112 window1=111,
113 window2=222,
114 window3=333,
115 publishedAt=datetime.now(),
116 )