Skip to content

Commit 1af7777

Browse files
authored
add method, add int and unit tests for method, refactor types (#90)
1 parent 85962c1 commit 1af7777

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

src/data/checker.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import Connection from '../connection';
2+
import { ConsistencyLevel } from './replication';
23
import { ObjectsPath } from './path';
34
import { CommandBase } from '../validation/commandBase';
45

56
export default class Checker extends CommandBase {
67
private className!: string;
8+
private consistencyLevel?: ConsistencyLevel;
79
private id!: string;
810
private tenant?: string;
911
private objectsPath: ObjectsPath;
@@ -28,6 +30,15 @@ export default class Checker extends CommandBase {
2830
return this;
2931
};
3032

33+
withConsistencyLevel = (consistencyLevel: ConsistencyLevel) => {
34+
this.consistencyLevel = consistencyLevel;
35+
return this;
36+
};
37+
38+
buildPath = () => {
39+
return this.objectsPath.buildCheck(this.id, this.className, this.consistencyLevel, this.tenant);
40+
};
41+
3142
validateIsSet = (prop: string | undefined | null, name: string, setter: string) => {
3243
if (prop == undefined || prop == null || prop.length == 0) {
3344
this.addError(`${name} must be set - set with ${setter}`);
@@ -42,14 +53,12 @@ export default class Checker extends CommandBase {
4253
this.validateId();
4354
};
4455

45-
do = () => {
56+
do = (): Promise<boolean> => {
4657
if (this.errors.length > 0) {
4758
return Promise.reject(new Error('invalid usage: ' + this.errors.join(', ')));
4859
}
4960
this.validate();
5061

51-
return this.objectsPath.buildCheck(this.id, this.className, this.tenant).then((path: string) => {
52-
return this.client.head(path, undefined);
53-
});
62+
return this.buildPath().then((path: string) => this.client.head(path, undefined));
5463
};
5564
}

src/data/journey.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const thingClassName = 'DataJourneyTestThing';
1414
const refSourceClassName = 'DataJourneyTestRefSource';
1515
const classCustomVectorClassName = 'ClassCustomVector';
1616

17+
const fail = (msg: string) => {
18+
throw new Error(msg);
19+
};
20+
1721
describe('data', () => {
1822
const client = weaviate.client({
1923
scheme: 'http',
@@ -699,6 +703,23 @@ describe('data', () => {
699703
});
700704
});
701705

706+
it('forms a exists query with consistency_level set', () => {
707+
const id = '00000000-0000-0000-0000-000000000000';
708+
709+
return client.data
710+
.checker()
711+
.withClassName(thingClassName)
712+
.withId(id)
713+
.withConsistencyLevel('QUORUM')
714+
.buildPath()
715+
.then((path: string) => {
716+
expect(path).toContain('consistency_level=QUORUM');
717+
})
718+
.catch((e: WeaviateError) => {
719+
throw new Error('it should not have errord: ' + e);
720+
});
721+
});
722+
702723
it('creates object with consistency_level set', async () => {
703724
const id = '144d1944-3ab4-4aa1-8095-92429d6cbaba';
704725
const properties = { foo: 'bar' };
@@ -1078,6 +1099,33 @@ describe('data', () => {
10781099
.catch((e) => fail('it should not have errord: ' + e));
10791100
});
10801101

1102+
it('checks an object exists with consistency_level set', async () => {
1103+
const id = 'e7c7f6d5-4c9d-4a4e-8e1b-9d3d5a0e4d9f';
1104+
const props = { stringProp: 'foobar' };
1105+
1106+
await client.data
1107+
.creator()
1108+
.withClassName(thingClassName)
1109+
.withProperties(props)
1110+
.withId(id)
1111+
.do()
1112+
.then((res) => {
1113+
expect(res.properties).toEqual(props);
1114+
expect(res.id).toEqual(id);
1115+
})
1116+
.catch((e) => fail('it should not have errord: ' + e));
1117+
1118+
return client.data
1119+
.checker()
1120+
.withId(id)
1121+
.withConsistencyLevel('QUORUM')
1122+
.do()
1123+
.then((exists) => {
1124+
expect(exists).toBe(true);
1125+
})
1126+
.catch((e) => fail('it should not have errord: ' + e));
1127+
});
1128+
10811129
it('tears down and cleans up', () => {
10821130
return Promise.all([
10831131
client.schema.classDeleter().withClassName(thingClassName).do(),

src/data/path.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ export class ObjectsPath {
2222
this.addQueryParams,
2323
]);
2424
}
25-
buildCheck(id: string, className: string, tenant?: string): Promise<string> {
26-
return this.build({ id, className, tenant: tenant }, [
25+
buildCheck(
26+
id: string,
27+
className: string,
28+
consistencyLevel?: ConsistencyLevel,
29+
tenant?: string
30+
): Promise<string> {
31+
return this.build({ id, className, consistencyLevel, tenant }, [
2732
this.addClassNameDeprecatedNotSupportedCheck,
2833
this.addId,
2934
this.addQueryParams,

0 commit comments

Comments
 (0)