Skip to content

Commit 7950ece

Browse files
api-clients-generation-pipeline[bot]zippolyteci.datadog-api-spec
authored
Add functions for flexible time in BDD tests (#484)
* add function for time in BDD * fix time addition * fix releativedelta * remove unused now * 🤦‍♂️ * Regenerate client from commit 4951cff of spec repo Co-authored-by: Hippolyte HENRY <[email protected]> Co-authored-by: api-clients-generation-pipeline[bot] <54105614+api-clients-generation-pipeline[bot]@users.noreply.github.com> Co-authored-by: ci.datadog-api-spec <[email protected]>
1 parent 38b6bf3 commit 7950ece

File tree

5 files changed

+52
-52
lines changed

5 files changed

+52
-52
lines changed

.apigentools-info

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"spec_versions": {
55
"v1": {
66
"apigentools_version": "1.4.1.dev8",
7-
"regenerated": "2021-06-17 07:14:30.122815",
8-
"spec_repo_commit": "ccc57f4"
7+
"regenerated": "2021-06-17 09:50:27.972538",
8+
"spec_repo_commit": "4951cff"
99
},
1010
"v2": {
1111
"apigentools_version": "1.4.1.dev8",
12-
"regenerated": "2021-06-17 07:14:52.391419",
13-
"spec_repo_commit": "ccc57f4"
12+
"regenerated": "2021-06-17 09:50:52.322016",
13+
"spec_repo_commit": "4951cff"
1414
}
1515
}
1616
}

src/datadog_api_client/v1/openapi.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19239,10 +19239,10 @@ paths:
1923919239
parameters:
1924019240
- name: body
1924119241
value: "{\n \"data\": {\n \"attributes\": {\n \"slo_id\": \"\
19242-
{{ slo.data[0].id }}\",\n \"start\": {{ now_ts }},\n \"end\"\
19243-
: {{ hour_later_ts }},\n \"category\": \"Other\",\n \"timezone\"\
19244-
: \"UTC\",\n \"description\": \"Test Correction\"\n },\n \
19245-
\ \"type\": \"correction\"\n }\n}"
19242+
{{ slo.data[0].id }}\",\n \"start\": {{ timestamp(\"now\") }},\n\
19243+
\ \"end\": {{ timestamp(\"now + 1h\") }},\n \"category\":\
19244+
\ \"Other\",\n \"timezone\": \"UTC\",\n \"description\": \"\
19245+
Test Correction\"\n },\n \"type\": \"correction\"\n }\n}"
1924619246
step: there is a valid "correction" for "slo"
1924719247
x-menu-order: 1
1924819248
x-undo:

tests/conftest.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ def pytest_configure(config):
5656
import pathlib
5757
import re
5858
import warnings
59-
from datetime import datetime, timedelta
59+
from datetime import datetime
6060

6161
import pytest
62+
from dateutil.relativedelta import relativedelta
6263
from jinja2 import Template
6364
from pytest_bdd import (
6465
given,
@@ -163,42 +164,45 @@ def unique_lower(request, freezer):
163164

164165

165166
@pytest.fixture
166-
def now_ts(freezer):
167+
def now(freezer):
167168
with freezer:
168-
return int(datetime.now().timestamp())
169+
return datetime.now()
170+
171+
172+
def relative_time(freezer, iso):
173+
time_re = re.compile(r"now( *([+-]) *(\d+)([smhdMy]))?")
174+
175+
def func(arg):
176+
with freezer:
177+
ret = datetime.now()
178+
m = time_re.match(arg)
179+
if m:
180+
if m.group(1):
181+
sign = m.group(2)
182+
num = int(sign + m.group(3))
183+
unit = m.group(4)
184+
if unit == "s":
185+
ret += relativedelta(seconds=num)
186+
elif unit == "m":
187+
ret += relativedelta(minutes=num)
188+
elif unit == "h":
189+
ret += relativedelta(hours=num)
190+
elif unit == "d":
191+
ret += relativedelta(days=num)
192+
elif unit == "M":
193+
ret += relativedelta(months=num)
194+
elif unit == "y":
195+
ret += relativedelta(years=num)
196+
if iso:
197+
return ret.isoformat(timespec="seconds")
198+
return int(ret.timestamp())
199+
return ""
200+
201+
return func
169202

170203

171204
@pytest.fixture
172-
def now_iso(freezer):
173-
with freezer:
174-
return datetime.now().isoformat(timespec="seconds")
175-
176-
177-
@pytest.fixture
178-
def hour_later_ts(freezer):
179-
with freezer:
180-
return int((datetime.now() + timedelta(hours=1)).timestamp())
181-
182-
183-
@pytest.fixture
184-
def hour_later_iso(freezer):
185-
with freezer:
186-
return (datetime.now() + timedelta(hours=1)).isoformat(timespec="seconds")
187-
188-
189-
@pytest.fixture
190-
def hour_ago_ts(freezer):
191-
with freezer:
192-
return int((datetime.now() + timedelta(hours=-1)).timestamp())
193-
194-
195-
@pytest.fixture
196-
def hour_ago_iso(freezer):
197-
with freezer:
198-
return (datetime.now() + timedelta(hours=-1)).isoformat(timespec="seconds")
199-
200-
@pytest.fixture
201-
def context(vcr, unique, unique_lower, now_ts, now_iso, hour_later_ts, hour_later_iso, hour_ago_ts, hour_ago_iso):
205+
def context(vcr, unique, unique_lower, freezer):
202206
"""
203207
Return a mapping with all defined fixtures, all objects created by `given` steps,
204208
and the undo operations to perform after a test scenario.
@@ -209,12 +213,8 @@ def context(vcr, unique, unique_lower, now_ts, now_iso, hour_later_ts, hour_late
209213
"unique_lower": unique_lower,
210214
"unique_alnum": re.sub(r"[^A-Za-z0-9]+", "", unique),
211215
"unique_lower_alnum": re.sub(r"[^A-Za-z0-9]+", "", unique).lower(),
212-
"now_ts": now_ts,
213-
"now_iso": now_iso,
214-
"hour_later_ts": hour_later_ts,
215-
"hour_later_iso": hour_later_iso,
216-
"hour_ago_ts": hour_ago_ts,
217-
"hour_ago_iso": hour_ago_iso,
216+
"timestamp": relative_time(freezer, False),
217+
"timeISO": relative_time(freezer, True),
218218
}
219219

220220
yield ctx

tests/v1/features/given.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"parameters": [
4040
{
4141
"name": "body",
42-
"value": "{\n \"data\": {\n \"attributes\": {\n \"slo_id\": \"{{ slo.data[0].id }}\",\n \"start\": {{ now_ts }},\n \"end\": {{ hour_later_ts }},\n \"category\": \"Other\",\n \"timezone\": \"UTC\",\n \"description\": \"Test Correction\"\n },\n \"type\": \"correction\"\n }\n}"
42+
"value": "{\n \"data\": {\n \"attributes\": {\n \"slo_id\": \"{{ slo.data[0].id }}\",\n \"start\": {{ timestamp(\"now\") }},\n \"end\": {{ timestamp(\"now + 1h\") }},\n \"category\": \"Other\",\n \"timezone\": \"UTC\",\n \"description\": \"Test Correction\"\n },\n \"type\": \"correction\"\n }\n}"
4343
}
4444
],
4545
"step": "there is a valid \"correction\" for \"slo\"",

tests/v1/features/service_level_objective_corrections.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ Feature: Service Level Objective Corrections
1616
Given there is a valid "slo" in the system
1717
And operation "CreateSLOCorrection" enabled
1818
And new "CreateSLOCorrection" request
19-
And body with value {"data": {"attributes": {"category": "Scheduled Maintenance", "description": "{{ unique }}", "end": {{ hour_later_ts }}, "start": {{ now_ts }}, "timezone": "UTC"}, "type": "correction"}}
19+
And body with value {"data": {"attributes": {"category": "Scheduled Maintenance", "description": "{{ unique }}", "end": {{ timestamp("now + 1h") }}, "start": {{ timestamp("now") }}, "timezone": "UTC"}, "type": "correction"}}
2020
When the request is sent
2121
Then the response status is 400 Bad Request
2222

2323
Scenario: Create an SLO correction returns "OK" response
2424
Given there is a valid "slo" in the system
2525
And operation "CreateSLOCorrection" enabled
2626
And new "CreateSLOCorrection" request
27-
And body with value {"data": {"attributes": {"category": "Scheduled Maintenance", "description": "{{ unique }}", "end": {{ hour_later_ts }}, "slo_id": "{{ slo.data[0].id }}", "start": {{ now_ts }}, "timezone": "UTC"}, "type": "correction"}}
27+
And body with value {"data": {"attributes": {"category": "Scheduled Maintenance", "description": "{{ unique }}", "end": {{ timestamp("now + 1h") }}, "slo_id": "{{ slo.data[0].id }}", "start": {{ timestamp("now") }}, "timezone": "UTC"}, "type": "correction"}}
2828
When the request is sent
2929
Then the response status is 200 OK
3030

3131
@skip
3232
Scenario: Create an SLO correction returns "SLO Not Found" response
3333
Given operation "CreateSLOCorrection" enabled
3434
And new "CreateSLOCorrection" request
35-
And body with value {"data": {"attributes": {"category": "Scheduled Maintenance", "description": "{{ unique }}", "end": {{ hour_later_ts }}, "slo_id": "sloId", "start": {{ now_ts }}, "timezone": "UTC"}, "type": "correction"}}
35+
And body with value {"data": {"attributes": {"category": "Scheduled Maintenance", "description": "{{ unique }}", "end": {{ timestamp("now + 1h") }}, "slo_id": "sloId", "start": {{ timestamp("now") }}, "timezone": "UTC"}, "type": "correction"}}
3636
When the request is sent
3737
Then the response status is 404 SLO Not Found
3838

@@ -103,7 +103,7 @@ Feature: Service Level Objective Corrections
103103
And operation "UpdateSLOCorrection" enabled
104104
And new "UpdateSLOCorrection" request
105105
And request contains "slo_correction_id" parameter from "correction.data.id"
106-
And body with value {"data": {"attributes": {"category": "Deployment", "description": "{{ unique }}", "end": {{ hour_later_ts }}, "start": {{ now_ts }}, "timezone": "UTC"}, "type": "correction"}}
106+
And body with value {"data": {"attributes": {"category": "Deployment", "description": "{{ unique }}", "end": {{ timestamp("now + 1h") }}, "start": {{ timestamp("now") }}, "timezone": "UTC"}, "type": "correction"}}
107107
When the request is sent
108108
Then the response status is 200 OK
109109
And the response "data.id" has the same value as "correction.data.id"

0 commit comments

Comments
 (0)