Skip to content

Commit dd76462

Browse files
authored
Unit tests (#34)
Adds unit tests for: - `ErrorTracker`. - `report/3` - with a runtime exception - with a bad arithmetic error - with an unknown function error - with a throw - with an exit - `resolve/1` - `unresolve/1` - `Telemetry` - verifies that events are emitted Tests can run either on PostgreSQL or SQLite3. This is determined by the `DB` env var so: - `DB=postgres mix test` runs the tests on PostgreSQL - `DB=sqlite mix test` runs the tests on SQLite3 The CI has been also updated to run tests on both databases. --- I've also unified the configuration under the `config/` folder. And the dev/test migrations under `priv/repo/`. Previously we had some configuration in the root folder and other in `config/`. Regarding the migrations the `priv/repo/` folder is not included when publishing the package but is used in dev/test.
1 parent 0d8ae49 commit dd76462

File tree

22 files changed

+345
-125
lines changed

22 files changed

+345
-125
lines changed

.github/workflows/elixir.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ jobs:
3939
steps:
4040
- uses: actions/checkout@v4
4141

42+
- name: Generate test configuration
43+
run: cp config/test.example.exs config/test.exs
44+
4245
- uses: erlef/setup-beam@v1
4346
with:
4447
otp-version: ${{ matrix.erlang }}
@@ -71,5 +74,12 @@ jobs:
7174
- name: Check Credo warnings
7275
run: mix credo
7376

74-
- name: Run Tests
77+
- name: Run Tests - SQLite3
7578
run: mix test
79+
env:
80+
DB: sqlite
81+
82+
- name: Run Tests - PostgreSQL
83+
run: mix test
84+
env:
85+
DB: postgres

.gitignore

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ error_tracker-*.tar
2525
# Temporary files, for example, from tests.
2626
/tmp/
2727

28-
/assets/node_modules
28+
# Configuration files (only the examples are committed)
29+
/config/dev.exs
30+
/config/test.exs
2931

32+
# Assets
33+
/assets/node_modules
3034

31-
dev.local.exs
32-
dev.db*
35+
# SQLite3 databases
36+
*.db
37+
*.db-shm
38+
*.db-wal

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,24 @@ Take a look at the [Getting Started](/guides/Getting%20Started.md) guide.
1515

1616
## Development
1717

18-
### Development server
18+
### Initial setup and dependencies
1919

20-
We have a `dev.exs` script that starts a development server.
20+
If this is the first time that you set up this project you will to generate the configuration files and adapt their content to your local environment:
2121

22-
To run it together with an `IEx` console you can do:
22+
```
23+
cp config/dev.example.exs config/dev.exs
24+
cp config/test.example.exs config/test.exs
25+
```
26+
27+
Then, you will need to download the dependencies:
2328

2429
```
25-
iex -S mix dev
30+
mix deps.get
2631
```
2732

2833
### Assets
2934

30-
In order to participate in the development of this library, you may need to
31-
know how to compile the assets needed to use the Web UI.
35+
In order to participate in the development of this project, you may need to know how to compile the assets needed to use the Web UI.
3236

3337
To do so, you need to first make a clean build:
3438

@@ -49,3 +53,15 @@ To do so you can execute this task in a separate terminal:
4953
```
5054
mix assets.watch
5155
```
56+
57+
58+
59+
### Development server
60+
61+
We have a `dev.exs` script that starts a development server.
62+
63+
To run it together with an `IEx` console you can do:
64+
65+
```
66+
iex -S mix dev
67+
```

config/config.exs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
11
import Config
22

3-
if config_env() == :dev do
4-
config :tailwind,
5-
version: "3.4.3",
6-
default: [
7-
args: ~w(
8-
--config=tailwind.config.js
9-
--input=css/app.css
10-
--output=../priv/static/app.css
11-
),
12-
cd: Path.expand("../assets", __DIR__)
13-
]
14-
15-
config :bun,
16-
version: "1.1.18",
17-
default: [
18-
args: ~w(build app.js --outdir=../../priv/static),
19-
cd: Path.expand("../assets/js", __DIR__),
20-
env: %{}
21-
]
22-
end
3+
import_config "#{config_env()}.exs"

config/dev.example.exs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Config
2+
3+
config :tailwind,
4+
version: "3.4.3",
5+
default: [
6+
args: ~w(
7+
--config=tailwind.config.js
8+
--input=css/app.css
9+
--output=../priv/static/app.css
10+
),
11+
cd: Path.expand("../assets", __DIR__)
12+
]
13+
14+
config :bun,
15+
version: "1.1.18",
16+
default: [
17+
args: ~w(build app.js --outdir=../../priv/static),
18+
cd: Path.expand("../assets/js", __DIR__),
19+
env: %{}
20+
]
21+
22+
# PostgreSQL adapter
23+
#
24+
# To use SQLite3 on your local development machine uncomment these lines and
25+
# comment the lines of other adapters.
26+
27+
config :error_tracker, :ecto_adapter, :postgres
28+
29+
config :error_tracker, ErrorTrackerDev.Repo,
30+
url: "ecto://postgres:[email protected]/error_tracker_dev"
31+
32+
# SQLite3 adapter
33+
#
34+
# To use SQLite3 on your local development machine uncomment these lines and
35+
# comment the lines of other adapters.
36+
37+
# config :error_tracker, :ecto_adapter, :sqlite3
38+
39+
# config :error_tracker, ErrorTrackerDev.Repo,
40+
# database: System.get_env("SQLITE_DB") || "dev.db"

config/test.example.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Config
2+
3+
config :error_tracker, ErrorTracker.Test.Repo,
4+
url: "ecto://postgres:[email protected]/error_tracker_test",
5+
pool: Ecto.Adapters.SQL.Sandbox,
6+
log: false
7+
8+
config :error_tracker, ErrorTracker.Test.LiteRepo,
9+
database: "priv/lite_repo/test.db",
10+
pool: Ecto.Adapters.SQL.Sandbox,
11+
log: false,
12+
# Use the same migrations as the PostgreSQL repo
13+
priv: "priv/repo"
14+
15+
config :error_tracker, ecto_repos: [ErrorTracker.Test.Repo]
16+
17+
# Repo is selected in the test_helper.exs based on the given ENV vars
18+
config :error_tracker, otp_app: :error_tracker

dev.exs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
#######################################
1010
Logger.configure(level: :debug)
1111

12-
# Get local configuration
13-
Code.require_file("dev.local.exs")
12+
# Get configuration
13+
Config.Reader.read!("config/config.exs", env: :dev)
1414

1515
# Prepare the repo
16-
1716
adapter =
1817
case Application.get_env(:error_tracker, :ecto_adapter) do
1918
:postgres -> Ecto.Adapters.Postgres
@@ -173,13 +172,6 @@ defmodule ErrorTrackerDev.Telemetry do
173172
end
174173
end
175174

176-
defmodule Migration0 do
177-
use Ecto.Migration
178-
179-
def up, do: ErrorTracker.Migration.up(prefix: "private")
180-
def down, do: ErrorTracker.Migration.down(prefix: "private")
181-
end
182-
183175
Application.put_env(:phoenix, :serve_endpoints, true)
184176

185177
Task.async(fn ->
@@ -194,10 +186,7 @@ Task.async(fn ->
194186
{:ok, _} = Supervisor.start_link(children, strategy: :one_for_one)
195187

196188
# Automatically run the migrations on boot
197-
Ecto.Migrator.run(ErrorTrackerDev.Repo, [{0, Migration0}], :up,
198-
all: true,
199-
log_migrations_sql: :debug
200-
)
189+
Ecto.Migrator.run(ErrorTrackerDev.Repo, :up, all: true, log_migrations_sql: :debug)
201190

202191
Process.sleep(:infinity)
203192
end)

dev.local.example.exs

Lines changed: 0 additions & 22 deletions
This file was deleted.

lib/error_tracker/integrations/oban.ex

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ defmodule ErrorTracker.Integrations.Oban do
3636
[:oban, :job, :exception]
3737
]
3838

39-
@doc """
40-
Attaches to Oban's Telemetry events if the library is detected.
41-
42-
This function is usually called internally during the startup process so you
43-
don't have to.
44-
"""
39+
@doc false
4540
def attach do
4641
if Application.spec(:oban) do
4742
:telemetry.attach_many(__MODULE__, @events, &__MODULE__.handle_event/4, :no_config)

lib/error_tracker/integrations/phoenix.ex

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,7 @@ defmodule ErrorTracker.Integrations.Phoenix do
6363
[:phoenix, :live_view, :render, :exception]
6464
]
6565

66-
@doc """
67-
Attaches to Phoenix's Telemetry events if the library is detected.
68-
69-
This function is usually called internally during the startup process so you
70-
don't have to.
71-
"""
66+
@doc false
7267
def attach do
7368
if Application.spec(:phoenix) do
7469
:telemetry.attach_many(__MODULE__, @events, &__MODULE__.handle_event/4, :no_config)

0 commit comments

Comments
 (0)