2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
require 'fast_spec_helper'
|
2022-04-04 11:22:00 +05:30
|
|
|
require 'oj'
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
RSpec.describe Serializers::Json do
|
2019-02-15 15:39:39 +05:30
|
|
|
describe '.dump' do
|
|
|
|
let(:obj) { { key: "value" } }
|
|
|
|
|
|
|
|
subject { described_class.dump(obj) }
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it 'returns a hash' do
|
|
|
|
is_expected.to eq(obj)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.load' do
|
|
|
|
let(:data_string) { '{"key":"value","variables":[{"key":"VAR1","value":"VALUE1"}]}' }
|
2020-05-24 23:13:21 +05:30
|
|
|
let(:data_hash) { Gitlab::Json.parse(data_string) }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
context 'when loading a hash' do
|
|
|
|
subject { described_class.load(data_hash) }
|
|
|
|
|
|
|
|
it 'decodes a string' do
|
|
|
|
is_expected.to be_a(Hash)
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
it 'allows to access with symbols' do
|
|
|
|
expect(subject[:key]).to eq('value')
|
|
|
|
expect(subject[:variables].first[:key]).to eq('VAR1')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows to access with strings' do
|
|
|
|
expect(subject["key"]).to eq('value')
|
|
|
|
expect(subject["variables"].first["key"]).to eq('VAR1')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
context 'when loading a nil' do
|
|
|
|
subject { described_class.load(nil) }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
it 'returns nil' do
|
|
|
|
is_expected.to be_nil
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|