Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # frozen_string_literal: true
- require 'rails_helper'
- RSpec.describe Api::V1::StopsController, type: :controller do
- let_it_be(:account){ create(:account) }
- let_it_be(:user) { create(:user, account: account) }
- let_it_be(:service) { create(:service, name: :plan) }
- let_it_be(:action) { create(:action, name: :planner, service: service) }
- let_it_be(:user_action) { create(:user_action, action: action, user: user) }
- let_it_be(:app_module) { create(:app_module, name: :plan, service: service) }
- let_it_be(:app_module_user) { create(:app_module_user, app_module: app_module, user: user) }
- let_it_be(:stop_group, reload: true) { create(:stop_group, name: 'Stop Group 1', account: account) }
- let_it_be(:stop, reload: true) { create(:stop, stop_group: stop_group) }
- let_it_be(:place) { create(:place, account: account) }
- let_it_be(:place_two) { create(:place, account: account) }
- let(:expected_keys) { %i[identification address latitude longitude delivery_times service_time priority] }
- let(:omitted_keys) { {} }
- let(:expected_relationships) { %i[stop_group items tags place contact] }
- let(:stop_count) { 10 }
- let(:filters) { {} }
- let_it_be(:identification) { Faker::Number.number(digits: 10) }
- let_it_be(:address) { Faker::Address.street_name }
- let_it_be(:latitude) { rand(33.450288..33.452268) * -1 }
- let_it_be(:longitude) { rand(70.6323647..70.7236467) * -1}
- let_it_be(:priority) { rand(0..9) }
- let_it_be(:service_time) { rand(10..60) }
- let_it_be(:contact_identification) { "#{Faker::Invoice.unique.creditor_reference}_#{rand(1000)}" }
- let_it_be(:name) { Faker::Name.name }
- let_it_be(:email) { Faker::Internet.email }
- let_it_be(:phone) { Faker::PhoneNumber.phone_number }
- let_it_be(:item_name) { Faker::House.furniture }
- let_it_be(:item_identification) { Faker::Bank.account_number }
- let_it_be(:quantity) { Faker::Number.number(digits: 1) }
- let_it_be(:capacity_one) { Faker::Number.decimal(l_digits: 2, r_digits: 3) }
- let_it_be(:capacity_two) { Faker::Number.decimal(l_digits: 2, r_digits: 3) }
- let_it_be(:tag_type_one) { create(:tag_type, account: account) }
- let_it_be(:tag_type_two) { create(:tag_type, account: account) }
- let_it_be(:tag_one) { create(:tag, tag_type: tag_type_one) }
- let_it_be(:tag_two) { create(:tag, tag_type: tag_type_two) }
- let_it_be(:tag_three) { create(:tag, tag_type: tag_type_two) }
- let_it_be(:group_type_one) { create(:group_type, account: account) }
- let_it_be(:group_type_two) { create(:group_type, account: account) }
- let_it_be(:group_one) { create(:group, group_type: group_type_one, account: account) }
- let_it_be(:group_two) { create(:group, group_type: group_type_two, account: account) }
- let_it_be(:group_three) { create(:group, group_type: group_type_two, account: account) }
- let(:create_params) do
- {
- data: {
- attributes: {
- identification: identification,
- address: address,
- latitude: latitude,
- longitude: longitude,
- service_time: service_time,
- priority: priority,
- },
- relationships: {
- contact: {
- data: {
- type: 'contact',
- attributes: {
- identification: contact_identification,
- name: name,
- email: email,
- phone: phone
- }
- }
- },
- place: {
- data: {
- type: 'place',
- id: place.id
- }
- },
- delivery_times: {
- data:[
- {
- type: 'delivery_time',
- attributes: {
- min_delivery_time: DateTime.now.midday.in_time_zone(account.timezone),
- max_delivery_time: DateTime.now.end_of_day.in_time_zone(account.timezone)
- }
- }]
- },
- items: {
- data: [
- {
- type: 'item',
- attributes: {
- name: item_name,
- identification: item_identification,
- quantity: quantity,
- capacity_one: capacity_one,
- capacity_two: capacity_two
- }
- },
- {
- type: 'item',
- attributes: {
- name: item_name,
- identification: item_identification,
- quantity: quantity,
- capacity_one: capacity_one,
- capacity_two: capacity_two
- }
- }
- ]
- },
- entity_tags: {
- data: [
- { type: 'entity_tag', attributes: {tag_id: tag_one.id} },
- { type: 'entity_tag', attributes: {tag_id: tag_two.id} }
- ]
- },
- entity_groups: {
- data: [
- { type: 'entity_group', attributes: {group_id: group_one.id} },
- { type: 'entity_group', attributes: {group_id: group_two.id} }
- ]
- }
- }
- }
- }
- end
- before_all do
- 10.times do |i|
- stop = create(:stop, stop_group: stop_group)
- if i % 2 == 0
- tag = create(:tag)
- stop.tags << tag
- end
- end
- end
- let(:min_delivery_time_updated) { (Time.now + 1.month).midday.in_time_zone(account.timezone) }
- let(:max_delivery_time_updated) { (Time.now + 1.month).end_of_day.in_time_zone(account.timezone) }
- let(:new_contact_name) { 'Example Contact Two' }
- let(:update_params) do
- {
- data: {
- id: stop.id,
- attributes: {
- identification: Faker::Number.number(digits: 10),
- address: Faker::Address.street_name,
- latitude: rand(33.450288..33.452268) * -1,
- longitude: rand(33.450288..33.452268) * -1,
- service_time: rand(10..60),
- priority: rand(0..9)
- },
- relationships: {
- contact: {
- data: {
- id: stop.contact.id,
- type: 'contact',
- attributes: {
- identification: contact_identification,
- name: new_contact_name,
- email: email,
- phone: phone
- }
- }
- },
- place: {
- data: {
- type: 'place',
- id: place_two.id
- }
- },
- delivery_times: {
- data:[
- {
- id: stop.delivery_times.first.id,
- type: 'delivery_time',
- attributes: {
- min_delivery_time: min_delivery_time_updated,
- max_delivery_time: max_delivery_time_updated
- }
- }]
- },
- items: {
- data: [
- {
- type: 'item',
- attributes: {
- name: item_name,
- identification: item_identification,
- quantity: quantity,
- capacity_one: capacity_one,
- capacity_two: capacity_two
- }
- }
- ]
- },
- entity_tags: {
- data: [
- { type: 'entity_tag', attributes: {id: tag_one.id} },
- { type: 'entity_tag', attributes: {id: tag_three.id} }
- ]
- },
- entity_groups: {
- data: [
- { type: 'entity_group', attributes: {group_id: group_one.id} },
- { type: 'entity_group', attributes: {group_id: group_three.id} }
- ]
- }
- }
- }
- }
- end
- before :each do
- sign_in user
- end
- describe 'Creating a stop group stop' do
- it 'creates a stop successfully' do
- api_create :create, create_params.merge!(stop_group_id: stop_group.id)
- stop = Stop.find(api_response.data.id)
- expect(api_response.data.attributes.identification).to eq identification.to_s
- expect(api_response.data.attributes.address).to eq address
- expect(api_response.data.attributes.latitude).to eq latitude
- expect(api_response.data.attributes.longitude).to eq longitude
- expect(api_response.data.attributes.service_time).to eq service_time
- expect(api_response.data.attributes.priority).to eq priority
- expect(api_response.data.relationships.contact.data.id).to_not be_nil
- contact = Contact.find(api_response.data.relationships.contact.data.id)
- expect(contact.identification).to eq contact_identification
- expect(contact.name).to eq name
- expect(contact.email).to eq email
- expect(contact.phone).to eq phone
- expect(api_response.data.relationships.place.data.id).to_not be_nil
- place = Place.find(api_response.data.relationships.place.data.id)
- expect(place.name).to eq place.name
- expect(api_response.data.relationships.items.data).to_not be_empty
- item_one = Item.find(api_response.data.relationships.items.data.first.id)
- item_two = Item.find(api_response.data.relationships.items.data.last.id)
- expect(item_one.identification).to eq item_identification
- expect(item_two.identification).to eq item_identification
- expect(api_response.data.relationships.tags.data).to_not be_empty
- tag_one_object = Tag.find(api_response.data.relationships.tags.data.first.id)
- tag_two_object = Tag.find(api_response.data.relationships.tags.data.last.id)
- expect(stop.tags.where(value: tag_one_object.value)).to_not be_nil
- expect(stop.tags.where(value: tag_two_object.value)).to_not be_nil
- expect(api_response.data.relationships.groups.data).to_not be_empty
- group_one_object = Group.find(api_response.data.relationships.groups.data.first.id)
- group_two_object = Group.find(api_response.data.relationships.groups.data.last.id)
- expect(stop.tags.where(value: group_one_object.name)).to_not be_nil
- expect(stop.tags.where(value: group_two_object.name)).to_not be_nil
- end
- end
- describe 'updating a stop group stop' do
- it 'updates stop without delete tags if them are not sent' do
- stop.tags << tag_one
- create_params[:data][:id] = stop.id
- create_params[:data][:attributes][:address] = 'Fake address 123'
- create_params[:data][:relationships] = create_params[:data][:relationships].except(:entity_tags)
- api_update :update, { stop_group_id: stop_group.id, id: stop.id }, create_params
- stop = Stop.find(api_response.data.id)
- expect(api_response.data.attributes.address).to eq 'Fake address 123'
- expect(stop.tags.where(value: tag_one.value)).to_not be_nil
- end
- it 'update entity tags' do
- val = Stop.find(update_params[:data][:id])
- update_params[:data][:relationships][:entity_tags] = {
- data: [
- { type: 'entity_tag', attributes: { id: val.entity_tags.first.id, _destroy: true } }
- ]
- }
- api_update :update, { stop_group_id: val.stop_group.id, id: val.id }, update_params
- stop = Stop.find(api_response.data.id)
- expect(stop.tags.where(value: tag_two.value)).to_not be_nil
- end
- it 'update entity groups' do
- stop.groups << group_one
- create_params[:data][:id] = stop.id
- create_params[:data][:relationships][:entity_groups] = {
- data: [
- { type: 'entity_group', attributes: {group_id: group_two.id} }
- ]
- }
- api_update :update, { stop_group_id: stop_group.id, id: stop.id }, create_params
- stop = Stop.find(api_response.data.id)
- expect(stop.groups.where(name: group_two.name)).to_not be_nil
- end
- it 'updates a stop succesfully' do
- api_update :update, { stop_group_id: stop_group.id, id: stop.id }, update_params
- stop = Stop.find(api_response.data.id)
- expect(data_response.attributes.address).to eq stop.reload.address
- expect(data_response.attributes.identification).to eq stop.identification
- expect(stop.contact.name).to eq new_contact_name
- expect(data_response.relationships.place.data.id.to_i).to eq place_two.id
- delivery_time = update_params[:data][:relationships][:delivery_times].first.second.first
- expect(stop.delivery_times.find(delivery_time[:id]).min_delivery_time.to_date).to eq min_delivery_time_updated.to_date
- expect(stop.delivery_times.find(delivery_time[:id]).max_delivery_time.to_date).to eq max_delivery_time_updated.to_date
- expect(data_response.relationships.items.data.count).to eq 1
- expect(data_response.relationships.tags.data.map{|k| k.id.to_i}.sort).to eq [tag_one.id, tag_three.id]
- expect(data_response.relationships.tags.data.map{|k| k.id.to_i}.sort).to eq [tag_one.id, tag_three.id]
- end
- end
- describe 'deleting a stop group stop' do
- it 'delete successfully' do
- stop_group_size = stop_group.stops.size
- api_destroy :destroy, { stop_group_id: stop_group.id, id: stop.id }, { stop_group_id: stop_group.id, id: stop.id }
- expect(api_response.data.id).to eq stop.id.to_s
- expect(stop_group.reload.stops_count).to eq(stop_group_size - 1)
- end
- end
- describe 'show a stop group stop' do
- it 'show successfully' do
- api_get :show, {stop_group_id: stop_group.id, id: stop.id}
- expect(api_response.data.id).to eq stop.id.to_s
- expect(api_response.data.relationships.place).to_not be_nil
- expect(api_response.data.relationships.items).to_not be_nil
- expect(api_response.data.relationships.contact).to_not be_nil
- expect(api_response.data.relationships.tags).to_not be_nil
- end
- end
- describe 'Listing stop group stops' do
- it 'list all stops in the stop group paginated by the default setting' do
- api_get :index, {stop_group_id: stop_group.id}
- compare_json_response(Stop, expected_keys, omitted_keys, filters)
- end
- it 'list all Stops in page 2 and per page 1' do
- params = { page: 2, per_page: 1, stop_group_id: stop_group.id }
- get :index, params: params
- compare_json_response(Stop, expected_keys, omitted_keys, filters, params)
- end
- it 'list all Stops in page 5 per page 1' do
- params = { page: 5, per_page: 1, stop_group_id: stop_group.id }
- get :index, params: params
- compare_json_response(Stop, expected_keys, omitted_keys, filters, params)
- end
- it 'list all Stops in page 2 per page 5' do
- params = { page: 2, per_page: 5, stop_group_id: stop_group.id }
- get :index, params: params
- compare_json_response(Stop, expected_keys, omitted_keys, filters, params)
- end
- describe 'sortering' do
- %w[longitude latitude identification address].each do |attribute|
- it "by #{attribute} desc" do
- params = {
- sort: "-#{attribute}",
- stop_group_id: stop_group.id
- }
- get :index, params: params
- stops = Stop.where(stop_group_id: stop_group.id).order("#{attribute} DESC")
- expect(api_response.data.first.attributes.send(attribute)).to eq stops.where(id: api_response.data.first.id.to_i).take.send(attribute)
- end
- it "by #{attribute} asc" do
- params = {
- sort: attribute,
- stop_group_id: stop_group.id
- }
- get :index, params: params
- stops = Stop.where(stop_group_id: stop_group.id).order("#{attribute} ASC")
- expect(api_response.data.first.attributes.send(attribute)).to eq stops.where(id: api_response.data.first.id.to_i).take.send(attribute)
- end
- end
- end
- describe 'filtering' do
- it 'by tag' do
- params = {
- filter: {
- tag: [
- stop_group.stops.first.tags.first.value
- ]
- },
- stop_group_id: stop_group.id
- }
- get :index, params: params
- expect(data_response.size).to eq 1
- end
- it 'by a tag that does not exist' do
- params = {
- filter: {
- tag: ['Radio']
- },
- stop_group_id: stop_group.id
- }
- get :index, params: params
- expect(data_response).to be_empty
- end
- it 'by group name' do
- stop.groups << group_one
- params = {
- filter: {
- group: [
- group_one.name
- ]
- },
- stop_group_id: stop_group.id
- }
- get :index, params: params
- expect(data_response.size).to eq 1
- end
- it 'by a group that does not exist' do
- params = {
- filter: {
- group: ['Radio Group']
- },
- stop_group_id: stop_group.id
- }
- get :index, params: params
- expect(data_response).to be_empty
- end
- end
- end
- end
- def permitted_attributes
- [
- :identification,
- :address,
- :latitude,
- :longitude,
- :service_time,
- :priority,
- :place_id,
- entity_tags_attributes: %i[id _destroy],
- entity_groups_attributes: %i[group_id],
- place_attributes: %i[id],
- items_attributes: %i[name identification quantity capacity_one capacity_two],
- contact_attributes: %i[id identification name email phone],
- delivery_times_attributes: %i[id min_delivery_time max_delivery_time]
- ]
- end
- accepts_nested_attributes_for :contact, :items, :delivery_times, :entity_groups
- accepts_nested_attributes_for :entity_tags, allow_destroy: true
Advertisement
Add Comment
Please, Sign In to add comment