Skip to content

Commit

Permalink
test: assert function definitions on target schema module
Browse files Browse the repository at this point in the history
  • Loading branch information
joeljuca committed Jul 11, 2023
1 parent 414455e commit 7c5765d
Showing 1 changed file with 206 additions and 0 deletions.
206 changes: 206 additions & 0 deletions test/swiss_schema_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,210 @@
defmodule SwissSchemaTest do
use ExUnit.Case
doctest SwissSchema

@database_path System.tmp_dir!() <> "db.sqlite"

defmodule SwissSchemaTest.Repo do
use Ecto.Repo,
otp_app: :swiss_schema,
adapter: Ecto.Adapters.SQLite3,
database: @database_path
end

defmodule SwissSchemaTest.User do
use Ecto.Schema
use SwissSchema, repo: SwissSchemaTest.Repo
import Ecto.Changeset

schema "users" do
field(:is_active, :boolean, default: true)
field(:username, :string)
field(:email, :string)
end

def changeset(%SwissSchemaTest.User{} = user, %{} = params) do
user
|> cast(params, [:is_active, :username, :email])
|> validate_required([:username, :email])
end
end

setup do
SwissSchemaTest.Repo.start_link()

sql = """
CREATE TABLE users (
id INTEGER PRIMARY KEY,
is_active INTEGER NOT NULL DEFAULT 1,
username TEXT NOT NULL,
email TEXT NOT NULL
)
"""

Ecto.Adapters.SQLite3.storage_up(database: @database_path)
Ecto.Adapters.SQLite3.dump_cmd([sql], [], database: @database_path)

on_exit(fn -> File.rm!(@database_path) end)

%{database_path: @database_path}
end

describe "use SwissSchema" do
test "requires a :repo option" do
assert_raise KeyError, fn ->
defmodule SwissSchemaTest.BadSchema do
use Ecto.Schema
use SwissSchema
end
end
end

test "define aggregate/1" do
assert function_exported?(SwissSchemaTest.User, :aggregate, 1)
end

test "define aggregate/2" do
assert function_exported?(SwissSchemaTest.User, :aggregate, 2)
end

test "define aggregate/3" do
assert function_exported?(SwissSchemaTest.User, :aggregate, 3)
end

test "define all/0" do
assert function_exported?(SwissSchemaTest.User, :all, 0)
end

test "define all/1" do
assert function_exported?(SwissSchemaTest.User, :all, 1)
end

test "define delete_all/0" do
assert function_exported?(SwissSchemaTest.User, :delete_all, 0)
end

test "define delete_all/1" do
assert function_exported?(SwissSchemaTest.User, :delete_all, 1)
end

test "define get/1" do
assert function_exported?(SwissSchemaTest.User, :get, 1)
end

test "define get/2" do
assert function_exported?(SwissSchemaTest.User, :get, 2)
end

test "define get!/1" do
assert function_exported?(SwissSchemaTest.User, :get!, 1)
end

test "define get!/2" do
assert function_exported?(SwissSchemaTest.User, :get!, 2)
end

test "define get_by/1" do
assert function_exported?(SwissSchemaTest.User, :get_by, 1)
end

test "define get_by/2" do
assert function_exported?(SwissSchemaTest.User, :get_by, 2)
end

test "define get_by!/1" do
assert function_exported?(SwissSchemaTest.User, :get_by!, 1)
end

test "define get_by!/2" do
assert function_exported?(SwissSchemaTest.User, :get_by!, 2)
end

test "define stream/0" do
assert function_exported?(SwissSchemaTest.User, :stream, 0)
end

test "define stream/1" do
assert function_exported?(SwissSchemaTest.User, :stream, 1)
end

test "define update_all/1" do
assert function_exported?(SwissSchemaTest.User, :update_all, 1)
end

test "define update_all/2" do
assert function_exported?(SwissSchemaTest.User, :update_all, 2)
end

test "define delete/1" do
assert function_exported?(SwissSchemaTest.User, :delete, 1)
end

test "define delete/2" do
assert function_exported?(SwissSchemaTest.User, :delete, 2)
end

test "define delete!/1" do
assert function_exported?(SwissSchemaTest.User, :delete!, 1)
end

test "define delete!/2" do
assert function_exported?(SwissSchemaTest.User, :delete!, 2)
end

test "define insert/1" do
assert function_exported?(SwissSchemaTest.User, :insert, 1)
end

test "define insert/2" do
assert function_exported?(SwissSchemaTest.User, :insert, 2)
end

test "define insert!/1" do
assert function_exported?(SwissSchemaTest.User, :insert!, 1)
end

test "define insert!/2" do
assert function_exported?(SwissSchemaTest.User, :insert!, 2)
end

test "define insert_all/1" do
assert function_exported?(SwissSchemaTest.User, :insert_all, 1)
end

test "define insert_all/2" do
assert function_exported?(SwissSchemaTest.User, :insert_all, 2)
end

test "define insert_or_update/1" do
assert function_exported?(SwissSchemaTest.User, :insert_or_update, 1)
end

test "define insert_or_update/2" do
assert function_exported?(SwissSchemaTest.User, :insert_or_update, 2)
end

test "define insert_or_update!/1" do
assert function_exported?(SwissSchemaTest.User, :insert_or_update!, 1)
end

test "define insert_or_update!/2" do
assert function_exported?(SwissSchemaTest.User, :insert_or_update!, 2)
end

test "define update/2" do
assert function_exported?(SwissSchemaTest.User, :update, 2)
end

test "define update/3" do
assert function_exported?(SwissSchemaTest.User, :update, 3)
end

test "define update!/2" do
assert function_exported?(SwissSchemaTest.User, :update!, 2)
end

test "define update!/3" do
assert function_exported?(SwissSchemaTest.User, :update!, 3)
end
end
end

0 comments on commit 7c5765d

Please sign in to comment.