#590 ✓resolved
Piotr Solnica (solnic)

when using composite keys dump method in a custom type gets value inside an array

Reported by Piotr Solnica (solnic) | October 14th, 2008 @ 11:29 AM | in 0.10.0

Here's the situation:



module DataMapper
  # this is a dummy type which does nothing
  module Types
    class ZooName < DataMapper::Type
      primitive String
      size 128

      def self.load(value, property)
        value
      end

      def self.dump(value, property)
        value.is_a?(Array) ? raise("OMG IT'S AN ARRAY!") : value
      end

      def self.typecast(value, property)
        value
      end
    end
  end
end

class Zoo
  include DataMapper::Resource

  property :id,   Integer, :key => true
  property :name, ZooName, :key => true
end
Zoo.auto_migrate!

class Animal
  include DataMapper::Resource

  property :id,       Serial
  property :zoo_id,   Integer
  property :zoo_name, ZooName
  property :name,     String
end
Animal.auto_migrate!

Zoo.has 1, :marty, :class_name => 'Animal'
Animal.belongs_to :zoo

zoo       = Zoo.create(:id => 1, :name => 'Madagascar')
# this will raise our exception
zoo.marty = Animal.create(:zoo => zoo, :name => 'Marty')

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) January 8th, 2009 @ 02:20 PM

    • State changed from “new” to “unconfirmed”
    • Assigned user cleared.
  • Michael Klishin (antares)

    Michael Klishin (antares) January 10th, 2009 @ 06:29 AM

    • Assigned user set to “Michael Klishin (antares)”
  • Martin Gamsjaeger (snusnu)

    Martin Gamsjaeger (snusnu) July 12th, 2009 @ 01:03 PM

    • State changed from “unconfirmed” to “resolved”
    • Milestone set to 0.10.0

    I tested that with current next branch and get no failures. I'm marking this as resolved. Here's what I get:

    require "rubygems"
    require "dm-core"
    require "dm-types"
    require "dm-aggregates"
    require "spec"
    
    DataMapper::Logger.new(STDOUT, :debug)
    DataMapper.setup(:default, "sqlite3::memory:")
    
    module DataMapper
      # this is a dummy type which does nothing
      module Types
        class ZooName < DataMapper::Type
          primitive String
          size 128
    
          def self.load(value, property)
            value
          end
    
          def self.dump(value, property)
            value.is_a?(Array) ? raise("OMG IT'S AN ARRAY!") : value
          end
    
          def self.typecast(value, property)
            value
          end
        end
      end
    end
    
    class Zoo
      include DataMapper::Resource
    
      property :id,   Integer, :key => true
      property :name, ZooName, :key => true
      
      has 1, :marty, 'Animal'
    end
    
    class Animal
      include DataMapper::Resource
    
      property :id,       Serial
      property :zoo_id,   Integer
      property :zoo_name, ZooName
      property :name,     String
      
      belongs_to :zoo
    end
    
    describe "When using composite keys, the dump method in a custom type" do
      before(:all) do
        DataMapper.auto_migrate!
      end
      it "should not receive value as an Array" do
        zoo = Zoo.create(:id => 1, :name => 'Madagascar')
        lambda {
          zoo.marty = Animal.create(:zoo => zoo, :name => 'Marty')
        }.should_not raise_error
      end
    end
    
    # mungo:snippets snusnu$ spec -cfs 590.rb 
    # 
    # When using composite keys, the dump method in a custom type
    #  ~ (0.000683) SELECT sqlite_version(*)
    #  ~ (0.000116) DROP TABLE IF EXISTS "zoos"
    #  ~ (0.000015) DROP TABLE IF EXISTS "animals"
    #  ~ (0.000024) PRAGMA table_info("zoos")
    #  ~ (0.000351) CREATE TABLE "zoos" ("id" INTEGER NOT NULL, "name" VARCHAR(50) NOT NULL, PRIMARY KEY("id", "name"))
    #  ~ (0.000009) PRAGMA table_info("animals")
    #  ~ (0.000205) CREATE TABLE "animals" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "zoo_id" INTEGER, "zoo_name" VARCHAR(50), "name" VARCHAR(50))
    #  ~ (0.000069) INSERT INTO "zoos" ("id", "name") VALUES (1, 'Madagascar')
    #  ~ (0.000049) INSERT INTO "animals" ("zoo_id", "zoo_name", "name") VALUES (1, 'Madagascar', 'Marty')
    #  ~ (0.000055) SELECT "id", "zoo_id", "zoo_name", "name" FROM "animals" WHERE "zoo_id" = 1 AND "zoo_name" = 'Madagascar' ORDER BY "id"
    # - should not receive value as an Array
    # 
    # Finished in 0.010488 seconds
    # 
    # 1 example, 0 failures
    

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Pages