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/test/items_api/migration_test.exs b/test/items_api/migration_test.exs new file mode 100644 index 0000000..6ea8721 --- /dev/null +++ b/test/items_api/migration_test.exs @@ -0,0 +1,38 @@ +defmodule ItemsApi.MigrationTest do + use ExUnit.Case, async: false + + setup do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(ItemsApi.Repo) + :ok + end + + describe "auto-migration on startup" do + test "items table exists after application start" do + # The application has already started (mix test starts it via mod:), + # so migrations should have run. Verify the items table exists. + {:ok, %{rows: tables}} = + Ecto.Adapters.SQL.query(ItemsApi.Repo, "SELECT name FROM sqlite_master WHERE type='table' AND name='items'") + + assert tables == [["items"]] + end + + test "items table has expected columns" do + {:ok, %{rows: columns}} = + Ecto.Adapters.SQL.query(ItemsApi.Repo, "PRAGMA table_info(items)") + + column_names = Enum.map(columns, fn [_cid, name | _rest] -> name end) + assert "id" in column_names + assert "name" in column_names + assert "description" in column_names + assert "inserted_at" in column_names + end + + test "re-running migrations is idempotent" do + migrations_path = Application.app_dir(:items_api, "priv/repo/migrations") + # Running migrations again should not crash + result = Ecto.Migrator.run(ItemsApi.Repo, migrations_path, :up, all: true) + # Returns empty list when all migrations already applied + assert result == [] + end + end +end