Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup integrations tests for MySQL and MSSQL #68

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
4 changes: 2 additions & 2 deletions .env.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
NODE_ENV=test
MSSQL_CREDENTIALS:Server=mssql,1433;Database=master;User Id=sa;Password=Pass@word;trustServerCertificate=true;
MSSQL_CREDENTIALS_READ_ONLY:Server=mssql,1433;Database=master;User Id=reader;Password=re@derP@ssw0rd;trustServerCertificate=true;
MYSQL_TEST_CREDENTIALS={"user":"root","host":"mysql","port":3306,"database":"mysql","ssl":"disabled"}
MSSQL_CREDENTIALS:Server=mssql,1433;Database=master;User Id=sa;Password=Pass@word;trustServerCertificate=true;
Sylvestre67 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
parserOptions: {
sourceType: "module",
ecmaVersion: 2018
ecmaVersion: 2022
},
env: {
node: true,
Expand Down
45 changes: 45 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Test
on: push

jobs:
test:
runs-on: ubuntu-20.04
defaults:
run:
working-directory: .
env:
DOCKER_PACKAGE: ghcr.io/${{ github.repository }}/database-proxy_test

steps:
- uses: actions/checkout@v3
- name: Docker login
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin ghcr.io
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Republish
id: republish
continue-on-error: true
if: ${{ needs.Changes.outputs.connector == 'false' }}
run: |
../.github/retry docker pull ${DOCKER_PACKAGE}:${{ github.event.before }}
docker tag ${DOCKER_PACKAGE}:${{ github.event.before }} ${DOCKER_PACKAGE}:${GITHUB_SHA}
../.github/retry docker push ${DOCKER_PACKAGE}:${GITHUB_SHA}

- name: Build
if: ${{ steps.republish.outcome != 'success' }}
run: docker-compose build
- name: Lint
if: ${{ steps.republish.outcome != 'success' }}
run: docker-compose run lint
- name: Test
if: ${{ steps.republish.outcome != 'success' }}
run: docker-compose run test
- name: Container logs
if: failure()
run: docker-compose logs --no-color --timestamps
- name: Publish
if: ${{ steps.republish.outcome != 'success' }}
run: |
docker tag database-proxy_test:latest ${DOCKER_PACKAGE}:${GITHUB_SHA}
../.github/retry docker push ${DOCKER_PACKAGE}:${GITHUB_SHA}
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM node:18.12.1-alpine

RUN apk update && apk --no-cache add git

RUN mkdir /app
WORKDIR /app

Expand Down
4 changes: 2 additions & 2 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ version: "3.7"
services:
mssql:
image: mcr.microsoft.com/azure-sql-edge
expose:
- "1433"
ports:
- "1433:1433"
16 changes: 13 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
version: "3.7"

services:
lint:
build: .
command: eslint .

test:
build: .
depends_on:
- mssql
- mysql
env_file:
- .env.test
networks:
- db_proxy_test
command: sh -c "set -o pipefail && wait-on -d 10000 -t 30000 tcp:mssql:1433 && node ./data/seed.mssql.js && TZ=UTC NODE_ENV=TEST node_modules/.bin/mocha"
command: sh -c "set -o pipefail && wait-on -d 15000 -t 30000 tcp:mysql:3306 tcp:mssql:1433 && node ./data/seed.mssql.js && TZ=UTC NODE_ENV=TEST node_modules/.bin/mocha"

mssql:
image: mcr.microsoft.com/mssql/server:2019-latest
Expand All @@ -20,8 +25,13 @@ services:
- MSSQL_SLEEP=7
volumes:
- ./data/AdventureWorks2019.bak:/var/opt/mssql/backup/test.bak
ports:
- "1433:1433"
networks:
- db_proxy_test

mysql:
image: mariadb:10.6.4
environment:
- MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=yes
networks:
- db_proxy_test

Expand Down
6 changes: 2 additions & 4 deletions lib/databricks.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,14 @@ export async function queryStream(req, res, connection) {
res.write(`${JSON.stringify(responseSchema)}`);
res.write("\n");

await new Promise(async (resolve, reject) => {
await new Promise((resolve, reject) => {
const stream = new Readable.from(rows);

stream.once("data", () => {
clearInterval(keepAlive);
});

stream.on("close", (error) => {
stream.on("close", () => {
resolve();
stream.destroy();
});
Expand Down Expand Up @@ -345,8 +345,6 @@ export async function check(req, res, connection) {
});

return {ok: true};
} catch (e) {
throw e;
} finally {
if (connection) {
try {
Expand Down
2 changes: 0 additions & 2 deletions lib/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,6 @@ export async function check(req, res, pool) {
);

return {ok: true};
} catch (e) {
throw e;
} finally {
if (connection) {
try {
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@
"@babel/preset-env": "^7.19.4",
"@babel/register": "^7.18.9",
"chai": "^4.3.6",
"eslint": "^8.32.0",
"eslint-plugin-prettier": "^4.2.1",
"mocha": "^10.1.0",
"mock-req": "^0.2.0",
"mock-res": "^0.6.0",
"nodemon": "^1.19.1",
"prettier": "^2.8.3",
"wait-on": "^6.0.1"
},
"peerDependencies": {
Expand All @@ -51,7 +54,7 @@
"test": "mocha",
"test:local": "docker-compose -f docker-compose.yml -f docker-compose.local.yml up --build",
"test:ci": "docker-compose -f docker-compose.yml up --build --exit-code-from test",
"test:db": "docker-compose -f docker-compose.yml -f docker-compose.local.yml up mssql"
"test:db": "docker-compose -f docker-compose.yml -f docker-compose.local.yml up mssql mysql"
},
"author": "Observable",
"license": "ISC",
Expand Down
31 changes: 17 additions & 14 deletions test/mssql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe("mssql", () => {

describe("when querying", () => {
it("should stream the results of simple query", () => {
return new Promise(async (resolve, reject) => {
return new Promise((resolve) => {
const req = new MockReq({method: "POST", url: "/query-stream"}).end({
sql: "SELECT TOP 2 CustomerID FROM test.SalesLT.Customer",
params: [],
Expand All @@ -41,7 +41,7 @@ describe("mssql", () => {
const res = new MockRes(onEnd);

const index = mssql(credentials);
await index(req, res);
index(req, res);

function onEnd() {
const [schema, row] = this._getString().split("\n");
Expand All @@ -62,17 +62,18 @@ describe("mssql", () => {
});
});
it("should handle parameter graciously", () => {
return new Promise(async (resolve, reject) => {
return new Promise((resolve) => {
const testCustomerId = 3;
const req = new MockReq({method: "POST", url: "/query-stream"}).end({
sql: "SELECT TOP 2 CustomerID FROM test.SalesLT.Customer WHERE CustomerID=@1",
sql:
"SELECT TOP 2 CustomerID FROM test.SalesLT.Customer WHERE CustomerID=@1",
params: [testCustomerId],
});

const res = new MockRes(onEnd);

const index = mssql(credentials);
await index(req, res);
index(req, res);

function onEnd() {
const [schema, row] = this._getString().split("\n");
Expand All @@ -93,17 +94,18 @@ describe("mssql", () => {
});
});
it("should replace cell reference in the SQL query", () => {
return new Promise(async (resolve, reject) => {
return new Promise((resolve) => {
const testCustomerId = 5;
const req = new MockReq({method: "POST", url: "/query-stream"}).end({
sql: "SELECT TOP 2 CustomerID FROM test.SalesLT.Customer WHERE CustomerID=@1",
sql:
"SELECT TOP 2 CustomerID FROM test.SalesLT.Customer WHERE CustomerID=@1",
params: [testCustomerId],
});

const res = new MockRes(onEnd);

const index = mssql(credentials);
await index(req, res);
index(req, res);

function onEnd() {
const [schema, row] = this._getString().split("\n");
Expand All @@ -124,7 +126,7 @@ describe("mssql", () => {
});
});
it("should handle duplicated column names", () => {
return new Promise(async (resolve, reject) => {
return new Promise((resolve) => {
const req = new MockReq({method: "POST", url: "/query-stream"}).end({
sql: "SELECT 1 as _a1, 2 as _a1 FROM test.SalesLT.SalesOrderDetail",
params: [],
Expand All @@ -133,10 +135,10 @@ describe("mssql", () => {
const res = new MockRes(onEnd);

const index = mssql(credentials);
await index(req, res);
index(req, res);

function onEnd() {
const [schema, row] = this._getString().split("\n");
const [, row] = this._getString().split("\n");

expect(row).to.equal(
JSON.stringify({
Expand All @@ -149,16 +151,17 @@ describe("mssql", () => {
});
});
it("should select the last value of any detected duplicated columns", () => {
return new Promise(async (resolve, reject) => {
return new Promise((resolve) => {
const req = new MockReq({method: "POST", url: "/query-stream"}).end({
sql: "SELECT TOP 1 ModifiedDate, ModifiedDate FROM test.SalesLT.SalesOrderDetail",
sql:
"SELECT TOP 1 ModifiedDate, ModifiedDate FROM test.SalesLT.SalesOrderDetail",
params: [],
});

const res = new MockRes(onEnd);

const index = mssql(credentials);
await index(req, res);
index(req, res);

function onEnd() {
const [schema, row] = this._getString().split("\n");
Expand Down
Loading