NameL3ss

Untitled

Mar 30th, 2021
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 17.43 KB | None | 0 0
  1. # frozen_string_literal: true
  2.  
  3. require 'rails_helper'
  4.  
  5. RSpec.describe Api::V1::StopsController, type: :controller do
  6.   let_it_be(:account){ create(:account) }
  7.   let_it_be(:user) { create(:user, account: account) }
  8.   let_it_be(:service) { create(:service, name: :plan) }
  9.   let_it_be(:action) { create(:action, name: :planner, service: service) }
  10.   let_it_be(:user_action) { create(:user_action, action: action, user: user) }
  11.   let_it_be(:app_module) { create(:app_module, name: :plan, service: service) }
  12.   let_it_be(:app_module_user) { create(:app_module_user, app_module: app_module, user: user) }
  13.   let_it_be(:stop_group, reload: true) { create(:stop_group, name: 'Stop Group 1', account: account) }
  14.   let_it_be(:stop, reload: true) { create(:stop, stop_group: stop_group) }
  15.   let_it_be(:place) { create(:place, account: account) }
  16.   let_it_be(:place_two) { create(:place, account: account) }
  17.   let(:expected_keys) { %i[identification address latitude longitude delivery_times service_time priority] }
  18.   let(:omitted_keys) { {} }
  19.   let(:expected_relationships) { %i[stop_group items tags place contact] }
  20.   let(:stop_count) { 10 }
  21.   let(:filters) { {} }
  22.  
  23.   let_it_be(:identification) { Faker::Number.number(digits: 10) }
  24.   let_it_be(:address) { Faker::Address.street_name }
  25.   let_it_be(:latitude) { rand(33.450288..33.452268) * -1 }
  26.   let_it_be(:longitude) { rand(70.6323647..70.7236467) * -1}
  27.   let_it_be(:priority) { rand(0..9) }
  28.   let_it_be(:service_time) { rand(10..60) }
  29.  
  30.   let_it_be(:contact_identification) { "#{Faker::Invoice.unique.creditor_reference}_#{rand(1000)}" }
  31.   let_it_be(:name) { Faker::Name.name }
  32.   let_it_be(:email) { Faker::Internet.email }
  33.   let_it_be(:phone) { Faker::PhoneNumber.phone_number }
  34.  
  35.   let_it_be(:item_name) { Faker::House.furniture }
  36.   let_it_be(:item_identification) { Faker::Bank.account_number }
  37.   let_it_be(:quantity) { Faker::Number.number(digits: 1) }
  38.   let_it_be(:capacity_one) { Faker::Number.decimal(l_digits: 2, r_digits: 3) }
  39.   let_it_be(:capacity_two) { Faker::Number.decimal(l_digits: 2, r_digits: 3) }
  40.  
  41.   let_it_be(:tag_type_one) { create(:tag_type, account: account) }
  42.   let_it_be(:tag_type_two) { create(:tag_type, account: account) }
  43.   let_it_be(:tag_one) { create(:tag, tag_type: tag_type_one) }
  44.   let_it_be(:tag_two) { create(:tag, tag_type: tag_type_two) }
  45.   let_it_be(:tag_three) { create(:tag, tag_type: tag_type_two) }
  46.  
  47.   let_it_be(:group_type_one) { create(:group_type, account: account) }
  48.   let_it_be(:group_type_two) { create(:group_type, account: account) }
  49.   let_it_be(:group_one) { create(:group, group_type: group_type_one, account: account) }
  50.   let_it_be(:group_two) { create(:group, group_type: group_type_two, account: account) }
  51.   let_it_be(:group_three) { create(:group, group_type: group_type_two, account: account) }
  52.  
  53.   let(:create_params) do
  54.     {
  55.       data: {
  56.         attributes: {
  57.           identification: identification,
  58.           address: address,
  59.           latitude: latitude,
  60.           longitude: longitude,
  61.           service_time: service_time,
  62.           priority: priority,
  63.         },
  64.         relationships: {
  65.           contact: {
  66.             data: {
  67.               type: 'contact',
  68.               attributes: {
  69.                 identification: contact_identification,
  70.                 name: name,
  71.                 email: email,
  72.                 phone: phone
  73.               }
  74.             }
  75.           },
  76.           place: {
  77.             data: {
  78.               type: 'place',
  79.               id: place.id
  80.             }
  81.           },
  82.           delivery_times: {
  83.             data:[
  84.             {
  85.               type: 'delivery_time',
  86.               attributes: {
  87.                 min_delivery_time: DateTime.now.midday.in_time_zone(account.timezone),
  88.                 max_delivery_time: DateTime.now.end_of_day.in_time_zone(account.timezone)
  89.               }
  90.             }]
  91.           },
  92.           items: {
  93.             data: [
  94.               {
  95.                 type: 'item',
  96.                 attributes: {
  97.                   name: item_name,
  98.                   identification: item_identification,
  99.                   quantity: quantity,
  100.                   capacity_one: capacity_one,
  101.                   capacity_two: capacity_two
  102.                 }
  103.               },
  104.               {
  105.                 type: 'item',
  106.                 attributes: {
  107.                   name: item_name,
  108.                   identification: item_identification,
  109.                   quantity: quantity,
  110.                   capacity_one: capacity_one,
  111.                   capacity_two: capacity_two
  112.                 }
  113.               }
  114.             ]
  115.           },
  116.           entity_tags: {
  117.             data: [
  118.               { type: 'entity_tag', attributes: {tag_id: tag_one.id} },
  119.               { type: 'entity_tag', attributes: {tag_id: tag_two.id} }
  120.             ]
  121.           },
  122.           entity_groups: {
  123.             data: [
  124.               { type: 'entity_group', attributes: {group_id: group_one.id} },
  125.               { type: 'entity_group', attributes: {group_id: group_two.id} }
  126.             ]
  127.           }
  128.         }
  129.       }
  130.   }
  131.   end
  132.  
  133.   before_all do
  134.     10.times do |i|
  135.       stop = create(:stop, stop_group: stop_group)
  136.       if i % 2 == 0
  137.         tag = create(:tag)
  138.         stop.tags << tag
  139.       end
  140.     end
  141.   end
  142.  
  143.   let(:min_delivery_time_updated) { (Time.now + 1.month).midday.in_time_zone(account.timezone) }
  144.   let(:max_delivery_time_updated) { (Time.now + 1.month).end_of_day.in_time_zone(account.timezone) }
  145.   let(:new_contact_name) { 'Example Contact Two' }
  146.   let(:update_params) do
  147.     {
  148.       data: {
  149.         id: stop.id,
  150.         attributes: {
  151.           identification: Faker::Number.number(digits: 10),
  152.           address: Faker::Address.street_name,
  153.           latitude: rand(33.450288..33.452268) * -1,
  154.           longitude: rand(33.450288..33.452268) * -1,
  155.           service_time: rand(10..60),
  156.           priority: rand(0..9)
  157.         },
  158.         relationships: {
  159.           contact: {
  160.             data: {
  161.               id: stop.contact.id,
  162.               type: 'contact',
  163.               attributes: {
  164.                 identification: contact_identification,
  165.                 name: new_contact_name,
  166.                 email: email,
  167.                 phone: phone
  168.               }
  169.             }
  170.           },
  171.           place: {
  172.             data: {
  173.               type: 'place',
  174.               id: place_two.id
  175.             }
  176.           },
  177.           delivery_times: {
  178.             data:[
  179.             {
  180.               id: stop.delivery_times.first.id,
  181.               type: 'delivery_time',
  182.               attributes: {
  183.                 min_delivery_time: min_delivery_time_updated,
  184.                 max_delivery_time: max_delivery_time_updated
  185.               }
  186.             }]
  187.           },
  188.           items: {
  189.             data: [
  190.               {
  191.                 type: 'item',
  192.                 attributes: {
  193.                   name: item_name,
  194.                   identification: item_identification,
  195.                   quantity: quantity,
  196.                   capacity_one: capacity_one,
  197.                   capacity_two: capacity_two
  198.                 }
  199.               }
  200.             ]
  201.           },
  202.           entity_tags: {
  203.             data: [
  204.               { type: 'entity_tag', attributes: {id: tag_one.id} },
  205.               { type: 'entity_tag', attributes: {id: tag_three.id} }
  206.             ]
  207.           },
  208.           entity_groups: {
  209.             data: [
  210.               { type: 'entity_group', attributes: {group_id: group_one.id} },
  211.               { type: 'entity_group', attributes: {group_id: group_three.id} }
  212.             ]
  213.           }
  214.         }
  215.       }
  216.   }
  217.   end
  218.  
  219.   before :each do
  220.     sign_in user
  221.   end
  222.  
  223.   describe 'Creating a stop group stop' do
  224.     it 'creates a stop successfully' do
  225.       api_create :create, create_params.merge!(stop_group_id: stop_group.id)
  226.  
  227.       stop = Stop.find(api_response.data.id)
  228.       expect(api_response.data.attributes.identification).to eq identification.to_s
  229.       expect(api_response.data.attributes.address).to eq address
  230.       expect(api_response.data.attributes.latitude).to eq latitude
  231.       expect(api_response.data.attributes.longitude).to eq longitude
  232.       expect(api_response.data.attributes.service_time).to eq service_time
  233.       expect(api_response.data.attributes.priority).to eq priority
  234.  
  235.       expect(api_response.data.relationships.contact.data.id).to_not be_nil
  236.       contact = Contact.find(api_response.data.relationships.contact.data.id)
  237.       expect(contact.identification).to eq contact_identification
  238.       expect(contact.name).to eq name
  239.       expect(contact.email).to eq email
  240.       expect(contact.phone).to eq phone
  241.  
  242.       expect(api_response.data.relationships.place.data.id).to_not be_nil
  243.       place = Place.find(api_response.data.relationships.place.data.id)
  244.       expect(place.name).to eq place.name
  245.  
  246.       expect(api_response.data.relationships.items.data).to_not be_empty
  247.       item_one = Item.find(api_response.data.relationships.items.data.first.id)
  248.       item_two = Item.find(api_response.data.relationships.items.data.last.id)
  249.       expect(item_one.identification).to eq item_identification
  250.       expect(item_two.identification).to eq item_identification
  251.  
  252.       expect(api_response.data.relationships.tags.data).to_not be_empty
  253.       tag_one_object = Tag.find(api_response.data.relationships.tags.data.first.id)
  254.       tag_two_object = Tag.find(api_response.data.relationships.tags.data.last.id)
  255.  
  256.       expect(stop.tags.where(value: tag_one_object.value)).to_not be_nil
  257.       expect(stop.tags.where(value: tag_two_object.value)).to_not be_nil
  258.  
  259.       expect(api_response.data.relationships.groups.data).to_not be_empty
  260.       group_one_object = Group.find(api_response.data.relationships.groups.data.first.id)
  261.       group_two_object = Group.find(api_response.data.relationships.groups.data.last.id)
  262.  
  263.       expect(stop.tags.where(value: group_one_object.name)).to_not be_nil
  264.       expect(stop.tags.where(value: group_two_object.name)).to_not be_nil
  265.     end
  266.   end
  267.  
  268.   describe 'updating a stop group stop' do
  269.     it 'updates stop without delete tags if them are not sent' do
  270.       stop.tags << tag_one
  271.       create_params[:data][:id] = stop.id
  272.       create_params[:data][:attributes][:address] = 'Fake address 123'
  273.       create_params[:data][:relationships] = create_params[:data][:relationships].except(:entity_tags)
  274.  
  275.       api_update :update, { stop_group_id: stop_group.id, id: stop.id }, create_params
  276.  
  277.       stop = Stop.find(api_response.data.id)
  278.       expect(api_response.data.attributes.address).to eq 'Fake address 123'
  279.       expect(stop.tags.where(value: tag_one.value)).to_not be_nil
  280.     end
  281.  
  282.     it 'update entity tags' do
  283.       val = Stop.find(update_params[:data][:id])
  284.       update_params[:data][:relationships][:entity_tags] = {
  285.         data: [
  286.           { type: 'entity_tag', attributes: { id: val.entity_tags.first.id, _destroy: true } }
  287.         ]
  288.       }
  289.       api_update :update, { stop_group_id: val.stop_group.id, id: val.id }, update_params
  290.  
  291.       stop = Stop.find(api_response.data.id)
  292.       expect(stop.tags.where(value: tag_two.value)).to_not be_nil
  293.     end
  294.  
  295.     it 'update entity groups' do
  296.       stop.groups << group_one
  297.       create_params[:data][:id] = stop.id
  298.       create_params[:data][:relationships][:entity_groups] = {
  299.         data: [
  300.           { type: 'entity_group', attributes: {group_id: group_two.id} }
  301.         ]
  302.       }
  303.       api_update :update, { stop_group_id: stop_group.id, id: stop.id }, create_params
  304.  
  305.       stop = Stop.find(api_response.data.id)
  306.       expect(stop.groups.where(name: group_two.name)).to_not be_nil
  307.     end
  308.  
  309.     it 'updates a stop succesfully' do
  310.       api_update :update, { stop_group_id: stop_group.id, id: stop.id }, update_params
  311.  
  312.       stop = Stop.find(api_response.data.id)
  313.       expect(data_response.attributes.address).to eq stop.reload.address
  314.       expect(data_response.attributes.identification).to eq stop.identification
  315.  
  316.       expect(stop.contact.name).to eq new_contact_name
  317.       expect(data_response.relationships.place.data.id.to_i).to eq place_two.id
  318.       delivery_time = update_params[:data][:relationships][:delivery_times].first.second.first
  319.       expect(stop.delivery_times.find(delivery_time[:id]).min_delivery_time.to_date).to eq min_delivery_time_updated.to_date
  320.       expect(stop.delivery_times.find(delivery_time[:id]).max_delivery_time.to_date).to eq max_delivery_time_updated.to_date
  321.       expect(data_response.relationships.items.data.count).to eq 1
  322.  
  323.       expect(data_response.relationships.tags.data.map{|k| k.id.to_i}.sort).to eq [tag_one.id, tag_three.id]
  324.       expect(data_response.relationships.tags.data.map{|k| k.id.to_i}.sort).to eq [tag_one.id, tag_three.id]
  325.     end
  326.   end
  327.  
  328.   describe 'deleting a stop group stop' do
  329.     it 'delete successfully' do
  330.       stop_group_size = stop_group.stops.size
  331.       api_destroy :destroy, { stop_group_id: stop_group.id, id: stop.id }, { stop_group_id: stop_group.id, id: stop.id }
  332.       expect(api_response.data.id).to eq stop.id.to_s
  333.       expect(stop_group.reload.stops_count).to eq(stop_group_size - 1)
  334.     end
  335.   end
  336.  
  337.   describe 'show a stop group stop' do
  338.     it 'show successfully' do
  339.       api_get :show, {stop_group_id: stop_group.id, id: stop.id}
  340.       expect(api_response.data.id).to eq stop.id.to_s
  341.       expect(api_response.data.relationships.place).to_not be_nil
  342.       expect(api_response.data.relationships.items).to_not be_nil
  343.       expect(api_response.data.relationships.contact).to_not be_nil
  344.       expect(api_response.data.relationships.tags).to_not be_nil
  345.     end
  346.   end
  347.  
  348.   describe 'Listing stop group stops' do
  349.     it 'list all stops in the stop group paginated by the default setting' do
  350.       api_get :index, {stop_group_id: stop_group.id}
  351.  
  352.       compare_json_response(Stop, expected_keys, omitted_keys, filters)
  353.     end
  354.  
  355.     it 'list all Stops in page 2 and per page 1' do
  356.       params = { page: 2, per_page: 1, stop_group_id: stop_group.id }
  357.       get :index, params: params
  358.  
  359.       compare_json_response(Stop, expected_keys, omitted_keys, filters, params)
  360.     end
  361.  
  362.     it 'list all Stops in page 5 per page 1' do
  363.       params = { page: 5, per_page: 1, stop_group_id: stop_group.id }
  364.       get :index, params: params
  365.  
  366.       compare_json_response(Stop, expected_keys, omitted_keys, filters, params)
  367.     end
  368.  
  369.     it 'list all Stops in page 2 per page 5' do
  370.       params = { page: 2, per_page: 5, stop_group_id: stop_group.id }
  371.       get :index, params: params
  372.  
  373.       compare_json_response(Stop, expected_keys, omitted_keys, filters, params)
  374.     end
  375.  
  376.     describe 'sortering' do
  377.       %w[longitude latitude identification address].each do |attribute|
  378.         it "by #{attribute} desc" do
  379.           params = {
  380.             sort: "-#{attribute}",
  381.             stop_group_id: stop_group.id
  382.           }
  383.  
  384.           get :index, params: params
  385.  
  386.           stops = Stop.where(stop_group_id: stop_group.id).order("#{attribute} DESC")
  387.           expect(api_response.data.first.attributes.send(attribute)).to eq stops.where(id: api_response.data.first.id.to_i).take.send(attribute)
  388.         end
  389.  
  390.         it "by #{attribute} asc" do
  391.           params = {
  392.             sort: attribute,
  393.             stop_group_id: stop_group.id
  394.           }
  395.  
  396.           get :index, params: params
  397.  
  398.           stops = Stop.where(stop_group_id: stop_group.id).order("#{attribute} ASC")
  399.           expect(api_response.data.first.attributes.send(attribute)).to eq stops.where(id: api_response.data.first.id.to_i).take.send(attribute)
  400.         end
  401.       end
  402.     end
  403.  
  404.     describe 'filtering' do
  405.       it 'by tag' do
  406.         params = {
  407.           filter: {
  408.             tag: [
  409.               stop_group.stops.first.tags.first.value
  410.             ]
  411.           },
  412.           stop_group_id: stop_group.id
  413.         }
  414.  
  415.         get :index, params: params
  416.  
  417.         expect(data_response.size).to eq 1
  418.       end
  419.  
  420.       it 'by a tag that does not exist' do
  421.         params = {
  422.           filter: {
  423.             tag: ['Radio']
  424.           },
  425.           stop_group_id: stop_group.id
  426.         }
  427.  
  428.         get :index, params: params
  429.  
  430.         expect(data_response).to be_empty
  431.       end
  432.  
  433.       it 'by group name' do
  434.         stop.groups << group_one
  435.  
  436.         params = {
  437.           filter: {
  438.             group: [
  439.               group_one.name
  440.             ]
  441.           },
  442.           stop_group_id: stop_group.id
  443.         }
  444.  
  445.         get :index, params: params
  446.  
  447.         expect(data_response.size).to eq 1
  448.       end
  449.  
  450.       it 'by a group that does not exist' do
  451.         params = {
  452.           filter: {
  453.             group: ['Radio Group']
  454.           },
  455.           stop_group_id: stop_group.id
  456.         }
  457.  
  458.         get :index, params: params
  459.  
  460.         expect(data_response).to be_empty
  461.       end
  462.     end
  463.   end
  464. end
  465.  
  466.  
  467.  def permitted_attributes
  468.         [
  469.           :identification,
  470.           :address,
  471.           :latitude,
  472.           :longitude,
  473.           :service_time,
  474.           :priority,
  475.           :place_id,
  476.           entity_tags_attributes: %i[id _destroy],
  477.           entity_groups_attributes: %i[group_id],
  478.           place_attributes: %i[id],
  479.           items_attributes: %i[name identification quantity capacity_one capacity_two],
  480.           contact_attributes: %i[id identification name email phone],
  481.           delivery_times_attributes: %i[id min_delivery_time max_delivery_time]
  482.         ]
  483.       end
  484.  
  485. accepts_nested_attributes_for :contact, :items, :delivery_times, :entity_groups
  486. accepts_nested_attributes_for :entity_tags, allow_destroy: true
Advertisement
Add Comment
Please, Sign In to add comment