1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { snakeCase } from 'lodash'
6 import { objectConverter, parseBytes } from '../../helpers/core-utils'
7 import validator from 'validator'
9 const expect = chai.expect
11 describe('Parse Bytes', function () {
13 it('Should pass when given valid value', async function () {
15 expect(parseBytes(1024)).to.be.eq(1024)
16 expect(parseBytes(1048576)).to.be.eq(1048576)
17 expect(parseBytes('1024')).to.be.eq(1024)
18 expect(parseBytes('1048576')).to.be.eq(1048576)
21 expect(parseBytes('1B')).to.be.eq(1024)
22 expect(parseBytes('1MB')).to.be.eq(1048576)
23 expect(parseBytes('1GB')).to.be.eq(1073741824)
24 expect(parseBytes('1TB')).to.be.eq(1099511627776)
26 expect(parseBytes('5GB')).to.be.eq(5368709120)
27 expect(parseBytes('5TB')).to.be.eq(5497558138880)
29 expect(parseBytes('1024B')).to.be.eq(1048576)
30 expect(parseBytes('1024MB')).to.be.eq(1073741824)
31 expect(parseBytes('1024GB')).to.be.eq(1099511627776)
32 expect(parseBytes('1024TB')).to.be.eq(1125899906842624)
35 expect(parseBytes('1 GB')).to.be.eq(1073741824)
36 expect(parseBytes('1\tGB')).to.be.eq(1073741824)
39 expect(parseBytes('1TB 1024MB')).to.be.eq(1100585369600)
40 expect(parseBytes('4GB 1024MB')).to.be.eq(5368709120)
41 expect(parseBytes('4TB 1024GB')).to.be.eq(5497558138880)
42 expect(parseBytes('4TB 1024GB 0MB')).to.be.eq(5497558138880)
43 expect(parseBytes('1024TB 1024GB 1024MB')).to.be.eq(1127000492212224)
46 it('Should be invalid when given invalid value', async function () {
47 expect(parseBytes('6GB 1GB')).to.be.eq(6)
50 it('Should convert an object', async function () {
51 function keyConverter (k: string) {
55 function valueConverter (v: any) {
56 if (validator.isNumeric(v + '')) return parseInt('' + v, 10)
66 mySuperSub2Key: 'hello',
67 mySuperSub3Key: [ '1', 'hello', 2 ],
80 const res = objectConverter(obj, keyConverter, valueConverter)
82 expect(res.my_super_key).to.equal('hello')
83 expect(res.my_super_2_key).to.equal(45)
84 expect(res.my_super_3_key.my_super_sub_key).to.equal(15)
85 expect(res.my_super_3_key.my_super_sub_2_key).to.equal('hello')
86 expect(res.my_super_3_key.my_super_sub_3_key).to.deep.equal([ 1, 'hello', 2 ])
87 expect(res.my_super_3_key.my_super_sub_4_key).to.equal(4)
88 expect(res.toto.super_key).to.equal(15)
89 expect(res.toto.super_key_2).to.equal('hello')
90 expect(res.super_key.super_key_4).to.equal(15)
93 expect(res.mySuperKey).to.be.undefined
94 expect(obj['my_super_key']).to.be.undefined