<commit_before>require 'spec_helper'

def save_and_expect_rel!
  proc do
    chris.save
    reloaded_chris = Student.find_by(chris.props)
    expect(reloaded_chris.lessons.include?(math)).to be_truthy
  end
end

describe 'association creation' do
  before do
    clear_model_memory_caches

    stub_active_node_class 'Student' do
      property :name
      has_many :out, :lessons, type: 'ENROLLED_IN'
      has_one :out, :favorite_class, type: 'FAVORITE_CLASS', model_class: 'Lesson'
    end

    stub_active_node_class 'Lesson' do
      property :subject
      validates_presence_of :subject
      has_many :in, :students, origin: :lesson
    end
  end

  before { [Student, Lesson].each(&:delete_all) }

  describe 'has_one' do
    context 'with persisted nodes' do
      let(:chris) { Student.create(name: 'Chris') }
      let(:math)  { Lesson.create(subject: 'Math') }

      it 'creates the relationship' do
        expect { chris.favorite_class = math }.to change { chris.favorite_class }
      end

      it 'creates the relationship by id' do
        expect { chris.favorite_class_id = math.id }.to change { chris.favorite_class }
      end
    end

    context 'assigning via #new method arguments' do
      let!(:math)  { Lesson.new(subject: 'Math') }
      let!(:chris) do
        Student.new(name: 'Chris', favorite_class: math)
      end

      it 'returns the node' do
        expect(chris.favorite_class).to eq(math)
      end

      context 'upon save...' do
        before { chris.save }

        it 'returns the node' do
          expect(chris.favorite_class).to eq(math)
        end
      end
    end

    context 'between two unpersisted nodes' do
      let!(:chris) { Student.new(name: 'Chris') }
      let!(:math)  { Lesson.new(subject: 'Math') }

      it 'does not raise an error' do
        expect { chris.favorite_class = math }.not_to raise_error
      end

      it 'is aware that there are pending associations' do
        expect { chris.favorite_class = math }.to change { chris.pending_deferred_creations? }
      end

      context 'upon save...' do
        before do
          chris.favorite_class = math<commit_msg>Failing spec for previous fix<commit_after>require 'spec_helper'

def save_and_expect_rel!
  proc do
    chris.save
    reloaded_chris = Student.find_by(chris.props)
    expect(reloaded_chris.lessons.include?(math)).to be_truthy
  end
end

describe 'association creation' do
  before do
    clear_model_memory_caches

    stub_active_node_class 'Student' do
      property :name
      has_many :out, :lessons, type: 'ENROLLED_IN'
      has_one :out, :favorite_class, type: 'FAVORITE_CLASS', model_class: 'Lesson'
    end

    stub_active_node_class 'Lesson' do
      property :subject
      validates_presence_of :subject
      has_many :in, :students, origin: :lesson
    end
  end

  before { [Student, Lesson].each(&:delete_all) }

  describe 'has_one' do
    context 'with persisted nodes' do
      let(:chris) { Student.create(name: 'Chris') }
      let(:math)  { Lesson.create(subject: 'Math') }

      it 'creates the relationship' do
        expect { chris.favorite_class = math }.to change { chris.favorite_class }
      end

      it 'creates the relationship by id' do
        expect { chris.favorite_class_id = math.id }.to change { chris.favorite_class }
      end
    end

    context 'assigning via #new method arguments' do
      let!(:math)  { Lesson.new(subject: 'Math') }
      let!(:chris) do
        Student.new(name: 'Chris', favorite_class: math)
      end

      it 'returns the node' do
        expect(chris.favorite_class).to eq(math)
      end

      context 'upon save...' do
        before { chris.save }

        it 'returns the node' do
          expect(chris.favorite_class).to eq(math)
          expect(chris.query_as(:c).match('(c)-[rel:FAVORITE_CLASS]-()').count(:rel)).to eq(1)
        end
      end
    end

    context 'between two unpersisted nodes' do
      let!(:chris) { Student.new(name: 'Chris') }
      let!(:math)  { Lesson.new(subject: 'Math') }

      it 'does not raise an error' do
        expect { chris.favorite_class = math }.not_to raise_error
      end

      it 'is aware that there are pending associations' do
        expect { chris.favorite_class = math }.to change { chris.pending_deferred_creations? }
      end

      context 'upon save...' do
        before do
          chris.favorite_class = math
{'fraction_non_alphanumeric': 0.10224438902743142, 'fraction_numerical': 0.00022670596236681024, 'mean_word_length': 2.6614107883817426, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4998852', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1432, 'n_words': 377}