|
| 1 | +import { afterAll, expect, jest } from '@jest/globals' |
| 2 | +import { getClickHouseHosts, getClient } from './clickhouse' |
| 3 | + |
| 4 | +import { createClient } from '@clickhouse/client' |
| 5 | +import { createClient as createClientWeb } from '@clickhouse/client-web' |
| 6 | + |
| 7 | +jest.mock('@clickhouse/client', () => ({ |
| 8 | + createClient: jest.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +jest.mock('@clickhouse/client-web', () => ({ |
| 12 | + createClient: jest.fn(), |
| 13 | +})) |
| 14 | + |
| 15 | +describe('getClickHouseHosts', () => { |
| 16 | + const originalEnv = process.env |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + jest.resetModules() // Clears the module cache |
| 20 | + process.env = { ...originalEnv } // Restores the original environment variables |
| 21 | + }) |
| 22 | + |
| 23 | + afterAll(() => { |
| 24 | + process.env = originalEnv // Restores the original environment variables |
| 25 | + }) |
| 26 | + |
| 27 | + it('should return an empty array if CLICKHOUSE_HOST is not set', () => { |
| 28 | + delete process.env.CLICKHOUSE_HOST |
| 29 | + const result = getClickHouseHosts() |
| 30 | + expect(result).toEqual([]) |
| 31 | + }) |
| 32 | + |
| 33 | + it('should return an array with a single host', () => { |
| 34 | + process.env.CLICKHOUSE_HOST = 'localhost' |
| 35 | + const result = getClickHouseHosts() |
| 36 | + expect(result).toEqual(['localhost']) |
| 37 | + }) |
| 38 | + |
| 39 | + it('should return an array with multiple hosts', () => { |
| 40 | + process.env.CLICKHOUSE_HOST = 'host1,host2,host3' |
| 41 | + const result = getClickHouseHosts() |
| 42 | + expect(result).toEqual(['host1', 'host2', 'host3']) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should trim hosts and filter out empty values', () => { |
| 46 | + process.env.CLICKHOUSE_HOST = ' host1 , , host2 , , host3 ' |
| 47 | + const result = getClickHouseHosts() |
| 48 | + expect(result).toEqual(['host1', 'host2', 'host3']) |
| 49 | + }) |
| 50 | + |
| 51 | + it('should handle environment variable with only spaces', () => { |
| 52 | + process.env.CLICKHOUSE_HOST = ' ' |
| 53 | + const result = getClickHouseHosts() |
| 54 | + expect(result).toEqual([]) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should handle environment variable with empty values', () => { |
| 58 | + process.env.CLICKHOUSE_HOST = ',,,' |
| 59 | + const result = getClickHouseHosts() |
| 60 | + expect(result).toEqual([]) |
| 61 | + }) |
| 62 | +}) |
| 63 | + |
| 64 | +describe('getClient', () => { |
| 65 | + const originalEnv = process.env |
| 66 | + |
| 67 | + beforeEach(() => { |
| 68 | + jest.resetModules() // Clears the module cache |
| 69 | + process.env = { ...originalEnv } // Restores the original environment variables |
| 70 | + }) |
| 71 | + |
| 72 | + afterAll(() => { |
| 73 | + process.env = originalEnv // Restores the original environment variables |
| 74 | + }) |
| 75 | + |
| 76 | + it('should create a ClickHouse client using the standard library', () => { |
| 77 | + process.env.CLICKHOUSE_HOST = 'localhost' |
| 78 | + const mockClient = {} |
| 79 | + ;(createClient as jest.Mock).mockReturnValue(mockClient) |
| 80 | + |
| 81 | + const client = getClient({ web: false }) |
| 82 | + |
| 83 | + expect(createClient).toHaveBeenCalledWith({ |
| 84 | + host: 'localhost', |
| 85 | + username: 'default', |
| 86 | + password: '', |
| 87 | + clickhouse_settings: { |
| 88 | + max_execution_time: 60, |
| 89 | + }, |
| 90 | + }) |
| 91 | + expect(client).toBe(mockClient) |
| 92 | + }) |
| 93 | + |
| 94 | + it('should create a ClickHouse client using the web library', () => { |
| 95 | + process.env.CLICKHOUSE_HOST = 'localhost' |
| 96 | + const mockClient = {} |
| 97 | + ;(createClientWeb as jest.Mock).mockReturnValue(mockClient) |
| 98 | + |
| 99 | + const client = getClient({ web: true }) |
| 100 | + |
| 101 | + expect(createClientWeb).toHaveBeenCalledWith({ |
| 102 | + host: 'localhost', |
| 103 | + username: 'default', |
| 104 | + password: '', |
| 105 | + clickhouse_settings: { |
| 106 | + max_execution_time: 60, |
| 107 | + }, |
| 108 | + }) |
| 109 | + expect(client).toBe(mockClient) |
| 110 | + }) |
| 111 | + |
| 112 | + it('should use environment variables for username, password, and max_execution_time', () => { |
| 113 | + process.env.CLICKHOUSE_HOST = 'localhost' |
| 114 | + process.env.CLICKHOUSE_USER = 'testuser' |
| 115 | + process.env.CLICKHOUSE_PASSWORD = 'testpassword' |
| 116 | + process.env.CLICKHOUSE_MAX_EXECUTION_TIME = '120' |
| 117 | + const mockClient = {} |
| 118 | + ;(createClient as jest.Mock).mockReturnValue(mockClient) |
| 119 | + |
| 120 | + const client = getClient({ web: false }) |
| 121 | + |
| 122 | + expect(createClient).toHaveBeenCalledWith({ |
| 123 | + host: 'localhost', |
| 124 | + username: 'testuser', |
| 125 | + password: 'testpassword', |
| 126 | + clickhouse_settings: { |
| 127 | + max_execution_time: 120, |
| 128 | + }, |
| 129 | + }) |
| 130 | + expect(client).toBe(mockClient) |
| 131 | + }) |
| 132 | +}) |
0 commit comments