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

Change .all and has_many to return *Collection object #176

Merged
merged 3 commits into from
Apr 26, 2018
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
150 changes: 125 additions & 25 deletions spec/granite_orm/associations/has_many_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,146 @@ module {{adapter.capitalize.id}}
teacher.klasss.size.should eq 2
end

describe "#has_many, through:" do
it "provides a method to retrieve associated objects through another table" do
student = Student.new
student.name = "test student"
student.save
context "querying association" do
it "#all" do
teacher = Teacher.new
teacher.name = "test teacher"
teacher.save

unrelated_student = Student.new
unrelated_student.name = "other student"
unrelated_student.save
klass1 = Klass.new
klass1.name = "Test class X"
klass1.teacher = teacher
klass1.save

klass2 = Klass.new
klass2.name = "Test class X"
klass2.teacher = teacher
klass2.save

klass3 = Klass.new
klass3.name = "Test class with different name"
klass3.teacher = teacher
klass3.save

klasses = teacher.klasss.all("AND klasss.name = ? ORDER BY klasss.id DESC", ["Test class X"])
klasses.map(&.id).should eq [klass2.id, klass1.id]
end

it "#find_by" do
teacher = Teacher.new
teacher.name = "test teacher"
teacher.save

klass1 = Klass.new
klass1.name = "Test class X"
klass1.teacher = teacher
klass1.save

klass2 = Klass.new
klass2.name = "Test class X"
klass2.teacher = teacher
klass2.save

klass3 = Klass.new
klass3.name = "Test class with different name"
klass3.teacher = teacher
klass3.save

klass = teacher.klasss.find_by(:name, "Test class with different name").not_nil!
klass.id.should eq klass3.id
klass.name.should eq "Test class with different name"
end

it "#find_by!" do
teacher = Teacher.new
teacher.name = "test teacher"
teacher.save

klass1 = Klass.new
klass1.name = "Test class"
klass1.name = "Test class X"
klass1.teacher = teacher
klass1.save

klass2 = Klass.new
klass2.name = "Test class"
klass2.name = "Test class X"
klass2.teacher = teacher
klass2.save

klass3 = Klass.new
klass3.name = "Test class"
klass3.name = "Test class with different name"
klass3.teacher = teacher
klass3.save

enrollment1 = Enrollment.new
enrollment1.student = student
enrollment1.klass = klass1
enrollment1.save
klass = teacher.klasss.find_by!(:name, "Test class with different name").not_nil!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need the not_nil! since you are using the find_by!

klass.id.should eq klass3.id
klass.name.should eq "Test class with different name"

expect_raises(
Granite::ORM::Querying::NotFound,
"Couldn't find #{Klass.name} with name=not_found"
) do
klass = teacher.klasss.find_by!(:name, "not_found")
end
end

it "#find" do
teacher = Teacher.new
teacher.name = "test teacher"
teacher.save

klass1 = Klass.new
klass1.name = "Test class X"
klass1.teacher = teacher
klass1.save

klass2 = Klass.new
klass2.name = "Test class X"
klass2.teacher = teacher
klass2.save

klass3 = Klass.new
klass3.name = "Test class with different name"
klass3.teacher = teacher
klass3.save

klass = teacher.klasss.find(klass1.id).not_nil!
klass.id.should eq klass1.id
klass.name.should eq "Test class X"
end

it "#find!" do
teacher = Teacher.new
teacher.name = "test teacher"
teacher.save

klass1 = Klass.new
klass1.name = "Test class X"
klass1.teacher = teacher
klass1.save

klass2 = Klass.new
klass2.name = "Test class X"
klass2.teacher = teacher
klass2.save

klass3 = Klass.new
klass3.name = "Test class with different name"
klass3.teacher = teacher
klass3.save

enrollment2 = Enrollment.new
enrollment2.student = student
enrollment2.klass = klass2
enrollment2.save

enrollment3 = Enrollment.new
enrollment3.klass = klass2
enrollment3.student = unrelated_student
enrollment3.save
klass = teacher.klasss.find!(klass1.id).not_nil!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

klass.id.should eq klass1.id
klass.name.should eq "Test class X"

student.klasss.map(&.id).compact.sort.should eq [klass1.id, klass2.id].compact.sort
id = klass3.id.as(Int64) + 42

klass2.students.map(&.id).compact.sort.should eq [student.id, unrelated_student.id].compact.sort
expect_raises(
Granite::ORM::Querying::NotFound,
"Couldn't find #{Klass.name} with id=#{id}"
) do
teacher.klasss.find!(id)
end
end
end
end
Expand Down
200 changes: 200 additions & 0 deletions spec/granite_orm/associations/has_many_through_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
require "../../spec_helper"

{% for adapter in GraniteExample::ADAPTERS %}
module {{adapter.capitalize.id}}
describe "{{ adapter.id }} has_many, through:" do
it "provides a method to retrieve associated objects through another table" do
student = Student.new
student.name = "test student"
student.save

unrelated_student = Student.new
unrelated_student.name = "other student"
unrelated_student.save

klass1 = Klass.new
klass1.name = "Test class"
klass1.save

klass2 = Klass.new
klass2.name = "Test class"
klass2.save

klass3 = Klass.new
klass3.name = "Test class"
klass3.save

enrollment1 = Enrollment.new
enrollment1.student = student
enrollment1.klass = klass1
enrollment1.save

enrollment2 = Enrollment.new
enrollment2.student = student
enrollment2.klass = klass2
enrollment2.save

enrollment3 = Enrollment.new
enrollment3.klass = klass2
enrollment3.student = unrelated_student
enrollment3.save

student.klasss.map(&.id).compact.sort.should eq [klass1.id, klass2.id].compact.sort

klass2.students.map(&.id).compact.sort.should eq [student.id, unrelated_student.id].compact.sort
end

context "querying association" do
it "#all" do
student = Student.create(name: "test student")

klass1 = Klass.create(name: "Test class X")
klass2 = Klass.create(name: "Test class X")
klass3 = Klass.create(name: "Test class with different name")

enrollment1 = Enrollment.new
enrollment1.student = student
enrollment1.klass = klass1
enrollment1.save

enrollment2 = Enrollment.new
enrollment2.student = student
enrollment2.klass = klass2
enrollment2.save

enrollment3 = Enrollment.new
enrollment3.klass = klass3
enrollment3.student = student
enrollment3.save

klasses = student.klasss.all("AND klasss.name = ? ORDER BY klasss.id DESC", ["Test class X"])
klasses.map(&.id).should eq [klass2.id, klass1.id]
end

it "#find_by" do
student = Student.create(name: "test student")

klass1 = Klass.create(name: "Test class X")
klass2 = Klass.create(name: "Test class X")
klass3 = Klass.create(name: "Test class with different name")

enrollment1 = Enrollment.new
enrollment1.student = student
enrollment1.klass = klass1
enrollment1.save

enrollment2 = Enrollment.new
enrollment2.student = student
enrollment2.klass = klass2
enrollment2.save

enrollment3 = Enrollment.new
enrollment3.klass = klass3
enrollment3.student = student
enrollment3.save

klass = student.klasss.find_by(:name, "Test class with different name").not_nil!
klass.id.should eq klass3.id
klass.name.should eq "Test class with different name"
end

it "#find_by!" do
student = Student.create(name: "test student")

klass1 = Klass.create(name: "Test class X")
klass2 = Klass.create(name: "Test class X")
klass3 = Klass.create(name: "Test class with different name")

enrollment1 = Enrollment.new
enrollment1.student = student
enrollment1.klass = klass1
enrollment1.save

enrollment2 = Enrollment.new
enrollment2.student = student
enrollment2.klass = klass2
enrollment2.save

enrollment3 = Enrollment.new
enrollment3.klass = klass3
enrollment3.student = student
enrollment3.save

klass = student.klasss.find_by!(:name, "Test class with different name").not_nil!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

klass.id.should eq klass3.id
klass.name.should eq "Test class with different name"

expect_raises(
Granite::ORM::Querying::NotFound,
"Couldn't find #{Klass.name} with name=not_found"
) do
klass = student.klasss.find_by!(:name, "not_found")
end
end

it "#find" do
student = Student.create(name: "test student")

klass1 = Klass.create(name: "Test class X")
klass2 = Klass.create(name: "Test class X")
klass3 = Klass.create(name: "Test class with different name")

enrollment1 = Enrollment.new
enrollment1.student = student
enrollment1.klass = klass1
enrollment1.save

enrollment2 = Enrollment.new
enrollment2.student = student
enrollment2.klass = klass2
enrollment2.save

enrollment3 = Enrollment.new
enrollment3.klass = klass3
enrollment3.student = student
enrollment3.save

klass = student.klasss.find(klass1.id).not_nil!
klass.id.should eq klass1.id
klass.name.should eq "Test class X"
end

it "#find!" do
student = Student.create(name: "test student")

klass1 = Klass.create(name: "Test class X")
klass2 = Klass.create(name: "Test class X")
klass3 = Klass.create(name: "Test class with different name")

enrollment1 = Enrollment.new
enrollment1.student = student
enrollment1.klass = klass1
enrollment1.save

enrollment2 = Enrollment.new
enrollment2.student = student
enrollment2.klass = klass2
enrollment2.save

enrollment3 = Enrollment.new
enrollment3.klass = klass3
enrollment3.student = student
enrollment3.save

klass = student.klasss.find!(klass1.id).not_nil!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

klass.id.should eq klass1.id
klass.name.should eq "Test class X"

id = klass3.id.as(Int64) + 42

expect_raises(
Granite::ORM::Querying::NotFound,
"Couldn't find #{Klass.name} with id=#{id}"
) do
student.klasss.find!(id)
end
end
end
end
end
{% end %}
Loading