Sha256: 83b50abd07f1c79d5dae3f97d256ec5d63b600bb120bd7233cf633545c2ef9ad

Contents?: true

Size: 1.95 KB

Versions: 5

Compression:

Stored size: 1.95 KB

Contents

require 'spec_helper'

describe Streamingly::SerDe do

  describe ".from_csv" do
    let(:value) { '19' }
    let(:text) { "#{type.name},#{value}" }

    context "given a namespaced class" do
      before do
        Namespace = Module.new
        Namespace::Namespaced = Struct.new(:value)
      end

      let(:type) { Namespace::Namespaced }

      it { expect(described_class.from_csv(text)).to eq(type.new(value)) }
    end

    context "given a non-namespaced class" do
      before do
        NonNamespaced = Struct.new(:value)
      end

      let(:type) { NonNamespaced }

      it { expect(described_class.from_csv(text)).to eq(type.new(value)) }
    end
  end

  describe '.to_csv' do
    it 'is identity function for a string' do
      record = 'test_string'
      expect(described_class.to_csv(record)).to eq record
    end

    it 'is equal to string version of Streamingly kv' do
      record = Streamingly::KV.new('key', 'value')
      expect(described_class.to_csv(record)).to eq record.to_s
    end

    it 'serializes struct to CSV, interpreting decimal fields as floats' do
      Record = Struct.new(:number, :string)
      record = Record.new(1, 'string_value')
      expect(described_class.to_csv(record)).to eq 'Record,1,string_value'
    end
  end

  describe '.from_string_or_csv' do
    it 'returns CSV serialization for CSV string' do
      expect(described_class.from_string_or_csv('1,2')).to eq ['1', '2']
    end

    it 'returns string if not containing a comma' do
      expect(described_class.from_string_or_csv('foo')).to eq 'foo'
    end

    it 'returns string if containing a comma but not valid CSV' do
      expect(described_class.from_string_or_csv('"foo,bar')).to eq '"foo,bar'
    end
  end

  describe '.from_tabbed_csv' do
    it 'returns nested KV pair structure with the first tab as the split' do
      expect(described_class.from_tabbed_csv("a\tb\tc")).to eq(
        Streamingly::KV.new('a', Streamingly::KV.new('b', 'c'))
      )
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
streamingly-0.2.7 spec/streamingly/serde_spec.rb
streamingly-0.2.6 spec/streamingly/serde_spec.rb
streamingly-0.2.4 spec/streamingly/serde_spec.rb
streamingly-0.2.3 spec/streamingly/serde_spec.rb
streamingly-0.2.2 spec/streamingly/serde_spec.rb