From c12961d3ee613245124113f3f0a406a4ea2c2825 Mon Sep 17 00:00:00 2001 From: zarbielli Date: Mon, 17 Sep 2018 19:45:53 -0300 Subject: [PATCH 01/26] Remove RatingRate from Locals model Co-authored-by: GabrielDVpereira --- ...917224258_remove_rating_rate_from_locals.rb | 5 +++++ indicaAi/db/schema.rb | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 indicaAi/db/migrate/20180917224258_remove_rating_rate_from_locals.rb diff --git a/indicaAi/db/migrate/20180917224258_remove_rating_rate_from_locals.rb b/indicaAi/db/migrate/20180917224258_remove_rating_rate_from_locals.rb new file mode 100644 index 00000000..7ba5b714 --- /dev/null +++ b/indicaAi/db/migrate/20180917224258_remove_rating_rate_from_locals.rb @@ -0,0 +1,5 @@ +class RemoveRatingRateFromLocals < ActiveRecord::Migration[5.1] + def change + remove_column :locals, :RatingRate, :float + end +end diff --git a/indicaAi/db/schema.rb b/indicaAi/db/schema.rb index e0038cec..7223f9c2 100644 --- a/indicaAi/db/schema.rb +++ b/indicaAi/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180913193142) do +ActiveRecord::Schema.define(version: 20180917224258) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -18,7 +18,14 @@ create_table "locals", force: :cascade do |t| t.string "name" t.text "description" - t.float "RatingRate" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "places", force: :cascade do |t| + t.string "name" + t.text "desc" + t.float "vote_average" t.datetime "created_at", null: false t.datetime "updated_at", null: false end @@ -29,4 +36,11 @@ t.datetime "updated_at", null: false end + create_table "users", force: :cascade do |t| + t.string "name" + t.string "email" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + end From a0fad8665d4d9435364c162f448c6b82341b14b2 Mon Sep 17 00:00:00 2001 From: zarbielli Date: Mon, 17 Sep 2018 20:08:37 -0300 Subject: [PATCH 02/26] Adding LocalRating model Co-authored-by: GabrielDVpereira --- indicaAi/app/models/local.rb | 3 ++- indicaAi/app/models/local_rating.rb | 3 +++ .../db/migrate/20180917225656_create_local_ratings.rb | 10 ++++++++++ indicaAi/db/schema.rb | 11 ++++++++++- indicaAi/spec/models/local_rating_spec.rb | 5 +++++ 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 indicaAi/app/models/local_rating.rb create mode 100644 indicaAi/db/migrate/20180917225656_create_local_ratings.rb create mode 100644 indicaAi/spec/models/local_rating_spec.rb diff --git a/indicaAi/app/models/local.rb b/indicaAi/app/models/local.rb index 56911297..076b5e94 100644 --- a/indicaAi/app/models/local.rb +++ b/indicaAi/app/models/local.rb @@ -1,7 +1,8 @@ # Description of Places Class class Local < ApplicationRecord + has_many :local_ratings validates :name, presence: true, length: { minimum: 2 } - validates :RatingRate, presence: true + def self.find_by_name(params) Local.where('upper(name) like ?', "%#{params.upcase}%") end diff --git a/indicaAi/app/models/local_rating.rb b/indicaAi/app/models/local_rating.rb new file mode 100644 index 00000000..1d52c612 --- /dev/null +++ b/indicaAi/app/models/local_rating.rb @@ -0,0 +1,3 @@ +class LocalRating < ApplicationRecord + belongs_to :local +end diff --git a/indicaAi/db/migrate/20180917225656_create_local_ratings.rb b/indicaAi/db/migrate/20180917225656_create_local_ratings.rb new file mode 100644 index 00000000..3aaa8363 --- /dev/null +++ b/indicaAi/db/migrate/20180917225656_create_local_ratings.rb @@ -0,0 +1,10 @@ +class CreateLocalRatings < ActiveRecord::Migration[5.1] + def change + create_table :local_ratings do |t| + t.integer :value + t.references :local, foreign_key: true + + t.timestamps + end + end +end diff --git a/indicaAi/db/schema.rb b/indicaAi/db/schema.rb index 7223f9c2..c1e37ef8 100644 --- a/indicaAi/db/schema.rb +++ b/indicaAi/db/schema.rb @@ -10,11 +10,19 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180917224258) do +ActiveRecord::Schema.define(version: 20180917225656) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "local_ratings", force: :cascade do |t| + t.integer "value" + t.bigint "local_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["local_id"], name: "index_local_ratings_on_local_id" + end + create_table "locals", force: :cascade do |t| t.string "name" t.text "description" @@ -43,4 +51,5 @@ t.datetime "updated_at", null: false end + add_foreign_key "local_ratings", "locals" end diff --git a/indicaAi/spec/models/local_rating_spec.rb b/indicaAi/spec/models/local_rating_spec.rb new file mode 100644 index 00000000..f39df228 --- /dev/null +++ b/indicaAi/spec/models/local_rating_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe LocalRating, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From bbcf1ef9acb7651b3e77aaf8d5dbc3bc42832b7e Mon Sep 17 00:00:00 2001 From: zarbielli Date: Mon, 17 Sep 2018 22:32:36 -0300 Subject: [PATCH 03/26] Add logic to show Locals Rating Co-authored-by: GabrielDVpreira --- indicaAi/app/controllers/locals_controller.rb | 6 ++++++ indicaAi/app/models/local.rb | 5 +++++ indicaAi/app/models/local_rating.rb | 1 + indicaAi/config/routes.rb | 1 + 4 files changed, 13 insertions(+) diff --git a/indicaAi/app/controllers/locals_controller.rb b/indicaAi/app/controllers/locals_controller.rb index cf1db2f9..7033d8ae 100644 --- a/indicaAi/app/controllers/locals_controller.rb +++ b/indicaAi/app/controllers/locals_controller.rb @@ -11,4 +11,10 @@ def search_locals locals = Local.find_by_name(params[:name]) json_response(locals) end + + def show_rating + rating = Local.find_local_ratings(params[:id_local]) + json_response(rating) + end + end diff --git a/indicaAi/app/models/local.rb b/indicaAi/app/models/local.rb index 076b5e94..63cc32f4 100644 --- a/indicaAi/app/models/local.rb +++ b/indicaAi/app/models/local.rb @@ -6,4 +6,9 @@ class Local < ApplicationRecord def self.find_by_name(params) Local.where('upper(name) like ?', "%#{params.upcase}%") end + + def self.find_local_ratings(params) + Local.find(params).local_ratings + end + end diff --git a/indicaAi/app/models/local_rating.rb b/indicaAi/app/models/local_rating.rb index 1d52c612..62f8f54c 100644 --- a/indicaAi/app/models/local_rating.rb +++ b/indicaAi/app/models/local_rating.rb @@ -1,3 +1,4 @@ class LocalRating < ApplicationRecord belongs_to :local + end diff --git a/indicaAi/config/routes.rb b/indicaAi/config/routes.rb index 4195fa28..1aff8ca3 100644 --- a/indicaAi/config/routes.rb +++ b/indicaAi/config/routes.rb @@ -2,5 +2,6 @@ get '/locals', to: 'locals#index' get '/locals/name/:name', to: 'locals#search_locals' + get '/locals/:id_local/ratings', to: 'locals#show_rating' end From 45d49520838e2005f6c2ca8dc51b3cf1791498f2 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Tue, 18 Sep 2018 12:49:36 -0300 Subject: [PATCH 04/26] added validation to the local and rating models Co-authored by: zarbielli --- indicaAi/app/models/local.rb | 2 ++ indicaAi/app/models/local_rating.rb | 5 ++++- indicaAi/db/schema.rb | 15 --------------- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/indicaAi/app/models/local.rb b/indicaAi/app/models/local.rb index 63cc32f4..ee14d92c 100644 --- a/indicaAi/app/models/local.rb +++ b/indicaAi/app/models/local.rb @@ -1,6 +1,8 @@ # Description of Places Class class Local < ApplicationRecord has_many :local_ratings + validates_associated :local_ratings + validates_presence_of :local_ratings validates :name, presence: true, length: { minimum: 2 } def self.find_by_name(params) diff --git a/indicaAi/app/models/local_rating.rb b/indicaAi/app/models/local_rating.rb index 62f8f54c..dac8dacb 100644 --- a/indicaAi/app/models/local_rating.rb +++ b/indicaAi/app/models/local_rating.rb @@ -1,4 +1,7 @@ class LocalRating < ApplicationRecord belongs_to :local - + validates :value, presence:true + validates :value, numericality: true + validates_numericality_of :value, :less_than_or_equal_to => 5 + validates_numericality_of :value, :greater_than_or_equal_to => 0 end diff --git a/indicaAi/db/schema.rb b/indicaAi/db/schema.rb index c1e37ef8..fc83a436 100644 --- a/indicaAi/db/schema.rb +++ b/indicaAi/db/schema.rb @@ -30,26 +30,11 @@ t.datetime "updated_at", null: false end - create_table "places", force: :cascade do |t| - t.string "name" - t.text "desc" - t.float "vote_average" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - create_table "user_identifiers", force: :cascade do |t| t.integer "userId" t.datetime "created_at", null: false t.datetime "updated_at", null: false end - create_table "users", force: :cascade do |t| - t.string "name" - t.string "email" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - add_foreign_key "local_ratings", "locals" end From df8c876a2532784adb2c60b63372184633af70f4 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Tue, 18 Sep 2018 17:38:37 -0300 Subject: [PATCH 05/26] #54 - Adding local and rating into local json Co-authored-by: Zarbielli Co-authored-by: mendesiasmin --- indicaAi/app/controllers/locals_controller.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/indicaAi/app/controllers/locals_controller.rb b/indicaAi/app/controllers/locals_controller.rb index 7033d8ae..d9fcb49c 100644 --- a/indicaAi/app/controllers/locals_controller.rb +++ b/indicaAi/app/controllers/locals_controller.rb @@ -3,18 +3,25 @@ class LocalsController < ApplicationController # GET /locals def index locals = Local.all - json_response(locals) + result = [] + locals.each do |local| + result << locals.as_json(methods: [:local_ratings]) + end + json_response(result) end # GET /locals/name/:name def search_locals locals = Local.find_by_name(params[:name]) - json_response(locals) + result = [] + result << locals.as_json(methods: [:local_ratings]) + json_response(result) end def show_rating rating = Local.find_local_ratings(params[:id_local]) - json_response(rating) + locals = Local.find(params[:id_local]) + json_response(locals: locals,rating: rating,) end end From 90148fa56371e2a1e56beee7423d594cb576248c Mon Sep 17 00:00:00 2001 From: Gabriel Date: Tue, 18 Sep 2018 17:42:24 -0300 Subject: [PATCH 06/26] #54 - removing validation of associatiion Co-authored-by: Zarbielli --- indicaAi/app/models/local.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/indicaAi/app/models/local.rb b/indicaAi/app/models/local.rb index ee14d92c..63cc32f4 100644 --- a/indicaAi/app/models/local.rb +++ b/indicaAi/app/models/local.rb @@ -1,8 +1,6 @@ # Description of Places Class class Local < ApplicationRecord has_many :local_ratings - validates_associated :local_ratings - validates_presence_of :local_ratings validates :name, presence: true, length: { minimum: 2 } def self.find_by_name(params) From 7cef18707a39fb685480b2b59dc142c1844d7ab1 Mon Sep 17 00:00:00 2001 From: zarbielli Date: Thu, 20 Sep 2018 14:47:27 -0300 Subject: [PATCH 07/26] #54 - Adding test of local_rating model Co-authored-by: GabrielDVpereira --- indicaAi/spec/models/local_rating_spec.rb | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/indicaAi/spec/models/local_rating_spec.rb b/indicaAi/spec/models/local_rating_spec.rb index f39df228..136a47bd 100644 --- a/indicaAi/spec/models/local_rating_spec.rb +++ b/indicaAi/spec/models/local_rating_spec.rb @@ -1,5 +1,25 @@ require 'rails_helper' RSpec.describe LocalRating, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + it 'Testing if value is integer - Should return true for value.class == Integer' do + note1 = LocalRating.new + note1.value = 3 + assert note1.value.class == Integer + end + + it 'Testing typecast - Should return integer if input is float' do + note2 = LocalRating.new + note2.value = 3.4 + assert note2.value == 3 + end + + it { should belong_to(:local) } + + it { should validate_presence_of(:value) } + + it { should validate_numericality_of(:value) } + + it { should validate_numericality_of(:value).is_greater_than_or_equal_to(0) } + + it { should validate_numericality_of(:value).is_less_than_or_equal_to(5) } end From a1531d3c0ab26f35847a0ccfc9b73eeb1d2fcd23 Mon Sep 17 00:00:00 2001 From: zarbielli Date: Thu, 20 Sep 2018 15:44:05 -0300 Subject: [PATCH 08/26] Adding test of Local model Co-authored-by: GabrielDVpereira --- indicaAi/spec/models/local_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/indicaAi/spec/models/local_spec.rb b/indicaAi/spec/models/local_spec.rb index fc627ed4..02ecabac 100644 --- a/indicaAi/spec/models/local_spec.rb +++ b/indicaAi/spec/models/local_spec.rb @@ -2,4 +2,21 @@ RSpec.describe Local, type: :model do it { should validate_presence_of(:name) } + + it 'Testing Search By Name - Should return similar names to params' do + params = 'FGA' + local = Local.new + local.name = 'FGA' + expect(Local.find_by_name(params)) == true + end + + it 'Testing Find Local Ratings - Should return rating associated to parms' do + params = 1 + local = Local.new + local.name = 'FGA' + local.save + expect(Local.find_local_ratings(params)) == true + end + end + From c4d04c3f7a2cd00f69fec8733adef76c4a52edc9 Mon Sep 17 00:00:00 2001 From: zarbielli Date: Thu, 20 Sep 2018 19:48:11 -0300 Subject: [PATCH 09/26] #54 - Adding locals_controller test Co-authored-by: GabrielDVpereira --- indicaAi/app/controllers/locals_controller.rb | 6 +++--- indicaAi/app/models/local.rb | 1 - indicaAi/app/models/local_rating.rb | 3 ++- indicaAi/spec/controllers/locals_controller_spec.rb | 9 +++++++++ indicaAi/spec/models/local_rating_spec.rb | 7 +++---- indicaAi/spec/models/local_spec.rb | 2 -- indicaAi/spec/requests/locals_spec.rb | 9 +++++++++ 7 files changed, 26 insertions(+), 11 deletions(-) diff --git a/indicaAi/app/controllers/locals_controller.rb b/indicaAi/app/controllers/locals_controller.rb index d9fcb49c..501649e9 100644 --- a/indicaAi/app/controllers/locals_controller.rb +++ b/indicaAi/app/controllers/locals_controller.rb @@ -5,7 +5,7 @@ def index locals = Local.all result = [] locals.each do |local| - result << locals.as_json(methods: [:local_ratings]) + result << local.as_json(methods: [:local_ratings]) end json_response(result) end @@ -18,10 +18,10 @@ def search_locals json_response(result) end + # GET /local/:id/rating def show_rating rating = Local.find_local_ratings(params[:id_local]) locals = Local.find(params[:id_local]) - json_response(locals: locals,rating: rating,) + json_response(locals: locals, rating: rating) end - end diff --git a/indicaAi/app/models/local.rb b/indicaAi/app/models/local.rb index 63cc32f4..8b0deb57 100644 --- a/indicaAi/app/models/local.rb +++ b/indicaAi/app/models/local.rb @@ -10,5 +10,4 @@ def self.find_by_name(params) def self.find_local_ratings(params) Local.find(params).local_ratings end - end diff --git a/indicaAi/app/models/local_rating.rb b/indicaAi/app/models/local_rating.rb index dac8dacb..4255a949 100644 --- a/indicaAi/app/models/local_rating.rb +++ b/indicaAi/app/models/local_rating.rb @@ -1,6 +1,7 @@ +# Description of Local Rating Class class LocalRating < ApplicationRecord belongs_to :local - validates :value, presence:true + validates :value, presence: true validates :value, numericality: true validates_numericality_of :value, :less_than_or_equal_to => 5 validates_numericality_of :value, :greater_than_or_equal_to => 0 diff --git a/indicaAi/spec/controllers/locals_controller_spec.rb b/indicaAi/spec/controllers/locals_controller_spec.rb index 06883a95..d8dd33cd 100644 --- a/indicaAi/spec/controllers/locals_controller_spec.rb +++ b/indicaAi/spec/controllers/locals_controller_spec.rb @@ -1,4 +1,13 @@ require 'rails_helper' RSpec.describe LocalsController, type: :controller do + it 'Test index - Should return all locals as JSON' do + get :index, format: :JSON + expect(response).to be_success + end + + it 'Test Search Locals - Should return matching locals' do + get :search_locals, format: :JSON + expect(response).to be_success + end end diff --git a/indicaAi/spec/models/local_rating_spec.rb b/indicaAi/spec/models/local_rating_spec.rb index 136a47bd..574bf720 100644 --- a/indicaAi/spec/models/local_rating_spec.rb +++ b/indicaAi/spec/models/local_rating_spec.rb @@ -1,18 +1,17 @@ require 'rails_helper' RSpec.describe LocalRating, type: :model do - it 'Testing if value is integer - Should return true for value.class == Integer' do + it 'Testing if value is integer - Should return true' do note1 = LocalRating.new note1.value = 3 - assert note1.value.class == Integer - end + assert note1.value.class == Integer + end it 'Testing typecast - Should return integer if input is float' do note2 = LocalRating.new note2.value = 3.4 assert note2.value == 3 end - it { should belong_to(:local) } it { should validate_presence_of(:value) } diff --git a/indicaAi/spec/models/local_spec.rb b/indicaAi/spec/models/local_spec.rb index 02ecabac..ee06897a 100644 --- a/indicaAi/spec/models/local_spec.rb +++ b/indicaAi/spec/models/local_spec.rb @@ -17,6 +17,4 @@ local.save expect(Local.find_local_ratings(params)) == true end - end - diff --git a/indicaAi/spec/requests/locals_spec.rb b/indicaAi/spec/requests/locals_spec.rb index 7812c6d6..db9a9078 100644 --- a/indicaAi/spec/requests/locals_spec.rb +++ b/indicaAi/spec/requests/locals_spec.rb @@ -18,4 +18,13 @@ expect(response).to have_http_status(200) end end + + describe 'GET /locals/name/:name' do + # make HTTP get request before each example + before { get '/locals/name/:name' } + + it 'return local by name' do + expect(json).not_to_be_empty + end + end end From 79d209d535c1c8ebeb2cdb84dc9d9cd111a0587f Mon Sep 17 00:00:00 2001 From: Gabriel Date: Thu, 20 Sep 2018 19:52:56 -0300 Subject: [PATCH 10/26] Fixing a bug on the index method Co-authored-by: Zarbielli --- indicaAi/app/controllers/locals_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indicaAi/app/controllers/locals_controller.rb b/indicaAi/app/controllers/locals_controller.rb index d9fcb49c..e87c58dd 100644 --- a/indicaAi/app/controllers/locals_controller.rb +++ b/indicaAi/app/controllers/locals_controller.rb @@ -5,7 +5,7 @@ def index locals = Local.all result = [] locals.each do |local| - result << locals.as_json(methods: [:local_ratings]) + result << local.as_json(methods: [:local_ratings]) end json_response(result) end From 42a60528f704b13420e236a95bafb6d14bb4bf49 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Thu, 20 Sep 2018 22:12:44 -0300 Subject: [PATCH 11/26] #54 Added test to the request spec Co-authored-by: Zarbielli --- indicaAi/spec/factories/local.rb | 3 +-- indicaAi/spec/requests/locals_spec.rb | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/indicaAi/spec/factories/local.rb b/indicaAi/spec/factories/local.rb index 5a58f55a..a87924e0 100644 --- a/indicaAi/spec/factories/local.rb +++ b/indicaAi/spec/factories/local.rb @@ -1,7 +1,6 @@ FactoryBot.define do factory :local do - name { Faker::Lorem.word } - RatingRate { Faker::Number.decimal(2) } + name { Faker::DragonBall.character } end end diff --git a/indicaAi/spec/requests/locals_spec.rb b/indicaAi/spec/requests/locals_spec.rb index db9a9078..996bda8a 100644 --- a/indicaAi/spec/requests/locals_spec.rb +++ b/indicaAi/spec/requests/locals_spec.rb @@ -10,6 +10,7 @@ before { get '/locals/' } it 'returns locals' do + expect(json).not_to be_empty expect(json.size).to eq(10) end @@ -24,7 +25,7 @@ before { get '/locals/name/:name' } it 'return local by name' do - expect(json).not_to_be_empty + expect(json).not_to be_empty end end end From 37013bbb0aa7c3aba07402fb5a33afdd07880e6c Mon Sep 17 00:00:00 2001 From: Gabriel Date: Fri, 21 Sep 2018 00:25:41 -0300 Subject: [PATCH 12/26] Adding tests for the controller Co-authored-by: Zarbielli --- .../controllers/locals_controller_spec.rb | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/indicaAi/spec/controllers/locals_controller_spec.rb b/indicaAi/spec/controllers/locals_controller_spec.rb index d8dd33cd..9820cf8f 100644 --- a/indicaAi/spec/controllers/locals_controller_spec.rb +++ b/indicaAi/spec/controllers/locals_controller_spec.rb @@ -1,13 +1,27 @@ require 'rails_helper' RSpec.describe LocalsController, type: :controller do - it 'Test index - Should return all locals as JSON' do + + let!(:local) { create(:local) } + + + it { should route(:get, '/locals').to(action: :index) } + let(:action) { LocalsController.new } + + it 'Test index - Should return all locals as JSON' do get :index, format: :JSON expect(response).to be_success end - it 'Test Search Locals - Should return matching locals' do - get :search_locals, format: :JSON - expect(response).to be_success + + describe 'GET #search_locals' do + context 'Return a local by its name as a param' do + it { should route(:get, '/locals/name/Padaria').to(action: :search_locals, name: 'Padaria') } + end + end + describe 'GET #show_rating' do + context 'Return a rating by its id as a param ' do + it { should route(:get, '/locals/name/Padaria').to(action: :search_locals, name: 'Padaria') } + end end end From b3aa7f4d78a4685dd28bc55a9224e317cc378c2b Mon Sep 17 00:00:00 2001 From: Gabriel Date: Fri, 21 Sep 2018 00:51:29 -0300 Subject: [PATCH 13/26] #54 Adjusting the file in order to match with the style sheet --- indicaAi/spec/requests/locals_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/indicaAi/spec/requests/locals_spec.rb b/indicaAi/spec/requests/locals_spec.rb index 996bda8a..aa679a52 100644 --- a/indicaAi/spec/requests/locals_spec.rb +++ b/indicaAi/spec/requests/locals_spec.rb @@ -10,7 +10,6 @@ before { get '/locals/' } it 'returns locals' do - expect(json).not_to be_empty expect(json.size).to eq(10) end From fce6964b0980673c869a7070ef717f537d383004 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Fri, 21 Sep 2018 00:52:53 -0300 Subject: [PATCH 14/26] Fixed a mistake in the controller spec test --- .../controllers/locals_controller_spec.rb | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/indicaAi/spec/controllers/locals_controller_spec.rb b/indicaAi/spec/controllers/locals_controller_spec.rb index 9820cf8f..3cddb008 100644 --- a/indicaAi/spec/controllers/locals_controller_spec.rb +++ b/indicaAi/spec/controllers/locals_controller_spec.rb @@ -1,27 +1,25 @@ require 'rails_helper' RSpec.describe LocalsController, type: :controller do - - let!(:local) { create(:local) } + + let!(:local) { create(:local) } + it { should route(:get, '/locals').to(action: :index) } + let(:action) { LocalsController.new } - it { should route(:get, '/locals').to(action: :index) } - let(:action) { LocalsController.new } + it 'Test index - Should return all locals as JSON' do + get :index, format: :JSON + expect(response).to be_success + end - it 'Test index - Should return all locals as JSON' do - get :index, format: :JSON - expect(response).to be_success - end - - - describe 'GET #search_locals' do - context 'Return a local by its name as a param' do - it { should route(:get, '/locals/name/Padaria').to(action: :search_locals, name: 'Padaria') } - end - end - describe 'GET #show_rating' do - context 'Return a rating by its id as a param ' do - it { should route(:get, '/locals/name/Padaria').to(action: :search_locals, name: 'Padaria') } - end - end + describe 'GET #search_locals' do + context 'Return a local by its name as a param' do + it { should route(:get, '/locals/name/P').to(action: :search_locals, name: 'P') } + end + end + describe 'GET #show_rating' do + context 'Return a rating by its id as a param ' do + it { should route(:get, '/locals/1/ratings').to(action: :show_rating, id_local: 1) } + end + end end From c277ddaea494e4c9390e86aadcb619d398141b75 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Fri, 21 Sep 2018 01:30:06 -0300 Subject: [PATCH 15/26] Fixing a mistake in the spec controller Co-authored-by: Zarbielli --- indicaAi/spec/controllers/locals_controller_spec.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/indicaAi/spec/controllers/locals_controller_spec.rb b/indicaAi/spec/controllers/locals_controller_spec.rb index 3cddb008..58963b6f 100644 --- a/indicaAi/spec/controllers/locals_controller_spec.rb +++ b/indicaAi/spec/controllers/locals_controller_spec.rb @@ -14,7 +14,10 @@ describe 'GET #search_locals' do context 'Return a local by its name as a param' do - it { should route(:get, '/locals/name/P').to(action: :search_locals, name: 'P') } + local = Local.new + local.name = "Xoxo" + local.save + it { should route(:get, '/locals/name/x').to(action: :search_locals, name: 'x') } end end describe 'GET #show_rating' do From 7c2fa5d5ae539fa9e0f92a606e7ae29306e837fc Mon Sep 17 00:00:00 2001 From: zarbielli Date: Fri, 21 Sep 2018 08:09:23 -0300 Subject: [PATCH 16/26] #54 - Adding local at Show_rating test --- indicaAi/spec/controllers/locals_controller_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/indicaAi/spec/controllers/locals_controller_spec.rb b/indicaAi/spec/controllers/locals_controller_spec.rb index 58963b6f..c91fdb3b 100644 --- a/indicaAi/spec/controllers/locals_controller_spec.rb +++ b/indicaAi/spec/controllers/locals_controller_spec.rb @@ -22,6 +22,9 @@ end describe 'GET #show_rating' do context 'Return a rating by its id as a param ' do + local1 = Local.new + local1.name = 'FGA' + local1.save it { should route(:get, '/locals/1/ratings').to(action: :show_rating, id_local: 1) } end end From d335227494fd62b7a26f4d04e8ef28285a2e41d0 Mon Sep 17 00:00:00 2001 From: zarbielli Date: Fri, 21 Sep 2018 08:34:18 -0300 Subject: [PATCH 17/26] #54 - Delete find locals rating test --- indicaAi/app/controllers/locals_controller.rb | 2 +- .../spec/controllers/locals_controller_spec.rb | 17 ++++------------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/indicaAi/app/controllers/locals_controller.rb b/indicaAi/app/controllers/locals_controller.rb index 501649e9..59153933 100644 --- a/indicaAi/app/controllers/locals_controller.rb +++ b/indicaAi/app/controllers/locals_controller.rb @@ -5,7 +5,7 @@ def index locals = Local.all result = [] locals.each do |local| - result << local.as_json(methods: [:local_ratings]) + result << local.as_json(methods: [:local_ratings]) end json_response(result) end diff --git a/indicaAi/spec/controllers/locals_controller_spec.rb b/indicaAi/spec/controllers/locals_controller_spec.rb index c91fdb3b..01157924 100644 --- a/indicaAi/spec/controllers/locals_controller_spec.rb +++ b/indicaAi/spec/controllers/locals_controller_spec.rb @@ -1,7 +1,6 @@ require 'rails_helper' RSpec.describe LocalsController, type: :controller do - let!(:local) { create(:local) } it { should route(:get, '/locals').to(action: :index) } @@ -14,18 +13,10 @@ describe 'GET #search_locals' do context 'Return a local by its name as a param' do - local = Local.new - local.name = "Xoxo" - local.save + local = Local.new + local.name = 'Xoxo' + local.save it { should route(:get, '/locals/name/x').to(action: :search_locals, name: 'x') } - end - end - describe 'GET #show_rating' do - context 'Return a rating by its id as a param ' do - local1 = Local.new - local1.name = 'FGA' - local1.save - it { should route(:get, '/locals/1/ratings').to(action: :show_rating, id_local: 1) } - end + end end end From 2c3e52fcfcbc645a9e0fd40b0620d82db8b30425 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Fri, 21 Sep 2018 11:11:58 -0300 Subject: [PATCH 18/26] Changed the model and the controller to be in accord with codeclimade and iasmin's feedback --- indicaAi/app/controllers/locals_controller.rb | 4 ++-- indicaAi/app/models/local.rb | 4 ++-- indicaAi/app/models/local_rating.rb | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/indicaAi/app/controllers/locals_controller.rb b/indicaAi/app/controllers/locals_controller.rb index 59153933..6aea45f4 100644 --- a/indicaAi/app/controllers/locals_controller.rb +++ b/indicaAi/app/controllers/locals_controller.rb @@ -21,7 +21,7 @@ def search_locals # GET /local/:id/rating def show_rating rating = Local.find_local_ratings(params[:id_local]) - locals = Local.find(params[:id_local]) - json_response(locals: locals, rating: rating) + local = Local.find(params[:id_local]) + json_response(locals: local, rating: rating) end end diff --git a/indicaAi/app/models/local.rb b/indicaAi/app/models/local.rb index 8b0deb57..b989b279 100644 --- a/indicaAi/app/models/local.rb +++ b/indicaAi/app/models/local.rb @@ -7,7 +7,7 @@ def self.find_by_name(params) Local.where('upper(name) like ?', "%#{params.upcase}%") end - def self.find_local_ratings(params) - Local.find(params).local_ratings + def self.find_local_ratings(local_id) + Local.find(local_id).local_ratings end end diff --git a/indicaAi/app/models/local_rating.rb b/indicaAi/app/models/local_rating.rb index 4255a949..fed253ef 100644 --- a/indicaAi/app/models/local_rating.rb +++ b/indicaAi/app/models/local_rating.rb @@ -3,6 +3,6 @@ class LocalRating < ApplicationRecord belongs_to :local validates :value, presence: true validates :value, numericality: true - validates_numericality_of :value, :less_than_or_equal_to => 5 - validates_numericality_of :value, :greater_than_or_equal_to => 0 + validates_numericality_of :value, less_than_or_equal_to: 5 + validates_numericality_of :value, greater_than_or_equal_to: 0 end From cbe2c1eb763b8d879109d59ca5bb35ccd5a5c1bb Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 22 Sep 2018 20:10:44 -0300 Subject: [PATCH 19/26] finished the tests the the sheet style for the controller --- .../spec/controllers/locals_controller_spec.rb | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/indicaAi/spec/controllers/locals_controller_spec.rb b/indicaAi/spec/controllers/locals_controller_spec.rb index 01157924..3e319df7 100644 --- a/indicaAi/spec/controllers/locals_controller_spec.rb +++ b/indicaAi/spec/controllers/locals_controller_spec.rb @@ -1,22 +1,16 @@ require 'rails_helper' RSpec.describe LocalsController, type: :controller do - let!(:local) { create(:local) } - it { should route(:get, '/locals').to(action: :index) } - let(:action) { LocalsController.new } - - it 'Test index - Should return all locals as JSON' do + it 'should return a JSON object' do get :index, format: :JSON expect(response).to be_success end - describe 'GET #search_locals' do - context 'Return a local by its name as a param' do - local = Local.new - local.name = 'Xoxo' - local.save - it { should route(:get, '/locals/name/x').to(action: :search_locals, name: 'x') } + context 'GET #search_locals' do + let!(:local) { create(:local, name: 'Xoxo') } + it 'should route locals/name/x to the corresponding action' do + should route(:get, '/locals/name/x').to(action: :search_locals, name: 'x') end end end From df341247f6a1d0642af1079c561f387326a484ae Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 22 Sep 2018 20:18:17 -0300 Subject: [PATCH 20/26] created new factories to do the tests and made it fits to the style sheet --- indicaAi/spec/factories/local.rb | 6 ++++-- indicaAi/spec/factories/local_rating.rb | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 indicaAi/spec/factories/local_rating.rb diff --git a/indicaAi/spec/factories/local.rb b/indicaAi/spec/factories/local.rb index a87924e0..fd0af9e4 100644 --- a/indicaAi/spec/factories/local.rb +++ b/indicaAi/spec/factories/local.rb @@ -1,6 +1,8 @@ - FactoryBot.define do factory :local do - name { Faker::DragonBall.character } + # generates locations from the tv serie 'Friends' :D + name { Faker::Friends.location } + # generates quotations from 'Friends' + description { Faker::Friends.quote } end end diff --git a/indicaAi/spec/factories/local_rating.rb b/indicaAi/spec/factories/local_rating.rb new file mode 100644 index 00000000..e4fef244 --- /dev/null +++ b/indicaAi/spec/factories/local_rating.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :local_rating do + value { Faker::Number.between(1, 5) } + local { create(:local) } + end +end From 72fcc057c5d84e10029002f7a691f42048f4013c Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 22 Sep 2018 20:30:01 -0300 Subject: [PATCH 21/26] Finished the tests on the models and made it fits to the style sheet --- indicaAi/spec/models/local_rating_spec.rb | 32 +++++++++++------------ indicaAi/spec/models/local_spec.rb | 21 ++++++++------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/indicaAi/spec/models/local_rating_spec.rb b/indicaAi/spec/models/local_rating_spec.rb index 574bf720..e8f1ddcf 100644 --- a/indicaAi/spec/models/local_rating_spec.rb +++ b/indicaAi/spec/models/local_rating_spec.rb @@ -1,24 +1,24 @@ require 'rails_helper' RSpec.describe LocalRating, type: :model do - it 'Testing if value is integer - Should return true' do - note1 = LocalRating.new - note1.value = 3 - assert note1.value.class == Integer - end - - it 'Testing typecast - Should return integer if input is float' do - note2 = LocalRating.new - note2.value = 3.4 - assert note2.value == 3 - end - it { should belong_to(:local) } + it { should validate_numericality_of(:value).is_greater_than_or_equal_to(0) } + context 'ratings validation tests' do + it 'should return a integer value' do + note = build(:local_rating) + assert note.value.class == Integer + end - it { should validate_presence_of(:value) } + it 'should return a integer though a float was given' do + note2 = LocalRating.new + note2.value = 3.4 + assert note2.value == 3 + end + it { should belong_to(:local) } - it { should validate_numericality_of(:value) } + it { should validate_presence_of(:value) } - it { should validate_numericality_of(:value).is_greater_than_or_equal_to(0) } + it { should validate_numericality_of(:value) } - it { should validate_numericality_of(:value).is_less_than_or_equal_to(5) } + it { should validate_numericality_of(:value).is_less_than_or_equal_to(5) } + end end diff --git a/indicaAi/spec/models/local_spec.rb b/indicaAi/spec/models/local_spec.rb index ee06897a..936f130f 100644 --- a/indicaAi/spec/models/local_spec.rb +++ b/indicaAi/spec/models/local_spec.rb @@ -4,17 +4,20 @@ it { should validate_presence_of(:name) } it 'Testing Search By Name - Should return similar names to params' do - params = 'FGA' - local = Local.new - local.name = 'FGA' - expect(Local.find_by_name(params)) == true + local = create(:local) + name = local.name + result = Local.find_by_name(name) + p result.first + p result + assert result.first, local end it 'Testing Find Local Ratings - Should return rating associated to parms' do - params = 1 - local = Local.new - local.name = 'FGA' - local.save - expect(Local.find_local_ratings(params)) == true + local = create(:local) + nota = create(:local_rating, local: local) + + id = local.id + result = Local.find_local_ratings(id) + assert result.first, nota end end From 81f2adab1152bcbffdf649ec8ce4d195896f5b66 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 22 Sep 2018 20:43:48 -0300 Subject: [PATCH 22/26] Finished the tests on the request spec and made it fits to the style sheet --- indicaAi/spec/requests/locals_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indicaAi/spec/requests/locals_spec.rb b/indicaAi/spec/requests/locals_spec.rb index aa679a52..c94c3e24 100644 --- a/indicaAi/spec/requests/locals_spec.rb +++ b/indicaAi/spec/requests/locals_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' RSpec.describe 'Local API', type: :request do - let!(:locals) { create_list(:local, 10) } + let!(:local) { create_list(:local, 10) } let(:local_id) { locals.first.id } describe 'GET /locals' do From 1c3d55e6dc8a4130d02335e98fa7be5746953b45 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 22 Sep 2018 21:19:12 -0300 Subject: [PATCH 23/26] #54 trying to fit the file to be in accord with codeclimate --- indicaAi/spec/models/local_rating_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indicaAi/spec/models/local_rating_spec.rb b/indicaAi/spec/models/local_rating_spec.rb index e8f1ddcf..0a9e0878 100644 --- a/indicaAi/spec/models/local_rating_spec.rb +++ b/indicaAi/spec/models/local_rating_spec.rb @@ -4,7 +4,7 @@ it { should validate_numericality_of(:value).is_greater_than_or_equal_to(0) } context 'ratings validation tests' do it 'should return a integer value' do - note = build(:local_rating) + note = create(:local_rating) assert note.value.class == Integer end From bfe58846eaf5ae4b520246995eff30f730e1f7d3 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 22 Sep 2018 21:49:16 -0300 Subject: [PATCH 24/26] Trying hard to make this be in accord with the codeclimate --- indicaAi/spec/models/local_rating_spec.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/indicaAi/spec/models/local_rating_spec.rb b/indicaAi/spec/models/local_rating_spec.rb index 0a9e0878..f6c24464 100644 --- a/indicaAi/spec/models/local_rating_spec.rb +++ b/indicaAi/spec/models/local_rating_spec.rb @@ -2,12 +2,16 @@ RSpec.describe LocalRating, type: :model do it { should validate_numericality_of(:value).is_greater_than_or_equal_to(0) } - context 'ratings validation tests' do - it 'should return a integer value' do - note = create(:local_rating) - assert note.value.class == Integer - end + describe 'testing validations' do + context 'ratings validation tests' do + let!(:note) { create(:local_rating) } + it 'should return a integer value' do + note.save + # note = create(:local_rating) + assert note.value.class == Integer + end + end it 'should return a integer though a float was given' do note2 = LocalRating.new note2.value = 3.4 From c37880881f6c15b42472fbafa2280af371840fd5 Mon Sep 17 00:00:00 2001 From: Lucas Maciel Date: Tue, 25 Sep 2018 23:08:21 -0300 Subject: [PATCH 25/26] #54 Adding a new validation on the local rating's model --- indicaAi/app/models/local_rating.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/indicaAi/app/models/local_rating.rb b/indicaAi/app/models/local_rating.rb index fed253ef..d276e6a7 100644 --- a/indicaAi/app/models/local_rating.rb +++ b/indicaAi/app/models/local_rating.rb @@ -1,6 +1,7 @@ # Description of Local Rating Class class LocalRating < ApplicationRecord belongs_to :local + validates_associated :local validates :value, presence: true validates :value, numericality: true validates_numericality_of :value, less_than_or_equal_to: 5 From 9469f22963e0d009cb1d0dd3f270626518bb46b7 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Tue, 25 Sep 2018 23:21:51 -0300 Subject: [PATCH 26/26] #54 changed some tests --- indicaAi/spec/factories/local_rating.rb | 2 +- indicaAi/spec/models/local_rating_spec.rb | 3 +-- indicaAi/spec/models/local_spec.rb | 2 -- indicaAi/spec/requests/locals_spec.rb | 7 ++++--- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/indicaAi/spec/factories/local_rating.rb b/indicaAi/spec/factories/local_rating.rb index e4fef244..bc808f54 100644 --- a/indicaAi/spec/factories/local_rating.rb +++ b/indicaAi/spec/factories/local_rating.rb @@ -1,6 +1,6 @@ FactoryBot.define do factory :local_rating do - value { Faker::Number.between(1, 5) } + value { Faker::Number.between(0, 5) } local { create(:local) } end end diff --git a/indicaAi/spec/models/local_rating_spec.rb b/indicaAi/spec/models/local_rating_spec.rb index f6c24464..1c67d1b9 100644 --- a/indicaAi/spec/models/local_rating_spec.rb +++ b/indicaAi/spec/models/local_rating_spec.rb @@ -7,8 +7,6 @@ context 'ratings validation tests' do let!(:note) { create(:local_rating) } it 'should return a integer value' do - note.save - # note = create(:local_rating) assert note.value.class == Integer end end @@ -17,6 +15,7 @@ note2.value = 3.4 assert note2.value == 3 end + it { should belong_to(:local) } it { should validate_presence_of(:value) } diff --git a/indicaAi/spec/models/local_spec.rb b/indicaAi/spec/models/local_spec.rb index 936f130f..452e9dc7 100644 --- a/indicaAi/spec/models/local_spec.rb +++ b/indicaAi/spec/models/local_spec.rb @@ -7,8 +7,6 @@ local = create(:local) name = local.name result = Local.find_by_name(name) - p result.first - p result assert result.first, local end diff --git a/indicaAi/spec/requests/locals_spec.rb b/indicaAi/spec/requests/locals_spec.rb index c94c3e24..761a6746 100644 --- a/indicaAi/spec/requests/locals_spec.rb +++ b/indicaAi/spec/requests/locals_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' RSpec.describe 'Local API', type: :request do - let!(:local) { create_list(:local, 10) } + let!(:locals) { create_list(:local, 10) } let(:local_id) { locals.first.id } describe 'GET /locals' do @@ -21,10 +21,11 @@ describe 'GET /locals/name/:name' do # make HTTP get request before each example - before { get '/locals/name/:name' } - + let!(:local) { create(:local, name: 'plaza') } + before { get '/locals/name/plaza' } it 'return local by name' do expect(json).not_to be_empty + expect(json[0][0]['name']).to eq('plaza') end end end