diff options
author | Kamil Trzciński <ayufan@ayufan.eu> | 2018-09-02 17:35:15 +0300 |
---|---|---|
committer | Kamil Trzciński <ayufan@ayufan.eu> | 2019-01-04 18:38:17 +0300 |
commit | 0103d5be960e620342c67436ddd64ca9e729d7a8 (patch) | |
tree | b4f2cdd4a5ef8f6c906d71c674cc5f13f791c889 /spec/lib/serializers | |
parent | b647ad96f6e7cd1e6ca078635bb1ea49ee7d589f (diff) |
Add config_options|variables to BuildMetadata
These are data columns that store runtime configuration
of build needed to execute it on runner and within pipeline.
The definition of this data is that once used, and when no longer
needed (due to retry capability) they can be freely removed.
They use `jsonb` on PostgreSQL, and `text` on MySQL (due to lacking
support for json datatype on old enough version).
Diffstat (limited to 'spec/lib/serializers')
-rw-r--r-- | spec/lib/serializers/json_spec.rb | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/spec/lib/serializers/json_spec.rb b/spec/lib/serializers/json_spec.rb new file mode 100644 index 00000000000..5d59d66e8b8 --- /dev/null +++ b/spec/lib/serializers/json_spec.rb @@ -0,0 +1,102 @@ +require 'fast_spec_helper' + +describe Serializers::JSON do + describe '.dump' do + let(:obj) { { key: "value" } } + + subject { described_class.dump(obj) } + + context 'when MySQL is used' do + before do + allow(Gitlab::Database).to receive(:adapter_name) { 'mysql2' } + end + + it 'encodes as string' do + is_expected.to eq('{"key":"value"}') + end + end + + context 'when PostgreSQL is used' do + before do + allow(Gitlab::Database).to receive(:adapter_name) { 'postgresql' } + end + + it 'returns a hash' do + is_expected.to eq(obj) + end + end + end + + describe '.load' do + let(:data_string) { '{"key":"value","variables":[{"key":"VAR1","value":"VALUE1"}]}' } + let(:data_hash) { JSON.parse(data_string) } + + shared_examples 'having consistent accessor' do + 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 + + context 'when MySQL is used' do + before do + allow(Gitlab::Database).to receive(:adapter_name) { 'mysql2' } + end + + context 'when loading a string' do + subject { described_class.load(data_string) } + + it 'decodes a string' do + is_expected.to be_a(Hash) + end + + it_behaves_like 'having consistent accessor' + end + + context 'when loading a different type' do + subject { described_class.load({ key: 'hash' }) } + + it 'raises an exception' do + expect { subject }.to raise_error(TypeError) + end + end + + context 'when loading a nil' do + subject { described_class.load(nil) } + + it 'returns nil' do + is_expected.to be_nil + end + end + end + + context 'when PostgreSQL is used' do + before do + allow(Gitlab::Database).to receive(:adapter_name) { 'postgresql' } + end + + context 'when loading a hash' do + subject { described_class.load(data_hash) } + + it 'decodes a string' do + is_expected.to be_a(Hash) + end + + it_behaves_like 'having consistent accessor' + end + + context 'when loading a nil' do + subject { described_class.load(nil) } + + it 'returns nil' do + is_expected.to be_nil + end + end + end + end +end |