File tree Expand file tree Collapse file tree 6 files changed +102
-3
lines changed Expand file tree Collapse file tree 6 files changed +102
-3
lines changed Original file line number Diff line number Diff line change @@ -223,6 +223,32 @@ def write_concern
223
223
@write_concern ||= WriteConcern . get ( options [ :write ] )
224
224
end
225
225
226
+ # Get the names of all databases.
227
+ #
228
+ # @example Get the database names.
229
+ # client.database_names
230
+ #
231
+ # @return [ Array<String> ] The names of the databases.
232
+ #
233
+ # @since 2.0.5
234
+ def database_names
235
+ list_databases . collect { |info | info [ 'name' ] }
236
+ end
237
+
238
+ # Get info for each database.
239
+ #
240
+ # @example Get the info for each database.
241
+ # client.list_databases
242
+ #
243
+ # @return [ Array<Hash> ] The info for each database.
244
+ #
245
+ # @since 2.0.5
246
+ def list_databases
247
+ use ( Database ::ADMIN ) . command (
248
+ listDatabases : 1
249
+ ) . first [ 'databases' ]
250
+ end
251
+
226
252
private
227
253
228
254
def create_from_addresses ( addresses , opts = { } )
Original file line number Diff line number Diff line change @@ -102,6 +102,18 @@ def collection_names(options = {})
102
102
View . new ( self ) . collection_names ( options )
103
103
end
104
104
105
+ # Get info on all the collections in the database.
106
+ #
107
+ # @example Get info on each collection.
108
+ # database.list_collections
109
+ #
110
+ # @return [ Array<Hash> ] Info for each collection in the database.
111
+ #
112
+ # @since 2.0.5
113
+ def list_collections
114
+ View . new ( self ) . list_collections
115
+ end
116
+
105
117
# Get all the collections that belong to this database.
106
118
#
107
119
# @example Get all the collections.
Original file line number Diff line number Diff line change @@ -57,6 +57,18 @@ def collection_names(options = {})
57
57
end
58
58
end
59
59
60
+ # Get info on all the collections in the database.
61
+ #
62
+ # @example Get info on each collection.
63
+ # database.list_collections
64
+ #
65
+ # @return [ Array<Hash> ] Info for each collection in the database.
66
+ #
67
+ # @since 2.0.5
68
+ def list_collections
69
+ collections_info ( next_primary )
70
+ end
71
+
60
72
# Create the new database view.
61
73
#
62
74
# @example Create the new database view.
Original file line number Diff line number Diff line change 569
569
end
570
570
end
571
571
end
572
+
573
+ describe '#database_names' do
574
+
575
+ it 'returns a list of database names' do
576
+ expect ( root_authorized_client . database_names ) . to include (
577
+ 'admin' , 'local' , TEST_DB
578
+ )
579
+ end
580
+ end
581
+
582
+ describe '#list_databases' do
583
+
584
+ it 'returns a list of database info documents' do
585
+ expect (
586
+ root_authorized_client . list_databases . collect do |i |
587
+ i [ 'name' ]
588
+ end ) . to include ( 'admin' , 'local' , TEST_DB )
589
+ end
590
+ end
572
591
end
Original file line number Diff line number Diff line change 95
95
end
96
96
end
97
97
98
+ describe '#list_collections' do
99
+
100
+ let ( :database ) do
101
+ described_class . new ( authorized_client , TEST_DB )
102
+ end
103
+
104
+ let ( :result ) do
105
+ database . list_collections . map do |info |
106
+ info [ 'name' ]
107
+ end
108
+ end
109
+
110
+ before do
111
+ database [ :users ] . create
112
+ end
113
+
114
+ after do
115
+ database [ :users ] . drop
116
+ end
117
+
118
+ it 'returns a list of the collections info' , if : write_command_enabled? do
119
+ expect ( result ) . to include ( 'users' )
120
+ end
121
+
122
+ it 'returns a list of the collections info' , unless : write_command_enabled? do
123
+ expect ( result ) . to include ( "#{ TEST_DB } .users" )
124
+ end
125
+ end
126
+
98
127
describe '#collections' do
99
128
100
129
context 'when the database exists' do
Original file line number Diff line number Diff line change 77
77
Mongo ::Auth ::Roles ::USER_ADMIN_ANY_DATABASE ,
78
78
Mongo ::Auth ::Roles ::DATABASE_ADMIN_ANY_DATABASE ,
79
79
Mongo ::Auth ::Roles ::READ_WRITE_ANY_DATABASE ,
80
- Mongo ::Auth ::Roles ::HOST_MANAGER
80
+ Mongo ::Auth ::Roles ::HOST_MANAGER ,
81
+ Mongo ::Auth ::Roles ::CLUSTER_ADMIN
81
82
]
82
83
)
83
84
105
106
# @since 2.0.
106
107
TEST_READ_WRITE_USER = Mongo ::Auth ::User . new (
107
108
database : TEST_DB ,
108
- user : 'test-user' ,
109
- password : ' password' ,
109
+ user : TEST_USER . name ,
110
+ password : TEST_USER . password ,
110
111
roles : [ Mongo ::Auth ::Roles ::READ_WRITE , Mongo ::Auth ::Roles ::DATABASE_ADMIN ]
111
112
)
112
113
You can’t perform that action at this time.
0 commit comments