Sha256: 789048e93f1d1aa4293ca980d073f7bf10ca3887e634acd7eaf33ce7ea26d2d1

Contents?: true

Size: 1.94 KB

Versions: 4

Compression:

Stored size: 1.94 KB

Contents

require "spec_helper"
require "shamu/entities"


describe Shamu::Entities::Entity do
  let( :klass ) do
    Class.new( Shamu::Entities::Entity ) do
      model :user
      attribute :name, on: :user
      attribute :email, on: :user
    end
  end

  context "with instance" do
    let( :user )     { OpenStruct.new( name: "Heisenberg", email: "blue@rock.com" ) }
    let( :instance ) { klass.new( user: user ) }

    describe "#to_attributes" do

      it "does not include model attributes" do
        expect( instance.to_attributes ).not_to have_key :user
      end
    end

    describe "#redact" do
      it "clears the assigned attribute" do
        redacted = instance.redact( :name )
        expect( redacted.name ).to be_nil
      end

      it "it returns instance of the same type" do
        redacted = instance.redact( :name )
        expect( redacted ).to be_a klass
      end

      it "assigns redacted values" do
        redacted = instance.redact( name: "REDACTED" )
        expect( redacted.name ).to eq "REDACTED"
      end
    end
  end

  {
    "SetEntity"                     => "Set",
    "Set"                           => "Set",
    "Domain::SetEntity"             => "Domain::Set",
    "Domain::SubDomain::SetEntity"  => "Domain::SubDomain::Set",
    "Domain::SubDomain::SetsEntity" => "Domain::SubDomain::Set",
  }.each do |full_name, expected|

    it "maps #{ full_name } to #{ expected }" do
      klass = Class.new( Shamu::Entities::Entity )
      klass.define_singleton_method :name do
        full_name
      end

      expect( klass.model_name.name ).to eq expected
    end
  end

  describe ".null_entity" do
    it "defines a NullEntity class" do
      expect( klass.null_entity ).to be < Shamu::Entities::NullEntity
    end

    it "overrides attributes with default values" do
      klass.null_entity do
        attribute :name do
          "Unknown"
        end
      end

      expect( klass.null_entity.new.name ).to eq "Unknown"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shamu-0.0.24 spec/lib/shamu/entities/entity_spec.rb
shamu-0.0.21 spec/lib/shamu/entities/entity_spec.rb
shamu-0.0.20 spec/lib/shamu/entities/entity_spec.rb
shamu-0.0.19 spec/lib/shamu/entities/entity_spec.rb