Should I use mock or sqlite to make some tests od database on node.js app? - node.js

I want to make some test for the database on my nodejs app. I want to know the pros and cons of using mocks or SqLite to test the database.
I found this question:
Should I use an In-Memory Database instead of mocking out my Repositories?
But it didn't give much information about each of them.

For me it depends. If I'm using an ORM I will use SQLite - I assume that the ORM stuff works as expected and it's easier than mocking the ORM.
If I'm writing my own code to do database stuff, I'll mock it. If I have a database service layer, I'll mock it.
Basically, if I write it I'll mock it and if I use an ORM or library I'll use SQLite.

Related

Are entities required for a NestJS backend?

NestJS encourages the use of TypeORM and entities to interact with database. I have experience with Express, but NestJS, TypeORM and their entities concepts are new to me.
Previously, when I worked with Express back end, I have been using raw SQL statements (and with command line in some cases) to interact with the database (from CRUD to database deployment). NestJS can also connect to an existing database (says, PostgreSQL in this case) without using entities (completely delete entities directory).
With that said, why do we need entities? Is it necessary for a NestJS backend when I'm familiar with raw SQL? And what's a good practice for working with database in NestJS?
Do we need entities?
Nope. Not at all. You can write raw SQL with pg or any other package all you want. It's actually what I do here
Why do we need entities?
It really depends on how you're managing your database schema, in my opinion. If you're letting TypeO/Mikro/prisma manage the schema for you, then entities are useful. If you're using something like Hasura, then the migrations are written in SQL and Hasura generates the types for you (I think prisma might be similar here actually).
Personally, what I prefer doing is making my own mini-orm that deserializes and validates the data from the database before passing it back on to the client. I can write raw SQL this way, and map it to my defined types, rather than relying than on a full ORM.

Integration testing with Sequelize

I've got a Express web api using sequelize that i want to do end to end testing with. I want to be able to do end to end testing with an in memory database so i can run it on whatever machine pleases me.
I use mysql database for development and production, however i was thinking about using an in-memory sqlite database for testing but i'm not sure what the best way is to get test data into it.
There are several modules around like squelize-fixtures but none of them seem to be able to just fill the database with data without the need to write code around it to manipulate and insert it.
Anyone here doing integration tests with sequelize and sqlite that has figured out a good way of doing it without all the boilerplate code?
If you test this way it won't actually be end-to-end testing since you're using a different database.
However, what is wrong with changing your Sequelize dialect to be sqlite and then using sequelize-fixtures to ingest a file of your data? If you feel this is too onerous you could slog your way through it one time and then just save the sqlite db file for future use. You would then instantiate your Sequelize object with
storage: 'path/to/database.sqlite'
No matter what you do or what storage you choose you're going to have to do some work to seed your database.

Why we use ORM or ODM to manage any graphDB?

Hey everyone I am working on nodeJS app. I searched some modules to manage my database (orientdb).
My question is: Why we use any ORM or ODM (or why is it recommenced), because there is a module which can provide many functions to manage DB.
I am still confused what should I use orientorm (https://github.com/mav-im/orientorm) or oriento (https://github.com/codemix/oriento)
Thank in advance..
Depending on the goal and depending on the ORM, ORMs have the advantage of adding support for:
schemas / models / collections: this makes it easier to create all classes/properties and, in some cases, migrations;
validations: make it easier to verify what gets saved in the DB.
All OrientDB ORM's I've seen for node.js expose Oriento, so that makes it easy to access the underlying oriento methods for doing more complex stuff.
Having said all this I recommend you try the waterline ORM with waterline-orientdb adapter. Waterline is an adapter based ORM with support for multiple databases (including support for associations between databases). Waterline-orientdb is the adapter for OrientDB which is based on Oriento. If at any point you need to use Oriento you can call .getDB() to access Oriento's instance.
Oriento is much more mature and supported. I suggest you to go with it.

Unit testing queries with MongoDB

I'm currently building a rest api and I'm struggling to find the best way to unit test each route.
A route handler performs various things and one of them is to execute a query to mongodb. I can unit test the route handler by using stubs, but if I'm testing the query I cannot stub the query itself, I need to have an in-memory mongodb that I could reset and insert new data for each test.
How do you test queries? I'm thinking that the only real way to ensure that the query does what I need is to use a real mongodb database installed in the testing machine (typically in the same machine used for developing).
yes, just as for relation databases, you need to have real base. if mongo offers in-memory auto-created version then it's easy. if not, then each developer has to have running mongo before he runs integration tests. for CI you can have one single dedicated mongo but than you have to prevent concurrent access (schema creation, multiple transactions etc). you should also implement automatic creation of schema if needed and empty database before each test. in relational db rollback is usually enough. when it's not enough then trimming all tables helps. although we had to implement it manually as we couldn't find any existing tools

Mock/Test Mongodb Database Node.js

I am learning nodejs and I have a mongodb database with which i have to interact with. I am currently thinking of using mocha for a unit test framework and zombie.js for a acceptance test framework. I was wondering how could I do full scale acceptance tests which hit the mongodb database. Is there a framework/module that helps with replacing the database with a test database or does either mocha or zombie.js have functionality that can easily be used to replace the database.
Also is there a framework that is similar to the idea of factories (instead of fixtures) in creating database objects.
A similar concept that I have encountered in the rails world is in rspec, there is a spec_helper.rb file which runs before the tests are run which set the projects configuration to decide which database to hit when running tests. And it uses database_cleaner to clean out the test database before tests are run. For factories, i have used Factory girl to create factory objects from database schema again in the rails world.
Thanks
If your database access is a separate module, you can mock out the module or parts of the module (e.g., the configuration part) using one of the following:
sinon
rewire
horaa
sandboxed-module
The answers to the following related question lists a number of possible solutions/approaches:
How do you mock MySQL (without an ORM) in Node.js?
In order to spin up a real in-memory mongodb for testing, https://github.com/nodkz/mongodb-memory-server helps as well.

Resources