<commit_before>require 'spec_helper'

describe ForeignKeyValidation::ModelExtension do

  # NOTE: it's important to not create the objects through relation (user.projects.create...)
  #       it looks like active_record is caching the classes - but we need to test different class configs

  context "without calling validation" do

    let(:user) { User.create }
    let(:project) { Project.create user: user }
    let(:idea) { Idea.create user: user, project: project }
    let(:issue) { Issue.create user: user, project: project }
    let(:comment) { Comment.create user: user, issue: issue }

    it "uses same user ids by default" do
      expect(project.user_id).to eq(user.id)
      expect(idea.user_id).to eq(user.id)
    end

    it "allow to rewrite user id of idea" do
      idea.user_id = 42
      idea.save
      idea.reload
      expect(idea.user_id).to eq(42)
    end

    it "allow to rewrite user id of project" do
      project.user_id = 42
      project.save
      project.reload
      expect(project.user_id).to eq(42)
    end

    it "allow to rewrite user id of issue" do
      issue.user_id = 42
      issue.save
      issue.reload
      expect(issue.user_id).to eq(42)
    end

    it "allow to rewrite user id of comment" do
      comment.user_id = 42
      comment.save
      comment.reload
      expect(comment.user_id).to eq(42)
    end

  end

  context "with calling validation" do
    before do
      Idea.send :validate_foreign_keys
      Project.send :validate_foreign_keys
      Issue.send :validate_foreign_keys
      Comment.send :validate_foreign_keys
      Member.send :validate_foreign_keys # sti model
    end

    let(:user) { User.create }
    let(:project) { Project.create user: user }
    let(:idea) { Idea.create user: user, project: project }
    let(:issue) { Issue.create user: user, project: project }
    let(:comment) { Comment.create user: user, issue: issue }
    # sti model
    let(:manager) { Manager.create user: user }
    let(:developer) { Developer.create user: user, boss: manager }

    it "uses same user ids by default" do
      expect(project.user_id).to eq(user.id)
      expect(idea.user_id).to eq(user.id)
    end

    it "does not allow to rewrite user id of idea" do
      idea.user_id = 42
      idea.save
      expect(idea.errors.messages.values.flatten).to include("user_id of project does not match ideas user_id")
      expect(idea.reload.user_id).to_not eq(42)
    end

    it "does not allow to rewrite user id of issue" do
      issue.user_id = 42
      issue.save
      expect(issue.errors.messages.values.flatten).to include("user_id of project does not match issues user_id")
      expect(issue.reload.user_id).to_not eq(42)
    end

    it "does not allow to rewrite user id of comment" do<commit_msg>add spec<commit_after>require 'spec_helper'

describe ForeignKeyValidation::ModelExtension do

  # NOTE: it's important to not create the objects through relation (user.projects.create...)
  #       it looks like active_record is caching the classes - but we need to test different class configs

  context "without calling validation" do

    let(:user) { User.create }
    let(:project) { Project.create user: user }
    let(:idea) { Idea.create user: user, project: project }
    let(:issue) { Issue.create user: user, project: project }
    let(:comment) { Comment.create user: user, issue: issue }
    # sti model
    let(:manager) { Manager.create user: user }
    let(:developer) { Developer.create user: user, boss: manager }

    it "uses same user ids by default" do
      expect(project.user_id).to eq(user.id)
      expect(idea.user_id).to eq(user.id)
    end

    it "allow to rewrite user id of idea" do
      idea.user_id = 42
      idea.save
      idea.reload
      expect(idea.user_id).to eq(42)
    end

    it "allow to rewrite user id of project" do
      project.user_id = 42
      project.save
      project.reload
      expect(project.user_id).to eq(42)
    end

    it "allow to rewrite user id of issue" do
      issue.user_id = 42
      issue.save
      issue.reload
      expect(issue.user_id).to eq(42)
    end

    it "allow to rewrite user id of comment" do
      comment.user_id = 42
      comment.save
      comment.reload
      expect(comment.user_id).to eq(42)
    end

    it "allow to rewrite user id of developer" do
      developer.user_id = 42
      developer.save
      developer.reload
      expect(developer.user_id).to eq(42)
    end

  end

  context "with calling validation" do
    before do
      Idea.send :validate_foreign_keys
      Project.send :validate_foreign_keys
      Issue.send :validate_foreign_keys
      Comment.send :validate_foreign_keys
      Member.send :validate_foreign_keys # sti model
    end

    let(:user) { User.create }
    let(:project) { Project.create user: user }
    let(:idea) { Idea.create user: user, project: project }
    let(:issue) { Issue.create user: user, project: project }
    let(:comment) { Comment.create user: user, issue: issue }
    # sti model
    let(:manager) { Manager.create user: user }
    let(:developer) { Developer.create user: user, boss: manager }

    it "uses same user ids by default" do
      expect(project.user_id).to eq(user.id)
      expect(idea.user_id).to eq(user.id)
    end

    it "does not allow to rewrite user id of idea" do
      idea.user_id = 42
      idea.save
      expect(idea.errors.messages.values.flatten).to include("user_id of project does not match ideas user_id")
      expect(idea.reload.user_id).to_not eq(42)
    end

    it "does not allow to rewrite user id of issue" do
      issue.user_id = 42
      issue.save
      expect(issue.errors.messages.values.flatten).to include("user_id of project does not match issues user_id")
      expect(issue.reload.user_id).to_not eq(42)
    end

    it "does not allow to rewrite user id of comment" do
{'fraction_non_alphanumeric': 0.09321745057941377, 'fraction_numerical': 0.008861622358554875, 'mean_word_length': 2.752557544757033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2612224', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1755, 'n_words': 570}