浏览代码

Generates Task model and adds relative unit tests

Lou 3 年前
父节点
当前提交
5584e328b1
共有 8 个文件被更改,包括 64 次插入2 次删除
  1. 2
    0
      app/models/activity.rb
  2. 7
    0
      app/models/task.rb
  3. 2
    0
      app/models/user.rb
  4. 3
    1
      config/routes.rb
  5. 12
    0
      db/migrate/20210510133939_create_tasks.rb
  6. 13
    1
      db/schema.rb
  7. 7
    0
      test/fixtures/tasks.yml
  8. 18
    0
      test/models/task_test.rb

+ 2
- 0
app/models/activity.rb 查看文件

@@ -4,6 +4,8 @@ class Activity < ApplicationRecord
4 4
   has_one :joined_user_activity
5 5
   has_one :user, :through => :joined_user_activity
6 6
 
7
+  has_many :tasks
8
+
7 9
   validates :name, presence: true
8 10
   validates :author, presence: true
9 11
 end

+ 7
- 0
app/models/task.rb 查看文件

@@ -0,0 +1,7 @@
1
+class Task < ApplicationRecord
2
+  validates :name, presence: true
3
+  validates :user, presence: true
4
+
5
+  belongs_to :activity
6
+  belongs_to :user
7
+end

+ 2
- 0
app/models/user.rb 查看文件

@@ -9,5 +9,7 @@ class User < ApplicationRecord
9 9
   has_many :joined_user_activities
10 10
   has_many :activities, :through => :joined_user_activities
11 11
 
12
+  has_many :tasks
13
+
12 14
   has_secure_password
13 15
 end

+ 3
- 1
config/routes.rb 查看文件

@@ -3,7 +3,9 @@ Rails.application.routes.draw do
3 3
     namespace :v1 do
4 4
       resources :users
5 5
       resources :tokens, only: %i[create]
6
-      resources :activities
6
+      resources :activities do
7
+        resources :tasks
8
+      end
7 9
     end
8 10
   end
9 11
 end

+ 12
- 0
db/migrate/20210510133939_create_tasks.rb 查看文件

@@ -0,0 +1,12 @@
1
+class CreateTasks < ActiveRecord::Migration[6.1]
2
+  def change
3
+    create_table :tasks do |t|
4
+      t.string :name, index: true
5
+      t.text :description
6
+      t.references :activity, null: false
7
+      t.references :user, null: false
8
+
9
+      t.timestamps
10
+    end
11
+  end
12
+end

+ 13
- 1
db/schema.rb 查看文件

@@ -10,7 +10,7 @@
10 10
 #
11 11
 # It's strongly recommended that you check this file into your version control system.
12 12
 
13
-ActiveRecord::Schema.define(version: 2021_04_29_184439) do
13
+ActiveRecord::Schema.define(version: 2021_05_10_133939) do
14 14
 
15 15
   create_table "activities", force: :cascade do |t|
16 16
     t.string "name", null: false
@@ -32,6 +32,18 @@ ActiveRecord::Schema.define(version: 2021_04_29_184439) do
32 32
     t.index ["user_id"], name: "index_joined_user_activities_on_user_id"
33 33
   end
34 34
 
35
+  create_table "tasks", force: :cascade do |t|
36
+    t.string "name"
37
+    t.text "description"
38
+    t.integer "activity_id", null: false
39
+    t.integer "user_id", null: false
40
+    t.datetime "created_at", precision: 6, null: false
41
+    t.datetime "updated_at", precision: 6, null: false
42
+    t.index ["activity_id"], name: "index_tasks_on_activity_id"
43
+    t.index ["name"], name: "index_tasks_on_name"
44
+    t.index ["user_id"], name: "index_tasks_on_user_id"
45
+  end
46
+
35 47
   create_table "users", force: :cascade do |t|
36 48
     t.string "email", null: false
37 49
     t.string "username", null: false

+ 7
- 0
test/fixtures/tasks.yml 查看文件

@@ -0,0 +1,7 @@
1
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+one:
4
+  name: Do something
5
+  description: Description of a task
6
+  user: one
7
+  activity: one

+ 18
- 0
test/models/task_test.rb 查看文件

@@ -0,0 +1,18 @@
1
+require "test_helper"
2
+
3
+class TaskTest < ActiveSupport::TestCase
4
+  setup do
5
+    @user = users(:one)
6
+    @activity = activities(:one)
7
+  end
8
+
9
+  test "task should be valid" do
10
+    task = Task.new(name: "Write", description: "Write a new line", user_id: @user.id, activity_id: @activity.id)
11
+    assert task.valid?
12
+  end
13
+
14
+  test "task should not be valid" do
15
+    task = Task.new(name: "Write", description: "Write a new line")
16
+    assert_not task.valid?
17
+  end
18
+end

正在加载...
取消
保存