<commit_before>require 'rspec'
$:.unshift File.expand_path('../../../../../lib', __FILE__)
require 'active_mocker'
require 'json'
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/object/deep_dup.rb'
require 'active_mocker/model_schema'
require 'active_mocker/model_schema'
require 'active_mocker/model_schema/generate'
require_relative '../../../unit_logger'

describe ActiveMocker::ModelSchema::Generate do

  let(:app_root) { File.expand_path('../../../../../', __FILE__) }
  let(:schema_file){ File.join(app_root, 'sample_app_rails_4/db/schema.rb') }
  # let(:schema_file){ '/Users/zeisler/dev/fbi/db/schema.rb' }
  let(:models_dir){ File.join(app_root, 'sample_app_rails_4/app/models') }
  # let(:models_dir){'/Users/zeisler/dev/fbi/app/models' }
  let(:run){described_class.new(schema_file: schema_file, models_dir: models_dir, logger: UnitLogger.unit).run
  }

  it 'test' do
    result = described_class.new(schema_file: schema_file, models_dir: models_dir, logger: UnitLogger.unit).run
    expect(result.count).to eq 3
  end

  it 'relationships' do
    expect(run[-1].relationships.map{ |r| JSON.parse(r.to_json)})
      .to eq [{"name" => "microposts", "class_name" => "Micropost", "type" => "has_many", "foreign_key" => "user_id"},
              {"name" => "relationships", "class_name" => "Relationship", "type" => "has_many", "foreign_key" => "follower_id"},
              {"name" => "followed_users", "class_name" => "FollowedUser", "type" => "has_many", "foreign_key" => "user_id", "through" => "relationships"},
              {"name" => "reverse_relationships", "class_name" => "Relationship", "type" => "has_many", "foreign_key" => "followed_id"},
              {"name" => "followers", "class_name" => "Follower", "type" => "has_many", "foreign_key" => "user_id", "through" => "reverse_relationships"}]
  end

  it 'methods' do
    expect(run[-1].methods.map { |r| JSON.parse(r.to_json) })
      .to eq([{"name" => "feed",       "arguments" => [],                     "type" => "instance"},
              {"name" => "following?", "arguments" => [["req", "other_user"]], "type" => "instance"},
              {"name" => "follow!",    "arguments" => [["req", "other_user"]], "type" => "instance"},
              {"name" => "unfollow!",  "arguments" => [["req", "other_user"]], "type" => "instance"},
              {"name" => "new_remember_token", "arguments" => [],             "type" => "class"},
              {"name" => "digest",     "arguments" => [["req", "token"]],      "type" => "class"}])
  end<commit_msg>add sort_by(:class_name) to see if it’s how the file system it loading the files<commit_after>require 'rspec'
$:.unshift File.expand_path('../../../../../lib', __FILE__)
require 'active_mocker'
require 'json'
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/object/deep_dup.rb'
require 'active_mocker/model_schema'
require 'active_mocker/model_schema'
require 'active_mocker/model_schema/generate'
require_relative '../../../unit_logger'

describe ActiveMocker::ModelSchema::Generate do

  let(:app_root) { File.expand_path('../../../../../', __FILE__) }
  let(:schema_file){ File.join(app_root, 'sample_app_rails_4/db/schema.rb') }
  # let(:schema_file){ '/Users/zeisler/dev/fbi/db/schema.rb' }
  let(:models_dir){ File.join(app_root, 'sample_app_rails_4/app/models') }
  # let(:models_dir){'/Users/zeisler/dev/fbi/app/models' }
  let(:run){described_class.new(schema_file: schema_file, models_dir: models_dir, logger: UnitLogger.unit).run.sort_by{|m| m.class_name}
  }

  it 'test' do
    result = described_class.new(schema_file: schema_file, models_dir: models_dir, logger: UnitLogger.unit).run
    expect(result.count).to eq 3
  end

  it 'relationships' do
    expect(run[-1].relationships.map{ |r| JSON.parse(r.to_json)})
      .to eq [{"name" => "microposts", "class_name" => "Micropost", "type" => "has_many", "foreign_key" => "user_id"},
              {"name" => "relationships", "class_name" => "Relationship", "type" => "has_many", "foreign_key" => "follower_id"},
              {"name" => "followed_users", "class_name" => "FollowedUser", "type" => "has_many", "foreign_key" => "user_id", "through" => "relationships"},
              {"name" => "reverse_relationships", "class_name" => "Relationship", "type" => "has_many", "foreign_key" => "followed_id"},
              {"name" => "followers", "class_name" => "Follower", "type" => "has_many", "foreign_key" => "user_id", "through" => "reverse_relationships"}]
  end

  it 'methods' do
    expect(run[-1].methods.map { |r| JSON.parse(r.to_json) })
      .to eq([{"name" => "feed",       "arguments" => [],                     "type" => "instance"},
              {"name" => "following?", "arguments" => [["req", "other_user"]], "type" => "instance"},
              {"name" => "follow!",    "arguments" => [["req", "other_user"]], "type" => "instance"},
              {"name" => "unfollow!",  "arguments" => [["req", "other_user"]], "type" => "instance"},
              {"name" => "new_remember_token", "arguments" => [],             "type" => "class"},
              {"name" => "digest",     "arguments" => [["req", "token"]],      "type" => "class"}])
  end
{'fraction_non_alphanumeric': 0.21473684210526317, 'fraction_numerical': 0.0019138755980861245, 'mean_word_length': 3.7901008249312556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 83, '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': '5931281', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1753, 'n_words': 323}