-
Notifications
You must be signed in to change notification settings - Fork 88
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
drujensen
merged 3 commits into
amberframework:master
from
Adam-Stomski:as-lazy-collections
Apr 26, 2018
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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! | ||
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! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 %} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thefind_by!