1
1
import pytest
2
2
3
3
from arangoasync .collection import StandardCollection
4
- from tests .helpers import generate_col_name , generate_db_name
4
+ from arangoasync .exceptions import (
5
+ CollectionCreateError ,
6
+ CollectionDeleteError ,
7
+ CollectionListError ,
8
+ DatabaseCreateError ,
9
+ DatabaseDeleteError ,
10
+ DatabaseListError ,
11
+ )
12
+ from arangoasync .typings import CollectionType , KeyOptions , UserInfo
13
+ from tests .helpers import generate_col_name , generate_db_name , generate_username
5
14
6
15
7
16
@pytest .mark .asyncio
@@ -11,29 +20,56 @@ async def test_database_misc_methods(sys_db):
11
20
12
21
13
22
@pytest .mark .asyncio
14
- async def test_create_drop_database (arango_client , sys_db , basic_auth_root ):
15
- # TODO also handle exceptions
16
- # TODO use more options (cluster must be enabled for that)
17
-
23
+ async def test_create_drop_database (
24
+ arango_client ,
25
+ sys_db ,
26
+ db ,
27
+ basic_auth_root ,
28
+ password ,
29
+ cluster ,
30
+ ):
18
31
# Create a new database
19
32
db_name = generate_db_name ()
20
- assert await sys_db .create_database (db_name ) is True
21
- new_db = await arango_client .db (
33
+ db_kwargs = dict (
34
+ name = db_name ,
35
+ users = [
36
+ dict (username = generate_username (), password = password , active = True ),
37
+ UserInfo (user = generate_username (), password = password , active = True ),
38
+ ],
39
+ )
40
+ if cluster :
41
+ db_kwargs ["replication_factor" ] = 3
42
+ db_kwargs ["write_concern" ] = 2
43
+ db_kwargs ["sharding" ] = "flexible"
44
+
45
+ assert await sys_db .create_database (** db_kwargs ) is True
46
+ await arango_client .db (
22
47
db_name , auth_method = "basic" , auth = basic_auth_root , verify = True
23
48
)
24
49
assert await sys_db .has_database (db_name ) is True
25
50
51
+ # Try to create a database without permissions
52
+ with pytest .raises (DatabaseCreateError ):
53
+ await db .create_database (generate_db_name ())
54
+
55
+ # Try to create a database that already exists
56
+ with pytest .raises (DatabaseCreateError ):
57
+ await sys_db .create_database (db_name )
58
+
26
59
# List available databases
27
60
dbs = await sys_db .databases ()
28
61
assert db_name in dbs
29
62
assert "_system" in dbs
30
63
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"
64
+ # Cannot list databases without permission
65
+ with pytest .raises (DatabaseListError ):
66
+ await db .databases ()
67
+ with pytest .raises (DatabaseListError ):
68
+ await db .has_database (db_name )
69
+
70
+ # Databases can only be dropped from the system database
71
+ with pytest .raises (DatabaseDeleteError ):
72
+ await db .delete_database (db_name )
37
73
38
74
# Drop the newly created database
39
75
assert await sys_db .delete_database (db_name ) is True
@@ -43,22 +79,87 @@ async def test_create_drop_database(arango_client, sys_db, basic_auth_root):
43
79
44
80
45
81
@pytest .mark .asyncio
46
- async def test_create_drop_collection (test_db ):
47
- # TODO also handle exceptions
48
-
82
+ async def test_create_drop_collection (db , bad_db , cluster ):
49
83
# Create a new collection
50
84
col_name = generate_col_name ()
51
- col = await test_db .create_collection (col_name )
85
+ col = await db .create_collection (col_name )
52
86
assert isinstance (col , StandardCollection )
53
- assert await test_db .has_collection (col_name )
54
- cols = await test_db .collections ()
87
+ assert await db .has_collection (col_name )
88
+ cols = await db .collections ()
55
89
assert any (c .name == col_name for c in cols )
56
90
91
+ # Try to create a collection that already exists
92
+ with pytest .raises (CollectionCreateError ):
93
+ await db .create_collection (col_name )
94
+
95
+ # Try collection methods from a non-existent db
96
+ with pytest .raises (CollectionCreateError ):
97
+ await bad_db .create_collection (generate_col_name ())
98
+ with pytest .raises (CollectionListError ):
99
+ await bad_db .collections ()
100
+ with pytest .raises (CollectionListError ):
101
+ await bad_db .has_collection (col_name )
102
+
103
+ # Try to create a collection with invalid args
104
+ with pytest .raises (ValueError ):
105
+ await db .create_collection (generate_col_name (), col_type = "invalid" )
106
+ with pytest .raises (ValueError ):
107
+ await db .create_collection (generate_col_name (), col_type = db )
108
+ with pytest .raises (ValueError ):
109
+ await db .create_collection (generate_col_name (), key_options = {})
110
+
57
111
# 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 )
112
+ assert await db .delete_collection (col_name ) is True
113
+ assert not await db .has_collection (col_name )
60
114
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
- )
115
+ assert await db .has_collection (non_existent_col ) is False
116
+ assert await db .delete_collection (non_existent_col , ignore_missing = True ) is False
117
+
118
+ # Do not ignore missing collection
119
+ with pytest .raises (CollectionDeleteError ):
120
+ await db .delete_collection (non_existent_col )
121
+
122
+ # Multiple arguments in a cluster setup
123
+ if cluster :
124
+ schema = {
125
+ "rule" : {
126
+ "type" : "object" ,
127
+ "properties" : {
128
+ "test_attr:" : {"type" : "string" },
129
+ },
130
+ "required" : ["test_attr" ],
131
+ },
132
+ "level" : "moderate" ,
133
+ "message" : "Schema Validation Failed." ,
134
+ "type" : "json" ,
135
+ }
136
+
137
+ computed_values = [
138
+ {
139
+ "name" : "foo" ,
140
+ "expression" : "RETURN 1" ,
141
+ "computeOn" : ["insert" , "update" , "replace" ],
142
+ "overwrite" : True ,
143
+ "failOnWarning" : False ,
144
+ "keepNull" : True ,
145
+ }
146
+ ]
147
+
148
+ col = await db .create_collection (
149
+ col_name ,
150
+ col_type = CollectionType .DOCUMENT ,
151
+ write_concern = 2 ,
152
+ wait_for_sync = True ,
153
+ number_of_shards = 1 ,
154
+ is_system = False ,
155
+ computed_values = computed_values ,
156
+ schema = schema ,
157
+ key_options = KeyOptions (
158
+ allow_user_keys = True ,
159
+ generator_type = "autoincrement" ,
160
+ increment = 5 ,
161
+ offset = 10 ,
162
+ ),
163
+ )
164
+ assert col .name == col_name
165
+ assert await db .delete_collection (col_name ) is True
0 commit comments