]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.spec.ts
update ceph source to reef 18.2.0
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / formatter.service.spec.ts
CommitLineData
f67539c2 1import { configureTestBed } from '~/testing/unit-test-helper';
11fdf7f2
TL
2import { DimlessBinaryPipe } from '../pipes/dimless-binary.pipe';
3import { DimlessPipe } from '../pipes/dimless.pipe';
4import { FormatterService } from './formatter.service';
5
6describe('FormatterService', () => {
7 let service: FormatterService;
8 let dimlessBinaryPipe: DimlessBinaryPipe;
9 let dimlessPipe: DimlessPipe;
10
11 const convertToBytesAndBack = (value: string, newValue?: string) => {
12 expect(dimlessBinaryPipe.transform(service.toBytes(value))).toBe(newValue || value);
13 };
14
15 configureTestBed({
16 providers: [FormatterService, DimlessBinaryPipe]
17 });
18
19 beforeEach(() => {
20 service = new FormatterService();
21 dimlessBinaryPipe = new DimlessBinaryPipe(service);
22 dimlessPipe = new DimlessPipe(service);
23 });
24
25 it('should be created', () => {
26 expect(service).toBeTruthy();
27 });
28
29 describe('format_number', () => {
30 const formats = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
31
32 it('should return minus for unsupported values', () => {
33 expect(service.format_number(service, 1024, formats)).toBe('-');
34 expect(service.format_number(undefined, 1024, formats)).toBe('-');
35 expect(service.format_number(null, 1024, formats)).toBe('-');
36 });
37
38 it('should test some values', () => {
39 expect(service.format_number('0', 1024, formats)).toBe('0 B');
40 expect(service.format_number('0.1', 1024, formats)).toBe('0.1 B');
41 expect(service.format_number('1.2', 1024, formats)).toBe('1.2 B');
42 expect(service.format_number('1', 1024, formats)).toBe('1 B');
43 expect(service.format_number('1024', 1024, formats)).toBe('1 KiB');
44 expect(service.format_number(23.45678 * Math.pow(1024, 3), 1024, formats)).toBe('23.5 GiB');
45 expect(service.format_number(23.45678 * Math.pow(1024, 3), 1024, formats, 2)).toBe(
46 '23.46 GiB'
47 );
48 });
49
50 it('should test some dimless values', () => {
51 expect(dimlessPipe.transform(0.6)).toBe('0.6');
52 expect(dimlessPipe.transform(1000.608)).toBe('1 k');
53 expect(dimlessPipe.transform(1e10)).toBe('10 G');
54 expect(dimlessPipe.transform(2.37e16)).toBe('23.7 P');
55 });
56 });
57
05a536ef
TL
58 describe('formatNumberFromTo', () => {
59 const formats = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
60 const formats2 = ['ns', 'μs', 'ms', 's'];
61
62 it('should test some values and data units', () => {
63 expect(service.formatNumberFromTo('0.1', 'B', 'TiB', 1024, formats)).toBe('0 TiB');
64 expect(service.formatNumberFromTo('1024', 'B', 'KiB', 1024, formats)).toBe('1 KiB');
65 expect(service.formatNumberFromTo(1000, 'mib', 'gib', 1024, formats, 3)).toBe('0.977 gib');
66 expect(service.formatNumberFromTo(1024, 'GiB', 'MiB', 1024, formats)).toBe('1048576 MiB');
67 expect(
68 service.formatNumberFromTo(23.45678 * Math.pow(1024, 3), 'B', 'GiB', 1024, formats)
69 ).toBe('23.5 GiB');
70 expect(
71 service.formatNumberFromTo(23.45678 * Math.pow(1024, 3), 'B', 'GiB', 1024, formats, 2)
72 ).toBe('23.46 GiB');
73
74 expect(service.formatNumberFromTo('128', 'ns', 'ms', 1000, formats2)).toBe('0 ms');
75 expect(service.formatNumberFromTo(128, 'ns', 'ms', 1000, formats2, 4)).toBe('0.0001 ms');
76 expect(service.formatNumberFromTo(20, 's', 'ms', 1000, formats2, 4)).toBe('20000 ms');
77 });
78 });
79
11fdf7f2
TL
80 describe('toBytes', () => {
81 it('should not convert wrong values', () => {
82 expect(service.toBytes('10xyz')).toBeNull();
83 expect(service.toBytes('1.1.1KiB')).toBeNull();
84 expect(service.toBytes('1.1 KiloByte')).toBeNull();
85 expect(service.toBytes('1.1 kib')).toBeNull();
86 expect(service.toBytes('1.kib')).toBeNull();
87 expect(service.toBytes('1 ki')).toBeNull();
88 expect(service.toBytes(undefined)).toBeNull();
89 expect(service.toBytes('')).toBeNull();
90 expect(service.toBytes('-')).toBeNull();
91 expect(service.toBytes(null)).toBeNull();
92 });
93
94 it('should convert values to bytes', () => {
95 expect(service.toBytes('4815162342')).toBe(4815162342);
96 expect(service.toBytes('100M')).toBe(104857600);
97 expect(service.toBytes('100 M')).toBe(104857600);
98 expect(service.toBytes('100 mIb')).toBe(104857600);
99 expect(service.toBytes('100 mb')).toBe(104857600);
100 expect(service.toBytes('100MIB')).toBe(104857600);
101 expect(service.toBytes('1.532KiB')).toBe(Math.round(1.532 * 1024));
102 expect(service.toBytes('0.000000000001TiB')).toBe(1);
103 });
104
105 it('should convert values to human readable again', () => {
106 convertToBytesAndBack('1.1 MiB');
107 convertToBytesAndBack('1.0MiB', '1 MiB');
108 convertToBytesAndBack('8.9 GiB');
109 convertToBytesAndBack('123.5 EiB');
110 });
111 });
112});