|
| 1 | +import databaseConfig from './database.config'; |
| 2 | + |
| 3 | +describe('databaseConfig', () => { |
| 4 | + const originalEnv = process.env; |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + // Reset process.env to its original state before each test |
| 8 | + process.env = { ...originalEnv }; |
| 9 | + }); |
| 10 | + |
| 11 | + beforeAll(() => { |
| 12 | + jest.spyOn(console, 'warn').mockImplementation(); |
| 13 | + jest.spyOn(console, 'error').mockImplementation(); |
| 14 | + jest.spyOn(console, 'info').mockImplementation(); |
| 15 | + }); |
| 16 | + |
| 17 | + describe('type', () => { |
| 18 | + it('should return the value of DATABASE_TYPE', async () => { |
| 19 | + process.env.DATABASE_TYPE = 'postgres'; |
| 20 | + const config = await databaseConfig(); |
| 21 | + expect(config.type).toBe('postgres'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should return the empty value when DATABASE_TYPE is an empty', async () => { |
| 25 | + process.env.DATABASE_TYPE = ''; |
| 26 | + const config = await databaseConfig(); |
| 27 | + expect(config.type).toBe(''); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should throw an error when DATABASE_TYPE is not set', async () => { |
| 31 | + delete process.env.DATABASE_TYPE; |
| 32 | + await expect(async () => await databaseConfig()).rejects.toThrow(Error); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('host', () => { |
| 37 | + it('should return the value of DATABASE_HOST', async () => { |
| 38 | + process.env.DATABASE_HOST = 'localhost'; |
| 39 | + const config = await databaseConfig(); |
| 40 | + expect(config.host).toBe('localhost'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return the empty value when DATABASE_HOST is an empty', async () => { |
| 44 | + process.env.DATABASE_HOST = ''; |
| 45 | + const config = await databaseConfig(); |
| 46 | + expect(config.host).toBe(''); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should throw an error when DATABASE_HOST is not set', async () => { |
| 50 | + delete process.env.DATABASE_HOST; |
| 51 | + await expect(async () => await databaseConfig()).rejects.toThrow(Error); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('port', () => { |
| 56 | + it('should return the value of DATABASE_PORT', async () => { |
| 57 | + process.env.DATABASE_PORT = '5432'; |
| 58 | + const config = await databaseConfig(); |
| 59 | + expect(config.port).toBe(5432); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should return 5432 when DATABASE_PORT is an empty', async () => { |
| 63 | + process.env.DATABASE_PORT = ''; |
| 64 | + const config = await databaseConfig(); |
| 65 | + expect(config.port).toBe(5432); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should throw an error when DATABASE_PORT is not set', async () => { |
| 69 | + delete process.env.DATABASE_PORT; |
| 70 | + await expect(async () => await databaseConfig()).rejects.toThrow(Error); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should throw an error when DATABASE_PORT is not a number', async () => { |
| 74 | + process.env.DATABASE_PORT = 'invalid'; |
| 75 | + await expect(async () => await databaseConfig()).rejects.toThrow(Error); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('name', () => { |
| 80 | + it('should return the value of DATABASE_NAME', async () => { |
| 81 | + process.env.DATABASE_NAME = 'name'; |
| 82 | + const config = await databaseConfig(); |
| 83 | + expect(config.name).toBe('name'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should return the empty value when DATABASE_NAME is an empty', async () => { |
| 87 | + process.env.DATABASE_NAME = ''; |
| 88 | + const config = await databaseConfig(); |
| 89 | + expect(config.name).toBe(''); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should throw an error when DATABASE_NAME is not set', async () => { |
| 93 | + delete process.env.DATABASE_NAME; |
| 94 | + await expect(async () => await databaseConfig()).rejects.toThrow(Error); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('username', () => { |
| 99 | + it('should return the value of DATABASE_USERNAME', async () => { |
| 100 | + process.env.DATABASE_USERNAME = 'username'; |
| 101 | + const config = await databaseConfig(); |
| 102 | + expect(config.username).toBe('username'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should return the empty value when DATABASE_USERNAME is an empty', async () => { |
| 106 | + process.env.DATABASE_USERNAME = ''; |
| 107 | + const config = await databaseConfig(); |
| 108 | + expect(config.username).toBe(''); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should throw an error when DATABASE_USERNAME is not set', async () => { |
| 112 | + delete process.env.DATABASE_USERNAME; |
| 113 | + await expect(async () => await databaseConfig()).rejects.toThrow(Error); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('password', () => { |
| 118 | + it('should return the value of DATABASE_PASSWORD', async () => { |
| 119 | + process.env.DATABASE_PASSWORD = 'password'; |
| 120 | + const config = await databaseConfig(); |
| 121 | + expect(config.password).toBe('password'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should return the empty value when DATABASE_PASSWORD is an empty', async () => { |
| 125 | + process.env.DATABASE_PASSWORD = ''; |
| 126 | + const config = await databaseConfig(); |
| 127 | + expect(config.password).toBe(''); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should throw an error when DATABASE_PASSWORD is not set', async () => { |
| 131 | + delete process.env.DATABASE_PASSWORD; |
| 132 | + await expect(async () => await databaseConfig()).rejects.toThrow(Error); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe('logging', () => { |
| 137 | + it('should return the value of DATABASE_LOGGING as a boolean', async () => { |
| 138 | + process.env.DATABASE_LOGGING = 'true'; |
| 139 | + const config = await databaseConfig(); |
| 140 | + expect(config.logging).toBe(true); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should return false when DATABASE_LOGGING is an empty', async () => { |
| 144 | + process.env.DATABASE_LOGGING = ''; |
| 145 | + const config = await databaseConfig(); |
| 146 | + expect(config.logging).toBe(false); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should return false when DATABASE_LOGGING is not set', async () => { |
| 150 | + delete process.env.DATABASE_LOGGING; |
| 151 | + const config = await databaseConfig(); |
| 152 | + expect(config.logging).toBe(false); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should return false when DATABASE_LOGGING is not a boolean', async () => { |
| 156 | + process.env.DATABASE_LOGGING = 'invalid'; |
| 157 | + const config = await databaseConfig(); |
| 158 | + expect(config.logging).toBe(false); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + describe('synchronize', () => { |
| 163 | + it('should return the value of DATABASE_SYNCHRONIZE as a boolean', async () => { |
| 164 | + process.env.DATABASE_SYNCHRONIZE = 'true'; |
| 165 | + const config = await databaseConfig(); |
| 166 | + expect(config.synchronize).toBe(true); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should return false when DATABASE_SYNCHRONIZE is an empty', async () => { |
| 170 | + process.env.DATABASE_SYNCHRONIZE = ''; |
| 171 | + const config = await databaseConfig(); |
| 172 | + expect(config.synchronize).toBe(false); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should return false when DATABASE_SYNCHRONIZE is not set', async () => { |
| 176 | + delete process.env.DATABASE_SYNCHRONIZE; |
| 177 | + const config = await databaseConfig(); |
| 178 | + expect(config.synchronize).toBe(false); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should return false when DATABASE_SYNCHRONIZE is not a boolean', async () => { |
| 182 | + process.env.DATABASE_SYNCHRONIZE = 'invalid'; |
| 183 | + const config = await databaseConfig(); |
| 184 | + expect(config.synchronize).toBe(false); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + describe('maxConnections', () => { |
| 189 | + it('should return the value of DATABASE_MAX_CONNECTIONS as a number', async () => { |
| 190 | + process.env.DATABASE_MAX_CONNECTIONS = '10'; |
| 191 | + const config = await databaseConfig(); |
| 192 | + expect(config.maxConnections).toBe(10); |
| 193 | + }); |
| 194 | + |
| 195 | + it('should return 100 when DATABASE_MAX_CONNECTIONS is not set', async () => { |
| 196 | + delete process.env.DATABASE_MAX_CONNECTIONS; |
| 197 | + const config = await databaseConfig(); |
| 198 | + expect(config.maxConnections).toBe(100); |
| 199 | + }); |
| 200 | + |
| 201 | + it('should throw an error when DATABASE_MAX_CONNECTIONS is an empty', async () => { |
| 202 | + process.env.DATABASE_MAX_CONNECTIONS = ''; |
| 203 | + await expect(async () => await databaseConfig()).rejects.toThrow(Error); |
| 204 | + }); |
| 205 | + |
| 206 | + it('should throw an error when DATABASE_MAX_CONNECTIONS is not a number', async () => { |
| 207 | + process.env.DATABASE_MAX_CONNECTIONS = 'invalid'; |
| 208 | + await expect(async () => await databaseConfig()).rejects.toThrow(Error); |
| 209 | + }); |
| 210 | + |
| 211 | + it('should throw an error when DATABASE_MAX_CONNECTIONS is a negative number', async () => { |
| 212 | + process.env.DATABASE_MAX_CONNECTIONS = '-10'; |
| 213 | + await expect(async () => await databaseConfig()).rejects.toThrow(Error); |
| 214 | + }); |
| 215 | + }); |
| 216 | + |
| 217 | + describe('sslEnabled', () => { |
| 218 | + it('should return the value of DATABASE_SSL_ENABLED as a boolean', async () => { |
| 219 | + process.env.DATABASE_SSL_ENABLED = 'true'; |
| 220 | + const config = await databaseConfig(); |
| 221 | + expect(config.sslEnabled).toBe(true); |
| 222 | + }); |
| 223 | + |
| 224 | + it('should return false when DATABASE_SSL_ENABLED is an empty', async () => { |
| 225 | + process.env.DATABASE_SSL_ENABLED = ''; |
| 226 | + const config = await databaseConfig(); |
| 227 | + expect(config.sslEnabled).toBe(false); |
| 228 | + }); |
| 229 | + |
| 230 | + it('should return false when DATABASE_SSL_ENABLED is not set', async () => { |
| 231 | + delete process.env.DATABASE_SSL_ENABLED; |
| 232 | + const config = await databaseConfig(); |
| 233 | + expect(config.sslEnabled).toBe(false); |
| 234 | + }); |
| 235 | + |
| 236 | + it('should return false when DATABASE_SSL_ENABLED is not a boolean', async () => { |
| 237 | + process.env.DATABASE_SSL_ENABLED = 'invalid'; |
| 238 | + const config = await databaseConfig(); |
| 239 | + expect(config.sslEnabled).toBe(false); |
| 240 | + }); |
| 241 | + }); |
| 242 | + |
| 243 | + describe('rejectUnauthorized', () => { |
| 244 | + it('should return the value of DATABASE_REJECT_UNAUTHORIZED as a boolean', async () => { |
| 245 | + process.env.DATABASE_REJECT_UNAUTHORIZED = 'true'; |
| 246 | + const config = await databaseConfig(); |
| 247 | + expect(config.rejectUnauthorized).toBe(true); |
| 248 | + }); |
| 249 | + |
| 250 | + it('should return false when DATABASE_REJECT_UNAUTHORIZED is an empty', async () => { |
| 251 | + process.env.DATABASE_REJECT_UNAUTHORIZED = ''; |
| 252 | + const config = await databaseConfig(); |
| 253 | + expect(config.rejectUnauthorized).toBe(false); |
| 254 | + }); |
| 255 | + |
| 256 | + it('should return false when DATABASE_REJECT_UNAUTHORIZED is not set', async () => { |
| 257 | + delete process.env.DATABASE_REJECT_UNAUTHORIZED; |
| 258 | + const config = await databaseConfig(); |
| 259 | + expect(config.rejectUnauthorized).toBe(false); |
| 260 | + }); |
| 261 | + |
| 262 | + it('should return false when DATABASE_REJECT_UNAUTHORIZED is not a boolean', async () => { |
| 263 | + process.env.DATABASE_REJECT_UNAUTHORIZED = 'invalid'; |
| 264 | + const config = await databaseConfig(); |
| 265 | + expect(config.rejectUnauthorized).toBe(false); |
| 266 | + }); |
| 267 | + }); |
| 268 | + |
| 269 | + describe('ca', () => { |
| 270 | + it('should return the value of DATABASE_CA', async () => { |
| 271 | + process.env.DATABASE_CA = 'ca'; |
| 272 | + const config = await databaseConfig(); |
| 273 | + expect(config.ca).toBe('ca'); |
| 274 | + }); |
| 275 | + |
| 276 | + it('should return the empty value when DATABASE_CA is an empty', async () => { |
| 277 | + process.env.DATABASE_CA = ''; |
| 278 | + const config = await databaseConfig(); |
| 279 | + expect(config.ca).toBe(''); |
| 280 | + }); |
| 281 | + |
| 282 | + it('should return the empty value when DATABASE_CA is not set', async () => { |
| 283 | + delete process.env.DATABASE_CA; |
| 284 | + const config = await databaseConfig(); |
| 285 | + expect(config.ca).toBe(undefined); |
| 286 | + }); |
| 287 | + }); |
| 288 | + |
| 289 | + describe('key', () => { |
| 290 | + it('should return the value of DATABASE_KEY', async () => { |
| 291 | + process.env.DATABASE_KEY = 'key'; |
| 292 | + const config = await databaseConfig(); |
| 293 | + expect(config.key).toBe('key'); |
| 294 | + }); |
| 295 | + |
| 296 | + it('should return the empty value when DATABASE_KEY is an empty', async () => { |
| 297 | + process.env.DATABASE_KEY = ''; |
| 298 | + const config = await databaseConfig(); |
| 299 | + expect(config.key).toBe(''); |
| 300 | + }); |
| 301 | + |
| 302 | + it('should return the empty value when DATABASE_KEY is not set', async () => { |
| 303 | + delete process.env.DATABASE_KEY; |
| 304 | + const config = await databaseConfig(); |
| 305 | + expect(config.key).toBe(undefined); |
| 306 | + }); |
| 307 | + }); |
| 308 | + |
| 309 | + describe('cert', () => { |
| 310 | + it('should return the value of DATABASE_CERT', async () => { |
| 311 | + process.env.DATABASE_CERT = 'cert'; |
| 312 | + const config = await databaseConfig(); |
| 313 | + expect(config.cert).toBe('cert'); |
| 314 | + }); |
| 315 | + |
| 316 | + it('should return the empty value when DATABASE_CERT is an empty', async () => { |
| 317 | + process.env.DATABASE_CERT = ''; |
| 318 | + const config = await databaseConfig(); |
| 319 | + expect(config.cert).toBe(''); |
| 320 | + }); |
| 321 | + |
| 322 | + it('should return the empty value when DATABASE_CERT is not set', async () => { |
| 323 | + delete process.env.DATABASE_CERT; |
| 324 | + const config = await databaseConfig(); |
| 325 | + expect(config.cert).toBe(undefined); |
| 326 | + }); |
| 327 | + }); |
| 328 | +}); |
0 commit comments