|
| 1 | +import { ActorQueryOperation } from '@comunica/bus-query-operation'; |
| 2 | +import { ActionContext, Bus } from '@comunica/core'; |
| 3 | +import type { IJoinEntry } from '@comunica/types'; |
| 4 | +import { BindingsFactory } from '@comunica/utils-bindings-factory'; |
| 5 | +import { MetadataValidationState } from '@comunica/utils-metadata'; |
| 6 | +import { getSafeBindings } from '@comunica/utils-query-operation'; |
| 7 | +import { ArrayIterator, UnionIterator } from 'asynciterator'; |
| 8 | +import { DataFactory } from 'rdf-data-factory'; |
| 9 | +import { ActorQueryOperationJoin } from '../lib/ActorQueryOperationJoin'; |
| 10 | +import '@comunica/utils-jest'; |
| 11 | + |
| 12 | +const DF = new DataFactory(); |
| 13 | +const BF = new BindingsFactory(DF); |
| 14 | + |
| 15 | +describe('ActorQueryOperationJoin', () => { |
| 16 | + let bus: any; |
| 17 | + let mediatorQueryOperation: any; |
| 18 | + let mediatorJoin: any; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + bus = new Bus({ name: 'bus' }); |
| 22 | + mediatorQueryOperation = { |
| 23 | + mediate: (arg: any) => Promise.resolve({ |
| 24 | + bindingsStream: new ArrayIterator([ |
| 25 | + BF.bindings([[ DF.variable('a'), DF.literal('1') ]]), |
| 26 | + BF.bindings([[ DF.variable('a'), DF.literal('2') ]]), |
| 27 | + BF.bindings([[ DF.variable('a'), DF.literal('3') ]]), |
| 28 | + ], { autoStart: false }), |
| 29 | + metadata: () => Promise.resolve({ |
| 30 | + state: new MetadataValidationState(), |
| 31 | + cardinality: { type: 'exact', value: 3 }, |
| 32 | + variables: [{ variable: DF.variable('a'), canBeUndef: false }], |
| 33 | + }), |
| 34 | + operated: arg, |
| 35 | + type: 'bindings', |
| 36 | + }), |
| 37 | + }; |
| 38 | + mediatorJoin = { |
| 39 | + mediate: (arg: any) => Promise.resolve({ |
| 40 | + bindingsStream: new UnionIterator(arg.entries.map((entry: IJoinEntry) => entry.output.bindingsStream)), |
| 41 | + metadata: () => Promise.resolve({ |
| 42 | + state: new MetadataValidationState(), |
| 43 | + cardinality: { type: 'exact', value: 100 }, |
| 44 | + variables: [ |
| 45 | + { variable: DF.variable('a'), canBeUndef: false }, |
| 46 | + { variable: DF.variable('b'), canBeUndef: false }, |
| 47 | + ], |
| 48 | + }), |
| 49 | + operated: arg, |
| 50 | + type: 'bindings', |
| 51 | + }), |
| 52 | + }; |
| 53 | + }); |
| 54 | + |
| 55 | + describe('The ActorQueryOperationJoin module', () => { |
| 56 | + it('should be a function', () => { |
| 57 | + expect(ActorQueryOperationJoin).toBeInstanceOf(Function); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should be a ActorQueryOperationJoin constructor', () => { |
| 61 | + expect(new (<any> ActorQueryOperationJoin)({ name: 'actor', bus, mediatorQueryOperation, mediatorJoin })) |
| 62 | + .toBeInstanceOf(ActorQueryOperationJoin); |
| 63 | + expect(new (<any> ActorQueryOperationJoin)({ name: 'actor', bus, mediatorQueryOperation, mediatorJoin })) |
| 64 | + .toBeInstanceOf(ActorQueryOperation); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should not be able to create new ActorQueryOperationJoin objects without \'new\'', () => { |
| 68 | + expect(() => { |
| 69 | + (<any> ActorQueryOperationJoin)(); |
| 70 | + }).toThrow(`Class constructor ActorQueryOperationJoin cannot be invoked without 'new'`); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('An ActorQueryOperationJoin instance', () => { |
| 75 | + let actor: ActorQueryOperationJoin; |
| 76 | + |
| 77 | + beforeEach(() => { |
| 78 | + actor = new ActorQueryOperationJoin({ name: 'actor', bus, mediatorQueryOperation, mediatorJoin }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should test on join', async() => { |
| 82 | + const op: any = { operation: { type: 'join' }}; |
| 83 | + await expect(actor.test(op)).resolves.toPassTestVoid(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should not test on non-join', async() => { |
| 87 | + const op: any = { operation: { type: 'some-other-type' }}; |
| 88 | + await expect(actor.test(op)).resolves.toFailTest(`Actor actor only supports join operations, but got some-other-type`); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should run', async() => { |
| 92 | + const op: any = { operation: { type: 'join', input: [{}, {}, {}]}, context: new ActionContext() }; |
| 93 | + const output = getSafeBindings(await actor.run(op, undefined)); |
| 94 | + expect(output.type).toBe('bindings'); |
| 95 | + await expect(output.metadata()).resolves.toEqual({ |
| 96 | + state: expect.any(MetadataValidationState), |
| 97 | + cardinality: { type: 'exact', value: 100 }, |
| 98 | + variables: [ |
| 99 | + { variable: DF.variable('a'), canBeUndef: false }, |
| 100 | + { variable: DF.variable('b'), canBeUndef: false }, |
| 101 | + ], |
| 102 | + }); |
| 103 | + await expect(output.bindingsStream).toEqualBindingsStream([ |
| 104 | + BF.bindings([[ DF.variable('a'), DF.literal('1') ]]), |
| 105 | + BF.bindings([[ DF.variable('a'), DF.literal('1') ]]), |
| 106 | + BF.bindings([[ DF.variable('a'), DF.literal('1') ]]), |
| 107 | + BF.bindings([[ DF.variable('a'), DF.literal('2') ]]), |
| 108 | + BF.bindings([[ DF.variable('a'), DF.literal('2') ]]), |
| 109 | + BF.bindings([[ DF.variable('a'), DF.literal('2') ]]), |
| 110 | + BF.bindings([[ DF.variable('a'), DF.literal('3') ]]), |
| 111 | + BF.bindings([[ DF.variable('a'), DF.literal('3') ]]), |
| 112 | + BF.bindings([[ DF.variable('a'), DF.literal('3') ]]), |
| 113 | + ]); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should run when one of the join entries has an estimated cardinality of 0', async() => { |
| 117 | + mediatorQueryOperation.mediate = (arg: any) => Promise.resolve({ |
| 118 | + bindingsStream: new ArrayIterator([ |
| 119 | + BF.bindings([[ DF.variable('a'), DF.literal('1') ]]), |
| 120 | + BF.bindings([[ DF.variable('a'), DF.literal('2') ]]), |
| 121 | + BF.bindings([[ DF.variable('a'), DF.literal('3') ]]), |
| 122 | + ], { autoStart: false }), |
| 123 | + metadata: () => Promise.resolve({ |
| 124 | + cardinality: { type: 'estimate', value: 0 }, |
| 125 | + variables: [{ variable: DF.variable('a'), canBeUndef: false }], |
| 126 | + }), |
| 127 | + operated: arg, |
| 128 | + type: 'bindings', |
| 129 | + }); |
| 130 | + |
| 131 | + const op: any = { operation: { type: 'join', input: [{}, {}, {}]}, context: new ActionContext() }; |
| 132 | + const output = getSafeBindings(await actor.run(op, undefined)); |
| 133 | + expect(output.type).toBe('bindings'); |
| 134 | + await expect(output.metadata()).resolves.toEqual({ |
| 135 | + state: expect.any(MetadataValidationState), |
| 136 | + cardinality: { type: 'exact', value: 100 }, |
| 137 | + variables: [ |
| 138 | + { variable: DF.variable('a'), canBeUndef: false }, |
| 139 | + { variable: DF.variable('b'), canBeUndef: false }, |
| 140 | + ], |
| 141 | + }); |
| 142 | + await expect(output.bindingsStream).toEqualBindingsStream([ |
| 143 | + BF.bindings([[ DF.variable('a'), DF.literal('1') ]]), |
| 144 | + BF.bindings([[ DF.variable('a'), DF.literal('1') ]]), |
| 145 | + BF.bindings([[ DF.variable('a'), DF.literal('1') ]]), |
| 146 | + BF.bindings([[ DF.variable('a'), DF.literal('2') ]]), |
| 147 | + BF.bindings([[ DF.variable('a'), DF.literal('2') ]]), |
| 148 | + BF.bindings([[ DF.variable('a'), DF.literal('2') ]]), |
| 149 | + BF.bindings([[ DF.variable('a'), DF.literal('3') ]]), |
| 150 | + BF.bindings([[ DF.variable('a'), DF.literal('3') ]]), |
| 151 | + BF.bindings([[ DF.variable('a'), DF.literal('3') ]]), |
| 152 | + ]); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments