<commit_before><%
fixture_create = case
  when defined? Fabricate
    "Fabricate"
  when defined? FactoryGirl
    "FactoryGirl"
end
-%>
# -*- coding: utf-8 -*-

require 'spec_helper'

describe <%= class_name %>, "db" do
  # 存在するカラムを列挙する
  #it{should have_db_column(:column)}

<% for attribute in attributes -%>
  it{should have_db_column(:<%= attribute.name %>)}
<% end -%>

  # 存在するインデックスを列挙する
  #it{should have_db_index(:column)}
  #it{should have_db_index([:column, :column2])}
  #it{should have_db_index(:column).uniq(true)}
end

describe <%= class_name %>, "mass_assignments" do
  # mass_assignment
  #it{should allow_mass_assignment_of(:column)}           # できる
  #it{should_not allow_mass_assignment_of(:column)}       # できない
  #it{should allow_mass_assignment_of(:column).as(:role)} # as: role ならできる

<% for attribute in attributes -%>
  it{should allow_mass_assignment_of(:<%= attribute.name %>)}
<% end -%>
end

describe <%= class_name %>, "associations" do
  # アソシエーション
  #it{should belong_to(:parent)}
  #it{should have_many(:children)}
  #it{should have_many(:children).through(:relation)}
  #it{should have_one(:partner)}
end

describe <%= class_name %>, "invalid values" do
  subject{<%= class_name %>.new <%= fixture_create %>.attributes_for(:<%= plural_name.singularize %>)}

  # データベースに保存できない値を渡す
  #it{should_not success_persistance_of(:name).values(["a"*256])}  # varchar(255)
  #it{should_not success_persistance_of(:name).values([10**10])}   # int(9)
  #it{should_not success_persistance_of(:name).values([nil])}      # not null カラム
  #it{should_not success_persistance_of(:name).values(["a", "a"])} # unique カラム

<% for attribute in attributes -%>
<% if values = (case attribute.type
  when :integer then '10**10'
  when :string  then '"a"*256'
end) -%>
  it{should_not success_persistance_of(:<%= attribute.name %>).values([<%= values %>])}
<% end -%>
<% end -%>
end

describe <%= class_name %>, "scope" do
  subject{<%= class_name %>}

#  it{should have_executable_scope(:scope).by_sql(<<__SQL)}<commit_msg>belongs_to カラムタイプに対応<commit_after><%
fixture_create = case
  when defined? Fabricate
    "Fabricate"
  when defined? FactoryGirl
    "FactoryGirl"
end
-%>
# -*- coding: utf-8 -*-

require 'spec_helper'

describe <%= class_name %>, "db" do
  # 存在するカラムを列挙する
  #it{should have_db_column(:column)}
<% for attribute in attributes -%>
  it{should have_db_column(:<%= (case attribute.type
when :belongs_to,:resources then "#{attribute.name}_id"
else
  attribute.name
end) %>)}
<% end -%>

  # 存在するインデックスを列挙する
  #it{should have_db_index(:column)}
  #it{should have_db_index([:column, :column2])}
  #it{should have_db_index(:column).uniq(true)}
<% for attribute in attributes -%>
<% if values = (case attribute.type
  when :belongs_to,:resources then true
end) -%>
  it{should have_db_index(:<%= attribute.name %>_id)}
<% end -%>
<% end -%>
end

describe <%= class_name %>, "mass_assignments" do
  # mass_assignment
  #it{should allow_mass_assignment_of(:column)}           # できる
  #it{should_not allow_mass_assignment_of(:column)}       # できない
  #it{should allow_mass_assignment_of(:column).as(:role)} # as: role ならできる
<% for attribute in attributes -%>
<% if (case attribute.type
when :belongs_to,:resources then false
else
  true
end) -%>
  it{should allow_mass_assignment_of(:<%= attribute.name %>)}
<% end -%>
<% end -%>
end

describe <%= class_name %>, "associations" do
  # アソシエーション
  #it{should belong_to(:parent)}
  #it{should have_many(:children)}
  #it{should have_many(:children).through(:relation)}
  #it{should have_one(:partner)}
end

describe <%= class_name %>, "invalid values" do
  #subject{<%= class_name %>.new <%= fixture_create %>.attributes_for(:<%= plural_name.singularize %>)}

  # データベースに保存できない値を渡す
  #it{should_not success_persistance_of(:name).values(["a"*256])}  # varchar(255)
  #it{should_not success_persistance_of(:name).values([10**10])}   # int(9)
  #it{should_not success_persistance_of(:name).values([nil])}      # not null カラム
  #it{should_not success_persistance_of(:name).values(["a", "a"])} # unique カラム
<% for attribute in attributes -%>
<% name, values = (case attribute.type
  when :integer then [nil, '10**10']
  when :belongs_to,:resources then ["#{attribute.name}_id", '10**10']
  when :string then [nil, '"a"*256']
end) -%>
<% if values -%>
  it{should_not success_persistance_of(:<%= name || attribute.name %>).values([<%= values %>])}
<% end -%>
<% end -%>
end

describe <%= class_name %>, "scope" do
  subject{<%= class_name %>}

#  it{should have_executable_scope(:scope).by_sql(<<__SQL)}
{'fraction_non_alphanumeric': 0.1777339469647162, 'fraction_numerical': 0.009642778873548105, 'mean_word_length': 5.226466575716235, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 55, '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': '1871859', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1725, 'n_words': 328}