Parcourir la source

Add activity model and unit tests

Lou il y a 3 ans
Parent
révision
a594b87387

+ 6
- 0
app/models/activity.rb Voir le fichier

1
+class Activity < ApplicationRecord
2
+  belongs_to :author, class_name: "User"
3
+  
4
+  validates :name, presence: true
5
+  validates :author, presence: true
6
+end

+ 1
- 0
app/models/user.rb Voir le fichier

4
   validates :email, format:{ with: /\S+@\S+[.]\S+/, message: "Must be a valid email format." }
4
   validates :email, format:{ with: /\S+@\S+[.]\S+/, message: "Must be a valid email format." }
5
   validates :password_digest, presence: true
5
   validates :password_digest, presence: true
6
 
6
 
7
+  has_many :created_activities, foreign_key: 'author_id', class_name: 'Activity', dependent: :destroy
7
   has_secure_password
8
   has_secure_password
8
 end
9
 end

+ 12
- 0
db/migrate/20210413142821_create_activities.rb Voir le fichier

1
+class CreateActivities < ActiveRecord::Migration[6.1]
2
+  def change
3
+    create_table :activities do |t|
4
+      t.string :name, index: true, null: false
5
+      t.string :client
6
+      t.string :description
7
+      t.references :author, null: false, foreign_key: { to_table: :users }
8
+
9
+      t.timestamps
10
+    end
11
+  end
12
+end

+ 13
- 1
db/schema.rb Voir le fichier

10
 #
10
 #
11
 # It's strongly recommended that you check this file into your version control system.
11
 # It's strongly recommended that you check this file into your version control system.
12
 
12
 
13
-ActiveRecord::Schema.define(version: 2021_04_13_093535) do
13
+ActiveRecord::Schema.define(version: 2021_04_13_142821) do
14
+
15
+  create_table "activities", force: :cascade do |t|
16
+    t.string "name", null: false
17
+    t.string "client"
18
+    t.string "description"
19
+    t.integer "author_id", null: false
20
+    t.datetime "created_at", precision: 6, null: false
21
+    t.datetime "updated_at", precision: 6, null: false
22
+    t.index ["author_id"], name: "index_activities_on_author_id"
23
+    t.index ["name"], name: "index_activities_on_name"
24
+  end
14
 
25
 
15
   create_table "users", force: :cascade do |t|
26
   create_table "users", force: :cascade do |t|
16
     t.string "email", null: false
27
     t.string "email", null: false
22
     t.index ["username"], name: "index_users_on_username", unique: true
33
     t.index ["username"], name: "index_users_on_username", unique: true
23
   end
34
   end
24
 
35
 
36
+  add_foreign_key "activities", "users", column: "author_id"
25
 end
37
 end

+ 7
- 0
test/fixtures/activities.yml Voir le fichier

1
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+one:
4
+  name: RandomActivity
5
+  client: MyClient
6
+  description: RandomActivity
7
+  author: one

+ 1
- 1
test/fixtures/users.yml Voir le fichier

3
 one:
3
 one:
4
   email: one@one.com
4
   email: one@one.com
5
   username: OneUsername
5
   username: OneUsername
6
-  password_digest: <%= BCrypt::Password.create('g00d_pa$$') %>
6
+  password_digest: <%= BCrypt::Password.create('g00d_pa$$') %>

+ 14
- 0
test/models/activity_test.rb Voir le fichier

1
+require "test_helper"
2
+
3
+class ActivityTest < ActiveSupport::TestCase
4
+  test "activity's author should exist" do
5
+    user = User.new(email: "test@test.com", username: "UserTest", password_digest: 'password')
6
+    activity = Activity.new(name: "Pyheatpump", description: "A software to control heat pumps", author: user)
7
+    assert activity.valid?
8
+  end
9
+
10
+  test "not valid if author does not exist" do
11
+    activity = Activity.new(name: "Pyheatpump", description: "A software to control heat pumps", author: User.find_by_id(666))
12
+    assert_not activity.valid?
13
+  end
14
+end

+ 6
- 0
test/models/user_test.rb Voir le fichier

22
   user = User.new(email: other_user.email, username: "UserTest", password_digest: "test_password")
22
   user = User.new(email: other_user.email, username: "UserTest", password_digest: "test_password")
23
   assert_not user.valid?
23
   assert_not user.valid?
24
   end
24
   end
25
+
26
+  test 'destroy user should destroy linked activity' do
27
+    assert_difference('Activity.count', -1) do
28
+      users(:one).destroy
29
+    end
30
+  end
25
 end
31
 end

Loading…
Annuler
Enregistrer