Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/express-4.19.2
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinMendelGleason authored Aug 12, 2024
2 parents 0f053c2 + aff000f commit 366a36a
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 73 deletions.
7 changes: 6 additions & 1 deletion integration_tests/create_database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Create a database, schema and insert data', () => {
})

test('Insert Document Child Tom', async () => {
const person = {"age":"10","name":"Tom","@type":"Child"}
const person = {"age":"10","name":"Tom","@type":"Child" }
const result = await client.addDocument(person);
expect(result).toStrictEqual(["terminusdb:///data/Child/Tom" ]);
})
Expand All @@ -47,6 +47,11 @@ describe('Create a database, schema and insert data', () => {
expect(result).toStrictEqual(["terminusdb:///data/Parent/Tom%20Senior" ]);
})

test('Get document history on an object', async () => {
const result = await client.getDocumentHistory("Child/Tom");
expect(result[0].message).toStrictEqual("add a new document");
})

test('Query Person by name', async () => {
const queryTemplate = {"name":"Tom", "@type":"Person" }
const result = await client.getDocument({query:queryTemplate});
Expand Down
5 changes: 5 additions & 0 deletions integration_tests/data/employees.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
EmployeeId,Name,Title,Team,Manager
001,Destiny Norris,Marketing Manager,Marketing,
002,Darci Prosser,Creative Writer,Marketing,001
003,Alanah Bloggs,Frontend Developer,IT,004
004,Fabian Dalby,Web Service Manager,IT,
16 changes: 16 additions & 0 deletions integration_tests/data/employees_limit1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const mock_employees_limit_1 = [
{
"Manager": {
"@type": "xsd:string",
"@value": "",
},
"Name": {
"@type": "xsd:string",
"@value": "Destiny Norris",
},
"Title": {
"@type": "xsd:string",
"@value": "Marketing Manager",
},
},
]
87 changes: 87 additions & 0 deletions integration_tests/woql_client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//@ts-check
import { describe, expect, test, beforeAll } from '@jest/globals';
import { WOQLClient, WOQL } from '../index.js';
import { DbDetails, DocParamsGet } from '../dist/typescript/lib/typedef.js';
import schemaJson from './persons_schema'
import { mock_employees_limit_1 } from './data/employees_limit1';
import fs from 'fs';

let client: WOQLClient //= new WOQLClient('http://localhost:6363');

beforeAll(() => {
client = new WOQLClient("http://localhost:6363", { user: 'admin', organization: 'admin', key: 'root' })
});

const db01 = 'db__test_woql';

describe('Create a database, schema and insert data', () => {
test('Create a database', async () => {
const dbObj: DbDetails = { label: db01, comment: 'add db', schema: true }
const result = await client.createDatabase(db01, dbObj);
//woqlClient return only the data no status
expect(result["@type"]).toEqual("api:DbCreateResponse");
expect(result["api:status"]).toEqual("api:success");
});

test('Create a schema', async () => {
const result = await client.addDocument(schemaJson, { graph_type: "schema", full_replace: true });
expect(result).toStrictEqual(["Child", "Person", "Parent"]);
})

test('Query with CSV upload from file', async () => {
const query = WOQL.limit(1).and(
WOQL.get(
WOQL.as('Name', 'v:Name')
.as('Manager', 'v:Manager')
.as('Title', 'v:Title'),
WOQL.post("./integration_tests/data/employees.csv")
),
);
const result = await client.query(query, undefined, undefined, undefined, undefined,);
expect(result?.bindings).toStrictEqual(mock_employees_limit_1);
});

test('Query with CSV upload as resource attachment', async () => {
const query = WOQL.limit(1).and(
WOQL.get(
WOQL.as('Name', 'v:Name')
.as('Manager', 'v:Manager')
.as('Title', 'v:Title'),
WOQL.post("employees.csv")
),
);
const data = fs.readFileSync('./integration_tests/data/employees.csv');
const result = await client.query(query, undefined, undefined, undefined, undefined, [{
filename: "employees.csv",
data: data,
}]);
expect(result?.bindings).toStrictEqual(mock_employees_limit_1);
});

test('Get branches from the server (only main)', async () => {
const result = await client.getBranches();
expect(result.main["@id"]).toStrictEqual("Branch/main");
expect(Object.keys(result)).toStrictEqual(["main"]);
});

test('Get commits log from the server', async () => {
const result = await client.getCommitsLog();
expect(result.length).toStrictEqual(1);
expect(result[0]["@type"]).toStrictEqual("ValidCommit");
});

test('Get prefixes from the server', async () => {
const result = await client.getPrefixes();
expect(result).toStrictEqual({"@base": "terminusdb:///data/", "@schema": "terminusdb:///schema#", "@type": "Context"});
});

test('Get userOrganisations from the server', async () => {
const result = (await client.getUserOrganizations()).filter(org => org["@id"] === "Organization/admin");
expect(result[0]["@id"]).toStrictEqual("Organization/admin");
});

test('Delete a database', async () => {
const result = await client.deleteDatabase(db01);
expect(result).toStrictEqual({ '@type': 'api:DbDeleteResponse', 'api:status': 'api:success' });
});
});
4 changes: 2 additions & 2 deletions lib/axiosInstance.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const axios = require('axios');
const axios = require('axios').default;

const axiosInstance = axios.create();
const axiosInstance = axios.create({});

module.exports = axiosInstance;
4 changes: 3 additions & 1 deletion lib/dispatchRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
}
default: {
options.headers = options.headers ? options.headers : {};
options.headers['Content-Type'] = 'application/json; charset=utf-8';
if (!options.headers['content-type'] && !options.headers['Content-Type']) {
options.headers['Content-Type'] = 'application/json; charset=utf-8';
}
const compressedContentPost = checkPayload(payload, options, compress);
return axiosInstance
.post(url, compressedContentPost || payload || {}, options)
Expand Down
6 changes: 6 additions & 0 deletions lib/typedef.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,10 @@ const { ACTIONS } = Utils.ACTIONS;
* @property {number} [start] - Amount of commits to show, 10 is the default
*/

/**
* @typedef {Object} NamedResourceData - { filename: "data.csv", data: "col1;col2\nval1;val2" }
* @property {string} filename - Filename referenced in the WOQL query
* @property {string|Blob} data - Attached data, such as CSV contents
*/

module.exports = {};
17 changes: 15 additions & 2 deletions lib/woqlClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,14 +635,19 @@ function getResourceObjects(queryObject, result_array) {
* @param {string} [lastDataVersion] the last data version tracking id.
* @param {boolean} [getDataVersion] If true the function will return object having result
* and dataVersion.
* @param {Array<NamedResourceData>} [resources] csv resources supplied as strings
* @returns {Promise} A promise that returns the call response object or object having *result*
* and *dataVersion* object if ***getDataVersion*** parameter is true, or an Error if rejected.
* @example
* const result = await client.query(WOQL.star())
*/
WOQLClient.prototype.query = function (woql, commitMsg, allWitnesses, lastDataVersion = '', getDataVersion = false) {
WOQLClient.prototype.query = function (woql, commitMsg, allWitnesses, lastDataVersion = '', getDataVersion = false, resources = []) {
allWitnesses = allWitnesses || false;
commitMsg = commitMsg || 'Commit generated with javascript client without message';

const providedResourcesLookupMap = (resources ?? [])
.reduce((map, res) => ({ ...map, [(res.filename).split('/').pop()]: res.data }), {});

if (woql && woql.json && (!woql.containsUpdate() || commitMsg)) {
const doql = woql.containsUpdate() ? this.generateCommitInfo(commitMsg) : {};
doql.query = woql.json();
Expand All @@ -655,9 +660,17 @@ WOQLClient.prototype.query = function (woql, commitMsg, allWitnesses, lastDataVe
const formData = new FormData();

resourceObjects.forEach((resourceObject) => {
const providedResourceInsteadOfFile = typeof resourceObject.source.post === 'string'
? providedResourcesLookupMap?.[resourceObject.source.post.split('/').pop()]
: undefined;

const fileName = resourceObject.source.post.split('/').pop();

formData.append('file', fs.createReadStream(resourceObject.source.post));
if (providedResourceInsteadOfFile) {
formData.append('file', providedResourceInsteadOfFile, { filename: fileName, contentType: 'application/csv' });
} else {
formData.append('file', fs.createReadStream(resourceObject.source.post));
}
resourceObject.source.post = fileName;
});

Expand Down
Loading

0 comments on commit 366a36a

Please sign in to comment.