Sha256: 785c522d7469c179716ebbf663865ca88277685012036b0e1620c9469d9ab073

Contents?: true

Size: 1.96 KB

Versions: 10

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

require 'spec_helper'
require 'fortnox/api/mappers/base/from_json'
require 'fortnox/api/mappers/contexts/json_conversion'

describe Fortnox::API::Mapper::FromJSON do
  include_context 'with JSON conversion'

  before do
    module Test
      class BaseMapper
        include Fortnox::API::Mapper::FromJSON
      end
    end

    register_mapper(:categories, Test::CategoryMapper)
    register_mapper(:designer, Test::ProductDesignerMapper)
  end

  let(:mapper) { Test::ProductMapper.new }

  describe 'wrapped_json_hash_to_entity_hash' do
    let(:entity_hash) { mapper.wrapped_json_hash_to_entity_hash(wrapped_json_hash) }
    let(:wrapped_json_hash) do
      {
        'Product' => {
          '@url': 'someurl@example.com',
          'Name': 'Ford Mustang',
          'VAT': 30_000,
          'Categories': [{ 'Name' => 'Cars', 'ID' => '1' }, { 'Name' => 'Fast cars', 'ID' => '2' }],
          'Designer': { 'Name' => 'John Najjar', 'ID' => '23' }
        }
      }
    end

    specify 'converts keys without mapping correctly' do
      expect(entity_hash[:name]).to eq 'Ford Mustang'
    end

    specify 'converts keys with mapping correctly' do
      expect(entity_hash[:vat]).to eq 30_000
    end

    specify 'converts keys starting with "@" correctly'
    # do
    # expect( entity_hash[:url] ).to eq 'someurl@example.com'
    # end

    context 'with nested models' do
      let(:expected_nested_model_hash) do
        [{ name: 'Cars', id: '1' }, { name: 'Fast cars', id: '2' }]
      end

      specify 'are converted correctly' do
        expect(entity_hash[:categories]).to eq(expected_nested_model_hash)
      end
    end

    context 'with nested model' do
      let(:expected_nested_model_hash) { { name: 'John Najjar', id: '23' } }

      specify 'is converted correctly' do
        expect(entity_hash[:designer]).to eq(expected_nested_model_hash)
      end
    end
  end

  describe 'wrapped_json_collection_to_entities_hash' do
    it 'is tested'
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
fortnox-api-0.8.2 spec/fortnox/api/mappers/base/from_json_spec.rb
fortnox-api-0.8.1 spec/fortnox/api/mappers/base/from_json_spec.rb
fortnox-api-0.8.0 spec/fortnox/api/mappers/base/from_json_spec.rb
fortnox-api-0.7.2 spec/fortnox/api/mappers/base/from_json_spec.rb
fortnox-api-0.7.1 spec/fortnox/api/mappers/base/from_json_spec.rb
fortnox-api-0.7.0 spec/fortnox/api/mappers/base/from_json_spec.rb
fortnox-api-0.6.3 spec/fortnox/api/mappers/base/from_json_spec.rb
fortnox-api-0.6.2 spec/fortnox/api/mappers/base/from_json_spec.rb
fortnox-api-0.6.1 spec/fortnox/api/mappers/base/from_json_spec.rb
fortnox-api-0.6.0 spec/fortnox/api/mappers/base/from_json_spec.rb