unpack brazilian-rails

This commit is contained in:
2013-07-14 11:09:25 -04:00
parent 7d287fe530
commit e563725dc5
131 changed files with 5496 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
# -*- encoding : utf-8 -*-
require 'active_record'
module ActiveRecord
class BaseWithoutTable < Base
self.abstract_class = true
def create_or_update
errors.empty?
end
def save
self.valid?
end
class << self
def columns()
@columns ||= []
end
def column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
reset_column_information
end
# Do not reset @columns
def reset_column_information
generated_methods.each { |name| undef_method(name) }
@column_names = @columns_hash = @content_columns = @dynamic_methods_hash = @read_methods = nil
end
end
end
end

View File

@@ -0,0 +1,114 @@
# -*- encoding : utf-8 -*-
require File.dirname(__FILE__) + '/spec_helper'
require File.dirname(__FILE__) + '/active_record/base_without_table'
class Empresa < ActiveRecord::Base
usar_como_cnpj :cnpj
end
describe "Using a model attribute as Cnpj" do
before(:each) do
@company = Empresa.new
end
it "should format the received number" do
@company.cnpj = "69103604000160"
@company.cnpj.numero.should == "69.103.604/0001-60"
end
it "should respond to cnpj_valido?" do
@company.respond_to?('cnpj_valido?').should be_true
end
it "should be invalid with an invalid cnpj number" do
@company.cnpj = "123545"
@company.should_not be_valid
end
it "should be invalid with a too long number" do
@company.cnpj = "12323456678654454"
@company.should_not be_valid
end
it "should not save the instance with an invalid cnpj" do
@company.cnpj = "sdwewe"
@company.save.should be_false
end
it "should have an error in the cnpj field when invalid" do
@company.cnpj = "232df"
@company.save
@company.errors[:cnpj].should == ["is invalid"]
end
it "should be valid with a null cnpj number" do
@company.cnpj = nil
@company.should be_valid
end
it "should be valid with an empty string in the constructor of an instance of cnpj" do
@company.cnpj = Cnpj.new("")
@company.should be_valid
end
it "should be valid with an empty string as the cnpj number" do
@company.cnpj = ""
@company.should be_valid
end
it "should accept an instance of Cnpj" do
@company.cnpj = Cnpj.new("69103604000160")
@company.cnpj.should be_instance_of(Cnpj)
end
it "should be able to receive parameters at initialization" do
@company = Empresa.new(:cnpj => "69103604000160")
@company.cnpj.numero.should == "69.103.604/0001-60"
end
end
describe "when validating" do
before do
Empresa.validates_presence_of :cnpj
end
describe "presence" do
it "should be invalid with a new cnpj number" do
e = Empresa.new(:nome => "Bla")
e.should_not be_valid
e.errors[:cnpj].should eql(["can't be blank"])
end
it "should be invalid using an empty string as the cnpj number" do
e = Empresa.new(:nome => "Bla", :cnpj => "")
e.should_not be_valid
e.errors[:cnpj].should_not be_empty
end
it "should be valid with a cnpj" do
Empresa.new(:nome => "Bla", :cnpj => "00012345000165").should be_valid
end
end
describe "uniqueness" do
before(:each) do
Empresa.validates_uniqueness_of :cnpj
@e1 = Empresa.new(:nome => "Bla", :cnpj => "69103604000160")
@e1.save
end
it "should validate uniqueness of cnpj" do
e2 = Empresa.new(:nome => "Ble", :cnpj => "69103604000160")
e2.should_not be_valid
e2.errors[:cnpj].should_not be_empty
end
it "should be valid using a new cnpj" do
e2 = Empresa.new(:nome => "Ble", :cnpj => "00012345000165")
e2.should be_valid
end
end
end

View File

@@ -0,0 +1,51 @@
# -*- encoding : utf-8 -*-
require File.dirname(__FILE__) + '/spec_helper'
describe Cnpj do
it "should be invalid with malformed number" do
numeros = %w(04.22A.284/0001-11 04.222-284.0001-11 04222/284/0001-11)
numeros.each do |n|
cnpj = Cnpj.new(n)
cnpj.should_not be_valido
end
end
it "should be invalid with invalid number" do
numeros = %w(69103604020160 00000000000000 69.103.604/0001-61 01618211000264)
numeros.each do |n|
cnpj = Cnpj.new(n)
cnpj.should_not be_valido
end
end
it "should be invalid with a number longer than 14 chars, even if the first 14 represent a valid number" do
%w(691036040001-601 69103604000160a 69103604000160ABC 6910360400016000).each do |n|
Cnpj.new(n).should_not be_valido
end
end
it "should be valid with correct number" do
numeros = %w(69103604000160 69.103.604/0001-60 01518211/000264 01.5182110002-64 00.000.000/1447-89)
numeros.each do |n|
cnpj = Cnpj.new(n)
cnpj.should be_valido
end
end
it "should return the formated cnpj" do
cnpj = Cnpj.new("69103604000160")
cnpj.to_s.should == "69.103.604/0001-60"
end
it "should format the received number at instantiation" do
cnpj = Cnpj.new("69103604000160")
cnpj.numero.should == "69.103.604/0001-60"
end
it "should be equal to another instance with the same number" do
Cnpj.new("69103604000160").should == Cnpj.new("69.103.604/0001-60")
end
end

View File

@@ -0,0 +1,22 @@
require File.dirname(__FILE__) + '/spec_helper'
require File.dirname(__FILE__) + '/active_record/base_without_table'
class Company < ActiveRecord::Base
validates :cnpj, :cnpj => true
end
describe CnpjValidator do
it "should isn't valid when the cnpj isn't valid" do
@empresa = Company.new(:cnpj => "12345")
@empresa.valid?.should be_false
@empresa.errors[:cnpj].should == ["is invalid"]
end
it "should accept a nil cnpj" do
@empresa = Company.new(:cnpj => nil)
@empresa.valid?.should be_true
end
it "should be valid with a valid CNPJ" do
@empresa = Company.new(:cnpj => "69103604000160")
@empresa.valid?.should be_true
end
end

View File

@@ -0,0 +1,120 @@
# -*- encoding : utf-8 -*-
require File.dirname(__FILE__) + '/spec_helper'
require File.dirname(__FILE__) + '/active_record/base_without_table'
class Pessoa < ActiveRecord::Base
usar_como_cpf :cpf
end
describe "Using a model attribute as Cpf" do
before(:each) do
@person = Pessoa.new(:nome => "Fulano")
end
it "should format the received number" do
@person.cpf = "11144477735"
@person.cpf.numero.should == "111.444.777-35"
end
it "should respond to cpf_valido?" do
@person.respond_to?('cpf_valido?').should be_true
end
it "should be invalid with an invalid cpf number" do
@person.cpf = "123545"
@person.should_not be_valid
end
it "should be invalid with a too long number" do
@person.cpf = "123456678654454"
@person.should_not be_valid
end
it "should be valid with an empty string in the constructor of an instance of Cpf" do
@person.cpf = Cpf.new("")
@person.should be_valid
end
it "should be valid with an empty string as the cpf number" do
@person.cpf = ""
@person.should be_valid
end
it "should not save the instance with an invalid cpf" do
@person.cpf = "sdwewe"
@person.save.should be_false
end
it "should have an error in the cpf field when invalid" do
@person.cpf = "232df"
@person.save.should be_false
@person.errors[:cpf].should == ["is invalid"]
end
it "should be valid with a null cpf number" do
@person.cpf = nil
@person.should be_valid
end
it "should accept an instance of Cpf" do
@person.cpf = Cpf.new("11144477735")
@person.cpf.should be_instance_of(Cpf)
end
it "should be able to receive parameters at initialization" do
@person = Pessoa.new(:cpf => "111.44477735")
@person.cpf.numero.should == "111.444.777-35"
end
it "should change the current attribute's value" do
@person.cpf = Cpf.new("13434")
lambda {
@person.cpf = Cpf.new("111.444.777-35")
}.should change(@person, :cpf)
end
end
describe "when validating" do
describe "presence" do
before do
Pessoa.validates_presence_of :cpf
end
it "should be invalid with a nil cpf number" do
p = Pessoa.new(:nome => "Fulano", :cpf => nil)
p.should_not be_valid
p.errors[:cpf].should eql(["can't be blank"])
end
it "should be invalid with an empty string as the cpf number" do
p = Pessoa.new(:nome => "Fulano", :cpf => "")
p.should_not be_valid
p.errors[:cpf].should_not be_empty
end
it "should be valid with a cpf" do
Pessoa.new(:nome => "Fulano", :cpf => "11144477735").should be_valid
end
end
describe "uniqueness" do
before(:each) do
Pessoa.validates_uniqueness_of :cpf
@p1 = Pessoa.new(:nome => "Beltrano", :cpf => "11144477735")
@p1.save
end
it "should validate uniqueness of cpf" do
p2 = Pessoa.new(:nome => "Beltrano", :cpf => "11144477735")
p2.should_not be_valid
p2.errors[:cpf].should_not be_empty
end
it "should be valid using a new cpf" do
p2 = Pessoa.new(:nome => "Fulano", :cpf => "00123456797")
p2.should be_valid
end
end
end

View File

@@ -0,0 +1,50 @@
# -*- encoding : utf-8 -*-
require File.dirname(__FILE__) + '/spec_helper'
describe Cpf do
it "should be invalid with malformed number" do
numeros = %w(345.65.67.3 567.765-87698 345456-654-01 123456)
numeros.each do |n|
cpf = Cpf.new(n)
cpf.should_not be_valido
end
end
it "should be invalid with invalid number" do
numeros = %w(23342345699 34.543.567-98 456.676456-87 333333333-33 00000000000 000.000.000-00)
numeros.each do |n|
cpf = Cpf.new(n)
cpf.should_not be_valido
end
end
it "should be valid with correct number" do
numeros = %w(111.444.777-35 11144477735 111.444777-35 111444.777-35 111.444.77735)
numeros.each do |n|
cpf = Cpf.new(n)
cpf.should be_valido
end
end
it "should be invalid with a number longer than 11 chars, even if the first 11 char represent a valid cpf number" do
%w(111.444.777-3500 11144477735AB).each do |n|
Cpf.new(n).should_not be_valido
end
end
it "should return the formated cpf" do
cpf = Cpf.new("11144477735")
cpf.to_s.should == "111.444.777-35"
end
it "should format the received number at instantiation" do
cpf = Cpf.new("11144477735")
cpf.numero.should == "111.444.777-35"
end
it "should be equal to another instance with the same number" do
Cpf.new("11144477735").should == Cpf.new("111.444.777-35")
end
end

View File

@@ -0,0 +1,21 @@
require File.dirname(__FILE__) + '/spec_helper'
require File.dirname(__FILE__) + '/active_record/base_without_table'
class Person < ActiveRecord::Base
validates :cpf, :cpf => true
end
describe CpfValidator do
it "should isn't valid when the cpf isn't valid" do
@pessoa = Person.new(:cpf => "12345")
@pessoa.valid?.should be_false
@pessoa.errors[:cpf].should == ["is invalid"]
end
it "should accept a nil cpf" do
@pessoa = Person.new(:cpf => nil)
@pessoa.valid?.should be_true
end
it "should be valid with a valid cpf" do
@pessoa = Person.new(:cpf => "11144477735")
@pessoa.valid?.should be_true
end
end

View File

@@ -0,0 +1,29 @@
# -*- encoding : utf-8 -*-
class CreateTestingStructure < ActiveRecord::Migration
def self.up
create_table :empresas do |t|
t.string :nome
t.string :cnpj
end
create_table :pessoas do |t|
t.string :nome
t.string :cpf
end
create_table :companies do |t|
t.string :nome
t.string :cnpj
end
create_table :people do |t|
t.string :nome
t.string :cpf
end
end
def self.down
drop_table :pessoas
drop_table :empresas
drop_table :companies
drop_table :people
end
end

View File

@@ -0,0 +1,11 @@
# -*- encoding : utf-8 -*-
require "rubygems"
require "rspec"
require "active_record"
require File.expand_path(File.dirname(__FILE__) + "/../lib/brcpfcnpj")
ActiveRecord::Base.establish_connection(:adapter=>"sqlite3", :database => ":memory:")
require File.dirname(__FILE__) + "/db/create_testing_structure"
CreateTestingStructure.migrate(:up)