-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_oracle.py
More file actions
251 lines (217 loc) · 7.2 KB
/
Copy pathtest_oracle.py
File metadata and controls
251 lines (217 loc) · 7.2 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import secrets
import pytest
import pytest_asyncio
from httpx import AsyncClient
from onetl.connection import Oracle
from onetl.db import DBReader
from pyspark.sql import DataFrame
from sqlalchemy.ext.asyncio import AsyncSession
from syncmaster.db.models import Connection, Group, Queue, Status, Transfer
from tests.mocks import MockUser
from tests.test_unit.utils import create_transfer
from tests.utils import get_run_on_end
pytestmark = [pytest.mark.asyncio, pytest.mark.worker]
@pytest_asyncio.fixture
async def postgres_to_oracle(
session: AsyncSession,
group: Group,
queue: Queue,
oracle: Oracle,
oracle_connection: Connection,
postgres_connection: Connection,
):
result = await create_transfer(
session=session,
group_id=group.id,
name=f"postgres2oracle_{secrets.token_hex(5)}",
source_connection_id=postgres_connection.id,
target_connection_id=oracle_connection.id,
source_params={
"type": "postgres",
"table_name": "public.source_table",
},
target_params={
"type": "oracle",
"table_name": f"{oracle.user}.target_table",
},
queue_id=queue.id,
)
yield result
await session.delete(result)
await session.commit()
@pytest_asyncio.fixture
async def oracle_to_postgres(
session: AsyncSession,
group: Group,
queue: Queue,
oracle: Oracle,
oracle_connection: Connection,
postgres_connection: Connection,
):
result = await create_transfer(
session=session,
group_id=group.id,
name=f"oracle2postgres_{secrets.token_hex(5)}",
source_connection_id=oracle_connection.id,
target_connection_id=postgres_connection.id,
source_params={
"type": "oracle",
"table_name": f"{oracle.user}.source_table",
},
target_params={
"type": "postgres",
"table_name": "public.target_table",
},
queue_id=queue.id,
)
yield result
await session.delete(result)
await session.commit()
async def test_run_transfer_postgres_to_oracle(
client: AsyncClient,
group_owner: MockUser,
prepare_postgres,
prepare_oracle,
init_df: DataFrame,
postgres_to_oracle: Transfer,
):
# Arrange
_, fill_with_data = prepare_postgres
fill_with_data(init_df)
oracle, _ = prepare_oracle
# Act
result = await client.post(
"v1/runs",
headers={"Authorization": f"Bearer {group_owner.token}"},
json={"transfer_id": postgres_to_oracle.id},
)
# Assert
assert result.status_code == 200
run_data = await get_run_on_end(
client=client,
run_id=result.json()["id"],
token=group_owner.token,
)
assert run_data["status"] == Status.FINISHED.value
assert run_data["transfer_dump"]["source_connection"]["auth_data"]
assert run_data["transfer_dump"]["target_connection"]["auth_data"]
reader = DBReader(
connection=oracle,
table=f"{oracle.user}.target_table",
)
df = reader.run()
for field in init_df.schema:
df = df.withColumn(field.name, df[field.name].cast(field.dataType))
assert df.sort("ID").collect() == init_df.sort("ID").collect()
async def test_run_transfer_postgres_to_oracle_mixed_naming(
client: AsyncClient,
group_owner: MockUser,
prepare_postgres,
prepare_oracle,
init_df_with_mixed_column_naming: DataFrame,
postgres_to_oracle: Transfer,
):
# Arrange
_, fill_with_data = prepare_postgres
fill_with_data(init_df_with_mixed_column_naming)
oracle, _ = prepare_oracle
# Act
result = await client.post(
"v1/runs",
headers={"Authorization": f"Bearer {group_owner.token}"},
json={"transfer_id": postgres_to_oracle.id},
)
# Assert
assert result.status_code == 200
run_data = await get_run_on_end(
client=client,
run_id=result.json()["id"],
token=group_owner.token,
)
assert run_data["status"] == Status.FINISHED.value
assert run_data["transfer_dump"]["source_connection"]["auth_data"]
assert run_data["transfer_dump"]["target_connection"]["auth_data"]
reader = DBReader(
connection=oracle,
table=f"{oracle.user}.target_table",
)
df = reader.run()
assert df.columns != init_df_with_mixed_column_naming.columns
assert df.columns == [column.upper() for column in init_df_with_mixed_column_naming.columns]
for field in init_df_with_mixed_column_naming.schema:
df = df.withColumn(field.name, df[field.name].cast(field.dataType))
assert df.collect() == init_df_with_mixed_column_naming.collect()
async def test_run_transfer_oracle_to_postgres(
client: AsyncClient,
group_owner: MockUser,
prepare_oracle,
prepare_postgres,
init_df: DataFrame,
oracle_to_postgres: Transfer,
):
# Arrange
_, fill_with_data = prepare_oracle
fill_with_data(init_df)
postgres, _ = prepare_postgres
# Act
result = await client.post(
"v1/runs",
headers={"Authorization": f"Bearer {group_owner.token}"},
json={"transfer_id": oracle_to_postgres.id},
)
# Assert
assert result.status_code == 200
run_data = await get_run_on_end(
client=client,
run_id=result.json()["id"],
token=group_owner.token,
)
assert run_data["status"] == Status.FINISHED.value
assert run_data["transfer_dump"]["source_connection"]["auth_data"]
assert run_data["transfer_dump"]["target_connection"]["auth_data"]
reader = DBReader(
connection=postgres,
table="public.target_table",
)
df = reader.run()
for field in init_df.schema:
df = df.withColumn(field.name, df[field.name].cast(field.dataType))
assert df.sort("ID").collect() == init_df.sort("ID").collect()
async def test_run_transfer_oracle_to_postgres_mixed_naming(
client: AsyncClient,
group_owner: MockUser,
prepare_oracle,
prepare_postgres,
init_df_with_mixed_column_naming: DataFrame,
oracle_to_postgres: Transfer,
):
# Arrange
_, fill_with_data = prepare_oracle
fill_with_data(init_df_with_mixed_column_naming)
postgres, _ = prepare_postgres
# Act
result = await client.post(
"v1/runs",
headers={"Authorization": f"Bearer {group_owner.token}"},
json={"transfer_id": oracle_to_postgres.id},
)
# Assert
assert result.status_code == 200
run_data = await get_run_on_end(
client=client,
run_id=result.json()["id"],
token=group_owner.token,
)
assert run_data["status"] == Status.FINISHED.value
assert run_data["transfer_dump"]["source_connection"]["auth_data"]
assert run_data["transfer_dump"]["target_connection"]["auth_data"]
reader = DBReader(
connection=postgres,
table="public.target_table",
)
df = reader.run()
assert df.columns != init_df_with_mixed_column_naming.columns
assert df.columns == [column.lower() for column in init_df_with_mixed_column_naming.columns]
for field in init_df_with_mixed_column_naming.schema:
df = df.withColumn(field.name, df[field.name].cast(field.dataType))
assert df.collect() == init_df_with_mixed_column_naming.collect()