<commit_before>require 'spec_helper'

describe Spree::Address do
  context "validations" do
    it { should belong_to(:country) }
    it { should belong_to(:state) }
    it { should have_many(:shipments) }
    it { should validate_presence_of(:firstname) }
    it { should validate_presence_of(:lastname) }
    it { should validate_presence_of(:address1) }
    it { should validate_presence_of(:city) }
    it { should validate_presence_of(:zipcode) }
    it { should validate_presence_of(:country) }
    it { should validate_presence_of(:phone) }
  end

  context "validation" do
    let(:state) { Factory(:state, :name => 'maryland', :abbr => 'md') }
    before  { Spree::Config.stub(:get).with(:address_requires_state).and_return(true) }

    context "state_name is not nil and country does not have any states" do
      let(:address) { Factory(:address, :state => nil, :state_name => 'alabama')}
      specify { address.new_record?.should be_false }
    end

    context "state_name is nil" do
      let(:address) { Factory.build(:address, :state => nil, :state_name => nil, :country => state.country)}
      before { address.save }
      specify { address.errors.full_messages.first.should == "State can't be blank" }
    end

    context "full state name is in state_name and country does contain that state" do
      let(:address) { Factory(:address, :state => nil, :state_name => 'maryland', :country => state.country)}
      before do
        Spree::State.delete_all
        Spree::Country.delete_all
        @state = Factory(:state)
        @address = Factory(:address, :state => nil, :state_name => @state.name, :country => @state.country)
      end
      specify do
        address.should be_valid
        address.state_id.should_not be_nil
        address.state_name.should be_nil
      end
    end

    context "state abbr is in state_name and country does contain that state" do
        before do
          Spree::State.delete_all
          Spree::Country.delete_all
          @state = Factory(:state)
          @address = Factory(:address, :state => nil, :state_name => @state.abbr, :country => @state.country)
        end
      specify do
        @address.should be_valid
        @address.state_id.should_not be_nil
        @address.state_name.should be_nil
      end
    end

    context "state is entered but country does not contain that state" do
      let(:address) { Factory.build(:address, :state => state, :country => Factory(:country))}
      before { address.save }

      specify { address.errors.full_messages.first.should == 'State is invalid' }
    end

    context "both state and state_name are entered but country does not contain the state" do
      let(:address) { Factory(:address, :state => state, :state_name => 'maryland', :country => Factory(:country))}
      specify do
        address.should be_valid
        address.state_id.should be_nil
      end
    end

    context "both state and state_name are entered and country does contain the state" do
      let(:address) { Factory(:address, :state => state, :state_name => 'maryland', :country => state.country)}
      specify do
        address.should be_valid
        address.state_name.should be_nil
      end
    end

    context "address_requires_state preference is false" do
      pending "need to fix config settings for specs"

      before { Spree::Config.set :address_requires_state => false }

      #let(:address) { Factory(:address, :state => nil, :state_name => nil) }
      #specify { address.should be_valid }
    end

  end

  context '#full_name' do
    let(:address) { Factory(:address, :firstname => 'Michael', :lastname => 'Jackson') }
    specify { address.full_name.should == '<NAME>' }
  end

  context '#state_text' do
    context 'state is blank' do
      let(:address) { Factory(:address, :state => nil, :state_name => 'virginia') }<commit_msg>Remove pending line and comment symbols from address test for address_requires_state config value<commit_after>require 'spec_helper'

describe Spree::Address do
  context "validations" do
    it { should belong_to(:country) }
    it { should belong_to(:state) }
    it { should have_many(:shipments) }
    it { should validate_presence_of(:firstname) }
    it { should validate_presence_of(:lastname) }
    it { should validate_presence_of(:address1) }
    it { should validate_presence_of(:city) }
    it { should validate_presence_of(:zipcode) }
    it { should validate_presence_of(:country) }
    it { should validate_presence_of(:phone) }
  end

  context "validation" do
    let(:state) { Factory(:state, :name => 'maryland', :abbr => 'md') }
    before  { Spree::Config.stub(:get).with(:address_requires_state).and_return(true) }

    context "state_name is not nil and country does not have any states" do
      let(:address) { Factory(:address, :state => nil, :state_name => 'alabama')}
      specify { address.new_record?.should be_false }
    end

    context "state_name is nil" do
      let(:address) { Factory.build(:address, :state => nil, :state_name => nil, :country => state.country)}
      before { address.save }
      specify { address.errors.full_messages.first.should == "State can't be blank" }
    end

    context "full state name is in state_name and country does contain that state" do
      let(:address) { Factory(:address, :state => nil, :state_name => 'maryland', :country => state.country)}
      before do
        Spree::State.delete_all
        Spree::Country.delete_all
        @state = Factory(:state)
        @address = Factory(:address, :state => nil, :state_name => @state.name, :country => @state.country)
      end
      specify do
        address.should be_valid
        address.state_id.should_not be_nil
        address.state_name.should be_nil
      end
    end

    context "state abbr is in state_name and country does contain that state" do
        before do
          Spree::State.delete_all
          Spree::Country.delete_all
          @state = Factory(:state)
          @address = Factory(:address, :state => nil, :state_name => @state.abbr, :country => @state.country)
        end
      specify do
        @address.should be_valid
        @address.state_id.should_not be_nil
        @address.state_name.should be_nil
      end
    end

    context "state is entered but country does not contain that state" do
      let(:address) { Factory.build(:address, :state => state, :country => Factory(:country))}
      before { address.save }

      specify { address.errors.full_messages.first.should == 'State is invalid' }
    end

    context "both state and state_name are entered but country does not contain the state" do
      let(:address) { Factory(:address, :state => state, :state_name => 'maryland', :country => Factory(:country))}
      specify do
        address.should be_valid
        address.state_id.should be_nil
      end
    end

    context "both state and state_name are entered and country does contain the state" do
      let(:address) { Factory(:address, :state => state, :state_name => 'maryland', :country => state.country)}
      specify do
        address.should be_valid
        address.state_name.should be_nil
      end
    end

    context "address_requires_state preference is false" do

      before { Spree::Config.set :address_requires_state => false }

      let(:address) { Factory(:address, :state => nil, :state_name => nil) }
      specify { address.should be_valid }
    end

  end

  context '#full_name' do
    let(:address) { Factory(:address, :firstname => 'Michael', :lastname => 'Jackson') }
    specify { address.full_name.should == '<NAME>' }
  end

  context '#state_text' do
    context 'state is blank' do
      let(:address) { Factory(:address, :state => nil, :state_name => 'virginia') }
{'fraction_non_alphanumeric': 0.1142636058808357, 'fraction_numerical': 0.00025793139025019347, 'mean_word_length': 3.131592967501332, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, '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': '4916346', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2242, 'n_words': 650}