-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathprofileProxyConfigs.test.ts
More file actions
106 lines (103 loc) · 3.9 KB
/
Copy pathprofileProxyConfigs.test.ts
File metadata and controls
106 lines (103 loc) · 3.9 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
/*********************************************************************
* Copyright (c) Intel Corporation 2025
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
import PostgresDb from '../index.js'
import { API_UNEXPECTED_EXCEPTION } from '../../../utils/constants.js'
import { type ProfileProxyConfigs } from '../../../models/RCS.Config.js'
import { ProfileProxyConfigsTable } from './profileProxyConfigs.js'
import { jest } from '@jest/globals'
import { type SpyInstance, spyOn } from 'jest-mock'
describe('profileproxyconfig tests', () => {
let db: PostgresDb
let profilesProxyConfigsTable: ProfileProxyConfigsTable
let querySpy: SpyInstance<any>
const proxyConfigs: ProfileProxyConfigs[] = [
{ name: 'proxyConfig', priority: 1 } as any
]
const profileName = 'profileName'
const tenantId = 'tenantId'
beforeEach(() => {
db = new PostgresDb('')
profilesProxyConfigsTable = new ProfileProxyConfigsTable(db)
querySpy = spyOn(profilesProxyConfigsTable.db, 'query')
})
afterEach(() => {
jest.clearAllMocks()
})
describe('Get', () => {
test('should Get', async () => {
querySpy.mockResolvedValueOnce({ rows: [{}], rowCount: 1 })
const result = await profilesProxyConfigsTable.getProfileProxyConfigs(profileName)
expect(result).toStrictEqual([{}])
expect(querySpy).toBeCalledTimes(1)
expect(querySpy).toBeCalledWith(
`
SELECT
priority as "priority",
proxy_config_name as "name"
FROM profiles_proxyconfigs
WHERE profile_name = $1 and tenant_id = $2
ORDER BY priority`,
[profileName, '']
)
})
})
describe('Delete', () => {
test('should Delete', async () => {
querySpy.mockResolvedValueOnce({ rows: [{}], rowCount: 1 })
const result = await profilesProxyConfigsTable.deleteProfileProxyConfigs(profileName)
expect(result).toBe(true)
expect(querySpy).toBeCalledTimes(1)
expect(querySpy).toBeCalledWith(
`
DELETE
FROM profiles_proxyconfigs
WHERE profile_name = $1 and tenant_id = $2`,
[profileName, '']
)
})
test('should Delete with tenandId', async () => {
querySpy.mockResolvedValueOnce({ rows: [{}], rowCount: 1 })
const result = await profilesProxyConfigsTable.deleteProfileProxyConfigs(profileName, tenantId)
expect(result).toBe(true)
expect(querySpy).toBeCalledTimes(1)
expect(querySpy).toBeCalledWith(
`
DELETE
FROM profiles_proxyconfigs
WHERE profile_name = $1 and tenant_id = $2`,
[profileName, tenantId]
)
})
})
describe('Insert', () => {
test('should Insert', async () => {
querySpy.mockResolvedValueOnce({ rows: [{}], rowCount: 1 })
const result = await profilesProxyConfigsTable.createProfileProxyConfigs(proxyConfigs, profileName)
expect(result).toBe(true)
expect(querySpy).toBeCalledTimes(1)
expect(querySpy).toBeCalledWith(`
INSERT INTO
profiles_proxyconfigs (proxy_config_name, profile_name, priority, tenant_id)
VALUES ('proxyConfig', 'profileName', '1', '')`)
})
test('should NOT insert when no proxyconfigs', async () => {
await expect(profilesProxyConfigsTable.createProfileProxyConfigs([], profileName)).rejects.toThrow(
'Operation failed: profileName'
)
})
test('should NOT insert when constraint violation', async () => {
querySpy.mockRejectedValueOnce({ code: '23503', detail: 'error' })
await expect(profilesProxyConfigsTable.createProfileProxyConfigs(proxyConfigs, profileName)).rejects.toThrow(
'error'
)
})
test('should NOT insert when unknown error', async () => {
querySpy.mockRejectedValueOnce('unknown')
await expect(profilesProxyConfigsTable.createProfileProxyConfigs(proxyConfigs, profileName)).rejects.toThrow(
API_UNEXPECTED_EXCEPTION(profileName)
)
})
})
})