- MySQL is preferred - however tests can fallback on SQLite too
- MySQL is preferrable - however we fallback to SQLite
### Setup
### Setup
- All the tests are in `packages/nocodb` folder, which will be our working directory. Use the following command to get into that folder.
```bash
```bash
cd packages/nocodb
cd packages/nocodb
```
- Install the dependencies for `nocodb` package
```bash
npm install
npm install
```
#### Environment variables
- Add your `env` file with the following command
# add a .env file
``` bash
cp tests/unit/.env.sample tests/unit/.env
cp tests/unit/.env.sample tests/unit/.env
```
- Open the `.env` file
``` bash
# open .env file
open tests/unit/.env
open tests/unit/.env
````
```
Configure the following variables
- Configure the following variables
> DB_HOST : host </br>
> DB_PORT : port </br>
> DB_USER : mysql username </br>
> DB_USER : username </br>
> DB_PASSWORD : mysql password </br>
> DB_PASSWORD : password </br>
> DB_HOST : mysql host </br>
> DB_PORT : mysql port </br>
### How to run tests
### Run Tests
``` bash
``` bash
npm run test:unit
npm run test:unit
```
```
### Key points
## Unit tests : Notes
- All individual unit tests are independent of each other. We don't use any shared state between tests.
- All individual unit tests are independent of each other. We don't use any shared state between tests.
- Test environment includes `sakila` sample database and any change to it by a test is reverted before running other tests.
- Test environment includes `sakila` sample database and any change to it by a test is reverted before running other tests.
- While running unit tests, it tries to connect to mysql server running on `localhost:3306` with username `root` and password `password`(which can be configured) and if not found, it will use `sqlite` as a fallback, hence no requirement of any sql server to run tests.
- While running unit tests, it tries to connect to mysql server running on `localhost:3306` with username `root` and password `password`(which can be configured) and if not found, it will use `sqlite` as a fallback, hence no requirement of any sql server to run tests.
### Folder structure
The root folder for unit tests is `packages/tests/unit`
- `rest` folder contains all the test suites for rest apis.
- `model` folder contains all the test suites for models.
- `factory` folder contains all the helper functions to create test data.
- `init` folder contains helper functions to configure test environment.
- `index.test.ts` is the root test suite file which imports all the test suites.
- `TestDbMngr.ts` is a helper class to manage test databases (i.e. creating, dropping, etc.).
### Patterns to follow
- **Factories**
- Use factories for create/update/delete data. No data should be directly create/updated/deleted in the test.
- While writing a factory make sure that it can be used with as less parameters as possible and use default values for other parameters.
- Use named parameters for factories.
``` typescript
createUser({ email, password})
```
- Use one file per factory.
### Walk through of writing a unit test
### Walk through of writing a unit test
We will create an `Table` test suite as an example.
We will create an `Table` test suite as an example.
@ -153,33 +158,8 @@ export default function () {
We can then import the `Table` test suite to `Rest` test suite in `packages/nocodb/tests/unit/rest/index.test.ts` file(`Rest` test suite is imported in the root test suite file which is `packages/nocodb/tests/unit/index.test.ts`).
We can then import the `Table` test suite to `Rest` test suite in `packages/nocodb/tests/unit/rest/index.test.ts` file(`Rest` test suite is imported in the root test suite file which is `packages/nocodb/tests/unit/index.test.ts`).
### Folder structure
The root folder for unit tests is `packages/tests/unit`
- `rest` folder contains all the test suites for rest apis.
- `model` folder contains all the test suites for models.
- `factory` folder contains all the helper functions to create test data.
- `init` folder contains helper functions to configure test environment.
- `index.test.ts` is the root test suite file which imports all the test suites.
- `TestDbMngr.ts` is a helper class to manage test databases (i.e. creating, dropping, etc.).
### Patterns to follow
- **Factories**
- Use factories for create/update/delete data. No data should be directly create/updated/deleted in the test.
- While writing a factory make sure that it can be used with as less parameters as possible and use default values for other parameters.
- Use named parameters for factories.
``` typescript
createUser({ email, password})
```
- Use one file per factory.
### Using sakila db
To use sakila db use `createSakilaProject` from `factory/project` to create a project. This project will be seeded with `sakila` tables.