<commit_before>require 'spec_helper'

describe Embedson::Model do

  describe '#emdbeds_one' do
    context 'when only relation name is specified' do

      with_model :Parent do
        table do |t|
          t.json :embedded
        end

        model do
          extend Embedson::Model

          embeds_one :embedded
        end
      end

      class Embedded < OpenStruct

        def to_h
          { defined: 'in', embedded: true }
        end
      end

      let(:parent) { Parent.new() }
      let(:embedded) { Embedded.new() }

      it 'adds "embeds_one" method' do
        expect(Parent).to respond_to(:embeds_one)
      end

      it 'adds "embedded" method' do
        expect(Parent.new()).to respond_to(:embedded)
      end

      it 'adds "embedded=" method' do
        expect(Parent.new()).to respond_to("embedded=")
      end

      describe 'defined .embedded= method' do
        it 'assigns value of embedded class' do
          expect {
            parent.embedded = embedded
          }.to change{ parent.embedded }.from(nil).to(embedded)
        end

        context 'when assigning nil' do
          let(:parent) { Parent.new(embedded: embedded) }

          it 'removes assignmnent' do
            parent
            expect {
              parent.embedded = nil
            }.to change { parent.embedded }.from(embedded).to(nil)
          end
        end

        context 'when assigning wrong class' do
          it 'raises ClassTypeError' do
            expect{
              parent.embedded = 'something'
            }.to raise_error(Embedson::ClassTypeError)
          end
        end

        context 'when Parent is persisted' do
          before do
            parent.save!
          end

          it 'marks parent as changed' do
            expect {
              parent.embedded = embedded
            }.to change{ parent.changed? }.from(false).to(true)
          end

          context 'and saved after change' do
            it 'saves Embedded class to json column' do
              expect{
                parent.embedded = embedded
                parent.save!
              }.to change { parent.reload.read_attribute(:embedded) }.from(nil).to(embedded.to_h.stringify_keys)
            end
          end
        end
      end

      describe 'defined embedded method' do
        context 'when value column is null' do
          it 'returns nil' do
            expect(parent.embedded).to be nil
          end
        end

        it 'returns embedded class' do
          parent.embedded = embedded
          expect(parent.embedded).to be_a Embedded
        end
<commit_msg>small corrections in tests<commit_after>require 'spec_helper'

describe Embedson::Model do

  describe '#emdbeds_one' do
    context 'when only relation name is specified' do

      with_model :Parent do
        table do |t|
          t.json :embedded
        end

        model do
          extend Embedson::Model

          embeds_one :embedded
        end
      end

      class Embedded < OpenStruct

        def to_h
          { defined: 'in', embedded: true }
        end
      end

      let(:parent) { Parent.new() }
      let(:embedded) { Embedded.new() }

      it 'adds "embeds_one" method' do
        expect(Parent).to respond_to(:embeds_one)
      end

      it 'adds "embedded" method' do
        expect(parent).to respond_to(:embedded)
      end

      it 'adds "embedded=" method' do
        expect(parent).to respond_to("embedded=")
      end

      describe 'defined .embedded= method' do
        it 'assigns value of embedded class' do
          expect {
            parent.embedded = embedded
          }.to change{ parent.embedded }.from(nil).to(embedded)
        end

        context 'when assigning nil' do
          let(:parent) { Parent.new(embedded: embedded) }

          it 'removes assignmnent' do
            parent
            expect {
              parent.embedded = nil
            }.to change { parent.embedded }.from(embedded).to(nil)
          end
        end

        context 'when assigning wrong class' do
          it 'raises ClassTypeError' do
            expect{
              parent.embedded = 'something'
            }.to raise_error(Embedson::ClassTypeError)
          end
        end

        context 'when Parent is persisted' do
          before do
            parent.save!
          end

          it 'marks parent as changed' do
            expect {
              parent.embedded = embedded
            }.to change{ parent.changed? }.from(false).to(true)
          end

          context 'and saved after change' do
            it 'saves Embedded class in column' do
              expect{
                parent.embedded = embedded
                parent.save!
              }.to change { parent.reload.read_attribute(:embedded) }.from(nil).to(embedded.to_h.stringify_keys)
            end
          end
        end
      end

      describe 'defined .embedded method' do
        context 'when value column is null' do
          it 'returns nil' do
            expect(parent.embedded).to be nil
          end
        end

        it 'returns embedded class' do
          parent.embedded = embedded
          expect(parent.embedded).to be_a Embedded
        end

{'fraction_non_alphanumeric': 0.07942583732057416, 'fraction_numerical': 0.0, 'mean_word_length': 1.5718503937007875, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1047753', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1345, 'n_words': 428}