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..0d6eab3 --- /dev/null +++ b/test/items_api/repo_test.exs @@ -0,0 +1,23 @@ +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 + assert ItemsApi.Repo.__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