-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from ScottGarman/add-more-model-tests
Update models and add unit tests
- Loading branch information
Showing
7 changed files
with
372 additions
and
13 deletions.
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 |
---|---|---|
@@ -1,3 +1,12 @@ | ||
class Quote < ApplicationRecord | ||
belongs_to :user | ||
|
||
# Validation | ||
validates_presence_of :user_id | ||
|
||
validates :quotation, presence: true, | ||
length: { maximum: 255 } | ||
|
||
validates :source, presence: true, | ||
length: { maximum: 255 } | ||
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
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 |
---|---|---|
@@ -1,7 +1,56 @@ | ||
require "test_helper" | ||
|
||
class QuoteTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
def setup | ||
@user = User.new(first_name: "Bubba", | ||
last_name: "Jones", | ||
email_address: "bubbajones@example.com", | ||
password: "foobarbaz12345", | ||
password_confirmation: "foobarbaz12345") | ||
@user.save! | ||
|
||
@quote = Quote.new(quotation: "Sample Quotation", source: "Source") | ||
end | ||
|
||
test "should be valid" do | ||
@user.quotes << @quote | ||
assert @quote.valid? | ||
end | ||
|
||
test "quotation must be associated with a User" do | ||
assert @quote.invalid? | ||
assert @quote.errors[:user_id].any? | ||
end | ||
|
||
test "quotation should be present and not empty" do | ||
@quote.quotation = " " | ||
assert @quote.invalid? | ||
assert @quote.errors[:quotation].any? | ||
end | ||
|
||
test "source should be present and not empty" do | ||
@quote.source = " " | ||
assert @quote.invalid? | ||
assert @quote.errors[:source].any? | ||
end | ||
|
||
test "quotation should not be too long" do | ||
@user.quotes << @quote | ||
@quote.quotation = "a" * 256 | ||
assert @quote.invalid? | ||
assert @quote.errors[:quotation].any? | ||
|
||
@quote.quotation = "a" * 255 | ||
assert @quote.valid? | ||
end | ||
|
||
test "source should not be too long" do | ||
@user.quotes << @quote | ||
@quote.source = "a" * 256 | ||
assert @quote.invalid? | ||
assert @quote.errors[:source].any? | ||
|
||
@quote.source = "a" * 255 | ||
assert @quote.valid? | ||
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 |
---|---|---|
@@ -1,7 +1,25 @@ | ||
require "test_helper" | ||
|
||
class SettingTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
def setup | ||
@user = User.new(first_name: "Bubba", | ||
last_name: "Jones", | ||
email_address: "bubbajones@example.com", | ||
password: "foobarbaz12345", | ||
password_confirmation: "foobarbaz12345") | ||
@user.save! | ||
|
||
@setting = Setting.new | ||
end | ||
|
||
test "setting must be associated with a User" do | ||
assert @setting.invalid? | ||
assert @setting.errors[:user_id].any? | ||
|
||
# The User model generates a default setting when it was created | ||
assert @user.setting.valid? | ||
|
||
@user.setting = @setting | ||
assert @setting.valid? | ||
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 |
---|---|---|
@@ -1,7 +1,52 @@ | ||
require "test_helper" | ||
|
||
class TaskCategoryTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
def setup | ||
@user = User.new(first_name: "Bubba", | ||
last_name: "Jones", | ||
email_address: "bubbajones@example.com", | ||
password: "foobarbaz12345", | ||
password_confirmation: "foobarbaz12345") | ||
@user.save! | ||
|
||
@tc = TaskCategory.new(name: "Work") | ||
end | ||
|
||
test "should be valid" do | ||
@user.task_categories << @tc | ||
assert @tc.valid? | ||
end | ||
|
||
test "a TaskCategory must be associated with a User" do | ||
assert @tc.invalid? | ||
assert @tc.errors[:user_id].any? | ||
end | ||
|
||
test "name should be present and not empty" do | ||
@user.task_categories << @tc | ||
@tc.name = " " | ||
assert @tc.invalid? | ||
assert @tc.errors[:name].any? | ||
end | ||
|
||
test "name should not be too long" do | ||
@user.task_categories << @tc | ||
@tc.name = "a" * 51 | ||
assert @tc.invalid? | ||
assert @tc.errors[:name].any? | ||
|
||
@tc.name = "a" * 50 | ||
assert @tc.valid? | ||
end | ||
|
||
test "duplicate TaskCategory names cannot exist for the same User" do | ||
@user.task_categories << @tc | ||
assert_not_nil @user.task_categories.find_by_name("Work") | ||
|
||
tc2 = TaskCategory.new(name: "Work") | ||
assert_equal @user.task_categories.count, 2 | ||
@user.task_categories << tc2 | ||
assert tc2.invalid? | ||
assert tc2.errors[:name].any? | ||
end | ||
end |
Oops, something went wrong.