Sha256: d42b1e2e0717cf2b4f33b4f2b9a137904efcac13f3608c7c55606bb88219960f

Contents?: true

Size: 1.74 KB

Versions: 5

Compression:

Stored size: 1.74 KB

Contents

require "spec_helper"
require "ar_protobuf_store"

begin
  require "protobuf/message/message"
  require "protobuf/message/enum"
  require "protobuf/message/service"
  require "protobuf/message/extend"
  if !Gem.loaded_specs["ruby_protobuf"]
    raise LoadError, "Didn't load macks' ruby_protobuf library!"
  end

  describe "ArProtobufStore::MacksSupport" do
    around(:each) do |example|
      setup_db do
        create_table :foos do |t|
          t.column :extras, :binary
        end
      end

      example.run

      teardown_db
    end

    let(:ar_klass) {
      # Define everything using anonymous classes to reduce leakage.
      extras = Class.new(::Protobuf::Message) do
        optional :uint64, :int_attr, 1
        optional :string, :str_attr, 2
      end

      Class.new(::ActiveRecord::Base) do
        self.table_name = "foos"

        include ArProtobufStore

        protobuf_store :extras, extras
      end
    }

    describe ArProtobufStore::ClassMethods do
      it "should allow setting fields individually" do
        record = ar_klass.create!
        record.int_attr = 2
        record.str_attr = "TEST"
        record.save
        expect(record.persisted?).to eq(true)
        record_id = record.id

        record = ar_klass.find(record_id)
        expect(record.int_attr).to eq(2)
        expect(record.str_attr).to eq("TEST")
      end

      it "should allow setting fields in constructor" do
        record = ar_klass.create!(:int_attr => 2, :str_attr => "TEST")
        expect(record.persisted?).to eq(true)
        record_id = record.id

        record = ar_klass.find(record_id)
        expect(record.int_attr).to eq(2)
        expect(record.str_attr).to eq("TEST")
      end
    end
  end
rescue LoadError
  # Ignore: protobuf isn't loaded!
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ar_protobuf_store-0.2.3 spec/macks_protobuf_spec.rb
ar_protobuf_store-0.2.2 spec/macks_protobuf_spec.rb
ar_protobuf_store-0.2.1 spec/macks_protobuf_spec.rb
ar_protobuf_store-0.2.0 spec/macks_protobuf_spec.rb
ar_protobuf_store-0.1.0 spec/macks_protobuf_spec.rb