Skip to content

Commit

Permalink
feat: Fixing test and adding UsersTable test. #1
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Dec 28, 2022
1 parent c75aa24 commit 584a317
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ npm-debug.log
# Env files
.env
cache.dets
cache_test.dets
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1156,10 +1156,6 @@ defmodule UsersTable do
end
end

def list_users do
Pockets.to_map(@table)
end

def create_user(%{:stripe_id => stripe_id, :person_id => person_id, :status => status}) do
Pockets.put(@table, person_id, %{stripe_id: stripe_id, status: status})
end
Expand All @@ -1182,7 +1178,6 @@ All the functions being used are used
according to the [`ets` wrapper documentation](https://github.com/TheFirstAvenger/ets).
- the `init/0` function creates the table to store our users.
If the file already exists, we open the file.
- `list_users` returns the list of users.
- `create_user/3` receives a `stripe_id`, `person_id` and `status`
(pertaining to whether the payment has been made or not)
and creates a user object.
Expand Down
14 changes: 14 additions & 0 deletions coveralls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"coverage_options": {
"minimum_coverage": 100
},
"skip_files": [
"test/",
"lib/app/application.ex",
"lib/app/release.ex",
"lib/app_web.ex",
"lib/app_web/gettext.ex",
"lib/app_web/components/",
"lib/app_web/telemetry.ex"
]
}
13 changes: 6 additions & 7 deletions lib/app/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ defmodule UsersTable do

alias Pockets

@table :users_table
@filepath "cache.dets"
@table (if Mix.env() == :prod do :users_table else :users_test_table end)
@filepath (if Mix.env() == :prod do "cache.dets" else "cache_test.dets" end)

def init do
def init() do
case Pockets.new(@table, @filepath) do
# Not testing creating a table because when testing, it loads a sample table.
# coveralls-ignore-start
{:ok, set} -> {:ok, set}
# coveralls-ignore-end
{:error, _} ->
Pockets.open(@table, @filepath)
end
end

def list_users do
Pockets.to_map(@table)
end

def create_user(%{:stripe_id => stripe_id, :person_id => person_id, :status => status}) do
Pockets.put(@table, person_id, %{stripe_id: stripe_id, status: status})
end
Expand Down
19 changes: 16 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ defmodule App.MixProject do
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
aliases: aliases(),
deps: deps()
deps: deps(),
test_coverage: [tool: ExCoveralls],
preferred_cli_env: [
c: :test,
coveralls: :test,
"coveralls.json": :test,
"coveralls.html": :test,
t: :test
]
]
end

Expand Down Expand Up @@ -48,6 +56,9 @@ defmodule App.MixProject do
{:jason, "~> 1.2"},
{:plug_cowboy, "~> 2.5"},

# Tracking test coverage
{:excoveralls, "~> 0.15.1", only: [:test, :dev]},

# Auth
{:auth_plug, "~> 1.5.1"},

Expand All @@ -69,8 +80,10 @@ defmodule App.MixProject do
defp aliases do
[
setup: ["deps.get"],
test: ["test"],
"assets.deploy": ["tailwind default --minify", "esbuild default --minify", "phx.digest"]
"assets.deploy": ["tailwind default --minify", "esbuild default --minify", "phx.digest"],
t: ["test"],
c: ["coveralls.html"],
s: ["phx.server"]
]
end
end
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"},
"envar": {:hex, :envar, "1.1.0", "105bcac5a03800a1eb21e2c7e229edc687359b0cc184150ec1380db5928c115c", [:mix], [], "hexpm", "97028ab4a040a5c19e613fdf46a41cf51c6e848d99077e525b338e21d2993320"},
"esbuild": {:hex, :esbuild, "0.6.0", "9ba6ead054abd43cb3d7b14946a0cdd1493698ccd8e054e0e5d6286d7f0f509c", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "30f9a05d4a5bab0d3e37398f312f80864e1ee1a081ca09149d06d474318fd040"},
"excoveralls": {:hex, :excoveralls, "0.15.1", "83c8cf7973dd9d1d853dce37a2fb98aaf29b564bf7d01866e409abf59dac2c0e", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "f8416bd90c0082d56a2178cf46c837595a06575f70a5624f164a1ffe37de07e7"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"finch": {:hex, :finch, "0.14.0", "619bfdee18fc135190bf590356c4bf5d5f71f916adb12aec94caa3fa9267a4bc", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "5459acaf18c4fdb47a8c22fb3baff5d8173106217c8e56c5ba0b93e66501a8dd"},
"floki": {:hex, :floki, "0.34.0", "002d0cc194b48794d74711731db004fafeb328fe676976f160685262d43706a8", [:mix], [], "hexpm", "9c3a9f43f40dde00332a589bd9d389b90c1f518aef500364d00636acc5ebc99c"},
Expand Down
25 changes: 25 additions & 0 deletions test/app/users_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule App.UsersTest do

use ExUnit.Case

# "Cache_test.dets" has one user

setup_all do
UsersTable.init()
UsersTable.create_user(%{stripe_id: 1, person_id: 1, status: true})
UsersTable.create_user(%{stripe_id: 2, person_id: 2, status: false})

:ok
end

test "fetch user of person_id 1" do
user = UsersTable.fetch_user(1)
assert Map.get(user, :stripe_id) == 1
end

test "create user of person_id 3" do
UsersTable.create_user(%{stripe_id: 3, person_id: 3, status: true})
user = UsersTable.fetch_user(3)
assert Map.get(user, :stripe_id) == 3
end
end
2 changes: 1 addition & 1 deletion test/app_web/controllers/page_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ defmodule AppWeb.PageControllerTest do

test "GET /", %{conn: conn} do
conn = get(conn, ~p"/")
assert html_response(conn, 200) =~ "Peace of mind from prototype to production"
assert html_response(conn, 200) =~ "This is a small project showcasing how to integrate Stripe in a Phoenix project."
end
end
46 changes: 46 additions & 0 deletions test/support/data_case.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
defmodule App.DataCase do
@moduledoc """
This module defines the setup for tests requiring
access to the application's data layer.
You may define functions here to be used as helpers in
your tests.
Finally, if the test case interacts with the database,
we enable the SQL sandbox, so changes done to the database
are reverted at the end of every test. If you are using
PostgreSQL, you can even run database tests asynchronously
by setting `use App.DataCase, async: true`, although
this option is not recommended for other databases.
"""

use ExUnit.CaseTemplate

using do
quote do
import Ecto
end
end

setup tags do
App.DataCase.setup_sandbox(tags)
:ok
end

@doc """
Sets up the sandbox based on the test tags.
"""
def setup_sandbox(tags) do
end

@doc """
A helper that transforms changeset errors into a map of messages.
assert {:error, changeset} = Accounts.create_user(%{password: "short"})
assert "password is too short" in errors_on(changeset).password
assert %{password: ["password is too short"]} = errors_on(changeset)
"""
def errors_on(changeset) do
end
end

0 comments on commit 584a317

Please sign in to comment.