Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-onions committed Nov 19, 2024
1 parent 20f980e commit 9ea8276
Show file tree
Hide file tree
Showing 9 changed files with 569 additions and 13 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,4 @@ dist
# Minio
minio
fs
tmp
sample
tmp
30 changes: 29 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,39 @@ services:
- 9000:9000
- 9001:9001
command: server /data --console-address ":9001"

clickhouse:
image: clickhouse/clickhouse-server:23.8.14.6
ports:
- 8123:8123
- 9002:9000
environment:
- CLICKHOUSE_PASSWORD=dev_password_only


openhim-console:
image: jembi/openhim-console:v1.18.2
ports:
- 80:80
networks:
- openhim

openhim-core:
image: jembi/openhim-core:v8.5.1
environment:
- mongo_url=mongodb://mongo-db:27017/openhim
ports:
- 8080:8080
- 5000:5000
- 5001:5001
networks:
- openhim

mongo-db:
container_name: mongo-db
image: mongo:4.0
networks:
- openhim
restart: unless-stopped

networks:
openhim:
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"devDependencies": {
"@types/chai": "^4.3.11",
"@types/express": "^4.17.21",
"@types/mocha": "^10.0.6",
"@types/mocha": "^10.0.9",
"@types/multer": "^1.4.12",
"@types/nock": "^11.1.0",
"@types/node": "^22.9.0",
Expand Down
499 changes: 499 additions & 0 deletions sample/8c7870187cce937eb0fc0d9e5b20b66a-reduced.csv

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ async function setupMinio() {
}

setupMinio();

app.listen(3000, () => {
logger.info(`Server is running on port - ${3000}`);
});
8 changes: 1 addition & 7 deletions src/utils/clickhouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,7 @@ export async function createTable(fields: string[], tableName: string) {
}

export function generateDDL(fields: string[], tableName: string) {
return `CREATE TABLE IF NOT EXISTS ${tableName} (
table_id UUID DEFAULT generateUUIDv4(),
${fields.map((field) => `${field} VARCHAR`).join(', ')}
)
ENGINE = MergeTree
ORDER BY (table_id)
`;
return `CREATE TABLE ${tableName} (table_id UUID DEFAULT generateUUIDv4(),${fields.map((field) => `${field} VARCHAR`).join(', ')}) ENGINE = MergeTree ORDER BY (table_id)`;
}

export function flattenJson(json: any, prefix = ''): string[] {
Expand Down
2 changes: 1 addition & 1 deletion tests/config/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { config, expect } from 'chai';
import { Config } from '../../src/config/config';

describe('Config', () => {
describe('Config', function () {
const OLD_ENV = process.env;

beforeEach(() => {
Expand Down
32 changes: 32 additions & 0 deletions tests/services/table-creation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect } from 'chai';
import { getCsvHeaders } from '../../src/utils/file-validators';
import { flattenJson, generateDDL } from '../../src/utils/clickhouse';


describe('Create Tables based on files', function () {
this.timeout(60_000);

it('should extract columns based on a csv file', async () => {
//arrange
const csvFile = Buffer.from('id,name,age\n1,John,20\n2,Jane,21');
//act
const fields = getCsvHeaders(csvFile);
//assert
expect(fields).to.deep.equal(['id', 'name', 'age']);
});

it('should extract columns based on a json file', async () => {
const jsonFile = Buffer.from('[{"id":1,"name":"John","age":20}]');
const json = JSON.parse(jsonFile.toString());
const fields = flattenJson(json[0]);
expect(fields).to.deep.equal(['id', 'name', 'age']);
});

it('should create a table based on a csv file', async () => {
const csvFile = Buffer.from('id,name,age\n1,John,20\n2,Jane,21');
const fields = getCsvHeaders(csvFile);
if (!fields) throw new Error('No fields found');
const result = generateDDL(fields, 'test');
expect(result).to.equal('CREATE TABLE test (table_id UUID DEFAULT generateUUIDv4(),id VARCHAR, name VARCHAR, age VARCHAR) ENGINE = MergeTree ORDER BY (table_id)');
});
});

0 comments on commit 9ea8276

Please sign in to comment.