From efa756402cc174921f619b60dd9b443c129e2952 Mon Sep 17 00:00:00 2001 From: Jack Boberg Date: Tue, 25 Aug 2020 09:04:09 +0200 Subject: [PATCH 1/5] Create GitHub action Run `mix test` --- .github/workflows/mix-test.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 .github/workflows/mix-test.yml diff --git a/.github/workflows/mix-test.yml b/.github/workflows/mix-test.yml new file mode 100644 index 0000000..8e5151f --- /dev/null +++ b/.github/workflows/mix-test.yml @@ -0,0 +1,31 @@ +name: Elixir CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + + name: Build and test + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up Elixir + uses: actions/setup-elixir@v1 + with: + elixir-version: '1.10.3' # Define the elixir version [required] + otp-version: '22.3' # Define the OTP version [required] + - name: Restore dependencies cache + uses: actions/cache@v2 + with: + path: deps + key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }} + restore-keys: ${{ runner.os }}-mix- + - name: Install dependencies + run: mix deps.get + - name: Run tests + run: mix test From fb28bf8ba636b9be370959c267b5296de19e2b13 Mon Sep 17 00:00:00 2001 From: Jack Boberg Date: Tue, 25 Aug 2020 12:10:39 +0200 Subject: [PATCH 2/5] chore: Setup postgres for github action --- .github/workflows/mix-test.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.github/workflows/mix-test.yml b/.github/workflows/mix-test.yml index 8e5151f..191f5a6 100644 --- a/.github/workflows/mix-test.yml +++ b/.github/workflows/mix-test.yml @@ -12,6 +12,20 @@ jobs: name: Build and test runs-on: ubuntu-latest + services: + postgres: + image: postgres + env: + POSTGRES_PASSWORD: postgres + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 + + steps: - uses: actions/checkout@v2 - name: Set up Elixir From 442d64fdecb6326dba87024c2629bd8ed223dc1f Mon Sep 17 00:00:00 2001 From: Jack Boberg Date: Tue, 25 Aug 2020 12:28:01 +0200 Subject: [PATCH 3/5] chore: create DB --- .github/workflows/mix-test.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/mix-test.yml b/.github/workflows/mix-test.yml index 191f5a6..fd6c1d6 100644 --- a/.github/workflows/mix-test.yml +++ b/.github/workflows/mix-test.yml @@ -41,5 +41,9 @@ jobs: restore-keys: ${{ runner.os }}-mix- - name: Install dependencies run: mix deps.get + - name: Create DB + run: mix ecto.create + env: + MIX_ENV: test - name: Run tests run: mix test From 8cd9ff79de6e5189b6107a735d25f5a3b15b7c4d Mon Sep 17 00:00:00 2001 From: Jack Boberg Date: Tue, 25 Aug 2020 12:39:04 +0200 Subject: [PATCH 4/5] attempt to start repo --- test/support/repo_case.ex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/support/repo_case.ex b/test/support/repo_case.ex index ae8d14b..420d369 100644 --- a/test/support/repo_case.ex +++ b/test/support/repo_case.ex @@ -11,6 +11,9 @@ defmodule EctoResource.RepoCase do end setup tags do + pid = Ecto.Adapters.SQL.Sandbox.start_owner!(EctoResource.TestRepo, shared: not tags[:async]) + on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) + :ok = Ecto.Adapters.SQL.Sandbox.checkout(EctoResource.TestRepo) unless tags[:async] do From 37f719d94cda8db4c0688bd24be2ba287863dad3 Mon Sep 17 00:00:00 2001 From: Jack Boberg Date: Tue, 25 Aug 2020 12:46:01 +0200 Subject: [PATCH 5/5] use fake DB??? https://elixirforum.com/t/starting-up-application-a-repo-only-for-tests/28141/2 --- test/support/repo_case.ex | 12 ++++++------ test/test_helper.exs | 31 ++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/test/support/repo_case.ex b/test/support/repo_case.ex index 420d369..d293c80 100644 --- a/test/support/repo_case.ex +++ b/test/support/repo_case.ex @@ -1,9 +1,12 @@ defmodule EctoResource.RepoCase do use ExUnit.CaseTemplate + alias Ecto.Integration.TestRepo + using do quote do - alias EctoResource.TestRepo, as: Repo + alias Ecto.Integration.TestRepo, as: Repo + # alias EctoResource.TestRepo, as: Repo import Ecto import Ecto.Query import EctoResource.RepoCase @@ -11,13 +14,10 @@ defmodule EctoResource.RepoCase do end setup tags do - pid = Ecto.Adapters.SQL.Sandbox.start_owner!(EctoResource.TestRepo, shared: not tags[:async]) - on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) - - :ok = Ecto.Adapters.SQL.Sandbox.checkout(EctoResource.TestRepo) + :ok = Ecto.Adapters.SQL.Sandbox.checkout(TestRepo) unless tags[:async] do - Ecto.Adapters.SQL.Sandbox.mode(EctoResource.TestRepo, {:shared, self()}) + Ecto.Adapters.SQL.Sandbox.mode(TestRepo, {:shared, self()}) end :ok diff --git a/test/test_helper.exs b/test/test_helper.exs index fba854d..97d1887 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,2 +1,31 @@ ExUnit.start() -Ecto.Adapters.SQL.Sandbox.mode(EctoResource.TestRepo, :manual) +# Ecto.Adapters.SQL.Sandbox.mode(EctoResource.TestRepo, :manual) + +alias Ecto.Integration.TestRepo + +Application.put_env( + :ecto, + TestRepo, + adapter: Ecto.Adapters.Postgres, + url: System.get_env("DATABASE_URL", "ecto://localhost/ecto_network_test"), + pool: Ecto.Adapters.SQL.Sandbox +) + +defmodule Ecto.Integration.TestRepo do + use Ecto.Repo, + otp_app: :ecto, + adapter: Ecto.Adapters.Postgres +end + +{:ok, _} = Ecto.Adapters.Postgres.ensure_all_started(TestRepo, :temporary) + +_ = Ecto.Adapters.Postgres.storage_down(TestRepo.config()) +:ok = Ecto.Adapters.Postgres.storage_up(TestRepo.config()) + +{:ok, _pid} = TestRepo.start_link() + +Code.require_file("ecto_migration.exs", __DIR__) + +:ok = Ecto.Migrator.up(TestRepo, 0, Ecto.Integration.Migration, log: false) +Ecto.Adapters.SQL.Sandbox.mode(TestRepo, :manual) +Process.flag(:trap_exit, true)