Skip to content

Commit

Permalink
test: add some more tests for coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
kofrasa committed Oct 9, 2023
1 parent d8d2d97 commit 254afa6
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"vars": "all",
"varsIgnorePattern": "^_",
"args": "after-used",
"argsIgnorePattern": "^(_|options)$"
"argsIgnorePattern": "^_"
}
]
}
Expand Down
54 changes: 41 additions & 13 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export type AccumulatorOperator<R = AnyVal> = (

export type ExpressionOperator<R = AnyVal> = (
obj: RawObject,
expr: AnyVal | RawObject | RawArray,
expr: AnyVal,
options: Options
) => R;

Expand Down Expand Up @@ -344,7 +344,7 @@ export class Context {
this.operators = cloneDeep(ops) as typeof ops;
}

static init(ops: ContextMap): Context {
static init(ops: ContextMap = {}): Context {
return new Context(
merge(
{
Expand All @@ -365,7 +365,7 @@ export class Context {
return new Context(ctx.operators);
}

addOperators(type: OperatorType, ops: OperatorMap): Context {
private addOperators(type: OperatorType, ops: OperatorMap): Context {
for (const [name, fn] of Object.entries(ops)) {
if (!this.getOperator(type, name)) {
(this.operators[type] as OperatorMap)[name] = fn;
Expand Down Expand Up @@ -406,8 +406,8 @@ export class Context {
}
}

// operator definitions
const CONTEXT = Context.init({});
// global context
const GLOBAL_CONTEXT = Context.init();

/**
* Register fully specified operators for the given operator class.
Expand All @@ -428,9 +428,37 @@ export function useOperators(type: OperatorType, operators: OperatorMap): void {
);
}
// toss the operator salad :)
CONTEXT.addOperators(type, operators);
switch (type) {
case OperatorType.ACCUMULATOR:
GLOBAL_CONTEXT.addAccumulatorOps(operators as AccumulatorOps);
break;
case OperatorType.EXPRESSION:
GLOBAL_CONTEXT.addExpressionOps(operators as ExpressionOps);
break;
case OperatorType.PIPELINE:
GLOBAL_CONTEXT.addPipelineOps(operators as PipelineOps);
break;
case OperatorType.PROJECTION:
GLOBAL_CONTEXT.addProjectionOps(operators as ProjectionOps);
break;
case OperatorType.QUERY:
GLOBAL_CONTEXT.addQueryOps(operators as QueryOps);
break;
case OperatorType.WINDOW:
GLOBAL_CONTEXT.addWindowOps(operators as WindowOps);
break;
}
}

/**
* Overrides the current global context with this new one.
*
* @param context The new context to override the global one with.
*/
// export const setGlobalContext = (context: Context): void => {
// GLOBAL_CONTEXT = context;
// };

/**
* Returns the operator function or undefined if it is not found
* @param type Type of operator
Expand All @@ -443,7 +471,7 @@ export function getOperator(
): Operator {
const { context: ctx, useGlobalContext: fallback } = options || {};
const fn = ctx ? (ctx.getOperator(type, operator) as Operator) : null;
return !fn && fallback ? CONTEXT.getOperator(type, operator) : fn;
return !fn && fallback ? GLOBAL_CONTEXT.getOperator(type, operator) : fn;
}

/* eslint-disable unused-imports/no-unused-vars-ts */
Expand All @@ -453,16 +481,16 @@ export function getOperator(
* @type {Object}
*/
const systemVariables: Record<string, typeof redact> = {
$$ROOT(obj: AnyVal, expr: AnyVal, options: ComputeOptions) {
$$ROOT(_obj: AnyVal, _expr: AnyVal, options: ComputeOptions) {
return options.root;
},
$$CURRENT(obj: AnyVal, expr: AnyVal, options: ComputeOptions) {
$$CURRENT(obj: AnyVal, _expr: AnyVal, _options: ComputeOptions) {
return obj;
},
$$REMOVE(obj: AnyVal, expr: AnyVal, options: ComputeOptions) {
$$REMOVE(_obj: AnyVal, _expr: AnyVal, _options: ComputeOptions) {
return undefined;
},
$$NOW(obj: AnyVal, expr: AnyVal, options: ComputeOptions) {
$$NOW(_obj: AnyVal, _expr: AnyVal, options: ComputeOptions) {
return new Date(options.timestamp);
}
};
Expand All @@ -475,10 +503,10 @@ const systemVariables: Record<string, typeof redact> = {
* @type {Object}
*/
const redactVariables: Record<string, typeof redact> = {
$$KEEP(obj: AnyVal, expr: AnyVal, options: ComputeOptions): AnyVal {
$$KEEP(obj: AnyVal, _expr: AnyVal, _options: ComputeOptions): AnyVal {
return obj;
},
$$PRUNE(obj: AnyVal, expr: AnyVal, options: ComputeOptions): AnyVal {
$$PRUNE(_obj: AnyVal, _expr: AnyVal, _options: ComputeOptions): AnyVal {
return undefined;
},
$$DESCEND(obj: RawObject, expr: AnyVal, options: ComputeOptions): AnyVal {
Expand Down
18 changes: 6 additions & 12 deletions src/init/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,11 @@ useOperators(OperatorType.PROJECTION, projectionOperators as OperatorMap);
useOperators(OperatorType.QUERY, queryOperators as OperatorMap);

/** The basic context for queries. */
export const BASIC_CONTEXT = Context.init({
[OperatorType.EXPRESSION]: {
export const BASIC_CONTEXT = Context.init()
.addExpressionOps({
...booleanOperators,
...comparisonOperators
},
[OperatorType.PIPELINE]: {
$project,
$skip,
$limit,
$sort
},
[OperatorType.PROJECTION]: projectionOperators,
[OperatorType.QUERY]: queryOperators
});
})
.addPipelineOps({ $project, $skip, $limit, $sort })
.addProjectionOps(projectionOperators)
.addQueryOps(queryOperators);
3 changes: 2 additions & 1 deletion test/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ describe("core", () => {
};
}

// DEFAULT_OPTS.context.addQueryOps({ $between });
useOperators(OperatorType.QUERY, { $between });

const coll = [
Expand All @@ -113,7 +114,7 @@ describe("core", () => {
});

it("should add accumulator operator", () => {
useOperators(OperatorType.ACCUMULATOR, {
DEFAULT_OPTS.context.addAccumulatorOps({
$stddev: (collection: RawArray, expr: AnyVal, options?: Options) => {
const result = aggregate(
collection,
Expand Down
10 changes: 9 additions & 1 deletion test/lazy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ describe("lazy", () => {
expect(Lazy(data).size()).toBe(data.length);
});

it("throws when source is invalid", () => {
it("should throw when source is invalid", () => {
expect(() => Lazy(5 as unknown as Source)).toThrowError();
});

it("should iterate with for-loop", () => {
let i = 0;
for (const item of Lazy(data)) {
if (typeof item === "number") i++;
}
expect(i).toBe(data.length);
});
});

0 comments on commit 254afa6

Please sign in to comment.