Skip to content

Commit

Permalink
Fix queries over paged collections always being empty
Browse files Browse the repository at this point in the history
This was a regression of #1180 due to the v3 release refactoring.

This commit adds a test to ensure this one does not break again.

Closes #1180
  • Loading branch information
rubensworks committed May 11, 2024
1 parent 69d9298 commit a2e4700
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
12 changes: 12 additions & 0 deletions engines/query-sparql/test/QuerySparql-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,18 @@ SELECT * WHERE {
});
expect(called).toBe(0);
});

it('with two triple patterns over a paged collection', async() => {
const bindingsStream = await engine.queryBindings(`
SELECT *
WHERE {
<https://opendata.picturae.com/dataset/dre_a2a_webservice> <http://purl.org/dc/terms/identifier> ?i.
<https://opendata.picturae.com/dataset/dre_a2a_webservice> <http://purl.org/dc/terms/issued> ?issued.
}`, {
sources: [ 'https://opendata.picturae.com/catalog.ttl?page=1' ],
});
expect((await bindingsStream.toArray()).length > 0).toBeTruthy();
});
});

describe('property paths', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,19 @@ export abstract class LinkedRdfSourcesAsyncRdfIterator extends BufferedIterator<
const bindingsStream = sourceState.source.queryBindings(this.operation, this.context);
bindingsStream.getProperty('metadata', (metadata: MetadataBindings) => {
metadata.state = new MetadataValidationState();
resolve(metadata);
bindingsStream.destroy();
this.accumulateMetadata(sourceState.metadata, metadata)
.then((accumulatedMetadata) => {
// Also merge fields that were not explicitly accumulated
const returnMetadata = { ...sourceState.metadata, ...metadata, ...accumulatedMetadata };
resolve(returnMetadata);
})
.catch(() => {
resolve({
...sourceState.metadata,
state: new MetadataValidationState(),
});
});
});
})
.catch(reject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ describe('ActorQuerySourceIdentifyHypermedia', () => {
const bindings = querySource.source.queryBindings(operation, context);
await expect(new Promise(resolve => bindings.getProperty('metadata', resolve))).resolves
.toEqual({
a: 1,
cardinality: {
type: 'estimate',
value: Number.POSITIVE_INFINITY,
},
state: expect.any(MetadataValidationState),
firstMeta: true,
});
Expand Down Expand Up @@ -217,7 +222,14 @@ describe('ActorQuerySourceIdentifyHypermedia', () => {
const { querySource } = await actor.run({ context, querySourceUnidentified });
const bindings = querySource.source.queryBindings(operation, context);
await expect(new Promise(resolve => bindings.getProperty('metadata', resolve))).resolves
.toEqual({ state: expect.any(MetadataValidationState), firstMeta: true });
.toEqual({
cardinality: {
type: 'estimate',
value: Number.POSITIVE_INFINITY,
},
state: expect.any(MetadataValidationState),
firstMeta: true,
});

expect(spy).toHaveBeenCalledWith({
map: expect.anything(),
Expand Down Expand Up @@ -343,6 +355,11 @@ describe('ActorQuerySourceIdentifyHypermedia', () => {
const bindings = querySource.source.queryBindings(operation, context);
await expect(new Promise(resolve => bindings.getProperty('metadata', resolve))).resolves
.toEqual({
a: 1,
cardinality: {
type: 'estimate',
value: Number.POSITIVE_INFINITY,
},
state: expect.any(MetadataValidationState),
firstMeta: true,
});
Expand Down Expand Up @@ -425,6 +442,11 @@ describe('ActorQuerySourceIdentifyHypermedia', () => {
const bindings = querySource.source.queryBindings(operation, context);
await expect(new Promise(resolve => bindings.getProperty('metadata', resolve))).resolves
.toEqual({
a: 1,
cardinality: {
type: 'estimate',
value: Number.POSITIVE_INFINITY,
},
state: expect.any(MetadataValidationState),
firstMeta: true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,17 @@ describe('LinkedRdfSourcesAsyncRdfIterator', () => {
jest.spyOn(<any> it, 'startIteratorsForNextUrls');

await expect(new Promise(resolve => it.getProperty('metadata', resolve))).resolves.toEqual({
firstPageToken: true,
state: expect.any(MetadataValidationState),
next: 'P1',
requestedPage: 0,
subseq: true,
});

await expect(it).toEqualBindingsStream(data.flat());
});

it('handles metadata for a single page before consuming data and handle errors', async() => {
it('handles metadata for a single page before consuming data and handle source getter errors', async() => {
const sourceStateGetterOld = sourceStateGetter;
let called = false;
sourceStateGetter = async(link: ILink): Promise<ISourceState> => {
Expand All @@ -217,6 +219,24 @@ describe('LinkedRdfSourcesAsyncRdfIterator', () => {
await expect(it).toEqualBindingsStream(data.flat());
});

it('handles metadata for a single page before consuming data and handle accumulateMetadata errors', async() => {
data = toBindings([[
[ 'a', 'b', 'c' ],
[ 'd', 'e', 'f' ],
[ 'g', 'h', 'i' ],
]]);
const it =
new DummyIteratorErrorAccumulate(operation, queryBindingsOptions, context, 'first', sourceStateGetter);
jest.spyOn(<any> it, 'startIteratorsForNextUrls');

await expect(new Promise(resolve => it.getProperty('metadata', resolve))).resolves.toEqual({
firstPageToken: true,
state: expect.any(MetadataValidationState),
next: 'P1',
requestedPage: 0,
});
});

it('handles metadata for a single page after consuming data', async() => {
data = toBindings([[
[ 'a', 'b', 'c' ],
Expand Down Expand Up @@ -250,8 +270,10 @@ describe('LinkedRdfSourcesAsyncRdfIterator', () => {
jest.spyOn(<any> it, 'startIteratorsForNextUrls');

await expect(new Promise(resolve => it.getProperty('metadata', resolve))).resolves.toEqual({
firstPageToken: true,
state: expect.any(MetadataValidationState),
next: 'P1',
requestedPage: 0,
subseq: true,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ describe('QuerySourceHypermedia', () => {
}),
]);
await expect(metaPromise).resolves.toEqual({
a: 1,
cardinality: {
type: 'estimate',
value: Number.POSITIVE_INFINITY,
},
state: expect.any(MetadataValidationState),
firstMeta: true,
});
Expand Down Expand Up @@ -291,8 +296,10 @@ describe('QuerySourceHypermedia', () => {
}),
]);
await expect(metaPromise).resolves.toEqual({
a: 2,
state: expect.any(MetadataValidationState),
cardinality: { type: 'exact', value: 1 },
cardinality: { type: 'exact', value: 2 },
canContainUndefs: false,
firstMeta: true,
});
});
Expand Down Expand Up @@ -731,6 +738,7 @@ describe('QuerySourceHypermedia', () => {

expect(it1Meta).toHaveBeenCalledTimes(4);
expect(it1Meta).toHaveBeenNthCalledWith(1, {
a: 1,
state: expect.any(MetadataValidationState),
firstMeta: true,
cardinality: { type: 'exact', value: 2 },
Expand Down

0 comments on commit a2e4700

Please sign in to comment.