Skip to content

Commit 64e4033

Browse files
committed
Custom .env for tests
1 parent 2eef18b commit 64e4033

File tree

5 files changed

+73
-16
lines changed

5 files changed

+73
-16
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ web-frontend/plugins/
100100
backend/plugins/
101101
web-frontend/reports/
102102
backend/reports/
103+
backend/.env.testing*
103104

104105
.idea/
105106
*.iml

backend/src/baserow/config/settings/test.py

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
# flake8: noqa: F405
2+
import os
23
from copy import deepcopy
4+
from unittest.mock import patch
35

4-
from .base import * # noqa: F403, F401
6+
from dotenv import dotenv_values
7+
8+
# Create a .env.testing file in the backend directory to store different test settings and
9+
# override the default ones. For different test settings, provide the TEST_ENV_FILE
10+
# environment variable with the name of the file to use. Everything that starts with
11+
# .env.testing will be ignored by git.
12+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
13+
TEST_ENV_FILE = os.getenv("TEST_ENV_FILE", ".env.testing")
14+
TEST_ENV_VARS = dotenv_values(os.path.join(BASE_DIR, f"../../../{TEST_ENV_FILE}"))
15+
16+
17+
def getenv_for_tests(key: str, default: str = "") -> str:
18+
return TEST_ENV_VARS.get(key, default)
19+
20+
21+
with patch("os.getenv", getenv_for_tests) as load_dotenv:
22+
# Avoid loading .env settings to prevent conflicts with the test settings,
23+
# but allow custom settings to be loaded from the .env.test file in the
24+
# backend root directory.
25+
from .base import * # noqa: F403, F401
526

627
TESTS = True
728

@@ -67,20 +88,6 @@
6788
if "baserow.middleware.ConcurrentUserRequestsMiddleware" in MIDDLEWARE:
6889
MIDDLEWARE.remove("baserow.middleware.ConcurrentUserRequestsMiddleware")
6990

70-
71-
BASEROW_OPENAI_API_KEY = None
72-
BASEROW_OPENAI_ORGANIZATION = None
73-
BASEROW_OPENAI_MODELS = []
74-
BASEROW_OPENROUTER_API_KEY = None
75-
BASEROW_OPENROUTER_ORGANIZATION = None
76-
BASEROW_OPENROUTER_MODELS = []
77-
BASEROW_ANTHROPIC_API_KEY = None
78-
BASEROW_ANTHROPIC_MODELS = []
79-
BASEROW_MISTRAL_API_KEY = None
80-
BASEROW_MISTRAL_MODELS = []
81-
BASEROW_OLLAMA_HOST = None
82-
BASEROW_OLLAMA_MODELS = []
83-
8491
PUBLIC_BACKEND_URL = "http://localhost:8000"
8592
PUBLIC_WEB_FRONTEND_URL = "http://localhost:3000"
8693
BASEROW_EMBEDDED_SHARE_URL = "http://localhost:3000"

config/vscode/.vscode/env

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
DATABASE_HOST=localhost
1+
TEST_ENV_FILE=.env.testing-local
22
PYTHONPATH=backend/src:premium/backend/src:enterprise/backend/src:backend/flake8_plugins:$PYTHONPATH
33
DJANGO_SETTINGS_MODULE=baserow.config.settings.test

docs/development/running-tests.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Running Tests
2+
3+
## Backend
4+
5+
To run backend tests, start and attach to the backend container as described in
6+
[running-the-dev-environment.md](running-the-dev-environment.md). Once inside
7+
the container, execute `make test` or `make test-parallel` to run the tests.
8+
9+
The tests use the `config.settings.tests` configuration, which sets base
10+
variables and ignores environment variables in the `.env` file. The `.env` file
11+
is intended for production or development mode.
12+
13+
### Customize Test Settings
14+
15+
You can customize test settings by creating a `.env.testing` file in the
16+
backend directory. For example:
17+
18+
```env
19+
# backend/.env.testing
20+
BASEROW_MAX_FIELD_LIMIT=1
21+
```
22+
23+
### Running Tests Outside the Backend Container
24+
25+
To run tests outside the backend container, follow these steps:
26+
27+
1. Create a Python virtual environment. See [supported](../installation/supported.md)
28+
to determine the supported version of Python.
29+
2. From the backend directory, install the required packages with
30+
`pip install requirements/base.txt` and `pip install requirements/dev.txt`.
31+
3. Set environment variables to connect to the database. Create a
32+
`.env.testing-local` file in the backend directory. At a minimum, set
33+
`DATABASE_HOST` to `localhost` since the default value of `db` is only valid
34+
inside the docker network.
35+
36+
```env
37+
# backend/.env.testing-local
38+
DATABASE_HOST=localhost
39+
```
40+
41+
4. Export the `TEST_ENV_FILE` variable to specify the environment file:
42+
43+
```sh
44+
export TEST_ENV_FILE='.env.testing-local'
45+
```
46+
47+
5. Run `make test` or `make test-parallel` from your shell outside the
48+
containers in the backend directory.

docs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Everything related to contributing and developing for Baserow.
112112
and when to add your own.
113113
* [Metrics and Logs](./development/metrics-and-logs.md): How to work with metrics and logs
114114
to aid with monitoring Baserow as a developer.
115+
* [Backend Tests](development/running-tests.md): A guide on how to run python tests for the backend.
115116

116117
## Plugins
117118

0 commit comments

Comments
 (0)