|
1 |
| -import { Cls } from '../src'; |
2 |
| - |
3 |
| -describe('Cls', () => { |
4 |
| - const client = new Cls({ |
5 |
| - region: 'ap-guangzhou', |
6 |
| - secretId: process.env.TENCENT_SECRET_ID, |
7 |
| - secretKey: process.env.TENCENT_SECRET_KEY, |
8 |
| - token: process.env.TENCENT_TOKEN, |
9 |
| - debug: true, |
| 1 | +import { |
| 2 | + deepClone, |
| 3 | + getRealType, |
| 4 | + isArray, |
| 5 | + isObject, |
| 6 | + isEmpty, |
| 7 | + cleanEmptyValue, |
| 8 | + camelCaseProps, |
| 9 | + pascalCaseProps, |
| 10 | + CommonError, |
| 11 | +} from '../src'; |
| 12 | + |
| 13 | +describe('Common methods', () => { |
| 14 | + const testObj = { |
| 15 | + name: 'test', |
| 16 | + detail: { |
| 17 | + site: 'test.com', |
| 18 | + }, |
| 19 | + }; |
| 20 | + test('CommonError', async () => { |
| 21 | + try { |
| 22 | + throw new CommonError({ |
| 23 | + type: 'TEST_ApiError', |
| 24 | + message: 'This is a test error', |
| 25 | + stack: 'error stack', |
| 26 | + reqId: 123, |
| 27 | + code: 'abc', |
| 28 | + displayMsg: 'error test', |
| 29 | + }); |
| 30 | + } catch (e) { |
| 31 | + expect(e.type).toEqual('TEST_ApiError'); |
| 32 | + expect(e.message).toEqual('This is a test error'); |
| 33 | + expect(e.stack).toEqual('error stack'); |
| 34 | + expect(e.reqId).toEqual(123); |
| 35 | + expect(e.code).toEqual('abc'); |
| 36 | + expect(e.displayMsg).toEqual('error test'); |
| 37 | + } |
10 | 38 | });
|
11 |
| - |
12 |
| - let logset_id; |
13 |
| - let topic_id; |
14 |
| - |
15 |
| - test('create logset', async () => { |
16 |
| - const res = await client.createLogset({ |
17 |
| - logset_name: 'cls-test', |
18 |
| - period: 7, |
19 |
| - }); |
20 |
| - expect(res).toEqual({ |
21 |
| - requestId: expect.any(String), |
22 |
| - logset_id: expect.any(String), |
| 39 | + test('deepClone', async () => { |
| 40 | + expect(deepClone(testObj)).toEqual({ |
| 41 | + name: 'test', |
| 42 | + detail: { |
| 43 | + site: 'test.com', |
| 44 | + }, |
23 | 45 | });
|
24 |
| - |
25 |
| - logset_id = res.logset_id; |
26 | 46 | });
|
27 |
| - |
28 |
| - test('get logset', async () => { |
29 |
| - const res = await client.getLogset({ |
30 |
| - logset_id, |
31 |
| - }); |
32 |
| - expect(res).toEqual({ |
33 |
| - requestId: expect.any(String), |
34 |
| - create_time: expect.any(String), |
35 |
| - logset_id: logset_id, |
36 |
| - logset_name: 'cls-test', |
37 |
| - period: 7, |
38 |
| - topics_number: 0, |
39 |
| - }); |
40 |
| - |
41 |
| - logset_id = res.logset_id; |
| 47 | + test('getRealType', async () => { |
| 48 | + expect(getRealType(testObj)).toBe('Object'); |
| 49 | + expect(getRealType([])).toBe('Array'); |
| 50 | + expect(getRealType({})).toBe('Object'); |
| 51 | + expect(getRealType('hello')).toBe('String'); |
| 52 | + expect(getRealType(true)).toBe('Boolean'); |
| 53 | + expect(getRealType(1)).toBe('Number'); |
| 54 | + expect(getRealType(NaN)).toBe('Number'); |
42 | 55 | });
|
43 |
| - |
44 |
| - test('get logset list', async () => { |
45 |
| - const res = await client.getLogsetList(); |
46 |
| - expect(res).toEqual({ |
47 |
| - requestId: expect.any(String), |
48 |
| - logsets: expect.any(Array), |
49 |
| - }); |
50 |
| - |
51 |
| - const [exist] = res.logsets.filter((item) => item.logset_id === logset_id); |
52 |
| - expect(exist).toEqual({ |
53 |
| - create_time: expect.any(String), |
54 |
| - logset_id, |
55 |
| - logset_name: 'cls-test', |
56 |
| - period: 7, |
57 |
| - topics_number: 0, |
58 |
| - }); |
| 56 | + test('isArray', async () => { |
| 57 | + expect(isArray(testObj)).toBe(false); |
| 58 | + expect(isArray([])).toBe(true); |
59 | 59 | });
|
60 |
| - |
61 |
| - test('create topic', async () => { |
62 |
| - const res = await client.createTopic({ |
63 |
| - logset_id, |
64 |
| - topic_name: 'cls-test-topic', |
65 |
| - }); |
66 |
| - expect(res).toEqual({ |
67 |
| - requestId: expect.any(String), |
68 |
| - topic_id: expect.any(String), |
69 |
| - }); |
70 |
| - |
71 |
| - topic_id = res.topic_id; |
| 60 | + test('isObject', async () => { |
| 61 | + expect(isObject(testObj)).toBe(true); |
| 62 | + expect(isObject({})).toBe(true); |
| 63 | + expect(isObject([1])).toBe(false); |
72 | 64 | });
|
73 |
| - |
74 |
| - test('get topic', async () => { |
75 |
| - const res = await client.getTopic({ |
76 |
| - topic_id, |
77 |
| - }); |
78 |
| - expect(res).toEqual({ |
79 |
| - requestId: expect.any(String), |
80 |
| - ExcludePaths: [], |
81 |
| - collection: true, |
82 |
| - create_time: expect.any(String), |
83 |
| - extract_rule: { filter_keys: [], filter_regex: [] }, |
84 |
| - index: false, |
85 |
| - isolated: 0, |
86 |
| - log_format: '', |
87 |
| - log_type: 'minimalist_log', |
88 |
| - logset_id, |
89 |
| - multi_wild_path: [], |
90 |
| - partition_count: 1, |
91 |
| - path: '', |
92 |
| - shipper: false, |
93 |
| - sql_flag: true, |
94 |
| - topic_id, |
95 |
| - topic_name: 'cls-test-topic', |
96 |
| - }); |
| 65 | + test('isEmpty', async () => { |
| 66 | + expect(isEmpty(testObj)).toBe(false); |
| 67 | + expect(isEmpty({})).toBe(false); |
| 68 | + expect(isEmpty([])).toBe(false); |
| 69 | + expect(isEmpty(0)).toBe(false); |
| 70 | + expect(isEmpty('')).toBe(false); |
| 71 | + expect(isEmpty(false)).toBe(false); |
| 72 | + expect(isEmpty(undefined)).toBe(true); |
| 73 | + expect(isEmpty(null)).toBe(true); |
| 74 | + expect(isEmpty(NaN)).toBe(true); |
97 | 75 | });
|
98 | 76 |
|
99 |
| - test('update index', async () => { |
100 |
| - const res = await client.updateIndex({ |
101 |
| - topic_id, |
102 |
| - effective: true, |
103 |
| - rule: { |
104 |
| - full_text: { |
105 |
| - case_sensitive: true, |
106 |
| - tokenizer: '!@#%^&*()_="\', <>/?|\\;:\n\t\r[]{}', |
107 |
| - }, |
108 |
| - key_value: { |
109 |
| - case_sensitive: true, |
110 |
| - keys: ['SCF_RetMsg'], |
111 |
| - types: ['text'], |
112 |
| - tokenizers: [' '], |
| 77 | + test('cleanEmptyValue', async () => { |
| 78 | + expect( |
| 79 | + cleanEmptyValue({ |
| 80 | + name: 'test', |
| 81 | + isAdult: false, |
| 82 | + age: NaN, |
| 83 | + children: null, |
| 84 | + detail: { |
| 85 | + site: undefined, |
| 86 | + view: 0, |
113 | 87 | },
|
| 88 | + }), |
| 89 | + ).toEqual({ |
| 90 | + name: 'test', |
| 91 | + isAdult: false, |
| 92 | + detail: { |
| 93 | + view: 0, |
114 | 94 | },
|
115 | 95 | });
|
116 |
| - |
117 |
| - expect(res).toEqual({ |
118 |
| - requestId: expect.any(String), |
119 |
| - success: true, |
120 |
| - }); |
121 | 96 | });
|
122 |
| - |
123 |
| - test('get index', async () => { |
124 |
| - const res = await client.getIndex({ |
125 |
| - topic_id, |
126 |
| - }); |
127 |
| - |
128 |
| - expect(res).toEqual({ |
129 |
| - requestId: expect.any(String), |
130 |
| - effective: true, |
131 |
| - rule: { |
132 |
| - full_text: { |
133 |
| - case_sensitive: true, |
134 |
| - tokenizer: `!@#%^&*()_="', <>/?|\\;:\n\t\r[]{}`, |
135 |
| - }, |
136 |
| - key_value: { |
137 |
| - case_sensitive: true, |
138 |
| - template_type: 'static', |
139 |
| - keys: ['SCF_RetMsg'], |
140 |
| - types: ['text'], |
141 |
| - tokenizers: [' '], |
142 |
| - }, |
| 97 | + test('pascalCaseProps', async () => { |
| 98 | + expect(pascalCaseProps(testObj)).toEqual({ |
| 99 | + Name: 'test', |
| 100 | + Detail: { |
| 101 | + Site: 'test.com', |
143 | 102 | },
|
144 |
| - topic_id, |
145 |
| - }); |
146 |
| - }); |
147 |
| - |
148 |
| - test('delete topic', async () => { |
149 |
| - const res = await client.deleteTopic({ |
150 |
| - topic_id, |
151 |
| - }); |
152 |
| - expect(res).toEqual({ |
153 |
| - requestId: expect.any(String), |
154 |
| - success: true, |
155 | 103 | });
|
156 | 104 | });
|
157 |
| - |
158 |
| - test('delete logset', async () => { |
159 |
| - const res = await client.deleteLogset({ |
160 |
| - logset_id, |
161 |
| - }); |
162 |
| - expect(res).toEqual({ |
163 |
| - requestId: expect.any(String), |
164 |
| - success: true, |
165 |
| - }); |
| 105 | + test('camelCaseProps', async () => { |
| 106 | + expect( |
| 107 | + camelCaseProps({ |
| 108 | + Name: 'test', |
| 109 | + Detail: { |
| 110 | + Site: 'test.com', |
| 111 | + }, |
| 112 | + }), |
| 113 | + ).toEqual(testObj); |
166 | 114 | });
|
167 | 115 | });
|
0 commit comments