@@ -50,27 +50,113 @@ def test_project_id(self):
5050 client = firestore_async .client ()
5151 assert client is not None
5252 assert client .project == 'explicit-project-id'
53+ assert client ._database == '(default)'
5354
5455 def test_project_id_with_explicit_app (self ):
5556 cred = credentials .Certificate (testutils .resource_filename ('service_account.json' ))
5657 app = firebase_admin .initialize_app (cred , {'projectId' : 'explicit-project-id' })
5758 client = firestore_async .client (app = app )
5859 assert client is not None
5960 assert client .project == 'explicit-project-id'
61+ assert client ._database == '(default)'
6062
6163 def test_service_account (self ):
6264 cred = credentials .Certificate (testutils .resource_filename ('service_account.json' ))
6365 firebase_admin .initialize_app (cred )
6466 client = firestore_async .client ()
6567 assert client is not None
6668 assert client .project == 'mock-project-id'
69+ assert client ._database == '(default)'
6770
6871 def test_service_account_with_explicit_app (self ):
6972 cred = credentials .Certificate (testutils .resource_filename ('service_account.json' ))
7073 app = firebase_admin .initialize_app (cred )
7174 client = firestore_async .client (app = app )
7275 assert client is not None
7376 assert client .project == 'mock-project-id'
77+ assert client ._database == '(default)'
78+
79+ @pytest .mark .parametrize ('database_id' , [123 , False , True , {}, []])
80+ def test_invalid_database_id (self , database_id ):
81+ cred = credentials .Certificate (testutils .resource_filename ('service_account.json' ))
82+ firebase_admin .initialize_app (cred )
83+ with pytest .raises (ValueError ) as excinfo :
84+ firestore_async .client (database_id = database_id )
85+ assert str (excinfo .value ) == f'database_id "{ database_id } " must be a string or None.'
86+
87+ def test_database_id (self ):
88+ cred = credentials .Certificate (testutils .resource_filename ('service_account.json' ))
89+ firebase_admin .initialize_app (cred )
90+ database_id = 'mock-database-id'
91+ client = firestore_async .client (database_id = database_id )
92+ assert client is not None
93+ assert client .project == 'mock-project-id'
94+ assert client ._database == 'mock-database-id'
95+
96+ @pytest .mark .parametrize ('database_id' , ['' , '(default)' , None ])
97+ def test_database_id_with_default_id (self , database_id ):
98+ cred = credentials .Certificate (testutils .resource_filename ('service_account.json' ))
99+ firebase_admin .initialize_app (cred )
100+ client = firestore_async .client (database_id = database_id )
101+ assert client is not None
102+ assert client .project == 'mock-project-id'
103+ assert client ._database == '(default)'
104+
105+ def test_database_id_with_explicit_app (self ):
106+ cred = credentials .Certificate (testutils .resource_filename ('service_account.json' ))
107+ app = firebase_admin .initialize_app (cred )
108+ database_id = 'mock-database-id'
109+ client = firestore_async .client (app , database_id )
110+ assert client is not None
111+ assert client .project == 'mock-project-id'
112+ assert client ._database == 'mock-database-id'
113+
114+ def test_database_id_with_multi_db (self ):
115+ cred = credentials .Certificate (testutils .resource_filename ('service_account.json' ))
116+ firebase_admin .initialize_app (cred )
117+ database_id_1 = 'mock-database-id-1'
118+ database_id_2 = 'mock-database-id-2'
119+ client_1 = firestore_async .client (database_id = database_id_1 )
120+ client_2 = firestore_async .client (database_id = database_id_2 )
121+ assert (client_1 is not None ) and (client_2 is not None )
122+ assert client_1 is not client_2
123+ assert client_1 .project == 'mock-project-id'
124+ assert client_2 .project == 'mock-project-id'
125+ assert client_1 ._database == 'mock-database-id-1'
126+ assert client_2 ._database == 'mock-database-id-2'
127+
128+ def test_database_id_with_multi_db_uses_cache (self ):
129+ cred = credentials .Certificate (testutils .resource_filename ('service_account.json' ))
130+ firebase_admin .initialize_app (cred )
131+ database_id = 'mock-database-id'
132+ client_1 = firestore_async .client (database_id = database_id )
133+ client_2 = firestore_async .client (database_id = database_id )
134+ assert (client_1 is not None ) and (client_2 is not None )
135+ assert client_1 is client_2
136+ assert client_1 .project == 'mock-project-id'
137+ assert client_2 .project == 'mock-project-id'
138+ assert client_1 ._database == 'mock-database-id'
139+ assert client_2 ._database == 'mock-database-id'
140+
141+ def test_database_id_with_multi_db_uses_cache_default (self ):
142+ cred = credentials .Certificate (testutils .resource_filename ('service_account.json' ))
143+ firebase_admin .initialize_app (cred )
144+ database_id_1 = ''
145+ database_id_2 = '(default)'
146+ client_1 = firestore_async .client (database_id = database_id_1 )
147+ client_2 = firestore_async .client (database_id = database_id_2 )
148+ client_3 = firestore_async .client ()
149+ assert (client_1 is not None ) and (client_2 is not None ) and (client_3 is not None )
150+ assert client_1 is client_2
151+ assert client_1 is client_3
152+ assert client_2 is client_3
153+ assert client_1 .project == 'mock-project-id'
154+ assert client_2 .project == 'mock-project-id'
155+ assert client_3 .project == 'mock-project-id'
156+ assert client_1 ._database == '(default)'
157+ assert client_2 ._database == '(default)'
158+ assert client_3 ._database == '(default)'
159+
74160
75161 def test_geo_point (self ):
76162 geo_point = firestore_async .GeoPoint (10 , 20 ) # pylint: disable=no-member
0 commit comments