Fix for API float parsing in the inventory number type column [SCI-5604]

This commit is contained in:
Oleksii Kriuchykhin 2021-03-29 15:59:24 +02:00
parent 6ad3f7b92f
commit 2b2b6f7774
3 changed files with 133 additions and 4 deletions

View file

@ -19,11 +19,11 @@ class RepositoryNumberValue < ApplicationRecord
end
def data_changed?(new_data)
BigDecimal(new_data) != data
BigDecimal(new_data.to_s) != data
end
def update_data!(new_data, user)
self.data = BigDecimal(new_data)
self.data = BigDecimal(new_data.to_s)
self.last_modified_by = user
save!
end
@ -40,12 +40,12 @@ class RepositoryNumberValue < ApplicationRecord
def self.new_with_payload(payload, attributes)
value = new(attributes)
value.data = BigDecimal(payload)
value.data = BigDecimal(payload.to_s)
value
end
def self.import_from_text(text, attributes, _options = {})
new(attributes.merge(data: BigDecimal(text)))
new(attributes.merge(data: BigDecimal(text.to_s)))
rescue ArgumentError
nil
end

View file

@ -0,0 +1,15 @@
# frozen_string_literal: true
FactoryBot.define do
factory :repository_number_value do
created_by { create :user }
last_modified_by { created_by }
data { Faker::Number.decimal(l_digits: 4, r_digits: 4) }
after(:build) do |repository_number_value|
repository_number_value.repository_cell ||= build(:repository_cell,
:number_value,
repository_number_value: repository_number_value)
repository_number_value.repository_cell.value = repository_number_value
end
end
end

View file

@ -41,6 +41,7 @@ RSpec.describe 'Api::V1::InventoryCellsController', type: :request do
@time_range_column = create(:repository_column, repository: @valid_inventory, data_type: :RepositoryTimeRangeValue)
@date_time_range_column = create(:repository_column,
repository: @valid_inventory, data_type: :RepositoryDateTimeRangeValue)
@number_column = create(:repository_column, repository: @valid_inventory, data_type: :RepositoryNumberValue)
@valid_item = create(:repository_row, repository: @valid_inventory)
@ -76,6 +77,9 @@ RSpec.describe 'Api::V1::InventoryCellsController', type: :request do
start_time: DateTime.now,
end_time: DateTime.now + 1.day,
repository_cell_attributes: { repository_row: @valid_item, repository_column: @date_time_range_column })
create(:repository_number_value,
data: 1234.5678,
repository_cell_attributes: { repository_row: @valid_item, repository_column: @number_column })
@valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id),
@ -192,6 +196,24 @@ RSpec.describe 'Api::V1::InventoryCellsController', type: :request do
}
}
}
@valid_number_body = {
data: {
type: 'inventory_cells',
attributes: {
column_id: @number_column.id,
value: 1234.5678
}
}
}
@valid_number_as_text_body = {
data: {
type: 'inventory_cells',
attributes: {
column_id: @number_column.id,
value: '1234.5678'
}
}
}
@update_text_body = {
data: {
id: @valid_item.repository_cells.where(repository_column: @text_column).first.id,
@ -314,6 +336,26 @@ RSpec.describe 'Api::V1::InventoryCellsController', type: :request do
}
}
}
@update_number_body = {
data: {
id: @valid_item.repository_cells.where(repository_column: @number_column).first.id,
type: 'inventory_cells',
attributes: {
column_id: @number_column.id,
value: 8765.4321
}
}
}
@update_number_as_text_body = {
data: {
id: @valid_item.repository_cells.where(repository_column: @number_column).first.id,
type: 'inventory_cells',
attributes: {
column_id: @number_column.id,
value: '8765.4321'
}
}
}
end
describe 'GET inventory_cells, #index' do
@ -603,6 +645,42 @@ RSpec.describe 'Api::V1::InventoryCellsController', type: :request do
)
end
it 'Response with correct inventory cell, number cell' do
hash_body = nil
empty_item = create(:repository_row, repository: @valid_inventory)
post api_v1_team_inventory_item_cells_path(
team_id: @team.id,
inventory_id: @valid_inventory.id,
item_id: empty_item.id
), params: @valid_number_body.to_json, headers: @valid_headers
expect(response).to have_http_status 201
expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match(
ActiveModelSerializers::SerializableResource
.new(RepositoryCell.last,
serializer: Api::V1::InventoryCellSerializer)
.as_json[:data]
)
end
it 'Response with correct inventory cell, text number cell' do
hash_body = nil
empty_item = create(:repository_row, repository: @valid_inventory)
post api_v1_team_inventory_item_cells_path(
team_id: @team.id,
inventory_id: @valid_inventory.id,
item_id: empty_item.id
), params: @valid_number_as_text_body.to_json, headers: @valid_headers
expect(response).to have_http_status 201
expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match(
ActiveModelSerializers::SerializableResource
.new(RepositoryCell.last,
serializer: Api::V1::InventoryCellSerializer)
.as_json[:data]
)
end
it 'When invalid request, payload mismatches column type' do
hash_body = nil
invalid_file_body = @valid_file_body.dup
@ -855,6 +933,42 @@ RSpec.describe 'Api::V1::InventoryCellsController', type: :request do
)
end
it 'Response with correct inventory cell, number cell' do
hash_body = nil
patch api_v1_team_inventory_item_cell_path(
team_id: @team.id,
inventory_id: @valid_inventory.id,
item_id: @valid_item.id,
id: @valid_item.repository_cells.where(repository_column: @number_column).first.id
), params: @update_number_body.to_json, headers: @valid_headers
expect(response).to have_http_status 200
expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match(
ActiveModelSerializers::SerializableResource
.new(@valid_item.repository_cells.where(repository_column: @number_column).first,
serializer: Api::V1::InventoryCellSerializer)
.as_json[:data]
)
end
it 'Response with correct inventory cell, text number cell' do
hash_body = nil
patch api_v1_team_inventory_item_cell_path(
team_id: @team.id,
inventory_id: @valid_inventory.id,
item_id: @valid_item.id,
id: @valid_item.repository_cells.where(repository_column: @number_column).first.id
), params: @update_number_as_text_body.to_json, headers: @valid_headers
expect(response).to have_http_status 200
expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match(
ActiveModelSerializers::SerializableResource
.new(@valid_item.repository_cells.where(repository_column: @number_column).first,
serializer: Api::V1::InventoryCellSerializer)
.as_json[:data]
)
end
it 'When invalid request, payload mismatches column type' do
hash_body = nil
invalid_file_body = @valid_file_body.dup