Skip to content

Commit

Permalink
beneficiary: refactor unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atonks2 committed Oct 27, 2020
1 parent 5f6ed9c commit c79f7c2
Showing 1 changed file with 61 additions and 70 deletions.
131 changes: 61 additions & 70 deletions beneficiary_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package wire

import (
"github.com/moov-io/base"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

// mockBeneficiary creates a Beneficiary
Expand All @@ -21,144 +22,134 @@ func mockBeneficiary() *Beneficiary {
// TestMockBeneficiary validates mockBeneficiary
func TestMockBeneficiary(t *testing.T) {
ben := mockBeneficiary()
if err := ben.Validate(); err != nil {
t.Error("mockBeneficiary does not validate and will break other tests")
}

require.NoError(t, ben.Validate(), "mockBeneficiary does not validate and will break other tests")
}

// TestBeneficiaryIdentificationCodeValid validates Beneficiary IdentificationCode
func TestBeneficiaryIdentificationCodeValid(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.IdentificationCode = "Baseball Card ID"
if err := ben.Validate(); err != nil {
if !base.Match(err, ErrIdentificationCode) {
t.Errorf("%T: %s", err, err)
}
}

err := ben.Validate()

require.NotNil(t, err)
require.Equal(t, fieldError("IdentificationCode", ErrIdentificationCode, ben.Personal.IdentificationCode).Error(), err.Error())
}

// TestBeneficiaryIdentifierAlphaNumeric validates Beneficiary Identifier is alphanumeric
func TestBeneficiaryIdentifierAlphaNumeric(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.Identifier = "®"
if err := ben.Validate(); err != nil {
if !base.Match(err, ErrNonAlphanumeric) {
t.Errorf("%T: %s", err, err)
}
}

err := ben.Validate()

require.NotNil(t, err)
require.Equal(t, fieldError("Identifier", ErrNonAlphanumeric, ben.Personal.Identifier).Error(), err.Error())
}

// TestBeneficiaryNameAlphaNumeric validates Beneficiary Name is alphanumeric
func TestBeneficiaryNameAlphaNumeric(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.Name = "®"
if err := ben.Validate(); err != nil {
if !base.Match(err, ErrNonAlphanumeric) {
t.Errorf("%T: %s", err, err)
}
}

err := ben.Validate()

require.NotNil(t, err)
require.Equal(t, fieldError("Name", ErrNonAlphanumeric, ben.Personal.Name).Error(), err.Error())
}

// TestBeneficiaryAddressLineOneAlphaNumeric validates Beneficiary AddressLineOne is alphanumeric
func TestBeneficiaryAddressLineOneAlphaNumeric(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.Address.AddressLineOne = "®"
if err := ben.Validate(); err != nil {
if !base.Match(err, ErrNonAlphanumeric) {
t.Errorf("%T: %s", err, err)
}
}

err := ben.Validate()

require.NotNil(t, err)
require.Equal(t, fieldError("AddressLineOne", ErrNonAlphanumeric, ben.Personal.Address.AddressLineOne).Error(), err.Error())
}

// TestBeneficiaryAddressLineTwoAlphaNumeric validates Beneficiary AddressLineTwo is alphanumeric
func TestBeneficiaryAddressLineTwoAlphaNumeric(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.Address.AddressLineTwo = "®"
if err := ben.Validate(); err != nil {
if !base.Match(err, ErrNonAlphanumeric) {
t.Errorf("%T: %s", err, err)
}
}

err := ben.Validate()

require.NotNil(t, err)
require.Equal(t, fieldError("AddressLineTwo", ErrNonAlphanumeric, ben.Personal.Address.AddressLineTwo).Error(), err.Error())
}

// TestBeneficiaryAddressLineThreeAlphaNumeric validates Beneficiary AddressLineThree is alphanumeric
func TestBeneficiaryAddressLineThreeAlphaNumeric(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.Address.AddressLineThree = "®"
if err := ben.Validate(); err != nil {
if !base.Match(err, ErrNonAlphanumeric) {
t.Errorf("%T: %s", err, err)
}
}

err := ben.Validate()

require.NotNil(t, err)
require.Equal(t, fieldError("AddressLineThree", ErrNonAlphanumeric, ben.Personal.Address.AddressLineThree).Error(), err.Error())
}

// TestBeneficiaryIdentificationCodeRequired validates Beneficiary IdentificationCode is required
func TestBeneficiaryIdentificationCodeRequired(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.IdentificationCode = ""
if err := ben.Validate(); err != nil {
if !base.Match(err, ErrFieldRequired) {
t.Errorf("%T: %s", err, err)
}
}

err := ben.Validate()

require.NotNil(t, err)
require.Equal(t, fieldError("IdentificationCode", ErrFieldRequired).Error(), err.Error())
}

// TestBeneficiaryIdentifierRequired validates Beneficiary Identifier is required
func TestBeneficiaryIdentifierRequired(t *testing.T) {
ben := mockBeneficiary()
ben.Personal.Identifier = ""
if err := ben.Validate(); err != nil {
if !base.Match(err, ErrFieldRequired) {
t.Errorf("%T: %s", err, err)
}
}

err := ben.Validate()

require.NotNil(t, err)
require.Equal(t, fieldError("Identifier", ErrFieldRequired).Error(), err.Error())
}

// TestParseBeneficiaryWrongLength parses a wrong Beneficiary record length
func TestParseBeneficiaryWrongLength(t *testing.T) {
var line = "{4200}31234 Name Address One Address Two Address Three "
r := NewReader(strings.NewReader(line))
r.line = line
fwm := new(FEDWireMessage)
ben := mockBeneficiary()
fwm.SetBeneficiary(ben)

err := r.parseBeneficiary()
if err != nil {
if !base.Match(err, NewTagWrongLengthErr(181, len(r.line))) {
t.Errorf("%T: %s", err, err)
}
}

require.NotNil(t, err)
require.Contains(t, err.Error(), NewTagWrongLengthErr(181, len(r.line)).Error())
}

// TestParseBeneficiaryReaderParseError parses a wrong Beneficiary reader parse error
func TestParseBeneficiaryReaderParseError(t *testing.T) {
var line = "{4200}31234 Na®e Address One Address Two Address Three "
r := NewReader(strings.NewReader(line))
r.line = line
fwm := new(FEDWireMessage)
ben := mockBeneficiary()
fwm.SetBeneficiary(ben)

err := r.parseBeneficiary()
if err != nil {
if !base.Match(err, ErrNonAlphanumeric) {
t.Errorf("%T: %s", err, err)
}
}

require.NotNil(t, err)
require.Contains(t, err.Error(), ErrNonAlphanumeric.Error())

_, err = r.Read()
if err != nil {
if !base.Has(err, ErrNonAlphanumeric) {
t.Errorf("%T: %s", err, err)
}
}

require.NotNil(t, err)
require.Contains(t, err.Error(), ErrNonAlphanumeric.Error())
}

// TestBeneficiaryTagError validates Beneficiary tag
func TestBeneficiaryTagError(t *testing.T) {
ben := mockBeneficiary()
ben.tag = "{9999}"
if err := ben.Validate(); err != nil {
if !base.Match(err, ErrValidTagForType) {
t.Errorf("%T: %s", err, err)
}
}

err := ben.Validate()

require.NotNil(t, err)
require.Equal(t, fieldError("tag", ErrValidTagForType, ben.tag).Error(), err.Error())
}

0 comments on commit c79f7c2

Please sign in to comment.