Skip to content

Commit 98e0e85

Browse files
authored
Merge pull request #2485 from asfadmin/vitest-utility-functions
Vitest utility functions
2 parents d12b0dd + b46d89d commit 98e0e85

2 files changed

Lines changed: 212 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import * as models from '@models';
4+
import { productFactory } from '@testing/product-factory';
5+
import { beforeEach, describe, expect, it } from 'vitest';
6+
7+
import { DatasetForProductService } from './dataset-for-product.service';
8+
9+
describe('DatasetForProductService', () => {
10+
let service: DatasetForProductService;
11+
12+
beforeEach(() => {
13+
TestBed.configureTestingModule({});
14+
service = TestBed.inject(DatasetForProductService);
15+
});
16+
17+
describe('match()', () => {
18+
it('returns alos for ALOS dataset without AVNIR-2 instrument', () => {
19+
const product = productFactory
20+
.withBasicInfo('test-scene')
21+
.withDataset('ALOS')
22+
.build();
23+
expect(service.match(product)).toBe(models.alos);
24+
});
25+
26+
it('returns avnir for ALOS dataset with AVNIR-2 instrument', () => {
27+
const product = productFactory
28+
.withBasicInfo('test-scene')
29+
.withDataset('ALOS')
30+
.withInstrument('AVNIR-2')
31+
.build();
32+
expect(service.match(product)).toBe(models.avnir);
33+
});
34+
35+
it('returns sentinel_1_bursts for BURST product type', () => {
36+
const product = productFactory
37+
.withBasicInfo('test-scene')
38+
.withProductType('BURST')
39+
.build();
40+
expect(service.match(product)).toBe(models.sentinel_1_bursts);
41+
});
42+
43+
it('returns tropo for TROPO-ZENITH product type', () => {
44+
const product = productFactory
45+
.withBasicInfo('test-scene')
46+
.withProductType('TROPO-ZENITH')
47+
.build();
48+
expect(service.match(product)).toBe(models.tropo);
49+
});
50+
51+
it('returns tropo for ECMWF_TROPO product type', () => {
52+
const product = productFactory
53+
.withBasicInfo('test-scene')
54+
.withProductType('ECMWF_TROPO')
55+
.build();
56+
expect(service.match(product)).toBe(models.tropo);
57+
});
58+
59+
it('returns opera_s1 for OPERA product id', () => {
60+
const product = productFactory
61+
.withBasicInfo('test-scene')
62+
.withId('OPERA_L2_RTC-S1_T001')
63+
.build();
64+
expect(service.match(product)).toBe(models.opera_s1);
65+
});
66+
67+
it('returns SENTINEL-1 INTERFEROGRAM (BETA) for S1-GUNW name', () => {
68+
const product = productFactory
69+
.withBasicInfo('test-scene')
70+
.withName('S1-GUNW-D-N-123456-123456-HH-R-N-0001')
71+
.build();
72+
expect(service.match(product)).toBe(
73+
models.datasets['SENTINEL-1 INTERFEROGRAM (BETA)'],
74+
);
75+
});
76+
77+
it('returns sentinel-1 dataset for exact SENTINEL-1 dataset match', () => {
78+
const product = productFactory.withBasicInfo('test-scene').build();
79+
expect(service.match(product)).toBe(models.datasets['SENTINEL-1']);
80+
});
81+
82+
it('returns sentinel-1 dataset for Sentinel-1A via partial match', () => {
83+
const product = productFactory
84+
.withBasicInfo('test-scene')
85+
.withDataset('Sentinel-1A')
86+
.build();
87+
expect(service.match(product)).toBe(models.datasets['SENTINEL-1']);
88+
});
89+
// TODO: Change this to default to SENTINEL-1 again?
90+
it('returns NISAR for non matching dataset', () => {
91+
const product = productFactory
92+
.withBasicInfo('test-scene')
93+
.withDataset('')
94+
.build();
95+
expect(service.match(product)).toBe(models.datasets.NISAR);
96+
});
97+
});
98+
});

src/app/testing/product-factory.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { CMRProduct, CMRProductMetadata, FlightDirection } from '@models';
2+
import moment from 'moment';
3+
4+
function createProduct(product: CMRProduct): CMRProduct {
5+
return product;
6+
}
7+
8+
class ProductFactory {
9+
withBasicInfo(name: string): NestedProductFactory {
10+
return new NestedProductFactory(
11+
createProduct({
12+
name: name,
13+
productTypeDisplay: '',
14+
file: '',
15+
id: '',
16+
downloadUrl: '',
17+
bytes: 100 * 1000000,
18+
dataset: 'SENTINEL-1',
19+
browses: [],
20+
thumbnail: '',
21+
groupId: '',
22+
isUnzippedFile: false,
23+
isDummyProduct: false,
24+
metadata: {
25+
date: moment(),
26+
stopDate: moment().add(2),
27+
polygon: '',
28+
29+
productType: '',
30+
beamMode: '',
31+
polarization: '',
32+
flightDirection: FlightDirection.ASCENDING,
33+
34+
path: 0,
35+
frame: 0,
36+
absoluteOrbit: [],
37+
38+
collectionName: '',
39+
collectionID: '',
40+
41+
stackSize: 0,
42+
faradayRotation: 0,
43+
offNadirAngle: 0,
44+
instrument: '',
45+
pointingAngle: '',
46+
missionName: '',
47+
flightLine: '',
48+
perpendicular: 0,
49+
temporal: 0,
50+
canInSAR: false,
51+
burst: undefined,
52+
opera: undefined,
53+
nisar: undefined,
54+
fileName: '',
55+
job: undefined,
56+
pgeVersion: '',
57+
subproducts: [],
58+
parentID: '',
59+
ariaVersion: '',
60+
},
61+
}),
62+
);
63+
}
64+
}
65+
66+
class NestedProductFactory {
67+
constructor(private _product: Readonly<CMRProduct>) {}
68+
69+
build() {
70+
return this._product;
71+
}
72+
73+
withAOI(): NestedProductFactory {
74+
return this._extendWithMetadata({
75+
polygon:
76+
'POLYGON((-124.3885 36.7031,-120.6971 36.7031,-120.6971 38.9246,-124.3885 38.9246,-124.3885 36.7031))',
77+
});
78+
}
79+
80+
withDataset(dataset: string): NestedProductFactory {
81+
return this._extendWith({ dataset });
82+
}
83+
84+
withInstrument(instrument: string): NestedProductFactory {
85+
return this._extendWithMetadata({ instrument });
86+
}
87+
88+
withProductType(productType: string): NestedProductFactory {
89+
return this._extendWithMetadata({ productType });
90+
}
91+
92+
withId(id: string): NestedProductFactory {
93+
return this._extendWith({ id });
94+
}
95+
96+
withName(name: string): NestedProductFactory {
97+
return this._extendWith({ name });
98+
}
99+
100+
private _extendWith(product: Partial<CMRProduct>) {
101+
return new NestedProductFactory({ ...this._product, ...product });
102+
}
103+
private _extendWithMetadata(productMetadata: Partial<CMRProductMetadata>) {
104+
return new NestedProductFactory({
105+
...this._product,
106+
metadata: {
107+
...this._product.metadata,
108+
...productMetadata,
109+
},
110+
});
111+
}
112+
}
113+
114+
export const productFactory = new ProductFactory();

0 commit comments

Comments
 (0)