Create "In Memory Database" in mysql with NodeJs for Jasmine unit testing
Asked Answered
D

2

9

I have a ExpressJs application and unit testing with was done using Jasmine. Currently I'm using real database to run unit tests as well. But I want to do unit testing without using the real database. When I'm searching for a solution I came a cross with the concept of 'In Memory Database'. I read the mysql documentation. And it says we can create a table in memory by giving the engine as Memory

CREATE TABLE t (i INT) ENGINE = MEMORY;

Is there any possibility that I can create a Memory database and run my all unit tests by staring from creating the memory database, creating all tables and inserting data into that database in runtime by using a backedup .sql file?

Depose answered 12/2, 2020 at 4:55 Comment(2)
Of course, you may, if the options supported by MEMORY engine are enough for you. Simply edit your backup file and alter tables engines. Pay attention that the default index type differs from one in on-disk tables.Selfjustifying
Any luck? I am also interested in the answerBedmate
P
0

I noticed, that this question is quite old but as I faced a similar requirement a couple of days ago I decided to provide my solution.

I found a really helpful tool which starts a docker container with a mysql database which then can be used within a unit test.

If you are working with jest you can simply start and stop the container in global setup and teardown like:

"globalSetup": "<rootDir>/node_modules/@databases/mysql-test/jest/globalSetup",
"globalTeardown": "<rootDir>/node_modules/@databases/mysql-test/jest/globalTeardown",
Perforated answered 28/4, 2023 at 13:7 Comment(0)
P
-2

First thing, it is not 'in memory database', it is 'in memory table', and has different restrictions like unsupported TEXT columns and data size.

If you are really desperate to use 100% memory based full database implementation; you should go for RAMDisk & a database instance pointing database files to that RAMDisk space. You can achieve the same with SymLinks too.

Bear in mind that, there are certain things to follow, like you cannot start the database engine without activating the RAMDisk, and yes, of course you need to flush the RAMDisk to a real disk (as files/folders) after shutting down the database engine and must remount them before starting database engine.

There are a couple of tools to do this RAMDisk tasks automated, just Google for thism.

Plead answered 28/4, 2023 at 20:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.