]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/formatter.service.spec.ts
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / formatter.service.spec.ts
1 import { configureTestBed } from '../../../testing/unit-test-helper';
2 import { DimlessBinaryPipe } from '../pipes/dimless-binary.pipe';
3 import { DimlessPipe } from '../pipes/dimless.pipe';
4 import { FormatterService } from './formatter.service';
5
6 describe('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
58 describe('toBytes', () => {
59 it('should not convert wrong values', () => {
60 expect(service.toBytes('10xyz')).toBeNull();
61 expect(service.toBytes('1.1.1KiB')).toBeNull();
62 expect(service.toBytes('1.1 KiloByte')).toBeNull();
63 expect(service.toBytes('1.1 kib')).toBeNull();
64 expect(service.toBytes('1.kib')).toBeNull();
65 expect(service.toBytes('1 ki')).toBeNull();
66 expect(service.toBytes(undefined)).toBeNull();
67 expect(service.toBytes('')).toBeNull();
68 expect(service.toBytes('-')).toBeNull();
69 expect(service.toBytes(null)).toBeNull();
70 });
71
72 it('should convert values to bytes', () => {
73 expect(service.toBytes('4815162342')).toBe(4815162342);
74 expect(service.toBytes('100M')).toBe(104857600);
75 expect(service.toBytes('100 M')).toBe(104857600);
76 expect(service.toBytes('100 mIb')).toBe(104857600);
77 expect(service.toBytes('100 mb')).toBe(104857600);
78 expect(service.toBytes('100MIB')).toBe(104857600);
79 expect(service.toBytes('1.532KiB')).toBe(Math.round(1.532 * 1024));
80 expect(service.toBytes('0.000000000001TiB')).toBe(1);
81 });
82
83 it('should convert values to human readable again', () => {
84 convertToBytesAndBack('1.1 MiB');
85 convertToBytesAndBack('1.0MiB', '1 MiB');
86 convertToBytesAndBack('8.9 GiB');
87 convertToBytesAndBack('123.5 EiB');
88 });
89 });
90 });