]>
Commit | Line | Data |
---|---|---|
0e5ff97f BY |
1 | /* tslint:disable:no-unused-expression */ |
2 | ||
3 | import * as chai from 'chai' | |
4 | import 'mocha' | |
a4101923 | 5 | import { snakeCase, isNumber } from 'lodash' |
0e5ff97f | 6 | import { |
a4101923 | 7 | parseBytes, objectConverter |
0e5ff97f | 8 | } from '../../helpers/core-utils' |
a4101923 | 9 | import { isNumeric } from 'validator' |
0e5ff97f BY |
10 | |
11 | const expect = chai.expect | |
12 | ||
13 | describe('Parse Bytes', function () { | |
a4101923 | 14 | |
0e5ff97f BY |
15 | it('Should pass when given valid value', async function () { |
16 | // just return it | |
17 | expect(parseBytes(1024)).to.be.eq(1024) | |
18 | expect(parseBytes(1048576)).to.be.eq(1048576) | |
19 | expect(parseBytes('1024')).to.be.eq(1024) | |
20 | expect(parseBytes('1048576')).to.be.eq(1048576) | |
21 | ||
22 | // sizes | |
23 | expect(parseBytes('1B')).to.be.eq(1024) | |
24 | expect(parseBytes('1MB')).to.be.eq(1048576) | |
25 | expect(parseBytes('1GB')).to.be.eq(1073741824) | |
26 | expect(parseBytes('1TB')).to.be.eq(1099511627776) | |
27 | ||
28 | expect(parseBytes('5GB')).to.be.eq(5368709120) | |
29 | expect(parseBytes('5TB')).to.be.eq(5497558138880) | |
30 | ||
31 | expect(parseBytes('1024B')).to.be.eq(1048576) | |
32 | expect(parseBytes('1024MB')).to.be.eq(1073741824) | |
33 | expect(parseBytes('1024GB')).to.be.eq(1099511627776) | |
34 | expect(parseBytes('1024TB')).to.be.eq(1125899906842624) | |
35 | ||
36 | // with whitespace | |
37 | expect(parseBytes('1 GB')).to.be.eq(1073741824) | |
38 | expect(parseBytes('1\tGB')).to.be.eq(1073741824) | |
39 | ||
40 | // sum value | |
41 | expect(parseBytes('1TB 1024MB')).to.be.eq(1100585369600) | |
42 | expect(parseBytes('4GB 1024MB')).to.be.eq(5368709120) | |
43 | expect(parseBytes('4TB 1024GB')).to.be.eq(5497558138880) | |
44 | expect(parseBytes('4TB 1024GB 0MB')).to.be.eq(5497558138880) | |
45 | expect(parseBytes('1024TB 1024GB 1024MB')).to.be.eq(1127000492212224) | |
46 | }) | |
47 | ||
48 | it('Should be invalid when given invalid value', async function () { | |
49 | expect(parseBytes('6GB 1GB')).to.be.eq(6) | |
50 | }) | |
a4101923 C |
51 | |
52 | it('Should convert an object', async function () { | |
53 | function keyConverter (k: string) { | |
54 | return snakeCase(k) | |
55 | } | |
56 | ||
57 | function valueConverter (v: any) { | |
58 | if (isNumeric(v + '')) return parseInt('' + v, 10) | |
59 | ||
60 | return v | |
61 | } | |
62 | ||
63 | const obj = { | |
64 | mySuperKey: 'hello', | |
65 | mySuper2Key: '45', | |
66 | mySuper3Key: { | |
67 | mySuperSubKey: '15', | |
68 | mySuperSub2Key: 'hello', | |
69 | mySuperSub3Key: [ '1', 'hello', 2 ], | |
70 | mySuperSub4Key: 4 | |
71 | }, | |
72 | mySuper4Key: 45, | |
73 | toto: { | |
74 | super_key: '15', | |
75 | superKey2: 'hello' | |
76 | }, | |
77 | super_key: { | |
78 | superKey4: 15 | |
79 | } | |
80 | } | |
81 | ||
82 | const res = objectConverter(obj, keyConverter, valueConverter) | |
83 | ||
84 | expect(res.my_super_key).to.equal('hello') | |
85 | expect(res.my_super_2_key).to.equal(45) | |
86 | expect(res.my_super_3_key.my_super_sub_key).to.equal(15) | |
87 | expect(res.my_super_3_key.my_super_sub_2_key).to.equal('hello') | |
88 | expect(res.my_super_3_key.my_super_sub_3_key).to.deep.equal([ 1, 'hello', 2 ]) | |
89 | expect(res.my_super_3_key.my_super_sub_4_key).to.equal(4) | |
90 | expect(res.toto.super_key).to.equal(15) | |
91 | expect(res.toto.super_key_2).to.equal('hello') | |
92 | expect(res.super_key.super_key_4).to.equal(15) | |
93 | ||
94 | // Immutable | |
95 | expect(res.mySuperKey).to.be.undefined | |
96 | expect(obj['my_super_key']).to.be.undefined | |
97 | }) | |
0e5ff97f | 98 | }) |