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

Got all tests pass, a little confused about the tests involving sql #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion models/author.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Author < ActiveRecord::Base
class Author < ActiveRecord::Base
has_many :author_books
has_many :books, :through => :author_books
has_many :pages, :through => :books
Expand Down
120 changes: 63 additions & 57 deletions test/about_active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,107 +35,113 @@ class AboutActiveRecord < Test::Unit::TestCase
@library.books << @war_peace
@library.books << @doctor_zhivago
@library.books << @life_fate




@library.save!



@library.reload
@war_peace.reload
@leo.reload


end

context "in total" do
should "have the right number of models" do
assert_equal ___, Library.count
assert_equal ___, Book.count
assert_equal ___, Page.count
assert_equal ___, Cover.count
assert_equal ___, AuthorBook.count
assert_equal ___, Author.count
assert_equal 1, Library.count
assert_equal 3, Book.count
assert_equal 20, Page.count
assert_equal 3, Cover.count
assert_equal 3, AuthorBook.count
assert_equal 3, Author.count
end
end

context "the library" do
should "have many books" do
assert_equal ___, @library.respond_to?(:books)
assert_equal ___, @library.books.length
assert_equal ___, @library.books.first.class
assert_equal ___, @library.respond_to?(:book_id)
assert_equal ___, @library.respond_to?(:library_id)
assert_equal ___, @library.books.first.library
assert_equal true, @library.respond_to?(:books)
assert_equal 3, @library.books.length
assert_equal Book, @library.books.first.class
assert_equal false, @library.respond_to?(:book_id)
assert_equal false, @library.respond_to?(:library_id)
assert_equal @library, @library.books.first.library
end

should "know how many books are Historical fiction" do
results = @library.books.where(:genre => "Historical fiction")
assert_equal ___, results.length
assert_equal ___, results.first.class
assert_equal ___, results
assert_equal 2, results.length
assert_equal Book, results.first.class
assert_equal [@war_peace, @life_fate], results
end

should "know how many books have at least 7 pages" do
results = @library.books.includes(:pages).where('pages.number >= 7')
assert_equal ___, results.length
assert_equal ___, results.first.class
assert_equal ___, results
assert_equal 2, results.length
assert_equal Book, results.first.class
assert_equal [@war_peace, @doctor_zhivago], results
end
end

context "Leo Tolstoy" do
should "have written many pages" do
assert_equal ___, @leo.respond_to?(:pages)
assert_equal ___, @leo.pages.length
assert_equal ___, @leo.pages.first.class
assert_equal true, @leo.respond_to?(:pages)
assert_equal 10, @leo.pages.length
assert_equal Page, @leo.pages.first.class
end

should "be searchable by name" do
assert_equal ___, Author.find_by_name("Leo Tolstoy").class
assert_equal ___, Author.find_by_name("Leo Tolstoy")
assert_equal ___, Author.where(:name => "Leo Tolstoy").class
assert_equal ___, Author.where(:name => "Leo Tolstoy").first
assert_equal ___, Author.first
assert_equal Author, Author.find_by_name("Leo Tolstoy").class
assert_equal @leo , Author.find_by_name("Leo Tolstoy")
assert_equal ActiveRecord::Relation, Author.where(:name => "Leo Tolstoy").class
assert_equal @leo, Author.where(:name => "Leo Tolstoy").first
assert_equal @leo, Author.first
end
end

context "War and Peace" do
should "have one cover" do
assert_equal ___, @war_peace.respond_to?(:covers)
assert_equal ___, @war_peace.respond_to?(:cover)
assert_equal ___, @war_peace.cover.class
assert_equal ___, @war_peace.cover.book
assert_equal ___, @war_peace.respond_to?(:book_id)
assert_equal ___, @war_peace.respond_to?(:cover_id)
assert_equal ___, @war_peace.cover.respond_to?(:book_id)
assert_equal ___, @war_peace.cover.respond_to?(:cover_id)
assert_equal false, @war_peace.respond_to?(:covers)
assert_equal true, @war_peace.respond_to?(:cover)
assert_equal Cover, @war_peace.cover.class
assert_equal @war_peace, @war_peace.cover.book
assert_equal false, @war_peace.respond_to?(:book_id)
assert_equal false, @war_peace.respond_to?(:cover_id)
assert_equal true, @war_peace.cover.respond_to?(:book_id)
assert_equal false, @war_peace.cover.respond_to?(:cover_id)
end

should "have many pages" do
assert_equal ___, @war_peace.respond_to?(:pages)
assert_equal ___, @war_peace.pages.length
assert_equal ___, @war_peace.pages.first.class
assert_equal ___, @war_peace.pages.first.number
assert_equal ___, @war_peace.pages.first.book
assert_equal ___, @war_peace.respond_to?(:page_id)
assert_equal ___, @war_peace.pages.first.respond_to?(:book_id)
assert_equal ___, @war_peace.pages.first.respond_to?(:page_id)
assert_equal true, @war_peace.respond_to?(:pages)
assert_equal 10, @war_peace.pages.length #pages is a method which returns an array of page objects from the object @war_peace
assert_equal Page, @war_peace.pages.first.class
assert_equal 1, @war_peace.pages.first.number
assert_equal @war_peace, @war_peace.pages.first.book
assert_equal false, @war_peace.respond_to?(:page_id)
assert_equal true, @war_peace.pages.first.respond_to?(:book_id)
assert_equal false, @war_peace.pages.first.respond_to?(:page_id)
end

should "have many authors" do
assert_equal ___, @war_peace.respond_to?(:authors)
assert_equal ___, @war_peace.respond_to?(:author_books)
assert_equal ___, @war_peace.authors.class
assert_equal ___, @war_peace.authors.length
assert_equal ___, @war_peace.authors.first.name
assert_equal ___, @war_peace.respond_to?(:author_id)
assert_equal ___, @war_peace.respond_to?(:author_book_id)
assert_equal ___, @war_peace.authors.first.respond_to?(:book_id)
assert_equal ___, @war_peace.author_books.first.respond_to?(:book_id)
assert_equal ___, @war_peace.author_books.first.respond_to?(:author_id)
assert_equal ___, @war_peace.author_books.to_sql.chomp.gsub("\"", "'").squeeze(" ")
assert_equal ___, @war_peace.authors.to_sql.chomp.gsub("\"", "'").squeeze(" ")
assert_equal true, @war_peace.respond_to?(:authors)
assert_equal true, @war_peace.respond_to?(:author_books)
assert_equal Array, @war_peace.authors.class
assert_equal 1, @war_peace.authors.length
assert_equal "Leo Tolstoy", @war_peace.authors.first.name
assert_equal false, @war_peace.respond_to?(:author_id)
assert_equal false, @war_peace.respond_to?(:author_book_id)
assert_equal false, @war_peace.authors.first.respond_to?(:book_id)
assert_equal true, @war_peace.author_books.first.respond_to?(:book_id)
assert_equal true, @war_peace.author_books.first.respond_to?(:author_id)
assert_equal "SELECT 'author_books'.* FROM 'author_books' WHERE 'author_books'.'book_id' = 1", @war_peace.author_books.to_sql.chomp.gsub("\"", "'").squeeze(" ")
assert_equal "SELECT 'authors'.* FROM 'authors' INNER JOIN 'author_books' ON 'authors'.'id' = 'author_books'.'author_id' WHERE 'author_books'.'book_id' = 1", @war_peace.authors.to_sql.chomp.gsub("\"", "'").squeeze(" ")
end

should "be searchable by name" do
assert_equal ___, Book.where(:title => "War & Peace").class
assert_equal ___, Book.where(:title => "War & Peace").to_sql.chomp.gsub("\"", "'").squeeze(" ")
assert_equal ActiveRecord::Relation, Book.where(:title => "War & Peace").class
assert_equal "SELECT 'books'.* FROM 'books' WHERE 'books'.'title' = 'War & Peace'", Book.where(:title => "War & Peace").to_sql.chomp.gsub("\"", "'").squeeze(" ")
end
end

Expand Down
Binary file modified test/test.db
Binary file not shown.