Browse Source

Adds model validations and test units

Lou 3 years ago
parent
commit
1de186f65a

+ 4
- 0
app/models/record.rb View File

@@ -1,4 +1,8 @@
1 1
 class Record < ApplicationRecord
2 2
   belongs_to :user
3 3
   belongs_to :task
4
+
5
+  validates :duration, presence: :true
6
+  validates :task, presence: true
7
+  validates :user, presence: true
4 8
 end

+ 2
- 2
db/migrate/20210511212634_create_records.rb View File

@@ -2,10 +2,10 @@ class CreateRecords < ActiveRecord::Migration[6.1]
2 2
   def change
3 3
     create_table :records do |t|
4 4
       t.integer :duration, null: false
5
-      t.boolean :is_handled, default: false
6
-      t.references :handled_by, default: :null
7 5
       t.references :user, null: false
8 6
       t.references :task, null: false
7
+      t.boolean :is_handled, default: false
8
+      t.references :handled_by, default: nil
9 9
 
10 10
       t.timestamps
11 11
     end

+ 2
- 2
db/schema.rb View File

@@ -34,10 +34,10 @@ ActiveRecord::Schema.define(version: 2021_05_11_212634) do
34 34
 
35 35
   create_table "records", force: :cascade do |t|
36 36
     t.integer "duration", null: false
37
-    t.boolean "is_handled", default: false
38
-    t.integer "handled_by_id"
39 37
     t.integer "user_id", null: false
40 38
     t.integer "task_id", null: false
39
+    t.boolean "is_handled", default: false
40
+    t.integer "handled_by_id"
41 41
     t.datetime "created_at", precision: 6, null: false
42 42
     t.datetime "updated_at", precision: 6, null: false
43 43
     t.index ["handled_by_id"], name: "index_records_on_handled_by_id"

+ 2
- 8
test/fixtures/records.yml View File

@@ -1,13 +1,7 @@
1 1
 # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2 2
 
3 3
 one:
4
-  duration: 2021-05-11 23:26:34
5
-  handled: false
4
+  duration: 666
6 5
   user: one
7 6
   task: one
8
-
9
-two:
10
-  duration: 2021-05-11 23:26:34
11
-  handled: false
12
-  user: two
13
-  task: two
7
+  is_handled: false

+ 26
- 2
test/models/record_test.rb View File

@@ -1,7 +1,31 @@
1 1
 require "test_helper"
2 2
 
3 3
 class RecordTest < ActiveSupport::TestCase
4
-  # test "the truth" do
5
-  #   assert true
4
+  setup do
5
+    @user = users(:one)
6
+    @task = tasks(:one)
7
+    @record = records(:one)
8
+  end
9
+
10
+  test "record should have valid duration, user and task ids" do
11
+    record = Record.new(duration: 100, user_id: @user.id, task_id: @task.id)
12
+    assert record.valid?
13
+  end
14
+
15
+  test "record without a duration should not be valid" do
16
+    record = Record.new(user_id: @user.id, task_id: @task.id)
17
+    assert_not record.valid?
18
+  end
19
+
20
+  test "record without user and task ids should not be valid" do
21
+    record = Record.new(duration: 100)
22
+    assert_not record.valid?
23
+  end
24
+
25
+  # NOT SURE THE FOLLOWING TEST IS TESTING ANYTHING USEFULL
26
+  # test "handled records should have a valid handled_by value" do
27
+  #   @record.update(is_handled: true)
28
+  #   assert @record.valid?
6 29
   # end
30
+
7 31
 end

Loading…
Cancel
Save