Skip to content

Commit dae3f4c

Browse files
committed
fix addDocument
1 parent 6c8098d commit dae3f4c

File tree

8 files changed

+687
-10
lines changed

8 files changed

+687
-10
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
//@ts-check
2+
import {describe, expect, test, beforeAll} from '@jest/globals';
3+
//import WOQLClient from '../lib/woqlClient';
4+
import {WOQLClient} from '@terminusdb/terminusdb-client';
5+
import { DbDetails, DocParamsGet } from '@terminusdb/terminusdb-client/dist/typescript/lib/typedef';
6+
//import {ParamsObj,DbDetails} from '../lib/typedef';
7+
import schemaJson from './persons_schema'
8+
//console.log(typeof schemaJson)
9+
10+
let client : WOQLClient //= new WOQLClient('http://localhost:6363');
11+
12+
beforeAll(() => {
13+
client = new WOQLClient("http://localhost:6363",{ user: 'admin', organization: 'admin', key: 'root' })
14+
});
15+
16+
const db01 = 'db__test';
17+
18+
describe('Create a database, schema and insert data', () => {
19+
test('Create a database', async () => {
20+
const dbObj : DbDetails = { label: db01, comment: 'add db', schema: true }
21+
const result = await client.createDatabase(db01,dbObj);
22+
//woqlClient return only the data no status
23+
expect(result["@type"]).toEqual("api:DbCreateResponse");
24+
expect(result["api:status"]).toEqual("api:success");
25+
});
26+
27+
test('Create a schema', async () => {
28+
const result = await client.addDocument(schemaJson,{graph_type:"schema",full_replace:true});
29+
expect(result).toStrictEqual(["Child", "Person", "Parent" ]);
30+
})
31+
32+
test('Insert Document Child Tom', async () => {
33+
const person = {"age":10,"name":"Tom","@type":"Child"}
34+
const result = await client.addDocument(person);
35+
expect(result).toStrictEqual(["terminusdb:///data/Child/Tom" ]);
36+
})
37+
38+
test('Insert Document Child Anna', async () => {
39+
const person = {"age":20,"name":"Anna","@type":"Child"}
40+
const result = await client.addDocument(person);
41+
expect(result).toStrictEqual(["terminusdb:///data/Child/Anna" ]);
42+
})
43+
44+
test('Insert Document Parent Tom Senior', async () => {
45+
const person = {"age":40,"name":"Tom Senior","@type":"Parent" , "has_child":"Child/Tom"}
46+
const result = await client.addDocument(person);
47+
expect(result).toStrictEqual(["terminusdb:///data/Parent/Tom%20Senior" ]);
48+
})
49+
50+
test('Query Person by name', async () => {
51+
const queryTemplate = {"name":"Tom", "@type":"Person" }
52+
const result = await client.getDocument({query:queryTemplate});
53+
expect(result).toStrictEqual({ '@id': 'Child/Tom', '@type': 'Child', age: 10, name: 'Tom' });
54+
})
55+
56+
test('Query Person by ege', async () => {
57+
const queryTemplate = {"age":40, "@type":"Person" }
58+
const result = await client.getDocument({query:queryTemplate});
59+
expect(result).toStrictEqual({"@id": "Parent/Tom%20Senior", "age":40,"name":"Tom Senior","@type":"Parent" , "has_child":"Child/Tom"});
60+
})
61+
62+
const change_request = "change_request02";
63+
64+
test('Create Branch change_request', async () => {
65+
const result = await client.branch(change_request);
66+
expect(result).toStrictEqual({ '@type': 'api:BranchResponse', 'api:status': 'api:success' });
67+
})
68+
69+
test('Checkout Branch change_request', async () => {
70+
client.checkout(change_request)
71+
expect(client.checkout()).toStrictEqual(change_request);
72+
})
73+
74+
test('Update Child Tom, link Parent', async () => {
75+
const childTom = { '@id': 'Child/Tom', '@type': 'Child', age: 10, name: 'Tom' , has_parent:"Parent/Tom%20Senior"}
76+
const result = await client.updateDocument(childTom);
77+
expect(result).toStrictEqual(["terminusdb:///data/Child/Tom" ]);
78+
})
79+
80+
test('Diff beetwen main and change_request branch', async () => {
81+
const result = await client.getVersionDiff("main",change_request);
82+
expect(result).toStrictEqual([
83+
{
84+
'@id': 'Child/Tom',
85+
has_parent: {
86+
'@after': 'Parent/Tom%20Senior',
87+
'@before': null,
88+
'@op': 'SwapValue'
89+
}
90+
}
91+
]);
92+
})
93+
94+
test('Checkout Branch change_request', async () => {
95+
client.checkout("main")
96+
expect(client.checkout()).toStrictEqual("main");
97+
})
98+
99+
test('Merge beetwen main and change_request branch', async () => {
100+
const result = await client.apply("main", change_request, "merge change_request to main");
101+
expect(result).toStrictEqual( { '@type': 'api:ApplyResponse', 'api:status': 'api:success' });
102+
})
103+
104+
test('Check if merge worked. Query Person by age in main branch', async () => {
105+
const queryTemplate = {"age":"10", "@type":"Person" }
106+
const result = await client.getDocument({query:queryTemplate});
107+
//console.log(result)
108+
expect(result).toStrictEqual({
109+
'@id': 'Child/Tom',
110+
'@type': 'Child',
111+
age: 10,
112+
name: 'Tom',
113+
has_parent: 'Parent/Tom%20Senior'
114+
});
115+
})
116+
117+
test('Delete a branch', async () => {
118+
const result = await client.deleteBranch(change_request);
119+
expect(result).toStrictEqual({ '@type': 'api:BranchResponse', 'api:status': 'api:success' });
120+
});
121+
122+
test('Delete a database', async () => {
123+
const result = await client.deleteDatabase(db01);
124+
expect(result).toStrictEqual({ '@type': 'api:DbDeleteResponse', 'api:status': 'api:success' });
125+
});
126+
});

jest.config.ts

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
* For a detailed explanation regarding each configuration property, visit:
3+
* https://jestjs.io/docs/configuration
4+
*/
5+
6+
module.exports = {
7+
"testResultsProcessor": "./node_modules/jest-html-reporter",
8+
9+
// All imported modules in your tests should be mocked automatically
10+
// automock: false,
11+
12+
// Stop running tests after `n` failures
13+
// bail: 0,
14+
15+
// The directory where Jest should store its cached dependency information
16+
// cacheDirectory: "/private/var/folders/jt/3zsm_ptd56d_714rnyb9qsg80000gn/T/jest_dx",
17+
18+
// Automatically clear mock calls, instances, contexts and results before every test
19+
clearMocks: true,
20+
21+
// Indicates whether the coverage information should be collected while executing the test
22+
collectCoverage: true,
23+
24+
// An array of glob patterns indicating a set of files for which coverage information should be collected
25+
// collectCoverageFrom: undefined,
26+
27+
// The directory where Jest should output its coverage files
28+
coverageDirectory: "coverage",
29+
30+
// An array of regexp pattern strings used to skip coverage collection
31+
// coveragePathIgnorePatterns: [
32+
// "/node_modules/"
33+
// ],
34+
35+
// Indicates which provider should be used to instrument code for coverage
36+
coverageProvider: "v8",
37+
38+
// A list of reporter names that Jest uses when writing coverage reports
39+
// coverageReporters: [
40+
// "json",
41+
// "text",
42+
// "lcov",
43+
// "clover"
44+
// ],
45+
46+
// An object that configures minimum threshold enforcement for coverage results
47+
// coverageThreshold: undefined,
48+
49+
// A path to a custom dependency extractor
50+
// dependencyExtractor: undefined,
51+
52+
// Make calling deprecated APIs throw helpful error messages
53+
// errorOnDeprecated: false,
54+
55+
// The default configuration for fake timers
56+
// fakeTimers: {
57+
// "enableGlobally": false
58+
// },
59+
60+
// Force coverage collection from ignored files using an array of glob patterns
61+
// forceCoverageMatch: [],
62+
63+
// A path to a module which exports an async function that is triggered once before all test suites
64+
// globalSetup: undefined,
65+
66+
// A path to a module which exports an async function that is triggered once after all test suites
67+
// globalTeardown: undefined,
68+
69+
// A set of global variables that need to be available in all test environments
70+
// globals: {},
71+
72+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
73+
// maxWorkers: "50%",
74+
75+
// An array of directory names to be searched recursively up from the requiring module's location
76+
// moduleDirectories: [
77+
// "node_modules"
78+
// ],
79+
80+
// An array of file extensions your modules use
81+
// moduleFileExtensions: [
82+
// "js",
83+
// "mjs",
84+
// "cjs",
85+
// "jsx",
86+
// "ts",
87+
// "tsx",
88+
// "json",
89+
// "node"
90+
// ],
91+
92+
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
93+
// moduleNameMapper: {},
94+
95+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
96+
// modulePathIgnorePatterns: [],
97+
98+
// Activates notifications for test results
99+
// notify: false,
100+
101+
// An enum that specifies notification mode. Requires { notify: true }
102+
// notifyMode: "failure-change",
103+
104+
// A preset that is used as a base for Jest's configuration
105+
// preset: undefined,
106+
preset:"ts-jest",
107+
108+
// Run tests from one or more projects
109+
// projects: undefined,
110+
111+
// Use this configuration option to add custom reporters to Jest
112+
// reporters: undefined,
113+
114+
// Automatically reset mock state before every test
115+
// resetMocks: false,
116+
117+
// Reset the module registry before running each individual test
118+
// resetModules: false,
119+
120+
// A path to a custom resolver
121+
// resolver: undefined,
122+
123+
// Automatically restore mock state and implementation before every test
124+
// restoreMocks: false,
125+
126+
// The root directory that Jest should scan for tests and modules within
127+
// rootDir: undefined,
128+
129+
// A list of paths to directories that Jest should use to search for files in
130+
// roots: [
131+
// "<rootDir>"
132+
// ],
133+
134+
// Allows you to use a custom runner instead of Jest's default test runner
135+
// runner: "jest-runner",
136+
137+
// The paths to modules that run some code to configure or set up the testing environment before each test
138+
// setupFiles: [],
139+
140+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
141+
// setupFilesAfterEnv: [],
142+
143+
// The number of seconds after which a test is considered as slow and reported as such in the results.
144+
// slowTestThreshold: 5,
145+
146+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
147+
// snapshotSerializers: [],
148+
149+
// The test environment that will be used for testing
150+
// testEnvironment: "jest-environment-node",
151+
152+
// Options that will be passed to the testEnvironment
153+
// testEnvironmentOptions: {},
154+
155+
// Adds a location field to test results
156+
// testLocationInResults: false,
157+
158+
// The glob patterns Jest uses to detect test files
159+
"testMatch": ["<rootDir>/integration_test/*.test.ts"],
160+
161+
// testMatch: [
162+
// "**/__tests__/**/*.[jt]s?(x)",
163+
// "**/?(*.)+(spec|test).[tj]s?(x)"
164+
// ],
165+
166+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
167+
// testPathIgnorePatterns: [
168+
// "/node_modules/"
169+
// ],
170+
171+
// The regexp pattern or array of patterns that Jest uses to detect test files
172+
// testRegex: [],
173+
174+
// This option allows the use of a custom results processor
175+
// testResultsProcessor: undefined,
176+
177+
// This option allows use of a custom test runner
178+
// testRunner: "jest-circus/runner",
179+
180+
// A map from regular expressions to paths to transformers
181+
// transform: undefined,
182+
183+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
184+
// transformIgnorePatterns: [
185+
// "/node_modules/",
186+
// "\\.pnp\\.[^\\/]+$"
187+
// ],
188+
189+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
190+
// unmockedModulePathPatterns: undefined,
191+
192+
// Indicates whether each individual test should be reported during the run
193+
// verbose: undefined,
194+
195+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
196+
// watchPathIgnorePatterns: [],
197+
198+
// Whether to use watchman for file crawling
199+
// watchman: true,
200+
};

lib/connectionConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// /@ts-check
22
// connectionConfig
3-
const { encodeURISegment, URIEncodePayload } = require('./utils');
3+
const { encodeURISegment } = require('./utils');
44

55
/**
66
* @file Terminus DB connection configuration

lib/dispatchRequest.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
151151
case CONST.GET: {
152152
if (payload) {
153153
const ext = UTILS.URIEncodePayload(payload);
154-
console.log('getPayload', payload, ext);
155154
// eslint-disable-next-line no-param-reassign
156155
if (ext) url += `?${ext}`;
157156
}

lib/typedef.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { ACTIONS } = Utils.ACTIONS;
99

1010
/**
1111
*@typedef {Object} DocParamsGet - the GET document interface query parameters
12+
*@property {object} [query] - object that descrive the document query
1213
*@property {GraphType} [graph_type] - instance|schema, default value is instance.
1314
*Used to switch between getting documents from the instance or the schema graph.
1415
*@property {string} [type] - only documents of the given type are returned.
@@ -25,7 +26,6 @@ const { ACTIONS } = Utils.ACTIONS;
2526
*@property {boolean} [as_list] default is false, If true, don't return a stream of json objects,
2627
but a json list.
2728
*@property {string} [graph_type] - instance|schema default value is instance
28-
*@property {object} [query] - object that descrive the document query
2929
*/
3030

3131
/**

0 commit comments

Comments
 (0)