Permalink
Browse files

Add Address model and a pending spec

  • Loading branch information...
1 parent 9506be6 commit acb6519113d62126cb2e33c312bb553775f367f7 @solnic solnic committed Feb 14, 2012
Showing with 95 additions and 0 deletions.
  1. +73 −0 spec/integration/two_poro_mappers_spec.rb
  2. +22 −0 spec/spec_helper.rb
@@ -0,0 +1,73 @@
+require 'spec_helper'
+
+describe 'Two PORO mappers' do
+ before(:all) do
+ setup_db
+
+ insert_user 1, 'John', 18
+ insert_user 2, 'Jane', 21
+
+ insert_address 1, 1, 'Street 1/2', 'Chicago', '12345'
+ insert_address 2, 2, 'Street 2/4', 'Boston', '67890'
+
+ DataMapper.relation_registry << Veritas::Relation::Gateway.new(
+ DATABASE_ADAPTER, Address::Mapper.base_relation)
+
+ DataMapper.relation_registry << Veritas::Relation::Gateway.new(
+ DATABASE_ADAPTER, User::Mapper.base_relation)
+ end
+
+ let(:operation) do
+ left = DataMapper.relation_registry[:users]
+ right = DataMapper.relation_registry[:addresses].restrict { |r| r.city.eq('Boston') }
+
+ left.join(right)
+ end
+
+ class Address
+ attr_reader :id, :street, :zipcode, :city
+
+ def initialize(attributes)
+ @id, @street, @zipcode, @city = attributes.values_at(:id, :name, :zipcode, :city)
+ end
+
+ class Mapper < DataMapper::Mapper::VeritasMapper
+ map :id, :type => Integer
+ map :user_id, :type => Integer
+ map :street, :type => String
+ map :zipcode, :type => String
+ map :city, :type => String
+
+ model Address
+ name :addresses
+ end
+ end
+
+ class User
+ attr_reader :id, :name, :age
+
+ def initialize(attributes)
+ @id, @name, @age = attributes.values_at(:id, :name, :age)
+ end
+
+ class Mapper < DataMapper::Mapper::VeritasMapper
+ map :id, :type => Integer
+ map :name, :to => :username, :type => String
+ map :age, :type => Integer
+
+ model User
+ name :users
+ end
+ end
+
+ it 'finds user with a specific address' do
+ users = User::Mapper.new(operation.restrict { |r| r.city.eq('Boston') }.optimize).to_a
+ user = users.first
+
+ users.should have(1).item
+
+ user.should be_instance_of(User)
+ user.name.should eql('Jane')
+ end
+
+end
View
@@ -37,6 +37,7 @@ def setup_db
connection = DataObjects::Connection.new(DATABASE_URI)
connection.create_command('DROP TABLE IF EXISTS "users"').execute_non_query
+ connection.create_command('DROP TABLE IF EXISTS "addresses"').execute_non_query
connection.create_command(<<-SQL.gsub(/\s+/, ' ').strip).execute_non_query
CREATE TABLE "users"
@@ -46,6 +47,16 @@ def setup_db
)
SQL
+ connection.create_command(<<-SQL.gsub(/\s+/, ' ').strip).execute_non_query
+ CREATE TABLE "addresses"
+ ( "id" SERIAL NOT NULL PRIMARY KEY,
+ "user_id" INTEGER NOT NULL,
+ "street" VARCHAR(50) NOT NULL,
+ "zipcode" VARCHAR(10) NOT NULL,
+ "city" VARCHAR(50) NOT NULL
+ )
+ SQL
+
connection.close
end
@@ -65,3 +76,14 @@ def insert_user(id, name, age, connection = nil)
connection.close
end
+
+def insert_address(id, user_id, street, zipcode, city, connection = nil)
+ connection ||= DataObjects::Connection.new(DATABASE_URI)
+
+ insert_users = connection.create_command(
+ 'INSERT INTO "addresses" ("id", "user_id", "street", "zipcode", "city") VALUES (?, ?, ?, ?, ?)')
+
+ insert_users.execute_non_query(id, user_id, street, zipcode, city)
+
+ connection.close
+end

0 comments on commit acb6519

Please sign in to comment.