Skip to content

Commit

Permalink
Add User unit tests
Browse files Browse the repository at this point in the history
This also adds some model validations to other models, which I'll add
unit tests for in an upcoming commit. Fixtures also needed some fiddling
to refer to the named user fixtures.
  • Loading branch information
ScottGarman committed Jan 3, 2025
1 parent 128de98 commit b17f4db
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 37 deletions.
2 changes: 2 additions & 0 deletions app/models/setting.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class Setting < ApplicationRecord
belongs_to :user

validates_presence_of :user_id
end
19 changes: 19 additions & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
class Task < ApplicationRecord
belongs_to :task_category
belongs_to :user

# Validation
STATUS_TYPES = %w[INCOMPLETE COMPLETED].freeze

validates_presence_of :user_id

validates :summary, presence: true,
length: { maximum: 150 }

validates :priority, presence: true,
inclusion: { in: 1..3 }

validates :status, presence: true,
inclusion: { in: STATUS_TYPES }

# Scopes
scope :incomplete, -> { where(status: :INCOMPLETE) }
scope :completed, -> { where(status: :COMPLETED) }
scope :no_due_date, -> { where("due_at IS NULL") }
end
8 changes: 8 additions & 0 deletions app/models/task_category.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
class TaskCategory < ApplicationRecord
has_many :tasks
belongs_to :user

# Validation
validates_presence_of :user_id

validates :name, presence: true,
length: { maximum: 50 },
uniqueness: { scope: :user_id }
end
19 changes: 19 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class User < ApplicationRecord
has_secure_password
has_one :setting, dependent: :destroy
has_many :quotes, dependent: :destroy
has_many :task_categories, dependent: :destroy
has_many :tasks, dependent: :destroy
has_many :sessions, dependent: :destroy

# Validation
Expand All @@ -18,4 +22,19 @@ class User < ApplicationRecord
uniqueness: { case_sensitive: false }

validates :password, presence: true, length: { minimum: 14 }, allow_nil: true

after_create :create_user_settings, :create_default_task_category

private

# Ensure each User has an associated Setting object
def create_user_settings
self.setting = Setting.new
end

# Ensure each User has an 'Uncategorized' TaskCategory that will be used for
# tasks where a TaskCategory is not set
def create_default_task_category
task_categories << TaskCategory.new(name: "Uncategorized")
end
end
16 changes: 0 additions & 16 deletions test/fixtures/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,3 @@ leofsiege:
display_quotes: false
burnination: false
user: leofsiege

agj:
display_quotes: false
burnination: false
user: agj

onewheelskyward:
display_quotes: false
burnination: false
user: onewheelskyward

aaronpk:
display_quotes: false
burnination: false
user: aaronpk

21 changes: 0 additions & 21 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,3 @@ leofsiege:
email_address: leofsiege@example.com
password_digest: <%= password_digest %>
time_zone: 'Pacific Time (US & Canada)'

agj:
first_name: Anthony
last_name: AnthonyLN
email_address: agj@example.com
password_digest: <%= password_digest %>
time_zone: 'Pacific Time (US & Canada)'

onewheelskyward:
first_name: Andrew
last_name: AndrewLN
email_address: onewheelskyward@example.com
password_digest: <%= password_digest %>
time_zone: 'Pacific Time (US & Canada)'

aaronpk:
first_name: Aaron
last_name: AaronLN
email_address: aaronpk@example.com
password_digest: <%= password_digest %>
time_zone: 'Pacific Time (US & Canada)'
43 changes: 43 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,47 @@ def setup
@user.password = @user.password_confirmation = "a" * 14
assert @user.valid?
end

test "each new User should have a default Settings object" do
@user.save!
assert_not_nil @user.setting
end

test "each new User should have an Uncategorized TaskCategory object" do
@user.save!
assert_not_empty @user.task_categories
assert_equal 1, @user.task_categories.size
assert_equal "Uncategorized", @user.task_categories.first.name
end

test "ensure associated model objects get destroyed when a user is" \
" destroyed" do
@user.save!
assert_equal 0, @user.quotes.count
assert_equal 0, @user.tasks.count

quote = Quote.new(quotation: "Sample Quotation", source: "Source")
@user.quotes << quote
assert_equal 1, @user.quotes.count

task = Task.new(summary: "My first task",
task_category_id: @user.task_categories.first.id)
@user.tasks << task
assert_equal 1, @user.tasks.count

setting_id = @user.setting.id
assert_not_nil setting_id
tc_id = @user.task_categories.first.id
assert_not_nil tc_id
task_id = @user.tasks.first.id
assert_not_nil task_id
quote_id = @user.quotes.first.id
assert_not_nil quote_id

@user.destroy!
assert_nil Setting.find_by_id(setting_id)
assert_nil TaskCategory.find_by_id(tc_id)
assert_nil Task.find_by_id(task_id)
assert_nil Quote.find_by_id(quote_id)
end
end

0 comments on commit b17f4db

Please sign in to comment.