integration-tests/node: More improvements

This commit is contained in:
Taylor Bantle
2023-02-07 14:46:57 -08:00
parent 8732764233
commit 716909795c
7 changed files with 129 additions and 118 deletions

View File

@@ -1,9 +1,5 @@
const mysql = require("mysql2/promise");
const args = process.argv.slice(2);
const user = args[0];
const port = args[1];
const dbName = args[2];
import mysql from "mysql2/promise";
import { getConfig } from "./helpers";
async function createTable() {
const conn = await getConnection();
@@ -86,7 +82,7 @@ async function insertAuthor(name) {
async function validateCommits(name) {
const conn = await getConnection();
var results;
let results;
try {
results = await conn.query(
`select count(*) as c from dolt_log where message like 'created author%'`
@@ -109,12 +105,7 @@ async function validateCommits(name) {
}
async function getConnection() {
const connection = await mysql.createConnection({
host: "127.0.0.1",
port: port,
user: user,
database: dbName,
});
const connection = await mysql.createConnection(getConfig());
return connection;
}

View File

@@ -0,0 +1,26 @@
import mysql from "mysql";
export class Database {
constructor(config) {
this.connection = mysql.createConnection(config);
this.connection.connect();
}
query(sql, args) {
return new Promise((resolve, reject) => {
this.connection.query(sql, args, (err, rows) => {
if (err) return reject(err);
return resolve(rows);
});
});
}
close() {
this.connection.end((err) => {
if (err) {
console.error(err);
} else {
console.log("db connection closed");
}
});
}
}

View File

@@ -0,0 +1,18 @@
const args = process.argv.slice(2);
const user = args[0];
const port = args[1];
const dbName = args[2];
export function getArgs() {
return { user, port, dbName };
}
export function getConfig() {
const { user, port, dbName } = getArgs();
return {
host: "127.0.0.1",
port: port,
user: user,
database: dbName,
};
}

View File

@@ -1,63 +1,10 @@
const mysql = require("mysql");
import { Database } from "./database.js";
import { getConfig } from "./helpers.js";
const args = process.argv.slice(2);
const user = args[0];
const port = args[1];
const db = args[2];
const config = {
host: "127.0.0.1",
user: user,
port: port,
database: db,
};
class Database {
constructor(config) {
this.connection = mysql.createConnection(config);
this.connection.connect();
}
query(sql, args) {
return new Promise((resolve, reject) => {
this.connection.query(sql, args, (err, rows) => {
if (err) return reject(err);
return resolve(rows);
});
});
}
close() {
this.connection.end((err) => {
if (err) {
console.error(err);
} else {
console.log("db connection closed");
}
});
}
}
async function main() {
const queries = [
"create table test (pk int, `value` int, primary key(pk))",
"describe test",
"select * from test",
"insert into test (pk, `value`) values (0,0)",
"select * from test",
"call dolt_add('-A');",
"call dolt_commit('-m', 'my commit')",
"select COUNT(*) FROM dolt_log",
"call dolt_checkout('-b', 'mybranch')",
"insert into test (pk, `value`) values (1,1)",
"call dolt_commit('-a', '-m', 'my commit2')",
"call dolt_checkout('main')",
"call dolt_merge('mybranch')",
"select COUNT(*) FROM dolt_log",
];
const results = [
{
const tests = [
{
q: "create table test (pk int, `value` int, primary key(pk))",
res: {
fieldCount: 0,
affectedRows: 0,
insertId: 0,
@@ -67,7 +14,10 @@ async function main() {
protocol41: true,
changedRows: 0,
},
[
},
{
q: "describe test",
res: [
{
Field: "pk",
Type: "int",
@@ -85,8 +35,11 @@ async function main() {
Extra: "",
},
],
[],
{
},
{ q: "select * from test", res: [] },
{
q: "insert into test (pk, `value`) values (0,0)",
res: {
fieldCount: 0,
affectedRows: 1,
insertId: 0,
@@ -96,12 +49,15 @@ async function main() {
protocol41: true,
changedRows: 0,
},
[{ pk: 0, value: 0 }],
[{ status: 0 }],
[],
[{ "COUNT(*)": 2 }],
[{ status: 0 }],
{
},
{ q: "select * from test", res: [{ pk: 0, value: 0 }] },
{ q: "call dolt_add('-A');", res: [{ status: 0 }] },
{ q: "call dolt_commit('-m', 'my commit')", res: [] },
{ q: "select COUNT(*) FROM dolt_log", res: [{ "COUNT(*)": 2 }] },
{ q: "call dolt_checkout('-b', 'mybranch')", res: [{ status: 0 }] },
{
q: "insert into test (pk, `value`) values (1,1)",
res: {
fieldCount: 0,
affectedRows: 1,
insertId: 0,
@@ -111,32 +67,38 @@ async function main() {
protocol41: true,
changedRows: 0,
},
[],
[{ status: 0 }],
[{ fast_forward: 1, conflicts: 0 }],
[{ "COUNT(*)": 3 }],
];
},
{ q: "call dolt_commit('-a', '-m', 'my commit2')", res: [] },
{ q: "call dolt_checkout('main')", res: [{ status: 0 }] },
{
q: "call dolt_merge('mybranch')",
res: [{ fast_forward: 1, conflicts: 0 }],
},
{ q: "select COUNT(*) FROM dolt_log", res: [{ "COUNT(*)": 3 }] },
];
const database = new Database(config);
async function main() {
const database = new Database(getConfig());
await Promise.all(
queries.map((query, idx) => {
const expected = results[idx];
tests.map((test) => {
const expected = test.res;
return database
.query(query)
.query(test.q)
.then((rows) => {
const resultStr = JSON.stringify(rows);
const result = JSON.parse(resultStr);
if (
resultStr !== JSON.stringify(expected) &&
!query.includes("dolt_commit")
test.q.includes("dolt_commit") &&
!(rows.length === 1 && rows[0].hash.length > 0)
) {
console.log("Query:", query);
console.log("Query:", test.q);
console.log("Results:", result);
console.log("Expected:", expected);
throw new Error("Query failed");
} else {
console.log("Query succeeded:", query);
console.log("Query succeeded:", test.q);
}
})
.catch((err) => {

View File

@@ -1,24 +1,15 @@
const knex = require("knex");
const wtfnode = require("wtfnode");
Socket = require("net").Socket;
const args = process.argv.slice(2);
const user = args[0];
const port = args[1];
const dbName = args[2];
import knex from "knex";
import wtfnode from "wtfnode";
import { Socket } from "net";
import { getConfig } from "./helpers.js";
const db = knex({
client: "mysql2",
connection: {
host: "127.0.0.1",
port: port,
user: user,
database: dbName,
},
connection: getConfig(),
});
async function createTable() {
let val = await db.schema.createTable("test2", (table) => {
const val = await db.schema.createTable("test2", (table) => {
table.integer("id").primary();
table.integer("foo");
});
@@ -26,12 +17,12 @@ async function createTable() {
}
async function upsert(table, data) {
let val = await db(table).insert(data).onConflict().merge();
const val = await db(table).insert(data).onConflict().merge();
return val;
}
async function select() {
let val = await db.select("id", "foo").from("test2");
const val = await db.select("id", "foo").from("test2");
return val;
}
@@ -42,37 +33,33 @@ async function main() {
upsert("test2", { id: 2, foo: 2 }),
]);
let expectedResult = JSON.stringify([
const expectedResult = JSON.stringify([
{ id: 1, foo: 1 },
{ id: 2, foo: 2 },
]);
let result = await select();
const result = await select();
if (JSON.stringify(result) !== expectedResult) {
console.log("Results:", result);
console.log("Expected:", expectedResult);
process.exit(1);
throw new Error("Query failed");
}
await db.destroy();
// cc: https://github.com/dolthub/dolt/issues/3752
setTimeout(async () => {
let sockets = await getOpenSockets();
const sockets = await getOpenSockets();
if (sockets.length > 0) {
wtfnode.dump();
process.exit(1);
throw new Error(
"Database not properly destroyed. Hanging server connections"
);
}
}, 3000);
}
// cc: https://github.com/myndzi/wtfnode/blob/master/index.js#L457
async function getOpenSockets() {
let sockets = [];
const sockets = [];
process._getActiveHandles().forEach(function (h) {
// handles can be null now? early exit to guard against this
if (!h) {

View File

@@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "A simple node command line utility to show how to connect a node application to a Dolt database using the MySQL connector.",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},

View File

@@ -0,0 +1,26 @@
import { Database } from "./database.js";
import { getConfig } from "./helpers.js";
async function main() {
const database = new Database(getConfig());
try {
await database.query("call dolt_checkout('main')");
await database.query(
"call dolt_reset('--hard', 'p6dj4b4fng13r3eostv63di4iun58lr2')"
);
const status = await database.query("select * from dolt_status");
if (status.length > 0) {
await database.query("drop table test");
} else {
await database.query("call dolt_branch('-D', 'mybranch')");
}
} catch (err) {
console.error(err);
process.exit(1);
} finally {
database.close();
process.exit(0);
}
}
main();