Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add testing v2 #39

Merged
merged 3 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion apps/core/lib/core/schema/answer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ defmodule GoEscuelaLms.Core.Schema.Answer do

def all, do: Repo.all(Answer)

def find(uuid), do: Repo.get(Answer, uuid)
def find(uuid) do
case Ecto.UUID.dump(uuid) do
{:ok, _} ->
Repo.get(Answer, uuid)

_ ->
nil
end
end

def create(attrs \\ %{}) do
%Answer{}
Expand Down
16 changes: 11 additions & 5 deletions apps/core/lib/core/schema/course.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@ defmodule GoEscuelaLms.Core.Schema.Course do

def all, do: Repo.all(Course)

def all(instructor_id) do
def all(user_id) do
query =
from(c in Course,
join: e in Enrollment,
on: c.uuid == e.course_id,
where: e.user_id == ^instructor_id
where: e.user_id == ^user_id
)

Repo.all(query)
end

def find(uuid) do
Repo.get(Course, uuid)
|> Repo.preload(:topics)
|> Repo.preload(:enrollments)
case Ecto.UUID.dump(uuid) do
{:ok, _} ->
Repo.get(Course, uuid)
|> Repo.preload(:topics)
|> Repo.preload(:enrollments)

_ ->
nil
end
end

def create(attrs \\ %{}) do
Expand Down
6 changes: 4 additions & 2 deletions apps/core/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ defmodule Core.MixProject do
elixirc_paths: elixirc_paths(Mix.env()),
elixir: "~> 1.14",
start_permanent: Mix.env() == :prod,
deps: deps()
deps: deps(),
test_coverage: [tool: ExCoveralls]
]
end

Expand All @@ -38,7 +39,8 @@ defmodule Core.MixProject do
{:goth, "~> 1.4"},
{:solid, "~> 0.15.2"},
{:faker, "~> 0.18.0", only: [:dev, :test]},
{:mock, "~> 0.3.8"}
{:mock, "~> 0.3.8"},
{:excoveralls, "~> 0.18.0", only: :test}
]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule GoEscuelaLms.Core.Repo.Migrations.AddReferencesRequired do
use Ecto.Migration

def change do
alter table(:answers) do
modify(:question_id, references(:questions, type: :uuid, column: :uuid),
null: false,
from: references(:questions, type: :uuid, column: :uuid)
)
end

alter table(:questions) do
modify(:activity_id, references(:activities, type: :uuid, column: :uuid),
null: false,
from: references(:activities, type: :uuid, column: :uuid)
)
end

alter table(:enrollments) do
modify(:course_id, references(:courses, type: :uuid, column: :uuid),
null: false,
from: references(:courses, type: :uuid, column: :uuid)
)

modify(:user_id, references(:users, type: :uuid, column: :uuid),
null: false,
from: references(:users, type: :uuid, column: :uuid)
)
end
end
end
108 changes: 108 additions & 0 deletions apps/core/test/core/schema/answer_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
defmodule Core.AnswerTest do
use ExUnit.Case
use Core.DataCase

import Core.Factory

alias GoEscuelaLms.Core.Schema.Answer

setup do
course = insert!(:course)
topic = insert!(:topic, course_id: course.uuid)
activity = insert!(:activity, topic_id: topic.uuid)
question = insert!(:question, activity_id: activity.uuid)
{:ok, question: question}
end

describe "schema" do
test "metadata", _context do
assert Answer.__schema__(:source) == "answers"

assert Answer.__schema__(:fields) == [
:uuid,
:description,
:match_answer,
:feedback,
:correct_answer,
:options_answers,
:question_id,
:inserted_at,
:updated_at
]
end

test "types metadata", _context do
assert Answer.__schema__(:type, :uuid) == Ecto.UUID
assert Answer.__schema__(:type, :description) == :string
assert Answer.__schema__(:type, :feedback) == :string
assert Answer.__schema__(:type, :correct_answer) == :boolean
assert Answer.__schema__(:type, :options_answers) == {:array, :map}
end

test "associations", _context do
assert Answer.__schema__(:association, :question).__struct__ == Ecto.Association.BelongsTo
end
end

describe "all/0" do
test "should return all answers", %{question: question} = _context do
Enum.each(0..3, fn _ -> insert!(:answer, question_id: question.uuid) end)

assert Answer.all() |> Enum.count() == 4
end
end

describe "find/1" do
test "when exist", %{question: question} = _context do
answer = insert!(:answer, question_id: question.uuid)

assert Answer.find(answer.uuid) == answer
end

test "when does not exist", _context do
assert Answer.find("28a11d64-5fd9-4028-8707-aeac06c7d10e") == nil
end

test "with invalid uuid", _context do
assert Answer.find("xxxx") == nil
assert Answer.find(nil) == nil
end
end

describe "create/1" do
test "create valid answer", %{question: question} = _context do
answer =
build(:answer, question_id: question.uuid) |> Map.from_struct()

{:ok, created_answer} = Answer.create(answer)

assert created_answer.description == answer.description
assert created_answer.feedback == answer.feedback
assert created_answer.correct_answer == answer.correct_answer
assert created_answer.options_answers == answer.options_answers
end

test "invalid create answer", _context do
{:error, errors} = Answer.create(%{})

assert errors.valid? == false
end
end

describe "bulk_create/2" do
test "when is nil", %{question: question} = _context do
assert Answer.bulk_create(question, nil) == :ok
assert Answer.bulk_create(question, []) == :ok
end

test "valid create multiple answers", %{question: question} = _context do
answers =
[
build(:answer, question_id: question.uuid) |> Map.from_struct(),
build(:answer, question_id: question.uuid) |> Map.from_struct()
]

assert Answer.bulk_create(question, answers) == :ok
end
end
end
110 changes: 110 additions & 0 deletions apps/core/test/core/schema/course_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
defmodule Core.CourseTest do
use ExUnit.Case
use Core.DataCase

import Core.Factory

alias GoEscuelaLms.Core.Repo
alias GoEscuelaLms.Core.Schema.Course

describe "schema" do
test "metadata" do
assert Course.__schema__(:source) == "courses"

assert Course.__schema__(:fields) == [
:uuid,
:name,
:description,
:enabled,
:inserted_at,
:updated_at
]
end

test "types metadata" do
assert Course.__schema__(:type, :uuid) == Ecto.UUID
assert Course.__schema__(:type, :name) == :string
assert Course.__schema__(:type, :description) == :string
end

test "associations", _context do
assert Course.__schema__(:association, :enrollments).__struct__ == Ecto.Association.Has
assert Course.__schema__(:association, :topics).__struct__ == Ecto.Association.Has
end
end

describe "all/0" do
test "should return all courses" do
Enum.each(0..3, fn _ -> insert!(:course) end)

assert Course.all() |> Enum.count() == 4
end
end

describe("all/1") do
test "should return all courses enrollment to user" do
user = insert!(:user)
course = insert!(:course)
insert!(:enrollment, course_id: course.uuid, user_id: user.uuid)

assert Course.all(user.uuid) == [course]
end
end

describe "find/1" do
test "when exist" do
course = insert!(:course) |> Repo.preload(:topics) |> Repo.preload(:enrollments)

assert Course.find(course.uuid) == course
end

test "when does not exist", _context do
assert Course.find(Faker.UUID.v4()) == nil
end

test "with invalid uuid" do
assert Course.find("xxxx") == nil
assert Course.find(nil) == nil
end
end

describe "create/1" do
test "create valid course" do
course =
build(:course) |> Map.from_struct()

{:ok, course_created} = Course.create(course)

assert course_created.name == course.name
assert course_created.description == course.description
assert course_created.enabled == course.enabled
end

test "invalid create course" do
{:error, errors} = Course.create(%{})

assert errors.valid? == false
end
end

describe "update/1" do
test "update valid course" do
course = insert!(:course)
attrs = %{name: Faker.Lorem.word(), enabled: true, description: Faker.Lorem.sentence()}

{:ok, course_updated} = Course.update(course, attrs)

assert course_updated.name == attrs.name
assert course_updated.description == attrs.description
assert course_updated.enabled == attrs.enabled
end

test "invalid create course" do
course = insert!(:course)

{:error, errors} = Course.update(course, %{name: nil})

assert errors.valid? == false
end
end
end
16 changes: 15 additions & 1 deletion apps/core/test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Core.Factory do
"""

alias GoEscuelaLms.Core.Repo
alias GoEscuelaLms.Core.Schema.{Activity, Answer, Course, Question, Topic}
alias GoEscuelaLms.Core.Schema.{Activity, Answer, Course, Question, Enrollment, Topic, User}

def build(:activity) do
%Activity{
Expand Down Expand Up @@ -49,10 +49,24 @@ defmodule Core.Factory do
description: Faker.Lorem.word(),
match_answer: nil,
feedback: nil,
options_answers: [],
correct_answer: true
}
end

def build(:enrollment) do
%Enrollment{}
end

def build(:user) do
%User{
full_name: Faker.Person.name(),
email: Faker.Internet.email(),
role: :instructor,
password_hash: Faker.String.base64(100)
}
end

# Convenience API

def build(factory_name, attributes) do
Expand Down
6 changes: 4 additions & 2 deletions apps/web/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ defmodule Web.MixProject do
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
aliases: aliases(),
deps: deps()
deps: deps(),
test_coverage: [tool: ExCoveralls]
]
end

Expand Down Expand Up @@ -46,7 +47,8 @@ defmodule Web.MixProject do
{:guardian, "~> 2.3"},
{:guardian_db, "~> 3.0"},
{:core, in_umbrella: true},
{:tarams, "~> 1.8"}
{:tarams, "~> 1.8"},
{:excoveralls, "~> 0.18.0", only: :test}
]
end

Expand Down
6 changes: 6 additions & 0 deletions excoveralls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"skip_files": [
"folder_to_skip",
"test/support/data_case.ex"
]
}
Loading