<commit_before># encoding: utf-8

require 'spec_helper'

module ConstructorPages
  describe Field do
    before :each do
      Template.delete_all
      @template = Template.create name: 'Page', code_name: 'page'

      #Field.delete_all
      #Field::TYPES.each do |t|
      #  "constructor_pages/types/#{t}_type".classify.constantize.delete_all
      #end
    end

    describe '.create' do
      #it 'should be valid' do
       # _field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
       # _field.should be_valid
      #end

      it 'should create page fields' do
        #_field = Field.create name: 'Price', code_name: 'price', template: @template, type_value: 'text'

        @template.should_not be_nil
        Page.new name: 'hello world'#, template: @template
        #_page.save.should be_true
        #_page = Page.create name: 'Page'#, template_id: @template.id
        #_page.template_id.should_not be_nil
        #_page.price.should == ''
      end
    end

    describe '#destroy' do
      it 'should destroy page fields' do
        _field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        _page = Page.create name: 'Page', template: @template

        _page.content.should == ''
        _field.destroy
        _page.content.should be_nil
      end
    end

    describe '#type_class' do
      it 'should return constant of type_value' do
        field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'html'
        field.type_class.should == Types::HtmlType

        field.type_value = 'text'
        field.type_class.should == Types::TextType
      end
    end

    describe '#find_type_object' do
      it 'should find one object of type_value by page' do
        field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        page = Page.create name: 'Page', template: @template

        text_type = Types::TextType.find_by_field_id_and_page_id field.id, page.id

        field.find_type_object(page).should == text_type
      end
    end

    describe '#create_type_object' do
      it 'should create object of type_value by page' do
        field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        page = Page.create name: 'Page', template: @template

        text_type = field.create_type_object(page)
        text_type.should be_an_instance_of(Types::TextType)
        text_type.page_id.should == page.id
        text_type.field_id.should == field.id
      end
    end

    describe '#remove_type_object' do
      it 'should remote object of type_value by page' do
        field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        page = Page.create name: 'Page', template: @template

        field.find_type_object(page).should be_an_instance_of(Types::TextType)

        field.remove_type_object(page)
        field.find_type_object(page).should be_nil
      end
    end

    describe '#get_value_for' do
      it 'should get value of type field by page' do
        field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        page = Page.create name: 'Page', template: @template
        page.content = 'Hello world'

        field.get_value_for(page).should == 'Hello world'
      end
    end

    describe '#set_value_for' do
      it 'should set value for type field by page' do
        field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        page = Page.create name: 'Page', template: @template

        field.set_value_for(page, 'Hello')
        page.content.should == 'Hello'
      end
    end

    describe '#code_name' do
      it 'should be uniqueness in scope fields' do
        Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        _field = Field.create name: 'Content 2', code_name: 'content', template: @template, type_value: 'html'

        _field.should_not be_valid
      end

      it 'should be uniqueness in scope Page' do
        Page.create name: 'Page', template: @template
        _field = Field.create name: 'Content', code_name: 'get_field_value', template: @template, type_value: 'text'
        _field.should_not be_valid
      end

      it 'should be uniqueness in scope Template branch' do
        Page.create name: 'Page', template: @template

        _template = Template.create name: 'Brand', code_name: 'brand', parent: @template
        @template.reload

        _field = Field.create name: 'Content', code_name: 'brand', template: @template, type_value: 'text'
        _field.should_not be_valid

        _field.code_name = 'brands'
        _field.should_not be_valid

        _field.code_name = 'price'
        _field.should be_valid
      end
    end
  end
end<commit_msg>Revert commented specs<commit_after># encoding: utf-8

require 'spec_helper'

module ConstructorPages
  describe Field do
    before :each do
      Template.delete_all
      @template = Template.create name: 'Page', code_name: 'page'

      Field.delete_all
      Field::TYPES.each do |t|
        "constructor_pages/types/#{t}_type".classify.constantize.delete_all
      end
    end

    describe '.create' do
      it 'should be valid' do
        _field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        _field.should be_valid
      end

      it 'should create page fields' do
        _field = Field.create name: 'Price', code_name: 'price', template: @template, type_value: 'text'

        _page = Page.create name: 'Page', template_id: @template.id
        _page.price.should == ''
      end
    end

    describe '#destroy' do
      it 'should destroy page fields' do
        _field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        _page = Page.create name: 'Page', template: @template

        _page.content.should == ''
        _field.destroy
        _page.content.should be_nil
      end
    end

    describe '#type_class' do
      it 'should return constant of type_value' do
        field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'html'
        field.type_class.should == Types::HtmlType

        field.type_value = 'text'
        field.type_class.should == Types::TextType
      end
    end

    describe '#find_type_object' do
      it 'should find one object of type_value by page' do
        field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        page = Page.create name: 'Page', template: @template

        text_type = Types::TextType.find_by_field_id_and_page_id field.id, page.id

        field.find_type_object(page).should == text_type
      end
    end

    describe '#create_type_object' do
      it 'should create object of type_value by page' do
        field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        page = Page.create name: 'Page', template: @template

        text_type = field.create_type_object(page)
        text_type.should be_an_instance_of(Types::TextType)
        text_type.page_id.should == page.id
        text_type.field_id.should == field.id
      end
    end

    describe '#remove_type_object' do
      it 'should remote object of type_value by page' do
        field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        page = Page.create name: 'Page', template: @template

        field.find_type_object(page).should be_an_instance_of(Types::TextType)

        field.remove_type_object(page)
        field.find_type_object(page).should be_nil
      end
    end

    describe '#get_value_for' do
      it 'should get value of type field by page' do
        field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        page = Page.create name: 'Page', template: @template
        page.content = 'Hello world'

        field.get_value_for(page).should == 'Hello world'
      end
    end

    describe '#set_value_for' do
      it 'should set value for type field by page' do
        field = Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        page = Page.create name: 'Page', template: @template

        field.set_value_for(page, 'Hello')
        page.content.should == 'Hello'
      end
    end

    describe '#code_name' do
      it 'should be uniqueness in scope fields' do
        Field.create name: 'Content', code_name: 'content', template: @template, type_value: 'text'
        _field = Field.create name: 'Content 2', code_name: 'content', template: @template, type_value: 'html'

        _field.should_not be_valid
      end

      it 'should be uniqueness in scope Page' do
        Page.create name: 'Page', template: @template
        _field = Field.create name: 'Content', code_name: 'get_field_value', template: @template, type_value: 'text'
        _field.should_not be_valid
      end

      it 'should be uniqueness in scope Template branch' do
        Page.create name: 'Page', template: @template

        _template = Template.create name: 'Brand', code_name: 'brand', parent: @template
        @template.reload

        _field = Field.create name: 'Content', code_name: 'brand', template: @template, type_value: 'text'
        _field.should_not be_valid

        _field.code_name = 'brands'
        _field.should_not be_valid

        _field.code_name = 'price'
        _field.should be_valid
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.1043460434604346, 'fraction_numerical': 0.0004100041000410004, 'mean_word_length': 2.8217783000391696, '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': '4819947', 'n_tokens_mistral': 2932, 'n_tokens_neox': 2864, 'n_words': 855}