forked from PostHog/posthog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.test.ts
93 lines (85 loc) · 3.49 KB
/
config.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { buildIntegerMatcher, getDefaultConfig, overrideWithEnv } from '../src/config/config'
describe('config', () => {
test('overrideWithEnv 1', () => {
const defaultConfig = getDefaultConfig()
const env = {
CLICKHOUSE_SECURE: 'false',
TASK_TIMEOUT: '3008',
CLICKHOUSE_HOST: '0.0.0.0',
BASE_DIR: undefined,
}
const config = overrideWithEnv(getDefaultConfig(), env)
expect(config.CLICKHOUSE_SECURE).toEqual(false)
expect(config.TASK_TIMEOUT).toEqual(3008)
expect(config.CLICKHOUSE_HOST).toEqual('0.0.0.0')
expect(config.BASE_DIR).toEqual(defaultConfig.BASE_DIR)
})
test('overrideWithEnv 2', () => {
const env = {
CLICKHOUSE_SECURE: '1',
TASK_TIMEOUT: '3008.12',
}
const config = overrideWithEnv(getDefaultConfig(), env)
expect(config.CLICKHOUSE_SECURE).toEqual(true)
expect(config.TASK_TIMEOUT).toEqual(3008.12)
})
describe('DATABASE_URL', () => {
test('Error if DATABASE_URL is not set AND POSTHOG_DB_NAME is not set', () => {
const env = {
DATABASE_URL: '',
POSTHOG_DB_NAME: '',
}
expect(() => overrideWithEnv(getDefaultConfig(), env)).toThrowError(
'You must specify either DATABASE_URL or the database options POSTHOG_DB_NAME, POSTHOG_DB_USER, POSTHOG_DB_PASSWORD, POSTHOG_POSTGRES_HOST, POSTHOG_POSTGRES_PORT!'
)
})
test('Set DATABASE_URL to a string composed of URL-encoded connection options if DATABASE_URL is not explictly set', () => {
const env = {
DATABASE_URL: '',
POSTHOG_DB_NAME: 'mydb',
POSTHOG_DB_USER: 'user1@domain',
POSTHOG_DB_PASSWORD: 'strong?password',
POSTHOG_POSTGRES_HOST: 'my.host',
}
const config = overrideWithEnv(getDefaultConfig(), env)
expect(config.DATABASE_URL).toEqual('postgres://user1%40domain:strong%3Fpassword@my.host:5432/mydb')
})
test('DATABASE_URL takes precedence to individual config options', () => {
const env = {
DATABASE_URL: 'my_db_url',
POSTHOG_DB_NAME: 'mydb',
POSTHOG_DB_USER: 'user1',
POSTHOG_DB_PASSWORD: 'strongpassword',
POSTHOG_POSTGRES_HOST: 'my.host',
}
const config = overrideWithEnv(getDefaultConfig(), env)
expect(config.DATABASE_URL).toEqual('my_db_url')
})
})
})
describe('buildIntegerMatcher', () => {
test('empty input', () => {
const matcher = buildIntegerMatcher('', false)
expect(matcher(2)).toBe(false)
})
test('ignores star star when not allowed', () => {
const matcher = buildIntegerMatcher('*', false)
expect(matcher(2)).toBe(false)
})
test('matches star when allowed', () => {
const matcher = buildIntegerMatcher('*', true)
expect(matcher(2)).toBe(true)
})
test('can match on a single value', () => {
const matcher = buildIntegerMatcher('2', true)
expect(matcher(2)).toBe(true)
expect(matcher(3)).toBe(false)
})
test('can match on several values', () => {
const matcher = buildIntegerMatcher('2,3,4', true)
expect(matcher(2)).toBe(true)
expect(matcher(3)).toBe(true)
expect(matcher(4)).toBe(true)
expect(matcher(5)).toBe(false)
})
})