<commit_before>require 'spec_helper'

describe Address do
  before(:each) do
    @valid_attributes = {
      :house_number => 1,
      :street => "Penny Lane",
      :city => "San Francisco",
      :state => "CA",
      :zip => "94132",
      :country => 'USA'
    }
  end

  describe "with valid attributes" do

    it "should create a new instance given valid attributes" do
      lambda {
        Address.create(@valid_attributes)
      }.should change(Address, :count).by(1)
    end

    it "Canadian zip with letters and numbers should be accepted" do
      @valid_attributes[:zip] = "1N5 C6R"
      @valid_attributes[:country] = "Canada"
      lambda {
        Address.create(@valid_attributes)
      }.should change(Address, :count).by(1)
    end

  end

  describe "with invalid attributes" do
    # let's pretend that house numbers like 1A don't exist
    # if you have time you can go back and change this to checking
    # if house_number starts with a number
    it "should require a house number to be a number" do
      invalid_attributes = @valid_attributes.merge(:house_number => "a")
      a = Address.new(invalid_attributes)
      a.should_not be_valid
    end

    it "should require a street" do
      invalid_attributes = @valid_attributes
      invalid_attributes.delete(:street)
      a = Address.new(invalid_attributes)
      a.should_not be_valid
    end

    it "should require a city" do
      invalid_attributes = @valid_attributes
      invalid_attributes.delete(:city)
      a = Address.new(invalid_attributes)
      a.should_not be_valid
    end

    it "should require the state to be no longer than two characters" do
      invalid_attributes = @valid_attributes.merge(:state => "test")
      a = Address.new(invalid_attributes)
      a.should_not be_valid
    end

    it "should require the state to be longer than one character" do
      invalid_attributes = @valid_attributes.merge(:state => "a")
      a = Address.new(invalid_attributes)
      a.should_not be_valid
    end

    describe "missing country" do
      # Extra credit: Flesh out these specs.  The implementation code to make these tests pass requires a Rails
      # feature we haven't covered yet.  Hint: Look up before_validation and read about ActiveRecord callbacks in
      # http://api.rubyonrails.org
      it "should default the country to USA if missing"
      it "should leave the country unchanged if it's given"
    end
  end

end
<commit_msg>Flesh out address specs for defaulting country to USA if missing<commit_after>require 'spec_helper'

describe Address do
  before(:each) do
    @valid_attributes = {
      :house_number => 1,
      :street => "Penny Lane",
      :city => "San Francisco",
      :state => "CA",
      :zip => "94132",
      :country => 'USA'
    }
  end

  describe "with valid attributes" do

    it "should create a new instance given valid attributes" do
      lambda {
        Address.create(@valid_attributes)
      }.should change(Address, :count).by(1)
    end

    it "Canadian zip with letters and numbers should be accepted" do
      @valid_attributes[:zip] = "1N5 C6R"
      @valid_attributes[:country] = "Canada"
      lambda {
        Address.create(@valid_attributes)
      }.should change(Address, :count).by(1)
    end

  end

  describe "with invalid attributes" do
    # let's pretend that house numbers like 1A don't exist
    # if you have time you can go back and change this to checking
    # if house_number starts with a number
    it "should require a house number to be a number" do
      invalid_attributes = @valid_attributes.merge(:house_number => "a")
      a = Address.new(invalid_attributes)
      a.should_not be_valid
    end

    it "should require a street" do
      invalid_attributes = @valid_attributes
      invalid_attributes.delete(:street)
      a = Address.new(invalid_attributes)
      a.should_not be_valid
    end

    it "should require a city" do
      invalid_attributes = @valid_attributes
      invalid_attributes.delete(:city)
      a = Address.new(invalid_attributes)
      a.should_not be_valid
    end

    it "should require the state to be no longer than two characters" do
      invalid_attributes = @valid_attributes.merge(:state => "test")
      a = Address.new(invalid_attributes)
      a.should_not be_valid
    end

    it "should require the state to be longer than one character" do
      invalid_attributes = @valid_attributes.merge(:state => "a")
      a = Address.new(invalid_attributes)
      a.should_not be_valid
    end

    describe "missing country" do
      # Extra credit: Flesh out these specs.  The implementation code to make these tests pass requires a Rails
      # feature we haven't covered yet.  Hint: Look up before_validation and read about ActiveRecord callbacks in
      # http://api.rubyonrails.org
      it "should default the country to USA if missing" do
        @valid_attributes.delete(:country)
        @valid_attributes[:country].should be_nil
        addr = Address.create(@valid_attributes)
        addr.should_not be_new_record
        addr.country.should == 'USA'
      end
      it "should leave the country unchanged if it's given" do
        @valid_attributes[:country] = "Mexico"
        addr = Address.create(@valid_attributes)
        addr.should_not be_new_record
        addr.country.should == 'Mexico'        
      end
    end
  end

end

{'fraction_non_alphanumeric': 0.08076494615670256, 'fraction_numerical': 0.004455997029335313, 'mean_word_length': 2.823278921220724, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '4483401', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1480, 'n_words': 523}