From 590c861c2b184ee427ec4ccf798fce998869f91b Mon Sep 17 00:00:00 2001 From: Alex Banna Date: Wed, 25 Mar 2026 08:32:40 -0500 Subject: [PATCH 1/2] fix: make Repo.init respect config and add Repo tests - Fix Repo.init/2 to use config database path instead of hardcoding ./data.db - Support DATABASE_PATH env var override for deployment flexibility - Switch test config from :memory: to file-based ./test_data.db for ecto_sqlite3 Sandbox mode compatibility - Add test_data.db to .gitignore - Add Repo tests: query execution, adapter verification, otp_app check Implements: HAR-569 --- .github/workflows/ci.yml | 48 ++++++++++++++++++++++++++++++++++++ .gitignore | 2 ++ config/test.exs | 2 +- lib/items_api/repo.ex | 7 ++++-- test/items_api/repo_test.exs | 24 ++++++++++++++++++ 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 test/items_api/repo_test.exs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..9e57955 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,48 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + branches: [main] + +permissions: + contents: read + +jobs: + test: + name: Build and Test + runs-on: ubuntu-latest + + env: + MIX_ENV: test + ELIXIR_VERSION: "1.17.3" + OTP_VERSION: "27.2" + + steps: + - uses: actions/checkout@v4 + + - name: Set up Elixir + uses: erlef/setup-beam@v1 + with: + elixir-version: ${{ env.ELIXIR_VERSION }} + otp-version: ${{ env.OTP_VERSION }} + + - name: Cache deps + uses: actions/cache@v4 + with: + path: | + deps + _build + key: ${{ runner.os }}-mix-${{ env.ELIXIR_VERSION }}-${{ env.OTP_VERSION }}-${{ hashFiles('mix.lock') }} + restore-keys: | + ${{ runner.os }}-mix-${{ env.ELIXIR_VERSION }}-${{ env.OTP_VERSION }}- + + - name: Install dependencies + run: mix deps.get + + - name: Compile + run: mix compile --warnings-as-errors + + - name: Run tests + run: mix test diff --git a/.gitignore b/.gitignore index 53bde0c..a004d20 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ /deps/ /data.db /data.db-* +/test_data.db +/test_data.db-* erl_crash.dump # Generated files diff --git a/config/test.exs b/config/test.exs index f2c328c..cf32957 100644 --- a/config/test.exs +++ b/config/test.exs @@ -1,5 +1,5 @@ import Config config :items_api, ItemsApi.Repo, - database: ":memory:", + database: "./test_data.db", pool: Ecto.Adapters.SQL.Sandbox diff --git a/lib/items_api/repo.ex b/lib/items_api/repo.ex index ac82d62..1345d2b 100644 --- a/lib/items_api/repo.ex +++ b/lib/items_api/repo.ex @@ -4,7 +4,10 @@ defmodule ItemsApi.Repo do adapter: Ecto.Adapters.SQLite3 def init(_type, config) do - config = Keyword.put(config, :database, "./data.db") - {:ok, config} + database = + System.get_env("DATABASE_PATH") || + Keyword.get(config, :database, "./data.db") + + {:ok, Keyword.put(config, :database, database)} end end diff --git a/test/items_api/repo_test.exs b/test/items_api/repo_test.exs new file mode 100644 index 0000000..445856d --- /dev/null +++ b/test/items_api/repo_test.exs @@ -0,0 +1,24 @@ +defmodule ItemsApi.RepoTest do + use ExUnit.Case, async: false + + setup do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(ItemsApi.Repo) + :ok + end + + describe "Repo" do + test "is running and accepting queries" do + assert {:ok, %{rows: [[1]]}} = Ecto.Adapters.SQL.query(ItemsApi.Repo, "SELECT 1") + end + + test "uses SQLite3 adapter" do + config = ItemsApi.Repo.config() + assert config[:adapter] == Ecto.Adapters.SQLite3 + end + + test "has otp_app set to :items_api" do + config = ItemsApi.Repo.config() + assert config[:otp_app] == :items_api + end + end +end From 6bb26bcfcdc68212ddf6b934b84653dff09117e9 Mon Sep 17 00:00:00 2001 From: Alex Banna Date: Wed, 25 Mar 2026 08:34:12 -0500 Subject: [PATCH 2/2] fix: correct Repo adapter test to use __adapter__/0 Ecto.Repo.config/0 does not include :adapter in its return. Use __adapter__/0 which returns the compile-time adapter module. Implements: HAR-569 --- test/items_api/repo_test.exs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/items_api/repo_test.exs b/test/items_api/repo_test.exs index 445856d..0d6eab3 100644 --- a/test/items_api/repo_test.exs +++ b/test/items_api/repo_test.exs @@ -12,8 +12,7 @@ defmodule ItemsApi.RepoTest do end test "uses SQLite3 adapter" do - config = ItemsApi.Repo.config() - assert config[:adapter] == Ecto.Adapters.SQLite3 + assert ItemsApi.Repo.__adapter__() == Ecto.Adapters.SQLite3 end test "has otp_app set to :items_api" do