-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
179 lines (163 loc) · 5.22 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import os
import secrets
from collections import namedtuple
from datetime import date, datetime, timezone
from pathlib import Path
from unittest.mock import Mock
import etl_entities
import pytest
from etl_entities.hwm import (
ColumnDateHWM,
ColumnDateTimeHWM,
ColumnIntHWM,
FileListHWM,
KeyValueIntHWM,
)
from horizon.client.auth import LoginPassword
from horizon.commons.schemas.v1 import NamespaceCreateRequestV1
from packaging.version import Version
from horizon_hwm_store import HorizonHWMStore
PreparedDbInfo = namedtuple("PreparedDbInfo", ["full_name", "schema", "table"])
HORIZON_HOST = os.environ.get("HORIZON_HOST")
HORIZON_PORT = os.environ.get("HORIZON_PORT")
HORIZON_URL = f"http://{HORIZON_HOST}:{HORIZON_PORT}"
HORIZON_USER = os.environ.get("HORIZON_USER")
HORIZON_PASSWORD = os.environ.get("HORIZON_PASSWORD")
HORIZON_NAMESPACE = os.environ.get("HORIZON_NAMESPACE")
HWMS_WITH_VALUE = [
(
ColumnIntHWM(
name=f"{secrets.token_hex(5)}.{secrets.token_hex(5)}", # noqa: WPS204
# no source
expression=secrets.token_hex(5),
value=10,
),
15,
),
(
ColumnIntHWM(
name=f"{secrets.token_hex(5)}.{secrets.token_hex(5)}",
source=secrets.token_hex(5),
expression=secrets.token_hex(5),
value=10,
),
15,
),
(
ColumnDateHWM(
name=f"{secrets.token_hex(5)}.{secrets.token_hex(5)}",
source=secrets.token_hex(5),
expression=secrets.token_hex(5),
value=date(year=2023, month=8, day=15),
),
date(year=2023, month=9, day=15), # + 1 month
),
(
ColumnDateTimeHWM(
name=f"{secrets.token_hex(5)}.{secrets.token_hex(5)}",
source=secrets.token_hex(5),
expression=secrets.token_hex(5),
value=datetime(year=2023, month=8, day=15, hour=11, minute=22, second=33),
),
datetime(year=2023, month=8, day=15, hour=11, minute=23, second=33), # + 1 minute
),
(
FileListHWM(
name=f"{secrets.token_hex(5)}.{secrets.token_hex(5)}",
# no directory
value=["/some/file1", "/another/file2"],
),
["/some/file1", "/another/file2", "/more/file3"],
),
(
FileListHWM(
name=f"{secrets.token_hex(5)}.{secrets.token_hex(5)}",
directory="/absolute/path",
value=["/absolute/path/file1", "/absolute/path/file2"],
),
["/absolute/path/file1", "/absolute/path/file2", "/absolute/path/file3"],
),
(
KeyValueIntHWM(
name=f"{secrets.token_hex(5)}.{secrets.token_hex(5)}",
# no topic
expression="offset",
value={
0: 100,
1: 123,
},
),
{
0: 110,
1: 150,
},
),
(
KeyValueIntHWM(
name=f"{secrets.token_hex(5)}.{secrets.token_hex(5)}",
topic="topic_name",
expression="offset",
value={
0: 100,
1: 123,
},
),
{
0: 110,
1: 150,
},
),
]
if Version(etl_entities.__version__) >= Version("2.5.0"):
from etl_entities.hwm import FileModifiedTimeHWM
def file_with_mtime(mtime: datetime) -> Path:
result = Mock(spec=Path)
result.exists.return_value = True
result.is_file.return_value = True
result_stat = Mock(spec=os.stat_result)
result_stat.st_mtime = mtime.timestamp()
result.stat.return_value = result_stat
return result
HWMS_WITH_VALUE.extend(
[
(
FileModifiedTimeHWM(
name=secrets.token_hex(5),
# no directory
value=datetime(2025, 1, 1, 11, 22, 33, 456789, tzinfo=timezone.utc),
),
file_with_mtime(datetime(2025, 1, 1, 22, 33, 44, 567890, tzinfo=timezone.utc)),
),
(
FileModifiedTimeHWM(
name=secrets.token_hex(5),
directory="/absolute/path",
value=datetime(2025, 1, 1, 11, 22, 33, 456789, tzinfo=timezone.utc),
),
file_with_mtime(datetime(2025, 1, 1, 22, 33, 44, 567890, tzinfo=timezone.utc)),
),
],
)
@pytest.fixture(params=HWMS_WITH_VALUE)
def hwm_new_value(request):
return request.param
@pytest.fixture
def hwm_store():
return HorizonHWMStore(
api_url=HORIZON_URL,
auth=LoginPassword(login=HORIZON_USER, password=HORIZON_PASSWORD),
namespace=HORIZON_NAMESPACE,
)
@pytest.fixture(scope="module")
def ensure_namespace():
from requests.exceptions import HTTPError
store = HorizonHWMStore(
api_url=HORIZON_URL,
auth=LoginPassword(login=HORIZON_USER, password=HORIZON_PASSWORD),
namespace=HORIZON_NAMESPACE,
)
try:
store.client.create_namespace(NamespaceCreateRequestV1(name=HORIZON_NAMESPACE)) # noqa: WPS437
except HTTPError:
# exception: 409 Client Error: Conflict for url: http://horizon/v1/namespaces/ - namespace already exists
pass