Add activity model and unit tests
This commit is contained in:
parent
7e3f9cdce2
commit
a594b87387
8 changed files with 60 additions and 2 deletions
6
app/models/activity.rb
Normal file
6
app/models/activity.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
class Activity < ApplicationRecord
|
||||
belongs_to :author, class_name: "User"
|
||||
|
||||
validates :name, presence: true
|
||||
validates :author, presence: true
|
||||
end
|
||||
|
|
@ -4,5 +4,6 @@ class User < ApplicationRecord
|
|||
validates :email, format:{ with: /\S+@\S+[.]\S+/, message: "Must be a valid email format." }
|
||||
validates :password_digest, presence: true
|
||||
|
||||
has_many :created_activities, foreign_key: 'author_id', class_name: 'Activity', dependent: :destroy
|
||||
has_secure_password
|
||||
end
|
||||
|
|
|
|||
12
db/migrate/20210413142821_create_activities.rb
Normal file
12
db/migrate/20210413142821_create_activities.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
class CreateActivities < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
create_table :activities do |t|
|
||||
t.string :name, index: true, null: false
|
||||
t.string :client
|
||||
t.string :description
|
||||
t.references :author, null: false, foreign_key: { to_table: :users }
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
14
db/schema.rb
14
db/schema.rb
|
|
@ -10,7 +10,18 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2021_04_13_093535) do
|
||||
ActiveRecord::Schema.define(version: 2021_04_13_142821) do
|
||||
|
||||
create_table "activities", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "client"
|
||||
t.string "description"
|
||||
t.integer "author_id", null: false
|
||||
t.datetime "created_at", precision: 6, null: false
|
||||
t.datetime "updated_at", precision: 6, null: false
|
||||
t.index ["author_id"], name: "index_activities_on_author_id"
|
||||
t.index ["name"], name: "index_activities_on_name"
|
||||
end
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
t.string "email", null: false
|
||||
|
|
@ -22,4 +33,5 @@ ActiveRecord::Schema.define(version: 2021_04_13_093535) do
|
|||
t.index ["username"], name: "index_users_on_username", unique: true
|
||||
end
|
||||
|
||||
add_foreign_key "activities", "users", column: "author_id"
|
||||
end
|
||||
|
|
|
|||
7
test/fixtures/activities.yml
vendored
Normal file
7
test/fixtures/activities.yml
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
name: RandomActivity
|
||||
client: MyClient
|
||||
description: RandomActivity
|
||||
author: one
|
||||
2
test/fixtures/users.yml
vendored
2
test/fixtures/users.yml
vendored
|
|
@ -3,4 +3,4 @@
|
|||
one:
|
||||
email: one@one.com
|
||||
username: OneUsername
|
||||
password_digest: <%= BCrypt::Password.create('g00d_pa$$') %>
|
||||
password_digest: <%= BCrypt::Password.create('g00d_pa$$') %>
|
||||
14
test/models/activity_test.rb
Normal file
14
test/models/activity_test.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
require "test_helper"
|
||||
|
||||
class ActivityTest < ActiveSupport::TestCase
|
||||
test "activity's author should exist" do
|
||||
user = User.new(email: "test@test.com", username: "UserTest", password_digest: 'password')
|
||||
activity = Activity.new(name: "Pyheatpump", description: "A software to control heat pumps", author: user)
|
||||
assert activity.valid?
|
||||
end
|
||||
|
||||
test "not valid if author does not exist" do
|
||||
activity = Activity.new(name: "Pyheatpump", description: "A software to control heat pumps", author: User.find_by_id(666))
|
||||
assert_not activity.valid?
|
||||
end
|
||||
end
|
||||
|
|
@ -22,4 +22,10 @@ class UserTest < ActiveSupport::TestCase
|
|||
user = User.new(email: other_user.email, username: "UserTest", password_digest: "test_password")
|
||||
assert_not user.valid?
|
||||
end
|
||||
|
||||
test 'destroy user should destroy linked activity' do
|
||||
assert_difference('Activity.count', -1) do
|
||||
users(:one).destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue