@@ -105,4 +105,117 @@ describe('\n Parser Class', () => {
105105 expect ( Parser . reasonToStatusCode ( 'unauthorized' ) ) . toBe ( 401 )
106106 expect ( Parser . reasonToStatusCode ( 'INTERNAL_SERVER_ERROR' ) ) . toBe ( 500 )
107107 } )
108+
109+ it ( 'should parse the complete database url to object and object to complete database url' , async ( ) => {
110+ const url =
111+ 'postgresql://postgres:root@127.0.0.1:5432/postgres?paramOne=1¶mTwo=2¶mThree=3'
112+
113+ // database url to connection object
114+ const connectionObject = Parser . dbUrlToConnectionObj ( url )
115+
116+ expect ( connectionObject . protocol ) . toBe ( 'postgresql' )
117+ expect ( connectionObject . user ) . toBe ( 'postgres' )
118+ expect ( connectionObject . password ) . toBe ( 'root' )
119+ expect ( connectionObject . host ) . toBe ( '127.0.0.1' )
120+ expect ( connectionObject . port ) . toBe ( 5432 )
121+ expect ( connectionObject . database ) . toBe ( 'postgres' )
122+ expect ( connectionObject . options ) . toEqual ( {
123+ paramOne : '1' ,
124+ paramTwo : '2' ,
125+ paramThree : '3' ,
126+ } )
127+
128+ // connection object to database url
129+ const connectionUrl = Parser . connectionObjToDbUrl ( connectionObject )
130+
131+ expect ( connectionUrl ) . toBe ( url )
132+ } )
133+
134+ it ( 'should parse the without auth database url to object and object to without auth database url' , async ( ) => {
135+ const url = 'postgresql://127.0.0.1:5432/postgres'
136+
137+ // database url to connection object
138+ const connectionObject = Parser . dbUrlToConnectionObj ( url )
139+
140+ expect ( connectionObject . protocol ) . toBe ( 'postgresql' )
141+ expect ( connectionObject . user ) . toBe ( null )
142+ expect ( connectionObject . password ) . toBe ( null )
143+ expect ( connectionObject . host ) . toBe ( '127.0.0.1' )
144+ expect ( connectionObject . port ) . toBe ( 5432 )
145+ expect ( connectionObject . database ) . toBe ( 'postgres' )
146+ expect ( connectionObject . options ) . toEqual ( { } )
147+
148+ // connection object to database url
149+ const connectionUrl = Parser . connectionObjToDbUrl ( connectionObject )
150+
151+ expect ( connectionUrl ) . toBe ( url )
152+ } )
153+
154+ it ( 'should parse the without auth and port database url to object and object to without auth and port database url' , async ( ) => {
155+ const url = 'postgresql://127.0.0.1/postgres?options=10&test=10'
156+
157+ // database url to connection object
158+ const connectionObject = Parser . dbUrlToConnectionObj ( url )
159+
160+ expect ( connectionObject . protocol ) . toBe ( 'postgresql' )
161+ expect ( connectionObject . user ) . toBe ( null )
162+ expect ( connectionObject . password ) . toBe ( null )
163+ expect ( connectionObject . host ) . toBe ( '127.0.0.1' )
164+ expect ( connectionObject . port ) . toBe ( null )
165+ expect ( connectionObject . database ) . toBe ( 'postgres' )
166+ expect ( connectionObject . options ) . toEqual ( {
167+ options : '10' ,
168+ test : '10' ,
169+ } )
170+
171+ // connection object to database url
172+ const connectionUrl = Parser . connectionObjToDbUrl ( connectionObject )
173+
174+ expect ( connectionUrl ) . toBe ( url )
175+ } )
176+
177+ it ( 'should parse the without auth, port and options database url to object and object to without auth, port and options database url' , async ( ) => {
178+ const url = 'postgresql://127.0.0.1/postgres'
179+
180+ // database url to connection object
181+ const connectionObject = Parser . dbUrlToConnectionObj ( url )
182+
183+ expect ( connectionObject . protocol ) . toBe ( 'postgresql' )
184+ expect ( connectionObject . user ) . toBe ( null )
185+ expect ( connectionObject . password ) . toBe ( null )
186+ expect ( connectionObject . host ) . toBe ( '127.0.0.1' )
187+ expect ( connectionObject . port ) . toBe ( null )
188+ expect ( connectionObject . database ) . toBe ( 'postgres' )
189+ expect ( connectionObject . options ) . toEqual ( { } )
190+
191+ // connection object to database url
192+ const connectionUrl = Parser . connectionObjToDbUrl ( connectionObject )
193+
194+ expect ( connectionUrl ) . toBe ( url )
195+ } )
196+
197+ it ( 'should parse the cluster database url to object and object to cluster database url' , async ( ) => {
198+ const url =
199+ 'postgresql://postgres:root@127.0.0.1:5432,127.0.0.1:5433,127.0.0.1:5434/postgres'
200+
201+ // database url to connection object
202+ const connectionObject = Parser . dbUrlToConnectionObj ( url )
203+
204+ expect ( connectionObject . protocol ) . toBe ( 'postgresql' )
205+ expect ( connectionObject . user ) . toBe ( 'postgres' )
206+ expect ( connectionObject . password ) . toBe ( 'root' )
207+ expect ( connectionObject . host ) . toEqual ( [
208+ '127.0.0.1:5432' ,
209+ '127.0.0.1:5433' ,
210+ '127.0.0.1:5434' ,
211+ ] )
212+ expect ( connectionObject . port ) . toBe ( null )
213+ expect ( connectionObject . database ) . toBe ( 'postgres' )
214+ expect ( connectionObject . options ) . toEqual ( { } )
215+
216+ // connection object to database url
217+ const connectionUrl = Parser . connectionObjToDbUrl ( connectionObject )
218+
219+ expect ( connectionUrl ) . toBe ( url )
220+ } )
108221} )
0 commit comments