Skip to content

Commit

Permalink
fix(ngw-kit): fix like and ilike filter requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rendrom committed Nov 5, 2021
1 parent 179c372 commit 3654c2b
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 330 deletions.
2 changes: 1 addition & 1 deletion packages/ngw-connector/src/NgwConnector.ts
Expand Up @@ -385,7 +385,7 @@ export class NgwConnector {
}
// remove double slash
url = fixUrlStr(url);
return this._loadData(url, options);
return this._loadData(encodeURI(url), options);
} else {
throw new Error('Empty `url` not allowed');
}
Expand Down
12 changes: 11 additions & 1 deletion packages/ngw-kit/src/utils/featureLayerUtils.ts
Expand Up @@ -82,7 +82,17 @@ export function createFeatureFieldFilterQueries<
const createParam = (pf: PropertyFilter): [string, any] => {
const [field, operation, value] = pf;
const isFldStr = field !== 'id' ? 'fld_' : '';
return [`${isFldStr}${field}__${operation}`, value];
let vStart = '';
let vEnd = '';
const field_ = String(field)
.trim()
.replace(/^(%?)(.+?)(%?)$/, (m, a, b, c) => {
vStart = a;
vEnd = c;
return b;
});
const v = vStart + value + vEnd;
return [`${isFldStr}${field_}__${operation}`, v];
};

if (logic === 'any') {
Expand Down
4 changes: 3 additions & 1 deletion packages/ngw-orm/src/repository/BaseResource.ts
Expand Up @@ -72,7 +72,9 @@ export class BaseResource {
return [];
}

static clone(options: Partial<NgwResourceOptions> = {}): typeof BaseResource {
static clone<T extends typeof BaseResource = typeof BaseResource>(
options: Partial<NgwResourceOptions> = {},
): T {
const metadataArgsStorage = getMetadataArgsStorage();
const table = metadataArgsStorage.filterTables(
this,
Expand Down
9 changes: 9 additions & 0 deletions tests/helpers/ngw-orm/SandboxGroupNgwKit.ts
@@ -0,0 +1,9 @@
import { ResourceGroup, NgwResource } from '../../../packages/ngw-orm/src';

@NgwResource({
type: 'resource_group',
display_name: 'ngw-kit-tests',
description:
'Automatically created resource group when running tests for @nextgis/ngw-kit',
})
export class SandboxGroupNgwKit extends ResourceGroup {}
225 changes: 140 additions & 85 deletions tests/internet-specs/ngw-kit/ngw-kit.fetch.spec.ts
@@ -1,85 +1,140 @@
// import { Point, Position } from 'geojson';

// import { expect } from 'chai';
// import { Connection } from '../../../packages/ngw-orm/src';
// import { SandboxGroup } from '../../helpers/ngw-orm/SandboxGroup';
// import {
// SandboxPointLayer,
// ISandboxPointLayer,
// } from '../../helpers/ngw-orm/SandboxPointLayer';
// import { fetchNgwLayerItems } from '@nextgis/ngw-kit';

// const TESTS_GROUP_ID = 0;
// let CONNECTION: Connection;

// function getConnection(): Promise<Connection> {
// if (CONNECTION) {
// return Promise.resolve(CONNECTION);
// }
// return Connection.connect({
// baseUrl: 'https://sandbox.nextgis.com',
// auth: {
// login: 'administrator',
// password: 'demodemo',
// },
// }).then((connection) => {
// CONNECTION = connection;
// return connection;
// });
// }

// const features: [ISandboxPointLayer, Position][] = [
// [{ number: 12, test: 'foo' }, [1, 1]],
// [{ number: 11, test: 'Foo' }, [1, 1]],
// ];

// async function newPointLayer(name: string) {
// const connection = await getConnection();
// const Clone = SandboxPointLayer.clone({
// display_name: name,
// });

// const [Point, created] = await connection.getOrCreateResource(
// SandboxPointLayer,
// {
// parent: SandboxGroup,
// },
// );
// if (created) {
// for (const [properties, coordinates] of features) {
// const p = new Point({ properties, coordinates });
// await p.save();
// }
// }
// return Clone;
// }

// describe('NgwKit', function () {
// this.timeout(15000);

// before(async () => {
// const connection = await getConnection();
// await connection.getOrCreateResource(SandboxGroup, {
// parent: TESTS_GROUP_ID,
// });
// });

// after(async () => {
// const connection = await getConnection();
// await connection.deleteResource(SandboxGroup);
// });

// describe('#fetchNgwLayerItems', () => {
// it(`ilike filter`, async () => {
// const connection = await getConnection();
// const pointLayer = await newPointLayer('NgwKit items');
// const resourceId = pointLayer.item.resource.id;
// const items = await fetchNgwLayerItems<Point, ISandboxPointLayer>({
// connector: connection.driver,
// resourceId,
// filters: [['%test%', 'ilike', 'Fo']],
// });
// expect(items.length).to.be.equal(2);
// });
// });
// });
import { Point, Position } from 'geojson';

import { expect } from 'chai';
import { Connection } from '../../../packages/ngw-orm/src';
import { SandboxGroupNgwKit } from '../../helpers/ngw-orm/SandboxGroupNgwKit';
import {
SandboxPointLayer,
ISandboxPointLayer,
} from '../../helpers/ngw-orm/SandboxPointLayer';
import { fetchNgwLayerItems } from '@nextgis/ngw-kit';

const TESTS_GROUP_ID = 0;
let CONNECTION: Connection;

function getConnection(): Promise<Connection> {
if (CONNECTION) {
return Promise.resolve(CONNECTION);
}
return Connection.connect({
baseUrl: 'https://sandbox.nextgis.com',
auth: {
login: 'administrator',
password: 'demodemo',
},
}).then((connection) => {
CONNECTION = connection;
return connection;
});
}

const features: [ISandboxPointLayer, Position][] = [
[{ number: 12, test: 'foofoo' }, [1, 1]],
[{ number: 11, test: 'FooFoo' }, [0, 1]],
[{ number: 11, test: 'barbar' }, [0, 0]],
[{ number: 11, test: 'BarBar' }, [1, 0]],
[{ number: 11, test: 'FooBar' }, [1, 0]],
[{ number: 11, test: 'foobar' }, [1, 0]],
];

async function newPointLayer(name: string) {
const connection = await getConnection();
const Clone = SandboxPointLayer.clone<typeof SandboxPointLayer>({
display_name: name,
});

const [Point, created] = await connection.getOrCreateResource(Clone, {
parent: SandboxGroupNgwKit,
});
if (created) {
for (const [properties, coordinates] of features) {
const p = new Point({ properties, coordinates });
await p.save();
}
}
return Point;
}

describe('NgwKit', function () {
this.timeout(15000);

before(async () => {
const connection = await getConnection();
await connection.getOrCreateResource(SandboxGroupNgwKit, {
parent: TESTS_GROUP_ID,
});
});

after(async () => {
const connection = await getConnection();
await connection.deleteResource(SandboxGroupNgwKit);
});

describe('#fetchNgwLayerItems', () => {
it(`ilike filter`, async () => {
const connection = await getConnection();
const pointLayer = await newPointLayer('NgwKit items');
const resourceId = pointLayer.item.resource.id;
const items1 = await fetchNgwLayerItems<Point, ISandboxPointLayer>({
connector: connection.driver,
resourceId,
filters: [['%test', 'ilike', 'bar']],
});
expect(items1.length).to.be.equal(4);

const items2 = await fetchNgwLayerItems<Point, ISandboxPointLayer>({
connector: connection.driver,
resourceId,
filters: [['test%', 'ilike', 'foo']],
});
expect(items2.length).to.be.equal(4);

const items3 = await fetchNgwLayerItems<Point, ISandboxPointLayer>({
connector: connection.driver,
resourceId,
filters: [['%test%', 'ilike', 'oB']],
});
expect(items3.length).to.be.equal(2);

const items4 = await fetchNgwLayerItems<Point, ISandboxPointLayer>({
connector: connection.driver,
resourceId,
filters: [['test', 'ilike', 'FooFoo']],
});
expect(items4.length).to.be.equal(2);
});

it(`like filter`, async () => {
const connection = await getConnection();
const pointLayer = await newPointLayer('NgwKit items');
const resourceId = pointLayer.item.resource.id;
const items1 = await fetchNgwLayerItems<Point, ISandboxPointLayer>({
connector: connection.driver,
resourceId,
filters: [['%test', 'like', 'bar']],
});
expect(items1.length).to.be.equal(2);

const items2 = await fetchNgwLayerItems<Point, ISandboxPointLayer>({
connector: connection.driver,
resourceId,
filters: [['test%', 'like', 'foo']],
});
expect(items2.length).to.be.equal(2);

const items3 = await fetchNgwLayerItems<Point, ISandboxPointLayer>({
connector: connection.driver,
resourceId,
filters: [['%test%', 'like', 'oB']],
});
expect(items3.length).to.be.equal(1);

const items4 = await fetchNgwLayerItems<Point, ISandboxPointLayer>({
connector: connection.driver,
resourceId,
filters: [['test', 'like', 'FooFoo']],
});
expect(items4.length).to.be.equal(1);
});
});
});

0 comments on commit 3654c2b

Please sign in to comment.