-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_app.py
131 lines (108 loc) · 4.15 KB
/
test_app.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from datetime import datetime
import dateutil.parser
import pytest
from ex_back.types import OrderType
@pytest.mark.skip(reason="Functionality not yet updated")
def test_create_order(client):
# Test data
order_data = {
"type": "limit",
"side": "buy",
"instrument": "abcdefghijkl",
"limit_price": 150.00,
"quantity": 10,
}
# Make a request to the server
response = client.post("/v1/orders/", json=order_data)
# Print full response when test fails
if response.status_code != 201:
print(f"Response: {response.status_code}, {response.headers}, {response.text}")
# Assert that the response status code is 201 (Created)
assert response.status_code == 201
# Assert that the response data matches the test data
response_data = response.json()
assert response_data["order"]["type"] == order_data["type"]
assert response_data["order"]["side"] == order_data["side"]
assert response_data["order"]["instrument"] == order_data["instrument"]
assert response_data["order"]["limit_price"] == order_data["limit_price"]
assert response_data["order"]["quantity"] == order_data["quantity"]
assert "job_id" in response_data
# Parse string to datetime object
created_at = dateutil.parser.parse(response_data["order"]["created_at"])
assert isinstance(created_at, datetime)
@pytest.mark.skip(reason="Functionality not yet updated")
def test_create_order_invalid_limit_price_market_order(client):
data = {
"type": "market",
"side": "buy",
"instrument": "ABCDEF123456",
"limit_price": 100.0,
"quantity": 10,
}
response = client.post("/v1/orders", json=data)
assert response.status_code == 422
assert "Providing a `limit_price` is prohibited for type `market`" in str(
response.content
)
@pytest.mark.skip(reason="Functionality not yet updated")
def test_create_order_missing_limit_price_limit_order(client):
data = {
"type": "limit",
"side": "buy",
"instrument": "ABCDEF123456",
"quantity": 10,
}
response = client.post("/v1/orders", json=data)
assert response.status_code == 422
assert "Attribute `limit_price` is required for type `limit`" in str(
response.content
)
@pytest.mark.skip(reason="Functionality not yet updated")
def test_create_order_invalid_instrument_short(client):
data = {
"type": "market",
"side": "buy",
"instrument": "ABC", # invalid instrument
"quantity": 10,
}
response = client.post("/v1/orders", json=data)
assert response.status_code == 422
assert "ensure this value has at least 12 characters" in str(response.content)
@pytest.mark.skip(reason="Functionality not yet updated")
def test_create_order_invalid_instrument_long(client):
data = {
"type": "market",
"side": OrderType.MARKET.value,
"instrument": "ABCnthaoeurcg324shau", # invalid instrument
"quantity": 10,
}
response = client.post("/v1/orders", json=data)
assert response.status_code == 422
assert "ensure this value has at most 12 characters" in str(response.content)
@pytest.mark.skip(reason="Functionality not yet updated")
def test_create_order_invalid_quantity(client):
data = {
"type": "market",
"side": "buy",
"instrument": "ABCDEF123456",
"quantity": 0, # invalid quantity
}
response = client.post("/v1/orders", json=data)
assert response.status_code == 422
assert "ensure this value is greater than 0" in str(response.content)
@pytest.mark.skip(reason="Functionality not yet updated")
def test_list_orders(client, order_stub):
# Given
# Add an order to the database
order_response = client.post("/v1/orders/", json=order_stub)
assert order_response.status_code == 201
expected_order = order_response.json()["order"]
# When
# Fetch orders from the '/orders' endpoint
response = client.get("/v1/orders")
# Then
assert response.status_code == 200
orders = response.json()
# Check the returned orders contain the order we added
assert len(orders) > 0
assert expected_order in orders