lib/dm-is-reflective/test.rb in dm-is-reflective-1.2.0 vs lib/dm-is-reflective/test.rb in dm-is-reflective-1.3.0
- old
+ new
@@ -5,10 +5,36 @@
require 'dm-core'
require 'dm-migrations'
require 'dm-is-reflective'
module Abstract
+ class Cat
+ include DataMapper::Resource
+ property :id, Serial
+
+ belongs_to :user
+ belongs_to :super_user
+
+ property :user_id , Integer,
+ :unique_index => [:usu, :u]
+ property :super_user_id, Integer,
+ :unique_index => [:usu],
+ :index => [:su]
+ end
+
+ class Comment
+ include DataMapper::Resource
+ belongs_to :user, :required => false
+
+ property :id, Serial
+ property :title, String, :length => 50, :default => 'default title',
+ :allow_nil => false
+ property :body, Text
+
+ is :reflective
+ end
+
class User
include DataMapper::Resource
has n, :comments
property :id, Serial
@@ -25,24 +51,13 @@
property :bool, Boolean
is :reflective
end
- class Comment
- include DataMapper::Resource
- belongs_to :user, :required => false
+ Tables = %w[abstract_cats abstract_comments
+ abstract_super_users abstract_users]
- property :id, Serial
- property :title, String, :length => 50, :default => 'default title',
- :allow_nil => false
- property :body, Text
-
- is :reflective
- end
-
- Tables = ['abstract_comments', 'abstract_super_users', 'abstract_users']
-
AttrCommon = {:allow_nil => true}
AttrCommonPK = {:serial => true, :key => true, :allow_nil => false}
AttrText = {:length => 65535}.merge(AttrCommon)
def self.next_id
@@ -52,44 +67,62 @@
end
include Abstract
shared :reflective do
+ def cat_fields
+ @cat_fields ||=
+ [[:id, DataMapper::Property::Serial,
+ {:unique_index => :abstract_cats_pkey}.merge(AttrCommonPK)],
+ [:super_user_id, Integer,
+ {:unique_index => :unique_abstract_cats_usu,
+ :index => :index_abstract_cats_su }.merge(AttrCommon)],
+ [:user_id , Integer,
+ {:unique_index => [:unique_abstract_cats_usu,
+ :unique_abstract_cats_u]}.merge(AttrCommon)]]
+ end
+
+ def comment_fields
+ @comment_fields ||= begin
+ [[:body , DataMapper::Property::Text , AttrText],
+ [:id , DataMapper::Property::Serial,
+ {:unique_index => :abstract_comments_pkey}.merge(AttrCommonPK)],
+
+ [:title , String ,
+ {:length => 50, :default => 'default title', :allow_nil => false}],
+
+ [:user_id, Integer ,
+ {:index => :index_abstract_comments_user}.merge(AttrCommon)]]
+ end
+ end
+
def user_fields
+ @user_fields ||=
[[:created_at, DateTime, AttrCommon],
- [:id, DataMapper::Property::Serial, AttrCommonPK],
+ [:id, DataMapper::Property::Serial,
+ {:unique_index => :abstract_users_pkey}.merge(AttrCommonPK)],
[:login, String, {:length => 70}.merge(AttrCommon)],
[:sig, DataMapper::Property::Text, AttrText]]
end
- def comment_fields
- [[:body, DataMapper::Property::Text, AttrText],
- [:id, DataMapper::Property::Serial, AttrCommonPK],
- [:title, String, {:length => 50, :default => 'default title',
- :allow_nil => false}],
- [:user_id, Integer, AttrCommon]]
- end
-
- # there's differences between adapters
def super_user_fields
- mysql = defined?(DataMapper::Adapters::MysqlAdapter) &&
- DataMapper::Adapters::MysqlAdapter
- case DataMapper.repository.adapter
- when mysql
- # Mysql couldn't tell it's boolean or tinyint
- [[:bool, Integer, AttrCommon],
- [:id, DataMapper::Property::Serial, AttrCommonPK]]
-
- else
- [[:bool, DataMapper::Property::Boolean, AttrCommon],
- [:id, DataMapper::Property::Serial, AttrCommonPK]]
+ @super_user_fields ||= begin
+ type = case DataMapper.repository.adapter.class.name
+ when 'DataMapper::Adapters::MysqlAdapter'
+ Integer
+ else
+ DataMapper::Property::Boolean
+ end
+ [[:bool, type, AttrCommon],
+ [:id , DataMapper::Property::Serial,
+ {:unique_index => :abstract_super_users_pkey}.merge(AttrCommonPK)]]
end
end
before do
@dm = setup_data_mapper
- [User, Comment, SuperUser].each(&:auto_migrate!)
+ [Cat, Comment, User, SuperUser].each(&:auto_migrate!)
end
def sort_fields fields
fields.sort_by{ |f| f.first.to_s }
end
@@ -165,12 +198,13 @@
@dm.storages_and_fields.inject({}){ |r, i|
key, value = i
r[key] = value.sort_by{ |v| v.first.to_s }
r
- }.should.eq('abstract_users' => user_fields ,
- 'abstract_comments' => comment_fields ,
+ }.should.eq('abstract_cats' => cat_fields,
+ 'abstract_comments' => comment_fields,
+ 'abstract_users' => user_fields,
'abstract_super_users' => super_user_fields)
end
should 'reflect type' do
model, local_dm = create_fake_model
@@ -213,10 +247,11 @@
end
should 'auto_genclasses' do
scope = new_scope
@dm.auto_genclass!(:scope => scope).map(&:to_s).sort.should.eq \
- ["#{scope}::AbstractComment",
+ ["#{scope}::AbstractCat" ,
+ "#{scope}::AbstractComment" ,
"#{scope}::AbstractSuperUser",
"#{scope}::AbstractUser"]
comment = scope.const_get('AbstractComment')