1
+ import contextlib
1
2
import logging
2
3
import typing
3
4
10
11
11
12
@trio .testing .trio_test
12
13
async def test_dir_simple (DatastoreTests ):
13
- s1 = datastore .adapter .directory .ObjectDatastore (datastore .ObjectDictDatastore ())
14
- s2 = datastore .adapter .directory .ObjectDatastore (datastore .ObjectDictDatastore ())
15
- await DatastoreTests ([s1 , s2 ]).subtest_simple ()
14
+ async with contextlib .AsyncExitStack () as stack :
15
+ s1 = stack .push_async_exit (
16
+ datastore .adapter .directory .ObjectDatastore (datastore .ObjectDictDatastore ())
17
+ )
18
+ s2 = stack .push_async_exit (
19
+ datastore .adapter .directory .ObjectDatastore (datastore .ObjectDictDatastore ())
20
+ )
21
+ await DatastoreTests ([s1 , s2 ]).subtest_simple ()
16
22
17
23
18
24
##########################
@@ -29,106 +35,103 @@ class ObjectDirectoryDictDatastore(
29
35
30
36
@trio .testing .trio_test
31
37
async def test_directory_simple (DatastoreTests ):
32
- s1 = ObjectDirectoryDictDatastore ()
33
- s2 = ObjectDirectoryDictDatastore ()
34
- await DatastoreTests ([s1 , s2 ]).subtest_simple ()
38
+ async with contextlib .AsyncExitStack () as stack :
39
+ s1 = stack .push_async_exit (ObjectDirectoryDictDatastore ())
40
+ s2 = stack .push_async_exit (ObjectDirectoryDictDatastore ())
41
+ await DatastoreTests ([s1 , s2 ]).subtest_simple ()
35
42
36
43
37
44
@trio .testing .trio_test
38
45
async def test_directory_init ():
39
- ds = ObjectDirectoryDictDatastore ()
46
+ async with ObjectDirectoryDictDatastore () as ds :
47
+ # initialize directory at /foo
48
+ dir_key = datastore .Key ('/foo' )
49
+ await ds .directory (dir_key )
50
+ assert await ds .get_all (dir_key ) == []
40
51
41
- # initialize directory at /foo
42
- dir_key = datastore .Key ('/foo' )
43
- await ds .directory (dir_key )
44
- assert await ds .get_all (dir_key ) == []
52
+ # can add to dir
53
+ bar_key = datastore .Key ('/foo/bar ' )
54
+ await ds .directory_add (dir_key , bar_key )
55
+ assert await ds .get_all (dir_key ) == [str ( bar_key ) ]
45
56
46
- # can add to dir
47
- bar_key = datastore .Key ('/foo/bar' )
48
- await ds .directory_add (dir_key , bar_key )
49
- assert await ds .get_all (dir_key ) == [str (bar_key )]
50
-
51
- # re-init does not wipe out directory at /foo
52
- dir_key = datastore .Key ('/foo' )
53
- with pytest .raises (KeyError ):
54
- await ds .directory (dir_key , exist_ok = False )
55
- await ds .directory (dir_key , exist_ok = True )
56
- assert await ds .get_all (dir_key ) == [str (bar_key )]
57
+ # re-init does not wipe out directory at /foo
58
+ dir_key = datastore .Key ('/foo' )
59
+ with pytest .raises (KeyError ):
60
+ await ds .directory (dir_key , exist_ok = False )
61
+ await ds .directory (dir_key , exist_ok = True )
62
+ assert await ds .get_all (dir_key ) == [str (bar_key )]
57
63
58
64
59
65
@trio .testing .trio_test
60
66
async def test_directory_basic ():
61
- ds = ObjectDirectoryDictDatastore ()
62
-
63
- # initialize directory at /foo
64
- dir_key = datastore .Key ('/foo' )
65
- await ds .directory (dir_key )
66
-
67
- # adding directory entries
68
- bar_key = datastore .Key ('/foo/bar' )
69
- baz_key = datastore .Key ('/foo/baz' )
70
- await ds .directory_add (dir_key , bar_key )
71
- await ds .directory_add (dir_key , baz_key )
72
- keys = [key async for key in ds .directory_read (dir_key )]
73
- assert keys == [bar_key , baz_key ]
74
-
75
- # removing directory entries
76
- await ds .directory_remove (dir_key , bar_key )
77
- keys = [key async for key in ds .directory_read (dir_key )]
78
- assert keys == [baz_key ]
79
-
80
- await ds .directory_remove (dir_key , baz_key )
81
- keys = [key async for key in ds .directory_read (dir_key )]
82
- assert keys == []
83
-
84
- # generator
85
- with pytest .raises (StopAsyncIteration ):
86
- gen = ds .directory_read (dir_key ).__aiter__ ()
87
- await gen .__anext__ ()
67
+ async with ObjectDirectoryDictDatastore () as ds :
68
+ # initialize directory at /foo
69
+ dir_key = datastore .Key ('/foo' )
70
+ await ds .directory (dir_key )
71
+
72
+ # adding directory entries
73
+ bar_key = datastore .Key ('/foo/bar' )
74
+ baz_key = datastore .Key ('/foo/baz' )
75
+ await ds .directory_add (dir_key , bar_key )
76
+ await ds .directory_add (dir_key , baz_key )
77
+ keys = [key async for key in ds .directory_read (dir_key )]
78
+ assert keys == [bar_key , baz_key ]
79
+
80
+ # removing directory entries
81
+ await ds .directory_remove (dir_key , bar_key )
82
+ keys = [key async for key in ds .directory_read (dir_key )]
83
+ assert keys == [baz_key ]
84
+
85
+ await ds .directory_remove (dir_key , baz_key )
86
+ keys = [key async for key in ds .directory_read (dir_key )]
87
+ assert keys == []
88
+
89
+ # generator
90
+ with pytest .raises (StopAsyncIteration ):
91
+ gen = ds .directory_read (dir_key ).__aiter__ ()
92
+ await gen .__anext__ ()
88
93
89
94
90
95
@trio .testing .trio_test
91
96
async def test_directory_double_add ():
92
- ds = ObjectDirectoryDictDatastore ()
93
-
94
- # initialize directory at /foo
95
- dir_key = datastore .Key ('/foo' )
96
- await ds .directory (dir_key )
97
+ async with ObjectDirectoryDictDatastore () as ds :
98
+ # initialize directory at /foo
99
+ dir_key = datastore .Key ('/foo' )
100
+ await ds .directory (dir_key )
97
101
98
- # adding directory entries
99
- bar_key = datastore .Key ('/foo/bar' )
100
- baz_key = datastore .Key ('/foo/baz' )
101
- await ds .directory_add (dir_key , bar_key )
102
- await ds .directory_add (dir_key , baz_key )
103
- await ds .directory_add (dir_key , bar_key )
104
- await ds .directory_add (dir_key , baz_key )
105
- await ds .directory_add (dir_key , baz_key )
106
- await ds .directory_add (dir_key , bar_key )
102
+ # adding directory entries
103
+ bar_key = datastore .Key ('/foo/bar' )
104
+ baz_key = datastore .Key ('/foo/baz' )
105
+ await ds .directory_add (dir_key , bar_key )
106
+ await ds .directory_add (dir_key , baz_key )
107
+ await ds .directory_add (dir_key , bar_key )
108
+ await ds .directory_add (dir_key , baz_key )
109
+ await ds .directory_add (dir_key , baz_key )
110
+ await ds .directory_add (dir_key , bar_key )
107
111
108
- keys = [key async for key in ds .directory_read (dir_key )]
109
- assert keys == [bar_key , baz_key ]
112
+ keys = [key async for key in ds .directory_read (dir_key )]
113
+ assert keys == [bar_key , baz_key ]
110
114
111
115
112
116
@trio .testing .trio_test
113
117
async def test_directory_remove ():
114
- ds = ObjectDirectoryDictDatastore ()
115
-
116
- # initialize directory at /foo
117
- dir_key = datastore .Key ('/foo' )
118
- await ds .directory (dir_key )
119
-
120
- # adding directory entries
121
- bar_key = datastore .Key ('/foo/bar' )
122
- baz_key = datastore .Key ('/foo/baz' )
123
- await ds .directory_add (dir_key , bar_key )
124
- await ds .directory_add (dir_key , baz_key )
125
- keys = [key async for key in ds .directory_read (dir_key )]
126
- assert keys == [bar_key , baz_key ]
127
-
128
- # removing directory entries
129
- await ds .directory_remove (dir_key , bar_key )
130
- await ds .directory_remove (dir_key , bar_key , missing_ok = True )
131
- with pytest .raises (KeyError ):
132
- await ds .directory_remove (dir_key , bar_key , missing_ok = False )
133
- keys = [key async for key in ds .directory_read (dir_key )]
134
- assert keys == [baz_key ]
118
+ async with ObjectDirectoryDictDatastore () as ds :
119
+ # initialize directory at /foo
120
+ dir_key = datastore .Key ('/foo' )
121
+ await ds .directory (dir_key )
122
+
123
+ # adding directory entries
124
+ bar_key = datastore .Key ('/foo/bar' )
125
+ baz_key = datastore .Key ('/foo/baz' )
126
+ await ds .directory_add (dir_key , bar_key )
127
+ await ds .directory_add (dir_key , baz_key )
128
+ keys = [key async for key in ds .directory_read (dir_key )]
129
+ assert keys == [bar_key , baz_key ]
130
+
131
+ # removing directory entries
132
+ await ds .directory_remove (dir_key , bar_key )
133
+ await ds .directory_remove (dir_key , bar_key , missing_ok = True )
134
+ with pytest .raises (KeyError ):
135
+ await ds .directory_remove (dir_key , bar_key , missing_ok = False )
136
+ keys = [key async for key in ds .directory_read (dir_key )]
137
+ assert keys == [baz_key ]
0 commit comments