Skip to content

Commit 8c1efeb

Browse files
authored
Introduced more test fixtures (#26)
1 parent 0dbce33 commit 8c1efeb

File tree

5 files changed

+183
-82
lines changed

5 files changed

+183
-82
lines changed

arangoasync/connection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ async def process_request(self, request: Request) -> Response:
172172
ConnectionAbortedError: If it can't connect to host(s) within limit.
173173
"""
174174

175+
request.endpoint = f"{self._db_endpoint}{request.endpoint}"
175176
host_index = self._host_resolver.get_host_index()
176177
for tries in range(self._host_resolver.max_tries):
177178
try:

tests/conftest.py

Lines changed: 114 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import asyncio
12
from dataclasses import dataclass
23

34
import pytest
45
import pytest_asyncio
56

6-
from arangoasync.auth import JwtToken
7+
from arangoasync.auth import Auth, JwtToken
8+
from arangoasync.client import ArangoClient
9+
from arangoasync.typings import UserInfo
10+
from tests.helpers import generate_col_name, generate_db_name, generate_username
711

812

913
@dataclass
@@ -14,6 +18,7 @@ class GlobalData:
1418
secret: str = None
1519
token: str = None
1620
sys_db_name: str = "_system"
21+
username: str = generate_username()
1722

1823

1924
global_data = GlobalData()
@@ -49,31 +54,48 @@ def pytest_configure(config):
4954
global_data.token = JwtToken.generate_token(global_data.secret)
5055

5156

52-
@pytest.fixture(autouse=False)
57+
@pytest.fixture
5358
def url():
5459
return global_data.url
5560

5661

57-
@pytest.fixture(autouse=False)
62+
@pytest.fixture
5863
def root():
5964
return global_data.root
6065

6166

62-
@pytest.fixture(autouse=False)
67+
@pytest.fixture
6368
def password():
6469
return global_data.password
6570

6671

67-
@pytest.fixture(autouse=False)
72+
@pytest.fixture
73+
def basic_auth_root(root, password):
74+
return Auth(username=root, password=password)
75+
76+
77+
@pytest.fixture
78+
def username():
79+
return global_data.username
80+
81+
82+
@pytest.fixture
6883
def token():
6984
return global_data.token
7085

7186

72-
@pytest.fixture(autouse=False)
87+
@pytest.fixture
7388
def sys_db_name():
7489
return global_data.sys_db_name
7590

7691

92+
@pytest.fixture(scope="session")
93+
def event_loop():
94+
loop = asyncio.new_event_loop()
95+
yield loop
96+
loop.close()
97+
98+
7799
@pytest_asyncio.fixture
78100
async def client_session():
79101
"""Make sure we close all sessions after the test is done."""
@@ -88,3 +110,89 @@ def get_client_session(client, url):
88110

89111
for session in sessions:
90112
await session.close()
113+
114+
115+
@pytest_asyncio.fixture
116+
async def arango_client(url):
117+
async with ArangoClient(hosts=url) as client:
118+
yield client
119+
120+
121+
@pytest_asyncio.fixture
122+
async def sys_db(arango_client, sys_db_name, basic_auth_root):
123+
return await arango_client.db(
124+
sys_db_name, auth_method="basic", auth=basic_auth_root, verify=False
125+
)
126+
127+
128+
@pytest_asyncio.fixture
129+
async def test_db(arango_client, sys_db, password):
130+
tst_db_name = generate_db_name()
131+
tst_user = UserInfo(
132+
user=generate_username(),
133+
password=password,
134+
active=True,
135+
)
136+
await sys_db.create_database(tst_db_name, users=[tst_user])
137+
yield await arango_client.db(
138+
tst_db_name,
139+
auth_method="basic",
140+
auth=Auth(username=tst_user.user, password=password),
141+
verify=False,
142+
)
143+
await sys_db.delete_database(tst_db_name)
144+
145+
146+
@pytest_asyncio.fixture
147+
async def bad_db(arango_client):
148+
return await arango_client.db(
149+
generate_db_name(),
150+
auth_method="basic",
151+
auth=Auth(username="bad_user", password="bad_password"),
152+
verify=False,
153+
)
154+
155+
156+
@pytest_asyncio.fixture
157+
async def test_doc_col(test_db):
158+
col_name = generate_col_name()
159+
yield await test_db.create_collection(col_name)
160+
await test_db.delete_collection(col_name)
161+
162+
163+
@pytest_asyncio.fixture(scope="session", autouse=True)
164+
async def teardown():
165+
yield
166+
async with ArangoClient(hosts=global_data.url) as client:
167+
sys_db = await client.db(
168+
global_data.sys_db_name,
169+
auth_method="basic",
170+
auth=Auth(username=global_data.root, password=global_data.password),
171+
verify=False,
172+
)
173+
174+
# Remove all test users.
175+
tst_users = [
176+
user["user"]
177+
for user in await sys_db.users()
178+
if user["user"].startswith("test_user")
179+
]
180+
await asyncio.gather(*(sys_db.delete_user(user) for user in tst_users))
181+
182+
# Remove all test databases.
183+
tst_dbs = [
184+
db_name
185+
for db_name in await sys_db.databases()
186+
if db_name.startswith("test_database")
187+
]
188+
await asyncio.gather(*(sys_db.delete_database(db_name) for db_name in tst_dbs))
189+
190+
# Remove all test collections.
191+
tst_cols = [
192+
col_info.name
193+
for col_info in await sys_db.collections()
194+
if col_info.name.startswith("test_collection")
195+
]
196+
await asyncio.gather(
197+
*(sys_db.delete_collection(col_name) for col_name in tst_cols)
198+
)

tests/test_client.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from arangoasync.auth import Auth, JwtToken
3+
from arangoasync.auth import JwtToken
44
from arangoasync.client import ArangoClient
55
from arangoasync.compression import DefaultCompressionManager
66
from arangoasync.http import DefaultHTTPClient
@@ -62,12 +62,15 @@ async def test_client_bad_auth_method(url, sys_db_name):
6262

6363

6464
@pytest.mark.asyncio
65-
async def test_client_basic_auth(url, sys_db_name, root, password):
66-
auth = Auth(username=root, password=password)
67-
65+
async def test_client_basic_auth(url, sys_db_name, basic_auth_root):
6866
# successful authentication
6967
async with ArangoClient(hosts=url) as client:
70-
await client.db(sys_db_name, auth_method="basic", auth=auth, verify=True)
68+
await client.db(
69+
sys_db_name,
70+
auth_method="basic",
71+
auth=basic_auth_root,
72+
verify=True,
73+
)
7174

7275
# auth missing
7376
async with ArangoClient(hosts=url) as client:
@@ -82,13 +85,17 @@ async def test_client_basic_auth(url, sys_db_name, root, password):
8285

8386

8487
@pytest.mark.asyncio
85-
async def test_client_jwt_auth(url, sys_db_name, root, password):
86-
auth = Auth(username=root, password=password)
88+
async def test_client_jwt_auth(url, sys_db_name, basic_auth_root):
8789
token: JwtToken
8890

8991
# successful authentication with auth only
9092
async with ArangoClient(hosts=url) as client:
91-
db = await client.db(sys_db_name, auth_method="jwt", auth=auth, verify=True)
93+
db = await client.db(
94+
sys_db_name,
95+
auth_method="jwt",
96+
auth=basic_auth_root,
97+
verify=True,
98+
)
9299
token = db.connection.token
93100

94101
# successful authentication with token only
@@ -98,7 +105,11 @@ async def test_client_jwt_auth(url, sys_db_name, root, password):
98105
# successful authentication with both
99106
async with ArangoClient(hosts=url) as client:
100107
await client.db(
101-
sys_db_name, auth_method="jwt", auth=auth, token=token, verify=True
108+
sys_db_name,
109+
auth_method="jwt",
110+
auth=basic_auth_root,
111+
token=token,
112+
verify=True,
102113
)
103114

104115
# auth and token missing
@@ -108,9 +119,7 @@ async def test_client_jwt_auth(url, sys_db_name, root, password):
108119

109120

110121
@pytest.mark.asyncio
111-
async def test_client_jwt_superuser_auth(url, sys_db_name, root, password, token):
112-
auth = Auth(username=root, password=password)
113-
122+
async def test_client_jwt_superuser_auth(url, sys_db_name, basic_auth_root, token):
114123
# successful authentication
115124
async with ArangoClient(hosts=url) as client:
116125
await client.db(sys_db_name, auth_method="superuser", token=token, verify=True)
@@ -119,5 +128,5 @@ async def test_client_jwt_superuser_auth(url, sys_db_name, root, password, token
119128
async with ArangoClient(hosts=url) as client:
120129
with pytest.raises(ValueError):
121130
await client.db(
122-
sys_db_name, auth_method="superuser", auth=auth, verify=True
131+
sys_db_name, auth_method="superuser", auth=basic_auth_root, verify=True
123132
)

tests/test_database.py

Lines changed: 46 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,64 @@
11
import pytest
22

3-
from arangoasync.auth import Auth
4-
from arangoasync.client import ArangoClient
53
from arangoasync.collection import StandardCollection
64
from tests.helpers import generate_col_name, generate_db_name
75

86

97
@pytest.mark.asyncio
10-
async def test_database_misc_methods(url, sys_db_name, root, password):
11-
auth = Auth(username=root, password=password)
12-
13-
# TODO also handle exceptions
14-
async with ArangoClient(hosts=url) as client:
15-
db = await client.db(sys_db_name, auth_method="basic", auth=auth, verify=True)
16-
status = await db.status()
17-
assert status["server"] == "arango"
8+
async def test_database_misc_methods(sys_db):
9+
status = await sys_db.status()
10+
assert status["server"] == "arango"
1811

1912

2013
@pytest.mark.asyncio
21-
async def test_create_drop_database(url, sys_db_name, root, password):
22-
auth = Auth(username=root, password=password)
23-
14+
async def test_create_drop_database(arango_client, sys_db, basic_auth_root):
2415
# TODO also handle exceptions
2516
# TODO use more options (cluster must be enabled for that)
26-
async with ArangoClient(hosts=url) as client:
27-
sys_db = await client.db(
28-
sys_db_name, auth_method="basic", auth=auth, verify=True
29-
)
30-
31-
# Create a new database
32-
db_name = generate_db_name()
33-
assert await sys_db.create_database(db_name) is True
34-
new_db = await client.db(db_name, auth_method="basic", auth=auth, verify=True)
35-
assert await sys_db.has_database(db_name) is True
3617

37-
# List available databases
38-
dbs = await sys_db.databases()
39-
assert db_name in dbs
40-
assert "_system" in dbs
41-
42-
# TODO move this to a separate test
43-
col_name = generate_col_name()
44-
col = await new_db.create_collection(col_name)
45-
await col.insert({"_key": "1", "a": 1})
46-
doc = await col.get("1")
47-
assert doc["_key"] == "1"
48-
49-
# Drop the newly created database
50-
assert await sys_db.delete_database(db_name) is True
51-
non_existent_db = generate_db_name()
52-
assert await sys_db.has_database(non_existent_db) is False
53-
assert (
54-
await sys_db.delete_database(non_existent_db, ignore_missing=True) is False
55-
)
18+
# Create a new database
19+
db_name = generate_db_name()
20+
assert await sys_db.create_database(db_name) is True
21+
new_db = await arango_client.db(
22+
db_name, auth_method="basic", auth=basic_auth_root, verify=True
23+
)
24+
assert await sys_db.has_database(db_name) is True
25+
26+
# List available databases
27+
dbs = await sys_db.databases()
28+
assert db_name in dbs
29+
assert "_system" in dbs
30+
31+
# TODO move this to a separate test for documents
32+
col_name = generate_col_name()
33+
col = await new_db.create_collection(col_name)
34+
await col.insert({"_key": "1", "a": 1})
35+
doc = await col.get("1")
36+
assert doc["_key"] == "1"
37+
38+
# Drop the newly created database
39+
assert await sys_db.delete_database(db_name) is True
40+
non_existent_db = generate_db_name()
41+
assert await sys_db.has_database(non_existent_db) is False
42+
assert await sys_db.delete_database(non_existent_db, ignore_missing=True) is False
5643

5744

5845
@pytest.mark.asyncio
59-
async def test_create_drop_collection(url, sys_db_name, root, password):
60-
auth = Auth(username=root, password=password)
61-
46+
async def test_create_drop_collection(test_db):
6247
# TODO also handle exceptions
63-
async with ArangoClient(hosts=url) as client:
64-
db = await client.db(sys_db_name, auth_method="basic", auth=auth, verify=True)
65-
66-
# Create a new collection
67-
col_name = generate_col_name()
68-
col = await db.create_collection(col_name)
69-
assert isinstance(col, StandardCollection)
70-
assert await db.has_collection(col_name)
71-
cols = await db.collections()
72-
assert any(c.name == col_name for c in cols)
7348

74-
# Drop the newly created collection
75-
assert await db.delete_collection(col_name) is True
76-
assert not await db.has_collection(col_name)
77-
non_existent_col = generate_col_name()
78-
assert await db.has_collection(non_existent_col) is False
79-
assert (
80-
await db.delete_collection(non_existent_col, ignore_missing=True) is False
81-
)
49+
# Create a new collection
50+
col_name = generate_col_name()
51+
col = await test_db.create_collection(col_name)
52+
assert isinstance(col, StandardCollection)
53+
assert await test_db.has_collection(col_name)
54+
cols = await test_db.collections()
55+
assert any(c.name == col_name for c in cols)
56+
57+
# Drop the newly created collection
58+
assert await test_db.delete_collection(col_name) is True
59+
assert not await test_db.has_collection(col_name)
60+
non_existent_col = generate_col_name()
61+
assert await test_db.has_collection(non_existent_col) is False
62+
assert (
63+
await test_db.delete_collection(non_existent_col, ignore_missing=True) is False
64+
)
File renamed without changes.

0 commit comments

Comments
 (0)