Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
taeJungCaci committed Feb 7, 2025
1 parent e9647aa commit cf06032
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 18 deletions.
33 changes: 18 additions & 15 deletions pkg/models/mto_service_items.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,26 @@ func FetchRelatedDestinationSITServiceItems(tx *pop.Connection, mtoServiceItemID
}

func FetchServiceItem(db *pop.Connection, serviceItemID uuid.UUID) (MTOServiceItem, error) {
var serviceItem MTOServiceItem
err := db.Eager("SITDestinationOriginalAddress",
"SITDestinationFinalAddress",
"ReService",
"CustomerContacts",
"MTOShipment.PickupAddress",
"MTOShipment.DestinationAddress",
"Dimensions").Where("id = ?", serviceItemID).First(&serviceItem)

if err != nil {
if errors.Cause(err).Error() == RecordNotFoundErrorString {
return MTOServiceItem{}, ErrFetchNotFound
if db != nil {
var serviceItem MTOServiceItem
err := db.Eager("SITDestinationOriginalAddress",
"SITDestinationFinalAddress",
"ReService",
"CustomerContacts",
"MTOShipment.PickupAddress",
"MTOShipment.DestinationAddress",
"Dimensions").Where("id = ?", serviceItemID).First(&serviceItem)

if err != nil {
if errors.Cause(err).Error() == RecordNotFoundErrorString {
return MTOServiceItem{}, ErrFetchNotFound
}
return MTOServiceItem{}, err
}
return MTOServiceItem{}, err
return serviceItem, nil
} else {
return MTOServiceItem{}, errors.New("db connection is nil; unable to fetch service item")
}

return serviceItem, nil
}

func FetchRelatedDestinationSITFuelCharge(tx *pop.Connection, mtoServiceItemID uuid.UUID) (MTOServiceItem, error) {
Expand Down
52 changes: 52 additions & 0 deletions pkg/models/mto_service_items_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,55 @@ func (suite *ModelSuite) TestGetMTOServiceItemTypeFromServiceItem() {
suite.NotNil(returnedShipment)
})
}

func (suite *ModelSuite) TestFetchServiceItem() {
suite.Run("successful fetch service item", func() {
move := factory.BuildMove(suite.DB(), nil, nil)
shipment := factory.BuildMTOShipment(suite.DB(), []factory.Customization{
{
Model: move,
LinkOnly: true,
},
}, nil)
msServiceItem := factory.BuildMTOServiceItem(suite.DB(), []factory.Customization{
{
Model: shipment,
LinkOnly: true,
},
{
Model: models.ReService{
Code: models.ReServiceCodeMS,
},
},
}, nil)
factory.BuildMTOServiceItem(suite.DB(), []factory.Customization{
{
Model: shipment,
LinkOnly: true,
},
{
Model: models.ReService{
Code: models.ReServiceCodeCS,
},
},
}, nil)
serviceItem, fetchErr := models.FetchServiceItem(suite.DB(), msServiceItem.ID)
suite.NoError(fetchErr)
suite.NotNil(serviceItem)
})

suite.Run("failed fetch service item - db connection is nil", func() {
serviceItem, fetchErr := models.FetchServiceItem(nil, uuid.Must(uuid.NewV4()))
suite.Error(fetchErr)
suite.EqualError(fetchErr, "db connection is nil; unable to fetch service item")
suite.Empty(serviceItem)
})

suite.Run("failed fetch service item - record not found", func() {
nonExistentID := uuid.Must(uuid.NewV4())
serviceItem, fetchErr := models.FetchServiceItem(suite.DB(), nonExistentID)
suite.Error(fetchErr)
suite.Equal(fetchErr, models.ErrFetchNotFound)
suite.Empty(serviceItem)
})
}
4 changes: 2 additions & 2 deletions pkg/services/ghcrateengine/pricer_helpers_intl.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ func priceIntlCratingUncrating(appCtx appcontext.AppContext, cratingUncratingCod
return 0, nil, errors.New("Market is required")
}

if externalCrate && billedCubicFeet < 4.0 {
return 0, nil, fmt.Errorf("external crates must be billed for a minimum of 4 cubic feet")
if externalCrate && billedCubicFeet < minIntlExternalCrateBilledCubicFeet {
return 0, nil, fmt.Errorf("external crates must be billed for a minimum of %.2f cubic feet", minIntlExternalCrateBilledCubicFeet)
}

internationalAccessorialPrice, err := fetchInternationalAccessorialPrice(appCtx, contractCode, cratingUncratingCode, market)
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/ghcrateengine/pricer_helpers_intl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (suite *GHCRateEngineServiceSuite) TestPriceIntlCratingUncrating() {
_, _, err := priceIntlCratingUncrating(suite.AppContextForTest(), models.ReServiceCodeICRT, testdatagen.DefaultContractCode, icrtTestRequestedPickupDate, badSize, icrtTestStandaloneCrate, icrtTestStandaloneCrateCap, true, icrtTestMarket)

suite.Error(err)
suite.Contains(err.Error(), "external crates must be billed for a minimum of 4 cubic feet")
suite.Contains(err.Error(), "external crates must be billed for a minimum of 4.00 cubic feet")
})

suite.Run("not finding a rate record", func() {
Expand Down
3 changes: 3 additions & 0 deletions pkg/services/ghcrateengine/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const minIntlWeightHHG = unit.Pound(500)
// minInternationalWeight is the minimum weight used in international calculations (weights below this are upgraded to the min)
const minInternationalWeight = unit.Pound(500)

// minIntlExternalCrateBilledCubicFeet is the minimum billed cubic feet used in international external crate
const minIntlExternalCrateBilledCubicFeet = 4.00

// dateInYear represents a specific date in a year (without caring what year it is)
type dateInYear struct {
month time.Month
Expand Down

0 comments on commit cf06032

Please sign in to comment.