Skip to content

Commit

Permalink
test(max_feature_count): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tkohr committed Jan 23, 2025
1 parent c45fbda commit ddbd47b
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 +1,50 @@
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { By } from '@angular/platform-browser'
import { TranslateModule } from '@ngx-translate/core'
import { BehaviorSubject } from 'rxjs'
import { RecordDataPreviewComponent } from './record-data-preview.component'
import { BehaviorSubject, of } from 'rxjs'
import {
MAX_FEATURE_COUNT,
RecordDataPreviewComponent,
} from './record-data-preview.component'
import {
DataViewComponent,
DataViewShareComponent,
MapViewComponent,
MdViewFacade,
} from '@geonetwork-ui/feature/record'
import { MockBuilder } from 'ng-mocks'
import { MockBuilder, MockProvider } from 'ng-mocks'
import { MatTab, MatTabGroup } from '@angular/material/tabs'

class MdViewFacadeMock {
mapApiLinks$ = new BehaviorSubject([])
dataLinks$ = new BehaviorSubject([])
geoDataLinks$ = new BehaviorSubject([])
geoDataLinksWithGeometry$ = new BehaviorSubject([])
}
import { DataService } from '@geonetwork-ui/feature/dataviz'

describe('RecordDataPreviewComponent', () => {
let component: RecordDataPreviewComponent
let fixture: ComponentFixture<RecordDataPreviewComponent>
let facade
let dataService

beforeEach(() => MockBuilder(RecordDataPreviewComponent))

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [TranslateModule.forRoot()],
providers: [
MockProvider(MdViewFacade, {
mapApiLinks$: new BehaviorSubject([]),
dataLinks$: new BehaviorSubject([]),
geoDataLinks$: new BehaviorSubject([]),
geoDataLinksWithGeometry$: new BehaviorSubject([]),
}),
MockProvider(DataService, {
getWfsFeatureCount: jest.fn(),
}),
{
provide: MdViewFacade,
useClass: MdViewFacadeMock,
provide: MAX_FEATURE_COUNT,
useValue: 100,
},
],
}).compileComponents()
facade = TestBed.inject(MdViewFacade)
dataService = TestBed.inject(DataService)
})

beforeEach(() => {
Expand Down Expand Up @@ -202,4 +210,41 @@ describe('RecordDataPreviewComponent', () => {
})
})
})
describe('exceedsMaxFeatureCount$', () => {
it('should return false when no WFS link is present', (done) => {
facade.geoDataLinksWithGeometry$.next([])
component.exceedsMaxFeatureCount$.subscribe((result) => {
expect(result).toBe(false)
done()
})
})

it('should return false when WFS link feature count is less than maxFeatureCount', (done) => {
const link = {
accessServiceProtocol: 'wfs',
url: 'http://example.com',
name: 'test',
}
facade.geoDataLinksWithGeometry$.next([link])
dataService.getWfsFeatureCount.mockReturnValue(of(50))
component.exceedsMaxFeatureCount$.subscribe((result) => {
expect(result).toBe(false)
done()
})
})

it('should return true when WFS link feature count exceeds maxFeatureCount', (done) => {
const link = {
accessServiceProtocol: 'wfs',
url: 'http://example.com',
name: 'test',
}
facade.geoDataLinksWithGeometry$.next([link])
dataService.getWfsFeatureCount.mockReturnValue(of(150))
component.exceedsMaxFeatureCount$.subscribe((result) => {
expect(result).toBe(true)
done()
})
})
})
})
66 changes: 66 additions & 0 deletions libs/feature/dataviz/src/lib/service/data.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ jest.mock('@camptocamp/ogc-client', () => ({
: ['csv', 'xls', 'json', 'gml'],
}
}
getFeatureTypeFull(name) {
return name.indexOf('missing') > -1
? Promise.resolve(null)
: Promise.resolve({
objectCount: 100,
})
}
getFeatureTypes() {
if (this.url.indexOf('unique-feature-type') > -1) {
return [
Expand Down Expand Up @@ -393,6 +400,65 @@ describe('DataService', () => {
})
})

describe('#getWfsFeatureCount', () => {
it('should return the feature count when feature type is found', async () => {
const wfsUrl = 'http://local/wfs'
const featureTypeName = 'validFeatureType'
const count = await lastValueFrom(
service.getWfsFeatureCount(wfsUrl, featureTypeName)
)
expect(count).toBe(100)
})

it('should throw an error when feature type is not found', async () => {
const wfsUrl = 'http://local/wfs'
const featureTypeName = 'missingFeatureType'
try {
await lastValueFrom(
service.getWfsFeatureCount(wfsUrl, featureTypeName)
)
} catch (e) {
expect(e.message).toBe('wfs.featuretype.notfound')
}
})

it('should throw a relevant error when WFS is unreachable (CORS)', async () => {
const wfsUrl = 'http://error.cors/wfs'
const featureTypeName = 'validFeatureType'
try {
await lastValueFrom(
service.getWfsFeatureCount(wfsUrl, featureTypeName)
)
} catch (e) {
expect(e.message).toBe('wfs.unreachable.cors')
}
})

it('should throw a relevant error when WFS is unreachable (HTTP error)', async () => {
const wfsUrl = 'http://error.http/wfs'
const featureTypeName = 'validFeatureType'
try {
await lastValueFrom(
service.getWfsFeatureCount(wfsUrl, featureTypeName)
)
} catch (e) {
expect(e.message).toBe('wfs.unreachable.http')
}
})

it('should throw a relevant error when WFS is unreachable (unknown)', async () => {
const wfsUrl = 'http://error/wfs'
const featureTypeName = 'validFeatureType'
try {
await lastValueFrom(
service.getWfsFeatureCount(wfsUrl, featureTypeName)
)
} catch (e) {
expect(e.message).toBe('wfs.unreachable.unknown')
}
})
})

describe('#getGeoJsonDownloadUrlFromWfs', () => {
describe('WFS with GeoJSON support', () => {
it('returns an url', async () => {
Expand Down
50 changes: 50 additions & 0 deletions libs/feature/record/src/lib/map-view/map-view.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,56 @@ describe('MapViewComponent', () => {
})
})

describe('with links compatible with MAP_API and GEODATA usage (excludeWfs input set to true)', () => {
beforeEach(() => {
component.excludeWfs = true
mdViewFacade.mapApiLinks$.next([
{
url: new URL('http://abcd.com/'),
name: 'layer1',
type: 'service',
accessServiceProtocol: 'wms',
},
])
mdViewFacade.geoDataLinksWithGeometry$.next([
{
url: new URL('http://abcd.com/wfs'),
name: 'featuretype',
type: 'service',
accessServiceProtocol: 'wfs',
},
{
url: new URL('http://abcd.com/data.geojson'),
name: 'data.geojson',
type: 'download',
},
{
url: new URL('http://abcd.com/data/ogcapi'),
name: 'ogc api',
type: 'service',
accessServiceProtocol: 'ogcFeatures',
},
])
fixture.detectChanges()
})
it('provides a list of links to the dropdown (without listing the WFS)', () => {
expect(dropdownComponent.choices).toEqual([
{
value: 0,
label: 'layer1 (WMS)',
},
{
value: 1,
label: 'data.geojson (geojson)',
},
{
value: 2,
label: 'ogc api',
},
])
})
})

describe('with a link using WFS protocol', () => {
beforeEach(fakeAsync(() => {
mdViewFacade.mapApiLinks$.next([])
Expand Down
2 changes: 2 additions & 0 deletions libs/util/app-config/src/lib/app-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ describe('app config utils', () => {
[map]
max_zoom = 10
max_extent = [-418263.418776, 5251529.591305, 961272.067714, 6706890.609855]
max_feature_count = 5000
do_not_use_default_basemap = true
external_viewer_url_template = 'https://example.com/myviewer?'
external_viewer_open_new_tab = true
Expand All @@ -293,6 +294,7 @@ describe('app config utils', () => {
MAX_EXTENT: [
-418263.418776, 5251529.591305, 961272.067714, 6706890.609855,
],
MAX_FEATURE_COUNT: 5000,
DO_NOT_USE_DEFAULT_BASEMAP: true,
EXTERNAL_VIEWER_URL_TEMPLATE: 'https://example.com/myviewer?',
EXTERNAL_VIEWER_OPEN_NEW_TAB: true,
Expand Down

0 comments on commit ddbd47b

Please sign in to comment.