advancements in tests and storing messages
This commit is contained in:
52
back/test/mail/saveMessage-test.js
Normal file
52
back/test/mail/saveMessage-test.js
Normal file
@@ -0,0 +1,52 @@
|
||||
process.env["NODE_DEV"] = "TEST";
|
||||
const { saveFromParsedData } = require("../../mails/storeMessage");
|
||||
const { TestDb } = require("../sql/test-utilsDb");
|
||||
const MYSQL = require("../../db/config.json").MYSQL;
|
||||
|
||||
let db;
|
||||
beforeAll(async () => {
|
||||
const options = {
|
||||
databaseOptions: {
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
user: MYSQL.user,
|
||||
password: MYSQL.pwd,
|
||||
database: "mail_test",
|
||||
},
|
||||
dbSchema: "../../sql/structureV2.sql",
|
||||
};
|
||||
db = new TestDb(options);
|
||||
await db.init();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
db.cleanTables();
|
||||
});
|
||||
|
||||
describe("saveMessage", async () => {
|
||||
describe("rooms", async () => {
|
||||
it("messages not related and from same sender should be in the same room", async () => {
|
||||
await saveFromParsedData(
|
||||
{
|
||||
to: { value: [{ address: "address1@email.com" }] },
|
||||
from: { value: [{ address: "address2@email.com" }] },
|
||||
messageId: "<messageId1>",
|
||||
},
|
||||
1,
|
||||
);
|
||||
await saveFromParsedData(
|
||||
{
|
||||
to: { value: [{ address: "address1@email.com" }] },
|
||||
from: { value: [{ address: "address2@email.com" }] },
|
||||
messageId: "<messageId2>",
|
||||
},
|
||||
2,
|
||||
);
|
||||
// todo call parser
|
||||
const query = ""
|
||||
db.execQueryAsync().then((res) => {
|
||||
expect(res.length).toBe(2);
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
19
back/test/sql/saveMessageApp-test.js
Normal file
19
back/test/sql/saveMessageApp-test.js
Normal file
@@ -0,0 +1,19 @@
|
||||
beforeAll(async () => {
|
||||
// const schema = fs.readFileSync('../../sql/structureV2.sql', 'utf8');
|
||||
// await setupDB(mysqlConfig, schema);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
global.db.query("DROP database mail_test");
|
||||
});
|
||||
|
||||
describe('saveMessageApp', async () => {
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
});
|
||||
|
||||
it("", () => {
|
||||
|
||||
});
|
||||
});
|
||||
62
back/test/sql/test-utilsDb.js
Normal file
62
back/test/sql/test-utilsDb.js
Normal file
@@ -0,0 +1,62 @@
|
||||
const mysql = require("mysql");
|
||||
|
||||
export class TestDb {
|
||||
constructor (options) {
|
||||
this.options = options;
|
||||
this.db;
|
||||
}
|
||||
|
||||
async init () {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db = mysql.createConnection({
|
||||
host: this.options.databaseOptions.host,
|
||||
user: this.options.databaseOptions.user,
|
||||
password: this.options.databaseOptions.pwd,
|
||||
database: this.options.databaseOptions.database,
|
||||
});
|
||||
|
||||
this.db.connect(async function (err) {
|
||||
if (err) {
|
||||
reject("Impossible de se connecter", err.code)
|
||||
} else {
|
||||
if (this.options.dbSchema) {
|
||||
const schema = fs.readFileSync(this.options.dbSchema, 'utf8');
|
||||
await this.execQueryAsync(schema, []);
|
||||
resolve("Database successfully connected and created !")
|
||||
} else {
|
||||
resolve("Database successfully connected");
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
cleanTables() {
|
||||
const query = "SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_schema = ?";
|
||||
const values = [this.options.databaseOptions.database];
|
||||
this.execQueryAsync(query, values).then((results) => {
|
||||
this.execQuery("SET FOREIGN_KEY_CHECKS=0");
|
||||
results.map((table) => {
|
||||
execQuery("DROP TABLE " + table.table_name);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
execQuery(query, values) {
|
||||
db.query(query, values, (err, results, fields) => {
|
||||
});
|
||||
}
|
||||
|
||||
async execQueryAsync(query, values) {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.query(query, values, (err, results, fields) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user