Переглянути джерело

Add defs index and show, and relative unit tests

Lou 3 роки тому
джерело
коміт
02ca539cc8

+ 24
- 0
app/controllers/api/v1/records_controller.rb Переглянути файл

1
+class Api::V1::RecordsController < ApplicationController
2
+  before_action :set_record, only: %i[show update destroy]
3
+  before_action :check_login
4
+
5
+  def index
6
+    render json: RecordSerializer.new(Task.find(params[:task_id]).records).serializable_hash.to_json
7
+  end
8
+
9
+  def show
10
+    render json: RecordSerializer.new(@record).serializable_hash.to_json
11
+  end
12
+  
13
+
14
+  private
15
+
16
+  # Only allow a trusted parameter "white list" through.
17
+  def record_params
18
+    params.require(:record).permit(:duration, :user_id, :task_id, :is_handled, :handled_by_id)
19
+  end
20
+
21
+  def set_record
22
+    @record = Task.find(params[:task_id]).records.find(params[:id])
23
+  end
24
+end

+ 1
- 0
app/models/task.rb Переглянути файл

4
 
4
 
5
   belongs_to :activity
5
   belongs_to :activity
6
   belongs_to :user
6
   belongs_to :user
7
+  has_many :records
7
 end
8
 end

+ 6
- 0
app/serializers/record_serializer.rb Переглянути файл

1
+class RecordSerializer
2
+  include JSONAPI::Serializer
3
+  attributes :duration, :user_id, :task_id, :is_handled, :handled_by_id
4
+
5
+  cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
6
+end

+ 1
- 0
app/serializers/task_serializer.rb Переглянути файл

1
 class TaskSerializer
1
 class TaskSerializer
2
   include JSONAPI::Serializer
2
   include JSONAPI::Serializer
3
   attributes :name, :description, :user_id, :activity_id
3
   attributes :name, :description, :user_id, :activity_id
4
+  # has_many :records
4
 
5
 
5
   cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
6
   cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
6
 end
7
 end

+ 5
- 0
bin/rails Переглянути файл

1
 #!/usr/bin/env ruby
1
 #!/usr/bin/env ruby
2
+begin
3
+  load File.expand_path('../spring', __FILE__)
4
+rescue LoadError => e
5
+  raise unless e.message.include?('spring')
6
+end
2
 load File.expand_path("spring", __dir__)
7
 load File.expand_path("spring", __dir__)
3
 APP_PATH = File.expand_path('../config/application', __dir__)
8
 APP_PATH = File.expand_path('../config/application', __dir__)
4
 require_relative "../config/boot"
9
 require_relative "../config/boot"

+ 5
- 0
bin/rake Переглянути файл

1
 #!/usr/bin/env ruby
1
 #!/usr/bin/env ruby
2
+begin
3
+  load File.expand_path('../spring', __FILE__)
4
+rescue LoadError => e
5
+  raise unless e.message.include?('spring')
6
+end
2
 load File.expand_path("spring", __dir__)
7
 load File.expand_path("spring", __dir__)
3
 require_relative "../config/boot"
8
 require_relative "../config/boot"
4
 require "rake"
9
 require "rake"

+ 12
- 9
bin/spring Переглянути файл

1
 #!/usr/bin/env ruby
1
 #!/usr/bin/env ruby
2
-if !defined?(Spring) && [nil, "development", "test"].include?(ENV["RAILS_ENV"])
3
-  gem "bundler"
4
-  require "bundler"
5
 
2
 
6
-  # Load Spring without loading other gems in the Gemfile, for speed.
7
-  Bundler.locked_gems&.specs&.find { |spec| spec.name == "spring" }&.tap do |spring|
3
+# This file loads Spring without using Bundler, in order to be fast.
4
+# It gets overwritten when you run the `spring binstub` command.
5
+
6
+unless defined?(Spring)
7
+  require 'rubygems'
8
+  require 'bundler'
9
+
10
+  lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read)
11
+  spring = lockfile.specs.detect { |spec| spec.name == 'spring' }
12
+  if spring
8
     Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path
13
     Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path
9
-    gem "spring", spring.version
10
-    require "spring/binstub"
11
-  rescue Gem::LoadError
12
-    # Ignore when Spring is not installed.
14
+    gem 'spring', spring.version
15
+    require 'spring/binstub'
13
   end
16
   end
14
 end
17
 end

+ 3
- 1
config/routes.rb Переглянути файл

4
       resources :users
4
       resources :users
5
       resources :tokens, only: %i[create]
5
       resources :tokens, only: %i[create]
6
       resources :activities do
6
       resources :activities do
7
-        resources :tasks
7
+        resources :tasks do
8
+          resources :records
9
+        end
8
       end
10
       end
9
       resources :tasks
11
       resources :tasks
10
     end
12
     end

+ 1
- 1
db/migrate/20210511212634_create_records.rb Переглянути файл

5
       t.references :user, null: false
5
       t.references :user, null: false
6
       t.references :task, null: false
6
       t.references :task, null: false
7
       t.boolean :is_handled, default: false
7
       t.boolean :is_handled, default: false
8
-      t.references :handled_by, default: nil
8
+      t.references :handled_by, default: nil, null: true
9
 
9
 
10
       t.timestamps
10
       t.timestamps
11
     end
11
     end

+ 11
- 0
db/seeds.rb Переглянути файл

6
 JoinedUserActivity.reset_pk_sequence
6
 JoinedUserActivity.reset_pk_sequence
7
 Task.delete_all
7
 Task.delete_all
8
 Task.reset_pk_sequence
8
 Task.reset_pk_sequence
9
+Record.delete_all
10
+Record.reset_pk_sequence
9
 
11
 
10
 10.times do |i|
12
 10.times do |i|
11
   name = Faker::Name.first_name.downcase
13
   name = Faker::Name.first_name.downcase
37
     activity_id: Activity.all.sample.id
39
     activity_id: Activity.all.sample.id
38
   )
40
   )
39
   puts "Created TASK ##{i} - #{task.name}"
41
   puts "Created TASK ##{i} - #{task.name}"
42
+end
43
+
44
+20.times do |i|
45
+  record = Record.create(
46
+    duration: rand(60..480),
47
+    user_id: User.all.sample.id,
48
+    task_id: Task.all.sample.id
49
+  )
50
+  puts "Created RECORD ##{i}"
40
 end
51
 end


+ 37
- 0
test/controllers/api/v1/records_controller_test.rb Переглянути файл

1
+require "test_helper"
2
+
3
+class Api::V1::RecordsControllerTest < ActionDispatch::IntegrationTest
4
+  setup do
5
+    @record = records(:one)
6
+    @activity = activities(:one)
7
+    @task = tasks(:one)
8
+  end
9
+
10
+  # INDEX
11
+  test "should access index" do
12
+    get api_v1_activity_task_records_url(@activity, @task),
13
+    headers: { Authorization: JsonWebToken.encode(user_id: @record.user_id) },
14
+    as: :json
15
+    assert_response :success
16
+  end
17
+
18
+  test "should not access index" do
19
+    get api_v1_activity_task_records_url(@activity, @task),
20
+    as: :json
21
+    assert_response :forbidden
22
+  end
23
+
24
+  # SHOW
25
+  test "should access show" do
26
+    get api_v1_activity_task_record_url(@activity, @task, @record),
27
+    headers: { Authorization: JsonWebToken.encode(user_id: @record.user_id) },
28
+    as: :json
29
+    assert_response :success
30
+  end
31
+
32
+  test "should not access show" do
33
+    get api_v1_activity_task_record_url(@activity, @task, @record),
34
+    as: :json
35
+    assert_response :forbidden
36
+  end
37
+end

+ 7
- 1
test/fixtures/records.yml Переглянути файл

4
   duration: 666
4
   duration: 666
5
   user: one
5
   user: one
6
   task: one
6
   task: one
7
-  is_handled: false
7
+  is_handled: false
8
+
9
+two:
10
+  duration: 100
11
+  user: one
12
+  task: two 
13
+  is_handled: yes

+ 0
- 7
test/models/record_test.rb Переглянути файл

21
     record = Record.new(duration: 100)
21
     record = Record.new(duration: 100)
22
     assert_not record.valid?
22
     assert_not record.valid?
23
   end
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?
29
-  # end
30
-
31
 end
24
 end

Loading…
Відмінити
Зберегти