Skip to content

Commit 0e6148f

Browse files
committed
Fix parsing of data and float filters. Add unit tests for all filter types
1 parent 0a51d72 commit 0e6148f

File tree

2 files changed

+243
-3
lines changed

2 files changed

+243
-3
lines changed

src/collections/serialize/index.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class FilterGuards {
140140
};
141141

142142
static isFloat = (argument?: FilterValueType): argument is number => {
143-
return typeof argument === 'number';
143+
return typeof argument === 'number' && !Number.isInteger(argument);
144144
};
145145

146146
static isFloatArray = (argument?: FilterValueType): argument is number[] => {
@@ -793,8 +793,8 @@ export class Serialize {
793793
return FiltersGRPC.fromPartial({
794794
operator: Serialize.operator(filters.operator),
795795
target: filters.target,
796-
valueText: FilterGuards.isText(value) ? value : undefined,
797-
valueTextArray: FilterGuards.isTextArray(value) ? { values: value } : undefined,
796+
valueText: this.filtersGRPCValueText(value),
797+
valueTextArray: this.filtersGRPCValueTextArray(value),
798798
valueInt: FilterGuards.isInt(value) ? value : undefined,
799799
valueIntArray: FilterGuards.isIntArray(value) ? { values: value } : undefined,
800800
valueNumber: FilterGuards.isFloat(value) ? value : undefined,
@@ -806,6 +806,26 @@ export class Serialize {
806806
}
807807
};
808808

809+
private static filtersGRPCValueText = (value: any) => {
810+
if (FilterGuards.isText(value)) {
811+
return value;
812+
} else if (FilterGuards.isDate(value)) {
813+
return value.toISOString();
814+
} else {
815+
return undefined;
816+
}
817+
};
818+
819+
private static filtersGRPCValueTextArray = (value: any) => {
820+
if (FilterGuards.isTextArray(value)) {
821+
return { values: value };
822+
} else if (FilterGuards.isDateArray(value)) {
823+
return { values: value.map((v) => v.toISOString()) };
824+
} else {
825+
return undefined;
826+
}
827+
};
828+
809829
private static filterTargetToREST = (target: FilterTarget): string[] => {
810830
if (target.property) {
811831
return [target.property];

src/collections/serialize/unit.test.ts

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
NearVideoSearch,
3333
PropertiesRequest,
3434
} from '../../proto/v1/search_get.js';
35+
import { Filters as FiltersFactory } from '../filters/classes.js';
3536
import filter from '../filters/index.js';
3637
import { Reference } from '../references/index.js';
3738
import sort from '../sort/index.js';
@@ -489,6 +490,225 @@ describe('Unit testing of Serialize', () => {
489490
],
490491
});
491492
});
493+
494+
describe('.filtersGRPC', () => {
495+
it('should parse a text property', () => {
496+
const f = filter<any>().byProperty('name').equal('test');
497+
const args = Serialize.filtersGRPC(f);
498+
expect(args).toEqual<Filters>({
499+
operator: Filters_Operator.OPERATOR_EQUAL,
500+
on: [],
501+
filters: [],
502+
target: {
503+
property: 'name',
504+
},
505+
valueText: 'test',
506+
});
507+
});
508+
it('should parse a text array property', () => {
509+
const f = filter<any>().byProperty('name').equal(['test1', 'test2']);
510+
const args = Serialize.filtersGRPC(f);
511+
expect(args).toEqual<Filters>({
512+
operator: Filters_Operator.OPERATOR_EQUAL,
513+
on: [],
514+
filters: [],
515+
target: {
516+
property: 'name',
517+
},
518+
valueTextArray: { values: ['test1', 'test2'] },
519+
});
520+
});
521+
it('should parse an int property', () => {
522+
const f = filter<any>().byProperty('age').equal(10);
523+
const args = Serialize.filtersGRPC(f);
524+
expect(args).toEqual<Filters>({
525+
operator: Filters_Operator.OPERATOR_EQUAL,
526+
on: [],
527+
filters: [],
528+
target: {
529+
property: 'age',
530+
},
531+
valueInt: 10,
532+
});
533+
});
534+
it('should parse an int array property', () => {
535+
const f = filter<any>().byProperty('age').equal([10, 20]);
536+
const args = Serialize.filtersGRPC(f);
537+
expect(args).toEqual<Filters>({
538+
operator: Filters_Operator.OPERATOR_EQUAL,
539+
on: [],
540+
filters: [],
541+
target: {
542+
property: 'age',
543+
},
544+
valueIntArray: { values: [10, 20] },
545+
});
546+
});
547+
it('should parse a float property', () => {
548+
const f = filter<any>().byProperty('height').equal(1.8);
549+
const args = Serialize.filtersGRPC(f);
550+
expect(args).toEqual<Filters>({
551+
operator: Filters_Operator.OPERATOR_EQUAL,
552+
on: [],
553+
filters: [],
554+
target: {
555+
property: 'height',
556+
},
557+
valueNumber: 1.8,
558+
});
559+
});
560+
it('should parse a float array property', () => {
561+
const f = filter<any>().byProperty('height').equal([1.8, 2.8]);
562+
const args = Serialize.filtersGRPC(f);
563+
expect(args).toEqual<Filters>({
564+
operator: Filters_Operator.OPERATOR_EQUAL,
565+
on: [],
566+
filters: [],
567+
target: {
568+
property: 'height',
569+
},
570+
valueNumberArray: { values: [1.8, 2.8] },
571+
});
572+
});
573+
it('should parse a boolean property', () => {
574+
const f = filter<any>().byProperty('isHappy').equal(true);
575+
const args = Serialize.filtersGRPC(f);
576+
expect(args).toEqual<Filters>({
577+
operator: Filters_Operator.OPERATOR_EQUAL,
578+
on: [],
579+
filters: [],
580+
target: {
581+
property: 'isHappy',
582+
},
583+
valueBoolean: true,
584+
});
585+
});
586+
it('should parse a boolean array property', () => {
587+
const f = filter<any>().byProperty('isHappy').equal([true, false]);
588+
const args = Serialize.filtersGRPC(f);
589+
expect(args).toEqual<Filters>({
590+
operator: Filters_Operator.OPERATOR_EQUAL,
591+
on: [],
592+
filters: [],
593+
target: {
594+
property: 'isHappy',
595+
},
596+
valueBooleanArray: { values: [true, false] },
597+
});
598+
});
599+
it('should parse a date property', () => {
600+
const date = new Date();
601+
const f = filter<any>().byProperty('birthday').equal(date);
602+
const args = Serialize.filtersGRPC(f);
603+
expect(args).toEqual<Filters>({
604+
operator: Filters_Operator.OPERATOR_EQUAL,
605+
on: [],
606+
filters: [],
607+
target: {
608+
property: 'birthday',
609+
},
610+
valueText: date.toISOString(),
611+
});
612+
});
613+
it('should parse a date array property', () => {
614+
const date1 = new Date();
615+
const date2 = new Date();
616+
const f = filter<any>().byProperty('birthday').equal([date1, date2]);
617+
const args = Serialize.filtersGRPC(f);
618+
expect(args).toEqual<Filters>({
619+
operator: Filters_Operator.OPERATOR_EQUAL,
620+
on: [],
621+
filters: [],
622+
target: {
623+
property: 'birthday',
624+
},
625+
valueTextArray: { values: [date1.toISOString(), date2.toISOString()] },
626+
});
627+
});
628+
it('should parse a geo property', () => {
629+
const f = filter<any>()
630+
.byProperty('location')
631+
.withinGeoRange({ latitude: 1, longitude: 1, distance: 1 });
632+
const args = Serialize.filtersGRPC(f);
633+
expect(args).toEqual<Filters>({
634+
operator: Filters_Operator.OPERATOR_WITHIN_GEO_RANGE,
635+
on: [],
636+
filters: [],
637+
target: {
638+
property: 'location',
639+
},
640+
valueGeo: {
641+
distance: 1,
642+
latitude: 1,
643+
longitude: 1,
644+
},
645+
});
646+
});
647+
it('should parse several filters in a Filters.and', () => {
648+
const f = FiltersFactory.and(
649+
filter<any>().byProperty('name').equal('test'),
650+
filter<any>().byProperty('age').equal(10)
651+
);
652+
const args = Serialize.filtersGRPC(f);
653+
expect(args).toEqual<Filters>({
654+
operator: Filters_Operator.OPERATOR_AND,
655+
on: [],
656+
target: undefined,
657+
filters: [
658+
{
659+
operator: Filters_Operator.OPERATOR_EQUAL,
660+
on: [],
661+
filters: [],
662+
target: {
663+
property: 'name',
664+
},
665+
valueText: 'test',
666+
},
667+
{
668+
operator: Filters_Operator.OPERATOR_EQUAL,
669+
on: [],
670+
filters: [],
671+
target: {
672+
property: 'age',
673+
},
674+
valueInt: 10,
675+
},
676+
],
677+
});
678+
});
679+
it('should parse several filters in a Filters.or', () => {
680+
const f = FiltersFactory.or(
681+
filter<any>().byProperty('name').equal('test'),
682+
filter<any>().byProperty('age').equal(10)
683+
);
684+
const args = Serialize.filtersGRPC(f);
685+
expect(args).toEqual<Filters>({
686+
operator: Filters_Operator.OPERATOR_OR,
687+
on: [],
688+
target: undefined,
689+
filters: [
690+
{
691+
operator: Filters_Operator.OPERATOR_EQUAL,
692+
on: [],
693+
filters: [],
694+
target: {
695+
property: 'name',
696+
},
697+
valueText: 'test',
698+
},
699+
{
700+
operator: Filters_Operator.OPERATOR_EQUAL,
701+
on: [],
702+
filters: [],
703+
target: {
704+
property: 'age',
705+
},
706+
valueInt: 10,
707+
},
708+
],
709+
});
710+
});
711+
});
492712
});
493713

494714
describe('Unit testing of DataGuards', () => {

0 commit comments

Comments
 (0)