|
| 1 | +const parser = require('../src/Adapters/Storage/Postgres/PostgresConfigParser'); |
| 2 | + |
| 3 | +const queryParamTests = { |
| 4 | + 'a=1&b=2': { a: '1', b: '2' }, |
| 5 | + 'a=abcd%20efgh&b=abcd%3Defgh': { a: 'abcd efgh', b: 'abcd=efgh' }, |
| 6 | + 'a=1&b&c=true': { a: '1', b: '', c: 'true' } |
| 7 | +} |
| 8 | + |
| 9 | +describe('PostgresConfigParser.parseQueryParams', () => { |
| 10 | + it('creates a map from a query string', () => { |
| 11 | + |
| 12 | + for (const key in queryParamTests) { |
| 13 | + const result = parser.parseQueryParams(key); |
| 14 | + |
| 15 | + const testObj = queryParamTests[key]; |
| 16 | + |
| 17 | + expect(Object.keys(result).length) |
| 18 | + .toEqual(Object.keys(testObj).length); |
| 19 | + |
| 20 | + for (const k in result) { |
| 21 | + expect(result[k]).toEqual(testObj[k]); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + }) |
| 26 | +}); |
| 27 | + |
| 28 | +const baseURI = 'postgres://username:password@localhost:5432/db-name' |
| 29 | + |
| 30 | +const dbOptionsTest = {}; |
| 31 | +dbOptionsTest[`${baseURI}?ssl=true&binary=true&application_name=app_name&fallback_application_name=f_app_name&poolSize=10`] = { |
| 32 | + ssl: true, |
| 33 | + binary: true, |
| 34 | + application_name: 'app_name', |
| 35 | + fallback_application_name: 'f_app_name', |
| 36 | + poolSize: 10 |
| 37 | +}; |
| 38 | +dbOptionsTest[`${baseURI}?ssl=&binary=aa`] = { |
| 39 | + ssl: false, |
| 40 | + binary: false |
| 41 | +} |
| 42 | + |
| 43 | +describe('PostgresConfigParser.getDatabaseOptionsFromURI', () => { |
| 44 | + it('creates a db options map from a query string', () => { |
| 45 | + |
| 46 | + for (const key in dbOptionsTest) { |
| 47 | + const result = parser.getDatabaseOptionsFromURI(key); |
| 48 | + |
| 49 | + const testObj = dbOptionsTest[key]; |
| 50 | + |
| 51 | + for (const k in testObj) { |
| 52 | + expect(result[k]).toEqual(testObj[k]); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + }); |
| 57 | + |
| 58 | + it('sets the poolSize to 10 if the it is not a number', () => { |
| 59 | + |
| 60 | + const result = parser.getDatabaseOptionsFromURI(`${baseURI}?poolSize=sdf`); |
| 61 | + |
| 62 | + expect(result.poolSize).toEqual(10); |
| 63 | + |
| 64 | + }); |
| 65 | +}); |
0 commit comments