From f8a8df787ef7d8d036d87f7028386d6a86d6bf2f Mon Sep 17 00:00:00 2001 From: Christopher Lamb Date: Wed, 29 Mar 2023 11:46:03 -0400 Subject: [PATCH 1/2] Remove the database. --- Procfile | 1 - config/config.exs | 7 +- config/dev.exs | 8 -- config/prod.exs | 5 - config/test.exs | 15 -- elixir_buildpack.config | 4 - lib/api_mock/application.ex | 2 - lib/api_mock/crew/crew.ex | 136 +++++++++++------- lib/api_mock/crew/users.ex | 10 +- lib/api_mock/repo.ex | 13 -- .../controllers/users_controller.ex | 24 ---- lib/api_mock_web/router.ex | 2 +- mix.exs | 4 - mix.lock | 48 +++---- .../20180627160454_create_users.exs | 15 -- priv/repo/seeds.exs | 87 ----------- test/support/channel_case.ex | 9 -- test/support/conn_case.ex | 6 - test/support/data_case.ex | 20 --- test/test_helper.exs | 2 - 20 files changed, 113 insertions(+), 305 deletions(-) delete mode 100644 Procfile delete mode 100644 elixir_buildpack.config delete mode 100644 lib/api_mock/repo.ex delete mode 100644 priv/repo/migrations/20180627160454_create_users.exs delete mode 100644 priv/repo/seeds.exs diff --git a/Procfile b/Procfile deleted file mode 100644 index 66c4e41..0000000 --- a/Procfile +++ /dev/null @@ -1 +0,0 @@ -web: MIX_ENV=prod mix phx.server diff --git a/config/config.exs b/config/config.exs index e4a0697..3bc26c6 100644 --- a/config/config.exs +++ b/config/config.exs @@ -6,16 +6,11 @@ use Mix.Config # General application configuration -config :api_mock, - ecto_repos: [ApiMock.Repo] - # Configures the endpoint config :api_mock, ApiMockWeb.Endpoint, url: [host: "localhost"], secret_key_base: "BqOwcYcO6yzbc8qp6e4SxlldOLILuvJ8JS+EfaJAMC8xqrgHXtdgxP2BmbVIGZ0Q", - render_errors: [view: ApiMockWeb.ErrorView, accepts: ~w(json)], - pubsub: [name: ApiMock.PubSub, - adapter: Phoenix.PubSub.PG2] + render_errors: [view: ApiMockWeb.ErrorView, accepts: ~w(json)] # Use Jason for JSON parsing in Phoenix config :phoenix, :json_library, Jason diff --git a/config/dev.exs b/config/dev.exs index c4b356e..b219393 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -35,11 +35,3 @@ config :logger, :console, format: "[$level] $message\n" # Set a higher stacktrace during development. Avoid configuring such # in production as building large stacktraces may be expensive. config :phoenix, :stacktrace_depth, 20 - -# Configure your database -config :api_mock, ApiMock.Repo, - username: "apimock", - password: "apimock", - database: "api_mock_dev", - hostname: "localhost", - pool_size: 10 diff --git a/config/prod.exs b/config/prod.exs index c8858f3..6724796 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -23,11 +23,6 @@ config :api_mock, ApiMockWeb.Endpoint, # Do not print debug messages in production config :logger, level: :info -config :api_mock, ApiMock.Repo, - url: System.get_env("DATABASE_URL"), - pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"), - ssl: true - # ## SSL Support # # To get SSL working, you will need to add the `https` key diff --git a/config/test.exs b/config/test.exs index e8a769b..37cd03f 100644 --- a/config/test.exs +++ b/config/test.exs @@ -8,18 +8,3 @@ config :api_mock, ApiMockWeb.Endpoint, # Print only warnings and errors during test config :logger, level: :warn - -# Configure your database -config :api_mock, ApiMock.Repo, - username: "apimock", - password: "apimock", - database: "api_mock_test", - hostname: "localhost", - pool: Ecto.Adapters.SQL.Sandbox - -# Configure the database for GitHub Actions -if System.get_env("GITHUB_ACTIONS") do - config :api_mock, ApiMock.Repo, - username: "postgres", - password: "postgres" -end diff --git a/elixir_buildpack.config b/elixir_buildpack.config deleted file mode 100644 index 0fbb054..0000000 --- a/elixir_buildpack.config +++ /dev/null @@ -1,4 +0,0 @@ -erlang_version=21.1.2 -elixir_version=1.7.4 -always_rebuild=false -runtime_path=/app diff --git a/lib/api_mock/application.ex b/lib/api_mock/application.ex index 1d8adae..ba4e481 100644 --- a/lib/api_mock/application.ex +++ b/lib/api_mock/application.ex @@ -11,8 +11,6 @@ defmodule ApiMock.Application do # Define workers and child supervisors to be supervised children = [ - # Start the Ecto repository - supervisor(ApiMock.Repo, []), # Start the endpoint when the application starts supervisor(ApiMockWeb.Endpoint, []), # Start your own worker by calling: ApiMock.Worker.start_link(arg1, arg2, arg3) diff --git a/lib/api_mock/crew/crew.ex b/lib/api_mock/crew/crew.ex index fbe0303..483573d 100644 --- a/lib/api_mock/crew/crew.ex +++ b/lib/api_mock/crew/crew.ex @@ -3,9 +3,6 @@ defmodule ApiMock.Crew do The Crew context. """ - import Ecto.Query, warn: false - alias ApiMock.Repo - alias ApiMock.Crew.Users @doc """ @@ -18,7 +15,7 @@ defmodule ApiMock.Crew do """ def list_users do - Repo.all(Users) + list_users_json end @doc """ @@ -35,7 +32,10 @@ defmodule ApiMock.Crew do ** (Ecto.NoResultsError) """ - def get_users!(id), do: Repo.get!(Users, id) + def get_users!(id) do + list_users + |> Enum.find(nil, fn u -> u[:id] == id end ) + end @doc """ Creates a users. @@ -52,53 +52,87 @@ defmodule ApiMock.Crew do def create_users(attrs \\ %{}) do %Users{} |> Users.changeset(attrs) - |> Repo.insert() - end - - @doc """ - Updates a users. - - ## Examples - - iex> update_users(users, %{field: new_value}) - {:ok, %Users{}} - - iex> update_users(users, %{field: bad_value}) - {:error, %Ecto.Changeset{}} - - """ - def update_users(%Users{} = users, attrs) do - users - |> Users.changeset(attrs) - |> Repo.update() end - @doc """ - Deletes a Users. - - ## Examples - - iex> delete_users(users) - {:ok, %Users{}} - - iex> delete_users(users) - {:error, %Ecto.Changeset{}} - - """ - def delete_users(%Users{} = users) do - Repo.delete(users) - end - - @doc """ - Returns an `%Ecto.Changeset{}` for tracking users changes. - - ## Examples - - iex> change_users(users) - %Ecto.Changeset{source: %Users{}} - - """ - def change_users(%Users{} = users) do - Users.changeset(users, %{}) + def list_users_json() do + [ + %{name: "Philip J Fry", + position: "Delivery Boy", + id: "3aa03fb8-57c2-4926-8ab5-51459b477fd1", + profile: %{ + gender: "M", + species: "Human", + age: 25, + planet: "Earth", + status: "Alive" + }, + company: "Planet Express"}, + %{name: "Turanga Leela", + position: "Captain", + id: "5b829f59-ee8d-481e-9980-5aa8f9e46953", + profile: %{ + gender: "F", + species: "Mutant, Human", + age: 25, + planet: "Earth", + status: "Alive" + }, + company: "Planet Express"}, + %{name: "Bender Bending Rodríguez", + position: "Ship's Robot, Ship's Cook, Assistant Manager of Sales, Gunnery Chief", + id: "04921ff5-ccfe-40b1-9c92-1a369ea116a6", + profile: %{ + gender: "M", + species: "Robot", + age: 4, + planet: "Earth", + status: "Alive" + }, + company: "Planet Express"}, + %{name: "Planet Express Ship", + position: "Ship", + id: "33190659-6e73-4162-80ff-563cb2b256e1", + profile: %{ + gender: "N/A", + species: "Robotic Spacecraft", + age: "N/A", + planet: "Earth", + status: "N/A" + }, + company: "Planet Express"}, + %{name: "Dr. John A. Zoidberg Ph.D", + position: "Staff Doctor", + id: "88b5b4a0-c795-48a7-a6a2-5b344c760a94", + profile: %{ + gender: "M", + species: "Decapodian", + age: 86, + planet: "Decapod 10", + status: "Alive", + }, + company: "Planet Express"}, + %{name: "Zapp Brannigan", + position: "Captain", + id: "bc1e90a7-0aa5-47a9-8a64-98d4f0f2ade8", + profile: %{ + gender: "M", + species: "Human", + age: 28, + planet: "Earth", + status: "Alive" + }, + company: "Democratic Order of Planets"}, + %{name: "Kif Kroker", + position: "Lieutenant", + id: "d310ae2a-d301-497a-9956-e1394521ac26", + profile: %{ + gender: "M", + species: "Amphibiosans", + age: 24, + planet: "Amphibios 9", + status: "Alive" + }, + company: "Democratic Order of Planets"} + ] end end diff --git a/lib/api_mock/crew/users.ex b/lib/api_mock/crew/users.ex index 7646bff..c562559 100644 --- a/lib/api_mock/crew/users.ex +++ b/lib/api_mock/crew/users.ex @@ -3,7 +3,8 @@ defmodule ApiMock.Crew.Users do This module defines the Users model that are part of the Crew. """ use Ecto.Schema - import Ecto.Changeset + + @primary_key {:id, :binary_id, autogenerate: false} schema "users" do field :profile, :map @@ -13,11 +14,4 @@ defmodule ApiMock.Crew.Users do timestamps() end - - @doc false - def changeset(users, attrs) do - users - |> cast(attrs, [:profile, :name, :position, :company]) - |> validate_required([:name, :position, :company]) - end end diff --git a/lib/api_mock/repo.ex b/lib/api_mock/repo.ex deleted file mode 100644 index 2fcca0c..0000000 --- a/lib/api_mock/repo.ex +++ /dev/null @@ -1,13 +0,0 @@ -defmodule ApiMock.Repo do - use Ecto.Repo, - otp_app: :api_mock, - adapter: Ecto.Adapters.Postgres - - @doc """ - Dynamically loads the repository url from the - DATABASE_URL environment variable. - """ - def init(_, opts) do - {:ok, Keyword.put(opts, :url, System.get_env("DATABASE_URL"))} - end -end diff --git a/lib/api_mock_web/controllers/users_controller.ex b/lib/api_mock_web/controllers/users_controller.ex index cfc27d2..fdbe57a 100644 --- a/lib/api_mock_web/controllers/users_controller.ex +++ b/lib/api_mock_web/controllers/users_controller.ex @@ -11,32 +11,8 @@ defmodule ApiMockWeb.UsersController do render(conn, "index.json", users: users) end - def create(conn, %{"users" => users_params}) do - with {:ok, %Users{} = users} <- Crew.create_users(users_params) do - conn - |> put_status(:created) - |> put_resp_header("location", users_path(conn, :show, users)) - |> render("show.json", users: users) - end - end - def show(conn, %{"id" => id}) do users = Crew.get_users!(id) render(conn, "show.json", users: users) end - - def update(conn, %{"id" => id, "users" => users_params}) do - users = Crew.get_users!(id) - - with {:ok, %Users{} = users} <- Crew.update_users(users, users_params) do - render(conn, "show.json", users: users) - end - end - - def delete(conn, %{"id" => id}) do - users = Crew.get_users!(id) - with {:ok, %Users{}} <- Crew.delete_users(users) do - send_resp(conn, :no_content, "") - end - end end diff --git a/lib/api_mock_web/router.ex b/lib/api_mock_web/router.ex index c878ebe..e0fbe8c 100644 --- a/lib/api_mock_web/router.ex +++ b/lib/api_mock_web/router.ex @@ -7,6 +7,6 @@ defmodule ApiMockWeb.Router do scope "/api", ApiMockWeb do pipe_through :api - resources "/users", UsersController, except: [:new, :edit] + resources "/users", UsersController, only: [:index, :show] end end diff --git a/mix.exs b/mix.exs index ef0ae3d..6a75453 100644 --- a/mix.exs +++ b/mix.exs @@ -37,7 +37,6 @@ defmodule ApiMock.Mixfile do {:phoenix_pubsub, "~> 1.0"}, {:ecto_sql, "~> 3.0"}, {:phoenix_ecto, "~> 4.0"}, - {:postgrex, ">= 0.0.0"}, {:jason, "~> 1.0"}, {:gettext, "~> 0.11"}, {:plug_cowboy, "~> 2.0"}, @@ -55,9 +54,6 @@ defmodule ApiMock.Mixfile do # See the documentation for `Mix` for more info on aliases. defp aliases do [ - "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"], - "ecto.reset": ["ecto.drop", "ecto.setup"], - test: ["ecto.create --quiet", "ecto.migrate", "test"] ] end end diff --git a/mix.lock b/mix.lock index c84a680..c70d76e 100644 --- a/mix.lock +++ b/mix.lock @@ -1,26 +1,26 @@ %{ - "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"}, - "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"}, - "corsica": {:hex, :corsica, "1.1.3", "5f1de40bc9285753aa03afbdd10c364dac79b2ddbf2ba9c5c9c47b397ec06f40", [:mix], [{:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "8156b3a14a114a346262871333a931a1766b2597b56bf994fcfcb65443a348ad"}, - "cowboy": {:hex, :cowboy, "2.6.0", "dc1ff5354c89e36a3e3ef8d10433396dcff0dcbb1d4223b58c64c2d51a6d88d9", [:rebar3], [{:cowlib, "~> 2.7.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "ff7cf0fb7a2762f423d845468b32ed4edb5c7e0d78e6875b0628153239a7bb34"}, - "cowlib": {:hex, :cowlib, "2.7.0", "3ef16e77562f9855a2605900cedb15c1462d76fb1be6a32fc3ae91973ee543d2", [:rebar3], [], "hexpm", "59f952d504b9921a6f53226fdb8b4a671bb694277c7ccf1ddeaadcdc6d43d88e"}, - "credo": {:hex, :credo, "1.4.0", "92339d4cbadd1e88b5ee43d427b639b68a11071b6f73854e33638e30a0ea11f5", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "1fd3b70dce216574ce3c18bdf510b57e7c4c85c2ec9cad4bff854abaf7e58658"}, - "db_connection": {:hex, :db_connection, "2.0.2", "440c05518b0bdca0469dafaf45403597430448c1281def14ef9ccaa41833ea1e", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "da1cfb8ba1e2b77b594305ea62cce8d8c64776199f0d9a849718c4f0fceb9ad2"}, - "decimal": {:hex, :decimal, "1.5.0", "b0433a36d0e2430e3d50291b1c65f53c37d56f83665b43d79963684865beab68", [:mix], [], "hexpm", "130926580655f34d759dd25f5d723fd233c9bbe0399cde57e2a1adea9ed92e08"}, - "ecto": {:hex, :ecto, "3.0.3", "018a3df0956636f84eb3033d807485a7d3dea8474f47b90da5cb8073444c4384", [:mix], [{:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm", "30b0956495d12b53dbabe7965bfb40e4cd5c49c2f0b051b23fb94ac2854006ea"}, - "ecto_sql": {:hex, :ecto_sql, "3.0.0", "8d1883376bee02a0e76b5ef797e39d04333c34b9935d0b4785dbf3cbdb571e2a", [:mix], [{:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.0.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.9.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.14.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.2.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6fa4c5d69c4fd5be1a1a28fdc0fe98c8d9277eeb505c6da8f316d01330fa56e1"}, - "gettext": {:hex, :gettext, "0.16.1", "e2130b25eebcbe02bb343b119a07ae2c7e28bd4b146c4a154da2ffb2b3507af2", [:mix], [], "hexpm", "dd3a7ea5e3e87ee9df29452dd9560709b4c7cc8141537d0b070155038d92bdf1"}, - "jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fdf843bca858203ae1de16da2ee206f53416bbda5dc8c9e78f43243de4bc3afe"}, - "mime": {:hex, :mime, "1.3.0", "5e8d45a39e95c650900d03f897fbf99ae04f60ab1daa4a34c7a20a5151b7a5fe", [:mix], [], "hexpm", "5e839994289d60326aa86020c4fbd9c6938af188ecddab2579f07b66cd665328"}, - "phoenix": {:hex, :phoenix, "1.4.0", "56fe9a809e0e735f3e3b9b31c1b749d4b436e466d8da627b8d82f90eaae714d2", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm", "22da8f659cf13d3ba73b767f66b8c389113ddf0ef7b94225cc84e94b85eac90e"}, - "phoenix_ecto": {:hex, :phoenix_ecto, "4.0.0", "c43117a136e7399ea04ecaac73f8f23ee0ffe3e07acfcb8062fe5f4c9f0f6531", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "fe15d9fee5b82f5e64800502011ffe530650d42e1710ae9b14bc4c9be38bf303"}, - "phoenix_pubsub": {:hex, :phoenix_pubsub, "1.1.1", "6668d787e602981f24f17a5fbb69cc98f8ab085114ebfac6cc36e10a90c8e93c", [:mix], [], "hexpm", "a3d890aaa3156d51056179dcaaadaf32b844f71656bb27c58756f2b97875c36c"}, - "plug": {:hex, :plug, "1.7.1", "8516d565fb84a6a8b2ca722e74e2cd25ca0fc9d64f364ec9dbec09d33eb78ccd", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}], "hexpm", "daa5fee4209c12c3c48b05a96cf88c320b627c9575f987554dcdc1fdcdf2c15e"}, - "plug_cowboy": {:hex, :plug_cowboy, "2.0.0", "ab0c92728f2ba43c544cce85f0f220d8d30fc0c90eaa1e6203683ab039655062", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "fa9087d93f4962d099b9c4246dbea97c2f2e6aab1029e8b8206e733a1274cecc"}, - "plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm", "73c1682f0e414cfb5d9b95c8e8cd6ffcfdae699e3b05e1db744e58b7be857759"}, - "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"}, - "poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], [], "hexpm"}, - "postgrex": {:hex, :postgrex, "0.14.0", "f3d6ffea1ca8a156e0633900a5338a3d17b00435227726baed8982718232b694", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "ee6d8bc31ec7064f87030b98e431cf7ef0537052339de289b575b0a8872f501e"}, - "ranch": {:hex, :ranch, "1.7.0", "9583f47160ca62af7f8d5db11454068eaa32b56eeadf984d4f46e61a076df5f2", [:rebar3], [], "hexpm", "59f7501c3a56125b2fc5684c3048fac9d043c0bf4d173941b12ca927949af189"}, - "telemetry": {:hex, :telemetry, "0.2.0", "5b40caa3efe4deb30fb12d7cd8ed4f556f6d6bd15c374c2366772161311ce377", [:mix], [], "hexpm", "4e9071b8d1795d0f1ae00584594c3faf430c88821b69e4bd09b02e7840231f32"}, + "bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"}, + "connection": {:hex, :connection, "1.1.0", "ff2a49c4b75b6fb3e674bfc5536451607270aac754ffd1bdfe175abe4a6d7a68", [:mix], [], "hexpm", "722c1eb0a418fbe91ba7bd59a47e28008a189d47e37e0e7bb85585a016b2869c"}, + "corsica": {:hex, :corsica, "1.3.0", "bbec02ccbeca1fdf44ee23b25a8ae32f7c6c28fc127ef8836dd8420e8f65bd9b", [:mix], [{:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "8847ec817554047e9aa6d9933539cacb10c4ee60b58e0c15c3b380c5b737b35f"}, + "cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"}, + "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"}, + "cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"}, + "credo": {:hex, :credo, "1.7.0", "6119bee47272e85995598ee04f2ebbed3e947678dee048d10b5feca139435f75", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "6839fcf63d1f0d1c0f450abc8564a57c43d644077ab96f2934563e68b8a769d7"}, + "db_connection": {:hex, :db_connection, "2.4.3", "3b9aac9f27347ec65b271847e6baeb4443d8474289bd18c1d6f4de655b70c94d", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c127c15b0fa6cfb32eed07465e05da6c815b032508d4ed7c116122871df73c12"}, + "decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, + "ecto": {:hex, :ecto, "3.9.5", "9f0aa7ae44a1577b651c98791c6988cd1b69b21bc724e3fd67090b97f7604263", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d4f3115d8cbacdc0bfa4b742865459fb1371d0715515842a1fb17fe31920b74c"}, + "ecto_sql": {:hex, :ecto_sql, "3.9.2", "34227501abe92dba10d9c3495ab6770e75e79b836d114c41108a4bf2ce200ad5", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.9.2", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "1eb5eeb4358fdbcd42eac11c1fbd87e3affd7904e639d77903c1358b2abd3f70"}, + "expo": {:hex, :expo, "0.4.1", "1c61d18a5df197dfda38861673d392e642649a9cef7694d2f97a587b2cfb319b", [:mix], [], "hexpm", "2ff7ba7a798c8c543c12550fa0e2cbc81b95d4974c65855d8d15ba7b37a1ce47"}, + "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, + "gettext": {:hex, :gettext, "0.22.1", "e7942988383c3d9eed4bdc22fc63e712b655ae94a672a27e4900e3d4a2c43581", [:mix], [{:expo, "~> 0.4.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "ad105b8dab668ee3f90c0d3d94ba75e9aead27a62495c101d94f2657a190ac5d"}, + "jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"}, + "mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"}, + "phoenix": {:hex, :phoenix, "1.4.6", "8535f4a01291f0fbc2c30c78c4ca6a2eacc148db5178ad76e8b2fc976c590115", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm", "17aa6f4909e41eebfa7589b61c71f0ebe8fdea997194fd85596e629bbf8d3e15"}, + "phoenix_ecto": {:hex, :phoenix_ecto, "4.4.0", "0672ed4e4808b3fbed494dded89958e22fb882de47a97634c0b13e7b0b5f7720", [:mix], [{:ecto, "~> 3.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "09864e558ed31ee00bd48fcc1d4fc58ae9678c9e81649075431e69dbabb43cc1"}, + "phoenix_pubsub": {:hex, :phoenix_pubsub, "1.1.2", "496c303bdf1b2e98a9d26e89af5bba3ab487ba3a3735f74bf1f4064d2a845a3e", [:mix], [], "hexpm", "1f13f9f0f3e769a667a6b6828d29dec37497a082d195cc52dbef401a9b69bf38"}, + "plug": {:hex, :plug, "1.14.2", "cff7d4ec45b4ae176a227acd94a7ab536d9b37b942c8e8fa6dfc0fff98ff4d80", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "842fc50187e13cf4ac3b253d47d9474ed6c296a8732752835ce4a86acdf68d13"}, + "plug_cowboy": {:hex, :plug_cowboy, "2.6.1", "9a3bbfceeb65eff5f39dab529e5cd79137ac36e913c02067dba3963a26efe9b2", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "de36e1a21f451a18b790f37765db198075c25875c64834bcc82d90b309eb6613"}, + "plug_crypto": {:hex, :plug_crypto, "1.2.5", "918772575e48e81e455818229bf719d4ab4181fcbf7f85b68a35620f78d89ced", [:mix], [], "hexpm", "26549a1d6345e2172eb1c233866756ae44a9609bd33ee6f99147ab3fd87fd842"}, + "ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"}, + "telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"}, } diff --git a/priv/repo/migrations/20180627160454_create_users.exs b/priv/repo/migrations/20180627160454_create_users.exs deleted file mode 100644 index d23ae6a..0000000 --- a/priv/repo/migrations/20180627160454_create_users.exs +++ /dev/null @@ -1,15 +0,0 @@ -defmodule ApiMock.Repo.Migrations.CreateUsers do - use Ecto.Migration - - def change do - create table(:users) do - add :profile, :map - add :name, :string - add :position, :string - add :company, :string - - timestamps() - end - - end -end diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs deleted file mode 100644 index bf2691e..0000000 --- a/priv/repo/seeds.exs +++ /dev/null @@ -1,87 +0,0 @@ -# Script for populating the database. You can run it as: -# -# mix run priv/repo/seeds.exs -# -# Inside the script, you can read and write to any of your -# repositories directly: -# -# ApiMock.Repo.insert!(%ApiMock.SomeSchema{}) -# -# We recommend using the bang functions (`insert!`, `update!` -# and so on) as they will fail if something goes wrong. -# - -crew = [ - %{"name" => "Philip J Fry", - "position" => "Delivery Boy", - "profile" => %{ - "gender" => "M", - "species" => "Human", - "age" => 25, - "planet" => "Earth", - "status" => "Alive" - }, - "company" => "Planet Express"}, - %{"name" => "Turanga Leela", - "position" => "Captain", - "profile" => %{ - "gender" => "F", - "species" => "Mutant, Human", - "age" => 25, - "planet" => "Earth", - "status" => "Alive" - }, - "company" => "Planet Express"}, - %{"name" => "Bender Bending Rodríguez", - "position" => "Ship's Robot, Ship's Cook, Assistant Manager of Sales, Gunnery Chief", - "profile" => %{ - "gender" => "M", - "species" => "Robot", - "age" => 4, - "planet" => "Earth", - "status" => "Alive" - }, - "company" => "Planet Express"}, - %{"name" => "Planet Express Ship", - "position" => "Ship", - "profile" => %{ - "gender" => "N/A", - "species" => "Robotic Spacecraft", - "age" => "N/A", - "planet" => "Earth", - "status" => "N/A" - }, - "company" => "Planet Express"}, - %{"name" => "Dr. John A. Zoidberg Ph.D", - "position" => "Staff Doctor", - "profile" => %{ - "gender" => "M", - "species" => "Decapodian", - "age" => 86, - "planet" => "Decapod 10", - "status" => "Alive" - }, - "company" => "Planet Express"}, - %{"name" => "Zapp Brannigan", - "position" => "Captain", - "profile" => %{ - "gender" => "M", - "species" => "Human", - "age" => 28, - "planet" => "Earth", - "status" => "Alive" - }, - "company" => "Democratic Order of Planets"}, - %{"name" => "Kif Kroker", - "position" => "Lieutenant", - "profile" => %{ - "gender" => "M", - "species" => "Amphibiosans", - "age" => 24, - "planet" => "Amphibios 9", - "status" => "Alive" - }, - "company" => "Democratic Order of Planets"}, -] -crew - |> Enum.each(&ApiMock.Crew.create_users(&1)) diff --git a/test/support/channel_case.ex b/test/support/channel_case.ex index 32c5735..e1993ac 100644 --- a/test/support/channel_case.ex +++ b/test/support/channel_case.ex @@ -13,7 +13,6 @@ defmodule ApiMockWeb.ChannelCase do of the test unless the test case is marked as async. """ - alias Ecto.Adapters.SQL.Sandbox use ExUnit.CaseTemplate using do @@ -26,12 +25,4 @@ defmodule ApiMockWeb.ChannelCase do end end - setup tags do - :ok = Sandbox.checkout(ApiMock.Repo) - unless tags[:async] do - Sandbox.mode(ApiMock.Repo, {:shared, self()}) - end - :ok - end - end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 93e9eab..6f7edf5 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -12,8 +12,6 @@ defmodule ApiMockWeb.ConnCase do inside a transaction which is reset at the beginning of the test unless the test case is marked as async. """ - alias Ecto.Adapters.SQL.Sandbox - use ExUnit.CaseTemplate using do @@ -28,10 +26,6 @@ defmodule ApiMockWeb.ConnCase do end setup tags do - :ok = Sandbox.checkout(ApiMock.Repo) - unless tags[:async] do - Sandbox.mode(ApiMock.Repo, {:shared, self()}) - end {:ok, conn: Phoenix.ConnTest.build_conn()} end diff --git a/test/support/data_case.ex b/test/support/data_case.ex index d9792f0..4530a17 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -5,38 +5,18 @@ defmodule ApiMock.DataCase do You may define functions here to be used as helpers in your tests. - - Finally, if the test case interacts with the database, - it cannot be async. For this reason, every test runs - inside a transaction which is reset at the beginning - of the test unless the test case is marked as async. """ - alias Ecto.Adapters.SQL.Sandbox - use ExUnit.CaseTemplate using do quote do - alias ApiMock.Repo - import Ecto import Ecto.Changeset - import Ecto.Query import ApiMock.DataCase end end - setup tags do - :ok = Sandbox.checkout(ApiMock.Repo) - - unless tags[:async] do - Sandbox.mode(ApiMock.Repo, {:shared, self()}) - end - - :ok - end - @doc """ A helper that transform changeset errors to a map of messages. diff --git a/test/test_helper.exs b/test/test_helper.exs index 12c0d06..869559e 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,3 +1 @@ ExUnit.start() - -Ecto.Adapters.SQL.Sandbox.mode(ApiMock.Repo, :manual) From cc55ad144f916dd9d1ac5083e24148e94da73651 Mon Sep 17 00:00:00 2001 From: Christopher Lamb Date: Wed, 29 Mar 2023 13:29:22 -0400 Subject: [PATCH 2/2] Update controller show to reflect new structure. --- lib/api_mock/crew/crew.ex | 4 +- .../controllers/fallback_controller.ex | 6 -- .../controllers/users_controller.ex | 6 +- test/api_mock/crew/crew_test.exs | 93 ++++++++----------- .../controllers/users_controller_test.exs | 71 +------------- 5 files changed, 51 insertions(+), 129 deletions(-) diff --git a/lib/api_mock/crew/crew.ex b/lib/api_mock/crew/crew.ex index 483573d..d3dc32c 100644 --- a/lib/api_mock/crew/crew.ex +++ b/lib/api_mock/crew/crew.ex @@ -34,7 +34,9 @@ defmodule ApiMock.Crew do """ def get_users!(id) do list_users - |> Enum.find(nil, fn u -> u[:id] == id end ) + |> Enum.find_value({:error, :not_found}, fn u -> + if u[:id] == id, do: {:ok, u} + end) end @doc """ diff --git a/lib/api_mock_web/controllers/fallback_controller.ex b/lib/api_mock_web/controllers/fallback_controller.ex index 0ec7ae4..34677aa 100644 --- a/lib/api_mock_web/controllers/fallback_controller.ex +++ b/lib/api_mock_web/controllers/fallback_controller.ex @@ -6,12 +6,6 @@ defmodule ApiMockWeb.FallbackController do """ use ApiMockWeb, :controller - def call(conn, {:error, %Ecto.Changeset{} = changeset}) do - conn - |> put_status(:unprocessable_entity) - |> render(ApiMockWeb.ChangesetView, "error.json", changeset: changeset) - end - def call(conn, {:error, :not_found}) do conn |> put_status(:not_found) diff --git a/lib/api_mock_web/controllers/users_controller.ex b/lib/api_mock_web/controllers/users_controller.ex index fdbe57a..5dfd566 100644 --- a/lib/api_mock_web/controllers/users_controller.ex +++ b/lib/api_mock_web/controllers/users_controller.ex @@ -12,7 +12,9 @@ defmodule ApiMockWeb.UsersController do end def show(conn, %{"id" => id}) do - users = Crew.get_users!(id) - render(conn, "show.json", users: users) + with {:ok, user} <- Crew.get_users!(id) do + conn + |> render("show.json", users: user) + end end end diff --git a/test/api_mock/crew/crew_test.exs b/test/api_mock/crew/crew_test.exs index c10133d..f61dcb5 100644 --- a/test/api_mock/crew/crew_test.exs +++ b/test/api_mock/crew/crew_test.exs @@ -6,60 +6,45 @@ defmodule ApiMock.CrewTest do describe "users" do alias ApiMock.Crew.Users - @valid_attrs %{name: "Bender", position: "Ship's Cook", company: "Planet Express", profile: %{}} - @update_attrs %{name: "Bender", position: "Ship's Cook", company: "Planet Express", profile: %{}} - @invalid_attrs %{name: nil, position: nil, company: nil} - - def users_fixture(attrs \\ %{}) do - {:ok, users} = - attrs - |> Enum.into(@valid_attrs) - |> Crew.create_users() - - users - end - test "list_users/0 returns all users" do - users = users_fixture() - assert Crew.list_users() == [users] - end - - test "get_users!/1 returns the users with given id" do - users = users_fixture() - assert Crew.get_users!(users.id) == users - end - - test "create_users/1 with valid data creates a users" do - assert {:ok, %Users{} = users} = Crew.create_users(@valid_attrs) - assert users.profile == %{} - end - - test "create_users/1 with invalid data returns error changeset" do - assert {:error, %Ecto.Changeset{}} = Crew.create_users(@invalid_attrs) - end - - test "update_users/2 with valid data updates the users" do - users = users_fixture() - assert {:ok, users} = Crew.update_users(users, @update_attrs) - assert %Users{} = users - assert users.profile == %{} - end - - test "update_users/2 with invalid data returns error changeset" do - users = users_fixture() - assert {:error, %Ecto.Changeset{}} = Crew.update_users(users, @invalid_attrs) - assert users == Crew.get_users!(users.id) - end - - test "delete_users/1 deletes the users" do - users = users_fixture() - assert {:ok, %Users{}} = Crew.delete_users(users) - assert_raise Ecto.NoResultsError, fn -> Crew.get_users!(users.id) end - end - - test "change_users/1 returns a users changeset" do - users = users_fixture() - assert %Ecto.Changeset{} = Crew.change_users(users) - end + assert Crew.list_users() == [] + end + + # test "get_users!/1 returns the users with given id" do + # assert Crew.get_users!(1) == %{} + # end + + # test "create_users/1 with valid data creates a users" do + # assert {:ok, %Users{} = users} = Crew.create_users(@valid_attrs) + # assert users.profile == %{} + # end + + # test "create_users/1 with invalid data returns error changeset" do + # assert {:error, %Ecto.Changeset{}} = Crew.create_users(@invalid_attrs) + # end + + # test "update_users/2 with valid data updates the users" do + # users = users_fixture() + # assert {:ok, users} = Crew.update_users(users, @update_attrs) + # assert %Users{} = users + # assert users.profile == %{} + # end + + # test "update_users/2 with invalid data returns error changeset" do + # users = users_fixture() + # assert {:error, %Ecto.Changeset{}} = Crew.update_users(users, @invalid_attrs) + # assert users == Crew.get_users!(users.id) + # end + + # test "delete_users/1 deletes the users" do + # users = users_fixture() + # assert {:ok, %Users{}} = Crew.delete_users(users) + # assert_raise Ecto.NoResultsError, fn -> Crew.get_users!(users.id) end + # end + + # test "change_users/1 returns a users changeset" do + # users = users_fixture() + # assert %Ecto.Changeset{} = Crew.change_users(users) + # end end end diff --git a/test/api_mock_web/controllers/users_controller_test.exs b/test/api_mock_web/controllers/users_controller_test.exs index b8f2305..6b5f351 100644 --- a/test/api_mock_web/controllers/users_controller_test.exs +++ b/test/api_mock_web/controllers/users_controller_test.exs @@ -4,15 +4,6 @@ defmodule ApiMockWeb.UsersControllerTest do alias ApiMock.Crew alias ApiMock.Crew.Users - @create_attrs %{name: "Bender", position: "Ship's Cook", company: "Planet Express", profile: %{}} - @update_attrs %{name: "Bender", position: "Ship's Cook", company: "Planet Express", profile: %{}} - @invalid_attrs %{name: nil, position: nil, company: nil} - - def fixture(:users) do - {:ok, users} = Crew.create_users(@create_attrs) - users - end - setup %{conn: conn} do {:ok, conn: put_req_header(conn, "accept", "application/json")} end @@ -20,66 +11,14 @@ defmodule ApiMockWeb.UsersControllerTest do describe "index" do test "lists all users", %{conn: conn} do conn = get conn, users_path(conn, :index) - assert json_response(conn, 200)["users"] == [] + assert Enum.count(json_response(conn, 200)["users"]) == Enum.count(ApiMock.Crew.list_users) end end - describe "create users" do - test "renders users when data is valid", %{conn: conn} do - conn = post conn, users_path(conn, :create), users: @create_attrs - assert %{"id" => id} = json_response(conn, 201)["user"] - - conn = get conn, users_path(conn, :show, id) - assert json_response(conn, 200)["user"] == %{ - "id" => id, - "name" => "Bender", - "company" => "Planet Express", - "position" => "Ship's Cook", - "profile" => %{}} - end - - test "renders errors when data is invalid", %{conn: conn} do - conn = post conn, users_path(conn, :create), users: @invalid_attrs - assert json_response(conn, 422)["errors"] != %{} + describe "show" do + test "show an existing user", %{conn: conn} do + conn = get conn, users_path(conn, :show, "bad_id") + assert json_response(conn, 404)["user"] == nil end end - - describe "update users" do - setup [:create_users] - - test "renders users when data is valid", %{conn: conn, users: %Users{id: id} = users} do - conn = put conn, users_path(conn, :update, users), users: @update_attrs - assert %{"id" => ^id} = json_response(conn, 200)["user"] - - conn = get conn, users_path(conn, :show, id) - assert json_response(conn, 200)["user"] == %{ - "id" => id, - "name" => "Bender", - "company" => "Planet Express", - "position" => "Ship's Cook", - "profile" => %{}} - end - - test "renders errors when data is invalid", %{conn: conn, users: users} do - conn = put conn, users_path(conn, :update, users), users: @invalid_attrs - assert json_response(conn, 422)["errors"] != %{} - end - end - - describe "delete users" do - setup [:create_users] - - test "deletes chosen users", %{conn: conn, users: users} do - conn = delete conn, users_path(conn, :delete, users) - assert response(conn, 204) - assert_error_sent 404, fn -> - get conn, users_path(conn, :show, users) - end - end - end - - defp create_users(_) do - users = fixture(:users) - {:ok, users: users} - end end