Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/dataSources.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ const updateMetadata = (conn, name, metadata, opts = {}) => {
const query = (conn, name, dataSourceQuery) => {
const headers = conn.headers();
const isString = typeof dataSourceQuery === 'string';
headers.set(
'Content-Type',
isString ? 'text/plain' : 'application/json; charset=utf-8'
);
headers.set('Content-Type', isString ? 'text/plain' : 'application/json');
headers.set('Accept', 'application/json');
return fetch(conn.request('admin', 'data_sources', name, 'query'), {
method: 'POST',
Expand Down
121 changes: 121 additions & 0 deletions test/__snapshots__/dataSources.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`data_sources getMetadata returns the metadata of a data source 1`] = `
"@prefix tbl: <http://system.stardog.com/registry/jdbcDataSource/tableMetadata/table/> .
@prefix sqlcase: <http://system.stardog.com/registry/vegaSqlDialect/casing/> .
@prefix sdialect: <http://system.stardog.com/registry/vegaSqlDialect/> .
@prefix : <http://api.stardog.com/> .
@prefix tbldef: <http://system.stardog.com/registry/jdbcDataSource/tableMetadata/table#> .
@prefix jdbcmd: <http://system.stardog.com/registry/jdbcDataSourceMetadata/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix ds: <http://system.stardog.com/registry/dataSource/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix coldef: <http://system.stardog.com/registry/jdbcDataSource/tableMetadata/column#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix swrl: <http://www.w3.org/2003/11/swrl#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix qt: <http://system.stardog.com/registry/jdbcDataSource/table/> .
@prefix jdbcds: <http://system.stardog.com/registry/jdbcDataSource/> .
@prefix stardog: <tag:stardog:api:> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix md: <http://system.stardog.com/registry/metadata/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix swrlb: <http://www.w3.org/2003/11/swrlb#> .
@prefix so: <https://schema.org/> .
@prefix col: <http://system.stardog.com/registry/jdbcDataSource/tableMetadata/column/> .
@prefix sqldbprd: <http://system.stardog.com/registry/vegaSqlDialect/databaseProduct/> .
@prefix qtdef: <http://system.stardog.com/registry/jdbcDataSource/table/table#> .
@prefix sqlnullc: <http://system.stardog.com/registry/vegaSqlDialect/nullCollation/> .
@prefix tblmd: <http://system.stardog.com/registry/jdbcDataSource/tableMetadata/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix tbltype: <http://system.stardog.com/registry/jdbcDataSource/table/tableType/> .
@prefix sqldb: <http://system.stardog.com/registry/vegaSqlDialect/db/> .
@prefix dialect: <http://system.stardog.com/registry/dataSource/dialect/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix reg: <http://system.stardog.com/registry/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .


<http://system.stardog.com/registry/catalogSchema/stardogjs_test%5Cstardogjs_test%5C> jdbcmd:tableCount \\"1\\"^^xsd:int .
<data-source://stardogjs_test> jdbcmd:defaultCatalog \\"stardogjs_test\\" ;
jdbcmd:name \\"stardogjs_test\\" ;
jdbcmd:defaultSchema <http://system.stardog.com/registry/catalogSchema/stardogjs_test%5Cstardogjs_test%5C> .
qtdef:stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table qt:tableName \\"test_table\\" .
<http://system.stardog.com/registry/catalogSchema/stardogjs_test%5Cstardogjs_test%5C> <http://system.stardog.com/registry/catalogSchema/catalogName> \\"stardogjs_test\\" .
<data-source://stardogjs_test> jdbcmd:table qtdef:stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table .
qtdef:stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table qt:catalog \\"stardogjs_test\\" .
<data-source://stardogjs_test> jdbcmd:version \\"11.1.0\\" .
qtdef:stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table qt:tableType tbltype:TABLE ."
`;

exports[`data_sources getTableMetadata returns the metadata of a table 1`] = `
"@prefix tbl: <http://system.stardog.com/registry/jdbcDataSource/tableMetadata/table/> .
@prefix sqlcase: <http://system.stardog.com/registry/vegaSqlDialect/casing/> .
@prefix sdialect: <http://system.stardog.com/registry/vegaSqlDialect/> .
@prefix : <http://api.stardog.com/> .
@prefix tbldef: <http://system.stardog.com/registry/jdbcDataSource/tableMetadata/table#> .
@prefix jdbcmd: <http://system.stardog.com/registry/jdbcDataSourceMetadata/> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix ds: <http://system.stardog.com/registry/dataSource/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix coldef: <http://system.stardog.com/registry/jdbcDataSource/tableMetadata/column#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix swrl: <http://www.w3.org/2003/11/swrl#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix qt: <http://system.stardog.com/registry/jdbcDataSource/table/> .
@prefix jdbcds: <http://system.stardog.com/registry/jdbcDataSource/> .
@prefix stardog: <tag:stardog:api:> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix md: <http://system.stardog.com/registry/metadata/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix swrlb: <http://www.w3.org/2003/11/swrlb#> .
@prefix so: <https://schema.org/> .
@prefix col: <http://system.stardog.com/registry/jdbcDataSource/tableMetadata/column/> .
@prefix sqldbprd: <http://system.stardog.com/registry/vegaSqlDialect/databaseProduct/> .
@prefix qtdef: <http://system.stardog.com/registry/jdbcDataSource/table/table#> .
@prefix sqlnullc: <http://system.stardog.com/registry/vegaSqlDialect/nullCollation/> .
@prefix tblmd: <http://system.stardog.com/registry/jdbcDataSource/tableMetadata/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix tbltype: <http://system.stardog.com/registry/jdbcDataSource/table/tableType/> .
@prefix sqldb: <http://system.stardog.com/registry/vegaSqlDialect/db/> .
@prefix dialect: <http://system.stardog.com/registry/dataSource/dialect/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix reg: <http://system.stardog.com/registry/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .


<http://system.stardog.com/registry/jdbcDataSource/tableMetadata/index#stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table%5C0> tblmd:indexedColumn <http://system.stardog.com/registry/jdbcDataSource/tableMetadata/indexedColumn#stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table%5C0-0> ;
tblmd:isUnique true .
tbldef:stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table tblmd:column coldef:stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table%5C1 .
coldef:stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table%5C1 col:columnNullability false .
<http://system.stardog.com/registry/jdbcDataSource/tableMetadata/uniqueKeySet/#stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table%5C0> col:uniqueKey \\"0\\"^^xsd:int .
tbldef:stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table col:uniqueKeySet <http://system.stardog.com/registry/jdbcDataSource/tableMetadata/uniqueKeySet/#stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table%5C0> .
<http://system.stardog.com/registry/jdbcDataSource/tableMetadata/indexedColumn#stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table%5C0-0> tblmd:indexColumnId \\"0\\"^^xsd:int .
tbldef:stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table qt:tableType tbltype:TABLE ;
tblmd:primaryKey \\"0\\"^^xsd:int ;
qt:catalog \\"stardogjs_test\\" ;
tblmd:rowCount 0.0E0 ;
tblmd:hasIndex <http://system.stardog.com/registry/jdbcDataSource/tableMetadata/index#stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table%5C0> .
coldef:stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table%5C1 col:columnDataType \\"4\\"^^xsd:int ;
col:columnSize \\"10\\"^^xsd:int ;
col:columnOrdinalPosition \\"1\\"^^xsd:int .
<http://system.stardog.com/registry/jdbcDataSource/tableMetadata/index#stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table%5C0> tblmd:indexType \\"3\\" .
tbldef:stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table qt:tableName \\"test_table\\" .
coldef:stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table%5C1 col:columnTypeString \\"INT\\" ;
col:columnDecimalDigits \\"0\\"^^xsd:int ;
col:columnName \\"id\\" .
<http://system.stardog.com/registry/jdbcDataSource/tableMetadata/index#stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table%5C0> tblmd:indexName \\"PRIMARY\\" .
<http://system.stardog.com/registry/jdbcDataSource/tableMetadata/indexedColumn#stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table%5C0-0> tblmd:indexCardinality 0.0E0 .
tbldef:stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table tblmd:dataSourceName \\"stardogjs_test\\" .
<http://system.stardog.com/registry/jdbcDataSource/tableMetadata/indexedColumn#stardogjs_test%5CTABLE%5Cstardogjs_test%5C%5Ctest_table%5C0-0> tblmd:indexOrdinalPosition \\"0\\"^^xsd:int ."
`;

exports[`data_sources getTables returns the tables for a data source 1`] = `
Array [
Object {
"catalog": "stardogjs_test",
"table_name": "test_table",
"table_type": "TABLE",
},
]
`;
123 changes: 79 additions & 44 deletions test/dataSources.spec.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,54 @@
/* eslint-env jest */

const fs = require('fs');
const path = require('path');
const dataSources = require('../lib/dataSources');
const { ConnectionFactory } = require('./setup-database');
const snapshots = require('./__snapshots__/dataSources.spec.js.snap');

const dataSourceMetadata = fs.readFileSync(
path.resolve(`${__dirname}/fixtures/data_source_metadata.ttl`),
'utf8'
);
/*
Run the following to set up the MySQL database used in these tests:
echo "drop database if exists stardogjs_test; create database stardogjs_test; use stardogjs_test; create table test_table (id INT PRIMARY KEY);" | mysql -h host.docker.internal -uroot -pmy-secret
Comment on lines +8 to +9
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noting i got all these here to pass after running this (and removing the .only below)

*/

const dataSourceTables = fs.readFileSync(
path.resolve(`${__dirname}/fixtures/data_source_tables.json`),
'utf8'
);

const dataSourceTableMetadata = fs.readFileSync(
path.resolve(`${__dirname}/fixtures/data_source_table_metadata.ttl`),
'utf8'
);
const aDSName = 'stardogjs_test';
const aOptions = {
'jdbc.driver': 'com.mysql.jdbc.Driver',
'jdbc.url': 'jdbc:mysql://host.docker.internal:3306/stardogjs_test',
'jdbc.username': 'root',
'jdbc.password': 'my-secret',
};

describe('data_sources', () => {
let conn;
const aDSName = 'MyDataSource';
const aOptions = {
'jdbc.driver': 'com.mysql.jdbc.Driver',
'jdbc.url': 'jdbc:mysql://localhost:3306/sys',
'jdbc.username': 'root',
'jdbc.password': '',
'ext.serverTimezone': 'EST',
};

beforeEach(() => {
conn = ConnectionFactory();
});

const assureExists = () =>
dataSources.list(conn).then(res => {
dataSources.list(conn).then(listResponse => {
expect(listResponse.status).toBe(200);

const exists =
res.body.data_sources && res.body.data_sources.includes(aDSName);
listResponse.body.data_sources &&
listResponse.body.data_sources.includes(aDSName);
if (!exists) {
return dataSources.add(conn, aDSName, aOptions);
return dataSources.add(conn, aDSName, aOptions).then(addResponse => {
expect(addResponse.status).toBe(201);
});
}
return res;
return listResponse;
});

const assureNotExists = () =>
dataSources.list(conn).then(res => {
dataSources.list(conn).then(listResponse => {
const exists =
res.body.data_sources && res.body.data_sources.includes(aDSName);
listResponse.body.data_sources &&
listResponse.body.data_sources.includes(aDSName);
if (exists) {
return dataSources.remove(conn, aDSName);
return dataSources.remove(conn, aDSName).then(removeResponse => {
expect(removeResponse.status).toBe(204);
});
}
return res;
return listResponse;
});

describe('list', () => {
Expand Down Expand Up @@ -139,7 +135,11 @@ describe('data_sources', () => {
.then(() => dataSources.options(conn, aDSName))
.then(res => {
expect(res.status).toBe(200);
expect(res.body.options).toEqual(aOptions);
expect(res.body.options).toEqual({
...aOptions,
'jdbc.username': '****',
'jdbc.password': '****',
});
}));
});

Expand All @@ -149,7 +149,7 @@ describe('data_sources', () => {
.then(() => dataSources.getMetadata(conn, aDSName))
.then(res => {
expect(res.status).toBe(200);
expect(res.body).toEqual(dataSourceMetadata);
expect(res.body).toMatchSnapshot();
}));
});

Expand All @@ -159,7 +159,7 @@ describe('data_sources', () => {
.then(() => dataSources.getTables(conn, aDSName))
.then(res => {
expect(res.status).toBe(200);
expect(res.body).toEqual(dataSourceTables);
expect(res.body).toMatchSnapshot();
}));
});

Expand All @@ -168,32 +168,67 @@ describe('data_sources', () => {
assureExists()
.then(() =>
dataSources.getTableMetadata(conn, aDSName, {
table_name: 'table-name',
table_name: 'test_table',
table_type: 'TABLE',
catalog: 'catalog',
schema: 'schema',
catalog: 'stardogjs_test',
})
)
.then(res => {
expect(res.status).toBe(200);
expect(res.body).toEqual(dataSourceTableMetadata);
expect(res.body).toMatchSnapshot();
}));
});

describe('updateMetadata', () => {
it('updates the metadata of a data source', () =>
assureExists()
.then(() =>
dataSources.updateMetadata(conn, aDSName, dataSourceMetadata)
dataSources.updateMetadata(
conn,
aDSName,
snapshots[
'data_sources getMetadata returns the metadata of a data source 1'
]
.trim()
.slice(1, -1)
.replace(/\\"/g, '"')
)
)
.then(res => {
expect(res.status).toBe(204);
}));
});

describe('suggestions', () => {
const tableName = 'TODO';
describe('query', () => {
it('executes a query with a string query parameter', () =>
assureExists()
.then(() =>
dataSources.query(conn, aDSName, 'SELECT * FROM test_table')
)
.then(res => {
expect(res.status).toBe(200);
expect(typeof res.body).toBe('object');
}));

it('executes a query with an object query parameter', () =>
assureExists()
.then(() =>
dataSources.query(conn, aDSName, {
query: 'SELECT * FROM test_table',
options: {
'parser.sql.quoting': 'NATIVE',
'sql.functions': '',
'percent.encode': true,
},
})
)
.then(res => {
expect(res.status).toBe(200);
expect(typeof res.body).toBe('object');
}));
});

describe('suggestions', () => {
it('returns successfully when given a valid configuration', () =>
assureExists().then(() =>
dataSources
Expand All @@ -202,12 +237,12 @@ describe('data_sources', () => {
`
<tag:stardog:api:match:configuration> {
[] <tag:stardog:api:match:source> <data-source://${aDSName}> ;
<tag:stardog:api:match:sourceTable> "${tableName}" ;
<tag:stardog:api:match:sourceTable> "test_table" ;
<tag:stardog:api:match:target> <tag:stardog:project:model> ;
}

<tag:stardog:project:model> {

}
`
)
Expand Down
27 changes: 0 additions & 27 deletions test/fixtures/data_source_metadata.ttl

This file was deleted.

Loading