<commit_before>
require 'rubygems'
require 'dm-core'
require 'dm-mapping'

module Abstract
  def setup_data_mapper
    raise 'please provide a clean database because it is a destructive test!!'
  end

  AttrCommon = {:nullable => true}
  AttrCommonPK = {:serial => true, :key => true, :nullable => false}
  AttrText = {:size => 65535}.merge AttrCommon

  def user_fields
    [[:created_at, DateTime, AttrCommon],
     [:id,         DataMapper::Types::Serial,  AttrCommonPK],
     [:login,      String,   {:length => 70}.merge(AttrCommon)],
     [:sig,        DataMapper::Types::Text, AttrText]]
  end

  def comment_fields
    [[:body,    DataMapper::Types::Text,    AttrText],
     [:id,      DataMapper::Types::Serial,  AttrCommonPK],
     [:title,   String,   {:length => 50, :default => 'default title'}.
                            merge(AttrCommon)],
     [:user_id, Integer,  AttrCommon]]
  end

  # there's differences between adapters
  def super_user_fields
    case self
      when Sqlite3Test
        [[:bool, DataMapper::Types::Boolean, AttrCommon],
         [:id,   DataMapper::Types::Serial,  AttrCommonPK]]

      when MysqlTest
        [[:bool, Integer, AttrCommon],
         [:id,   DataMapper::Types::Serial, AttrCommonPK]]

      when PostgresTest
        [[:bool, DataMapper::Types::Boolean, AttrCommon],
         [:id,   DataMapper::Types::Serial,  AttrCommonPK]]

    end
  end

  class User
    include DataMapper::Resource
    has n, :comments

    property :id,         Serial
    property :login,      String, :length => 70
    property :sig,        Text
    property :created_at, DateTime

    is :reflexible
  end

  class SuperUser
    include DataMapper::Resource
    property :id, Serial
    property :bool, Boolean

    is :reflexible
  end

  class Comment
    include DataMapper::Resource
    belongs_to :user, :nullable => true

    property :id,    Serial
    property :title, String,  :length => 50, :default => 'default title'
    property :body,  Text

    is :reflexible
  end

  class Model; end

  Tables = ['abstract_comments', 'abstract_super_users', 'abstract_users']

  def sort_fields fields
    fields.sort_by{ |field|
      field.first.to_s
    }
  end

  def create_fake_model
    model = Model.dup.send(:include, DataMapper::Resource)
    model.is :reflexible
    [ model, setup_data_mapper ]
  end

  attr_reader :dm
  def setup
    @dm = setup_data_mapper
    # this is significant faster than DataMapper.auto_migrate!
    User.auto_migrate!
    Comment.auto_migrate!
    SuperUser.auto_migrate!
  end

  def new_scope
    self.class.const_set("Scope#{object_id.object_id}", Module.new)
  end

  def test_storages
    assert_equal Tables, dm.storages.sort
    assert_equal comment_fields, sort_fields(dm.fields('abstract_comments'))
  end

  def test_create_comment
    Comment.create(:title => 'XD')
    assert_equal 1, Comment.first.id
    assert_equal 'XD', Comment.first.title
  end

  def test_create_user
    now = Time.now
    User.create(:created_at => now)
    assert_equal 1, User.first.id
    assert_equal now.asctime, User.first.created_at.asctime

    return now
  end

  def test_mapping_all
    test_create_comment # for fixtures
    model, local_dm = create_fake_model
    model.storage_names[:default] = 'abstract_comments'

    assert_equal Tables, local_dm.storages.sort
    assert_equal 'abstract_comments', model.storage_name

    model.send :mapping
    assert_equal 1, model.all.size
    assert_equal comment_fields, sort_fields(model.fields)

    assert_equal 'XD', model.first.title
    assert_equal 1, model.first.id
  end

  def test_mapping_and_create
    model, local_dm = create_fake_model
    model.storage_names[:default] = 'abstract_comments'
    model.send :mapping

    model.create(:title => 'orz')
    assert_equal 'orz', model.first.title
    assert_equal 1, model.first.id

    model.create
    assert_equal 'default title', model.get(2).title
  end

  def test_storages_and_fields
    assert_equal user_fields, sort_fields(dm.fields('abstract_users'))
    assert_equal( {'abstract_users' => user_fields,
                   'abstract_comments' => comment_fields,
                   'abstract_super_users' => super_user_fields},
                  dm.storages_and_fields.inject({}){ |r, i|
                    key, value = i
                    r[key] = value.sort_by{ |v| v.first.to_s }
                    r
                  } )
  end

  def test_mapping_type
    model, local_dm = create_fake_model
    model.storage_names[:default] = 'abstract_comments'
    model.send :mapping, Integer

    assert_equal ['id', 'user_id'], model.properties.map(&:name).map(&:to_s).sort
  end

  def test_mapping_multiple
    model, local_dm = create_fake_model
    model.storage_names[:default] = 'abstract_users'
    model.send :mapping, :login, Integer

    assert_equal ['id', 'login'], model.properties.map(&:name).map(&:to_s).sort
  end

  def test_mapping_regexp
    model, local_dm = create_fake_model
    model.storage_names[:default] = 'abstract_comments'
    model.send :mapping, /id$/

    assert_equal ['id', 'user_id'], model.properties.map(&:name).map(&:to_s).sort
  end

  def test_invalid_argument
    assert_raises(ArgumentError){<commit_msg>Serial is not Integer anymore. foreign key is still Integer currently..<commit_after>
require 'rubygems'
require 'dm-core'
require 'dm-mapping'

module Abstract
  def setup_data_mapper
    raise 'please provide a clean database because it is a destructive test!!'
  end

  AttrCommon = {:nullable => true}
  AttrCommonPK = {:serial => true, :key => true, :nullable => false}
  AttrText = {:size => 65535}.merge AttrCommon

  def user_fields
    [[:created_at, DateTime, AttrCommon],
     [:id,         DataMapper::Types::Serial,  AttrCommonPK],
     [:login,      String,   {:length => 70}.merge(AttrCommon)],
     [:sig,        DataMapper::Types::Text, AttrText]]
  end

  def comment_fields
    [[:body,    DataMapper::Types::Text,    AttrText],
     [:id,      DataMapper::Types::Serial,  AttrCommonPK],
     [:title,   String,   {:length => 50, :default => 'default title'}.
                            merge(AttrCommon)],
     [:user_id, Integer,  AttrCommon]]
  end

  # there's differences between adapters
  def super_user_fields
    case self
      when Sqlite3Test
        [[:bool, DataMapper::Types::Boolean, AttrCommon],
         [:id,   DataMapper::Types::Serial,  AttrCommonPK]]

      when MysqlTest
        [[:bool, Integer, AttrCommon],
         [:id,   DataMapper::Types::Serial, AttrCommonPK]]

      when PostgresTest
        [[:bool, DataMapper::Types::Boolean, AttrCommon],
         [:id,   DataMapper::Types::Serial,  AttrCommonPK]]

    end
  end

  class User
    include DataMapper::Resource
    has n, :comments

    property :id,         Serial
    property :login,      String, :length => 70
    property :sig,        Text
    property :created_at, DateTime

    is :reflexible
  end

  class SuperUser
    include DataMapper::Resource
    property :id, Serial
    property :bool, Boolean

    is :reflexible
  end

  class Comment
    include DataMapper::Resource
    belongs_to :user, :nullable => true

    property :id,    Serial
    property :title, String,  :length => 50, :default => 'default title'
    property :body,  Text

    is :reflexible
  end

  class Model; end

  Tables = ['abstract_comments', 'abstract_super_users', 'abstract_users']

  def sort_fields fields
    fields.sort_by{ |field|
      field.first.to_s
    }
  end

  def create_fake_model
    model = Model.dup.send(:include, DataMapper::Resource)
    model.is :reflexible
    [ model, setup_data_mapper ]
  end

  attr_reader :dm
  def setup
    @dm = setup_data_mapper
    # this is significant faster than DataMapper.auto_migrate!
    User.auto_migrate!
    Comment.auto_migrate!
    SuperUser.auto_migrate!
  end

  def new_scope
    self.class.const_set("Scope#{object_id.object_id}", Module.new)
  end

  def test_storages
    assert_equal Tables, dm.storages.sort
    assert_equal comment_fields, sort_fields(dm.fields('abstract_comments'))
  end

  def test_create_comment
    Comment.create(:title => 'XD')
    assert_equal 1, Comment.first.id
    assert_equal 'XD', Comment.first.title
  end

  def test_create_user
    now = Time.now
    User.create(:created_at => now)
    assert_equal 1, User.first.id
    assert_equal now.asctime, User.first.created_at.asctime

    return now
  end

  def test_mapping_all
    test_create_comment # for fixtures
    model, local_dm = create_fake_model
    model.storage_names[:default] = 'abstract_comments'

    assert_equal Tables, local_dm.storages.sort
    assert_equal 'abstract_comments', model.storage_name

    model.send :mapping
    assert_equal 1, model.all.size
    assert_equal comment_fields, sort_fields(model.fields)

    assert_equal 'XD', model.first.title
    assert_equal 1, model.first.id
  end

  def test_mapping_and_create
    model, local_dm = create_fake_model
    model.storage_names[:default] = 'abstract_comments'
    model.send :mapping

    model.create(:title => 'orz')
    assert_equal 'orz', model.first.title
    assert_equal 1, model.first.id

    model.create
    assert_equal 'default title', model.get(2).title
  end

  def test_storages_and_fields
    assert_equal user_fields, sort_fields(dm.fields('abstract_users'))
    assert_equal( {'abstract_users' => user_fields,
                   'abstract_comments' => comment_fields,
                   'abstract_super_users' => super_user_fields},
                  dm.storages_and_fields.inject({}){ |r, i|
                    key, value = i
                    r[key] = value.sort_by{ |v| v.first.to_s }
                    r
                  } )
  end

  def test_mapping_type
    model, local_dm = create_fake_model
    model.storage_names[:default] = 'abstract_comments'

    model.send :mapping, DataMapper::Types::Serial
    assert_equal ['id'], model.properties.map(&:name).map(&:to_s).sort

    model.send :mapping, Integer
    assert_equal ['id', 'user_id'], model.properties.map(&:name).map(&:to_s).sort
  end

  def test_mapping_multiple
    model, local_dm = create_fake_model
    model.storage_names[:default] = 'abstract_users'
    model.send :mapping, :login, DataMapper::Types::Serial

    assert_equal ['id', 'login'], model.properties.map(&:name).map(&:to_s).sort
  end

  def test_mapping_regexp
    model, local_dm = create_fake_model
    model.storage_names[:default] = 'abstract_comments'
    model.send :mapping, /id$/

    assert_equal ['id', 'user_id'], model.properties.map(&:name).map(&:to_s).sort
  end

  def test_invalid_argument
    assert_raises(ArgumentError){
{'fraction_non_alphanumeric': 0.10817645406012925, 'fraction_numerical': 0.0037463707033810996, 'mean_word_length': 3.0974673829623947, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1306790', 'n_tokens_mistral': 3488, 'n_tokens_neox': 3306, 'n_words': 769}