From d9c711f2fd4590c4e76a7993f2344e93e213596f Mon Sep 17 00:00:00 2001 From: pambecker Date: Tue, 21 Jan 2025 18:26:58 +0000 Subject: [PATCH] tests again --- pkg/handlers/ghcapi/orders_test.go | 1 - pkg/handlers/internalapi/orders_test.go | 1 - pkg/models/address_test.go | 1 - .../transportation_office_fetcher_test.go | 384 ++++-------------- 4 files changed, 68 insertions(+), 319 deletions(-) diff --git a/pkg/handlers/ghcapi/orders_test.go b/pkg/handlers/ghcapi/orders_test.go index 5e75301d8cf..96e5c1b2381 100644 --- a/pkg/handlers/ghcapi/orders_test.go +++ b/pkg/handlers/ghcapi/orders_test.go @@ -153,7 +153,6 @@ func (suite *HandlerSuite) TestCreateOrderWithOCONUSValues() { }, }) suite.NotNil(rateArea) - suite.Nil(err) us_country, err := models.FetchCountryByCode(suite.DB(), "US") suite.NotNil(us_country) diff --git a/pkg/handlers/internalapi/orders_test.go b/pkg/handlers/internalapi/orders_test.go index 9531c43e9a6..65dd2ce9cfb 100644 --- a/pkg/handlers/internalapi/orders_test.go +++ b/pkg/handlers/internalapi/orders_test.go @@ -294,7 +294,6 @@ func (suite *HandlerSuite) TestCreateOrder() { }, }) suite.NotNil(rateArea) - suite.Nil(err) us_country, err := models.FetchCountryByCode(suite.DB(), "US") suite.NotNil(us_country) diff --git a/pkg/models/address_test.go b/pkg/models/address_test.go index 4cdbe2f5482..9dfe5a7fa1c 100644 --- a/pkg/models/address_test.go +++ b/pkg/models/address_test.go @@ -328,7 +328,6 @@ func (suite *ModelSuite) Test_FetchDutyLocationGblocForAK() { }, }) suite.NotNil(rateArea) - suite.Nil(err) us_country, err := m.FetchCountryByCode(suite.DB(), "US") suite.NotNil(us_country) diff --git a/pkg/services/transportation_office/transportation_office_fetcher_test.go b/pkg/services/transportation_office/transportation_office_fetcher_test.go index 9ce5bab765c..cd8345a8aa6 100644 --- a/pkg/services/transportation_office/transportation_office_fetcher_test.go +++ b/pkg/services/transportation_office/transportation_office_fetcher_test.go @@ -3,16 +3,15 @@ package transportationoffice import ( "fmt" "testing" - "time" "github.com/gofrs/uuid" "github.com/stretchr/testify/suite" - "github.com/transcom/mymove/pkg/appcontext" "github.com/transcom/mymove/pkg/auth" "github.com/transcom/mymove/pkg/factory" "github.com/transcom/mymove/pkg/models" "github.com/transcom/mymove/pkg/services" + "github.com/transcom/mymove/pkg/testdatagen" "github.com/transcom/mymove/pkg/testingsuite" ) @@ -190,19 +189,6 @@ func (suite *TransportationOfficeServiceSuite) Test_FindCounselingOffices() { } func (suite *TransportationOfficeServiceSuite) Test_Oconus_AK_FindCounselingOffices() { - testContractName := "Test_findOconusGblocDepartmentIndicator" - testContractCode := "Test_findOconusGblocDepartmentIndicator_Code" - testPostalCode := "99790" - testPostalCode2 := "99701" - testGbloc := "ABCD" - testGbloc2 := "EFGH" - testTransportationName := "TEST - PPO" - testTransportationName2 := "TEST - PPO2" - - serviceAffiliations := []models.ServiceMemberAffiliation{models.AffiliationARMY, - models.AffiliationNAVY, models.AffiliationMARINES, models.AffiliationAIRFORCE, models.AffiliationCOASTGUARD, - models.AffiliationSPACEFORCE} - setupServiceMember := func(serviceMemberAffiliation models.ServiceMemberAffiliation) models.ServiceMember { customServiceMember := models.ServiceMember{ FirstName: models.StringPointer("Gregory"), @@ -234,48 +220,19 @@ func (suite *TransportationOfficeServiceSuite) Test_Oconus_AK_FindCounselingOffi return serviceMember } - createContract := func(appCtx appcontext.AppContext, contractCode string, contractName string) (*models.ReContract, error) { - // See if contract code already exists. - exists, err := appCtx.DB().Where("code = ?", contractCode).Exists(&models.ReContract{}) - if err != nil { - return nil, fmt.Errorf("could not determine if contract code [%s] existed: %w", contractCode, err) - } - if exists { - return nil, fmt.Errorf("the provided contract code [%s] already exists", contractCode) - } + setupDataForOconusSearchCounselingOffice := func(postalCode string, gbloc string) (models.ReRateArea, models.OconusRateArea, models.UsPostRegionCity, models.DutyLocation) { + contract := testdatagen.FetchOrMakeReContract(suite.DB(), testdatagen.Assertions{}) - // Contract code is new; insert it. - contract := models.ReContract{ - Code: contractCode, - Name: contractName, - } - verrs, err := appCtx.DB().ValidateAndSave(&contract) - if verrs.HasAny() { - return nil, fmt.Errorf("validation errors when saving contract [%+v]: %w", contract, verrs) - } - if err != nil { - return nil, fmt.Errorf("could not save contract [%+v]: %w", contract, err) - } - return &contract, nil - } - - setupDataForOconusSearchCounselingOffice := func(contract models.ReContract, postalCode string, gbloc string, transportationName string) (models.ReRateArea, models.OconusRateArea, models.UsPostRegionCity, models.DutyLocation) { rateAreaCode := uuid.Must(uuid.NewV4()).String()[0:5] - rateArea := models.ReRateArea{ - ID: uuid.Must(uuid.NewV4()), - ContractID: contract.ID, - IsOconus: true, - Code: rateAreaCode, - Name: fmt.Sprintf("Alaska-%s", rateAreaCode), - Contract: contract, - } - verrs, err := suite.DB().ValidateAndCreate(&rateArea) - if verrs.HasAny() { - suite.Fail(verrs.Error()) - } - if err != nil { - suite.Fail(err.Error()) - } + rateArea := testdatagen.FetchOrMakeReRateArea(suite.DB(), testdatagen.Assertions{ + ReRateArea: models.ReRateArea{ + ContractID: contract.ID, + IsOconus: true, + Name: fmt.Sprintf("Alaska-%s", rateAreaCode), + Contract: contract, + }, + }) + suite.NotNil(rateArea) us_country, err := models.FetchCountryByCode(suite.DB(), "US") suite.NotNil(us_country) @@ -285,32 +242,18 @@ func (suite *TransportationOfficeServiceSuite) Test_Oconus_AK_FindCounselingOffi suite.NotNil(usprc) suite.FatalNoError(err) - oconusRateArea := models.OconusRateArea{ - ID: uuid.Must(uuid.NewV4()), - RateAreaId: rateArea.ID, - CountryId: us_country.ID, - UsPostRegionCityId: usprc.ID, - Active: true, - } - verrs, err = suite.DB().ValidateAndCreate(&oconusRateArea) - if verrs.HasAny() { - suite.Fail(verrs.Error()) - } - if err != nil { - suite.Fail(err.Error()) - } + oconusRateArea, err := models.FetchOconusRateAreaByCityId(suite.DB(), usprc.ID.String()) + suite.NotNil(oconusRateArea) + suite.Nil(err) - address := models.Address{ - StreetAddress1: "n/a", - City: "SomeCity", - State: "AK", - PostalCode: postalCode, - County: models.StringPointer("SomeCounty"), - IsOconus: models.BoolPointer(true), - UsPostRegionCityID: &usprc.ID, - CountryId: models.UUIDPointer(us_country.ID), - } - suite.MustSave(&address) + address := factory.BuildAddress(suite.DB(), []factory.Customization{ + { + Model: models.Address{ + IsOconus: models.BoolPointer(true), + UsPostRegionCityID: &usprc.ID, + }, + }, + }, nil) origDutyLocation := factory.BuildDutyLocation(suite.DB(), []factory.Customization{ { @@ -321,7 +264,7 @@ func (suite *TransportationOfficeServiceSuite) Test_Oconus_AK_FindCounselingOffi }, { Model: models.TransportationOffice{ - Name: transportationName, + Name: "TEST - PPO", Gbloc: gbloc, ProvidesCloseout: true, }, @@ -331,148 +274,68 @@ func (suite *TransportationOfficeServiceSuite) Test_Oconus_AK_FindCounselingOffi found_duty_location, _ := models.FetchDutyLocation(suite.DB(), origDutyLocation.ID) - return rateArea, oconusRateArea, *usprc, found_duty_location + return rateArea, *oconusRateArea, *usprc, found_duty_location } - suite.Run("success - findOconusGblocDepartmentIndicator - returns default GLOC for departmentAffiliation if no specific departmentAffilation mapping is defined", func() { - contract, err := createContract(suite.AppContextForTest(), testContractCode, testContractName) - suite.NotNil(contract) - suite.FatalNoError(err) - + suite.Run("success - findOconusGblocDepartmentIndicator - returns default GBLOC for departmentAffiliation if no specific departmentAffilation mapping is defined", func() { const fairbanksAlaskaPostalCode = "99790" - _, oconusRateArea, _, dutylocation := setupDataForOconusSearchCounselingOffice(*contract, fairbanksAlaskaPostalCode, testGbloc, testTransportationName) + _, oconusRateArea, _, dutylocation := setupDataForOconusSearchCounselingOffice(fairbanksAlaskaPostalCode, "JEAT") // setup department affiliation to GBLOC mappings - expected_gbloc := "TEST-GBLOC" - jppsoRegion := models.JppsoRegions{ - Code: expected_gbloc, - Name: "TEST PPM", - } - suite.MustSave(&jppsoRegion) - - gblocAors := models.GblocAors{ - JppsoRegionID: jppsoRegion.ID, - OconusRateAreaID: oconusRateArea.ID, - // DepartmentIndicator is nil, - } - suite.MustSave(&gblocAors) - - serviceAffiliations := []models.ServiceMemberAffiliation{models.AffiliationARMY, - models.AffiliationNAVY, models.AffiliationMARINES, models.AffiliationAIRFORCE, models.AffiliationCOASTGUARD, - models.AffiliationSPACEFORCE} - - // loop through and make sure all branches are using expected default GBLOC - for _, affiliation := range serviceAffiliations { - serviceMember := setupServiceMember(affiliation) - appCtx := suite.AppContextWithSessionForTest(&auth.Session{ - ServiceMemberID: serviceMember.ID, - }) - suite.Nil(err) - departmentIndictor, err := findOconusGblocDepartmentIndicator(appCtx, dutylocation) - suite.NotNil(departmentIndictor) - suite.Nil(err) - suite.Nil(departmentIndictor.DepartmentIndicator) - suite.Equal(expected_gbloc, departmentIndictor.Gbloc) - } - }) - - suite.Run("success - findOconusGblocDepartmentIndicator - returns specific GLOC for departmentAffiliation when a specific departmentAffilation mapping is defined", func() { - contract, err := createContract(suite.AppContextForTest(), testContractCode, testContractName) - suite.NotNil(contract) - suite.FatalNoError(err) + jppsoRegion, err := models.FetchJppsoRegionByCode(suite.DB(), "JEAT") + suite.NotNil(jppsoRegion) + suite.Nil(err) - _, oconusRateArea, _, dutylocation := setupDataForOconusSearchCounselingOffice(*contract, testPostalCode, testGbloc, testTransportationName) + gblocAors, err := models.FetchGblocAorsByJppsoCodeRateAreaDept(suite.DB(), jppsoRegion.ID, oconusRateArea.ID, models.DepartmentIndicatorARMY.String()) + suite.NotNil(gblocAors) + suite.Nil(err) - departmentIndicators := []models.DepartmentIndicator{models.DepartmentIndicatorARMY, - models.DepartmentIndicatorARMYCORPSOFENGINEERS, models.DepartmentIndicatorCOASTGUARD, - models.DepartmentIndicatorNAVYANDMARINES, models.DepartmentIndicatorAIRANDSPACEFORCE} + serviceMember := setupServiceMember(models.AffiliationARMY) + appCtx := suite.AppContextWithSessionForTest(&auth.Session{ + ServiceMemberID: serviceMember.ID, + }) + departmentIndictor, err := findOconusGblocDepartmentIndicator(appCtx, dutylocation) + suite.NotNil(departmentIndictor) + suite.Nil(err) + suite.Nil(departmentIndictor.DepartmentIndicator) + suite.Equal("JEAT", departmentIndictor.Gbloc) + }) - expectedAffiliationToDepartmentIndicatorMap := make(map[string]string, 0) - expectedAffiliationToDepartmentIndicatorMap[models.AffiliationARMY.String()] = models.DepartmentIndicatorARMY.String() - expectedAffiliationToDepartmentIndicatorMap[models.AffiliationNAVY.String()] = models.DepartmentIndicatorNAVYANDMARINES.String() - expectedAffiliationToDepartmentIndicatorMap[models.AffiliationMARINES.String()] = models.DepartmentIndicatorNAVYANDMARINES.String() - expectedAffiliationToDepartmentIndicatorMap[models.AffiliationAIRFORCE.String()] = models.DepartmentIndicatorAIRANDSPACEFORCE.String() - expectedAffiliationToDepartmentIndicatorMap[models.AffiliationCOASTGUARD.String()] = models.DepartmentIndicatorCOASTGUARD.String() - expectedAffiliationToDepartmentIndicatorMap[models.AffiliationSPACEFORCE.String()] = models.DepartmentIndicatorAIRANDSPACEFORCE.String() + suite.Run("success - findOconusGblocDepartmentIndicator - returns specific GBLOC for departmentAffiliation when a specific departmentAffilation mapping is defined -- simulate Zone 2 scenerio", func() { + const fairbanksAlaskaPostalCode = "99790" + _, oconusRateArea, _, dutylocation := setupDataForOconusSearchCounselingOffice(fairbanksAlaskaPostalCode, "MBFL") // setup department affiliation to GBLOC mappings - expected_gbloc := "TEST-GBLOC" - jppsoRegion := models.JppsoRegions{ - Code: expected_gbloc, - Name: "TEST PPM", - } - suite.MustSave(&jppsoRegion) + jppsoRegion, err := models.FetchJppsoRegionByCode(suite.DB(), "MBFL") + suite.NotNil(jppsoRegion) + suite.Nil(err) - defaultGblocAors := models.GblocAors{ - JppsoRegionID: jppsoRegion.ID, - OconusRateAreaID: oconusRateArea.ID, - //DepartmentIndicator is nil, - } - suite.MustSave(&defaultGblocAors) - - // setup specific departmentAffiliation mapping for each branch - for _, departmentIndicator := range departmentIndicators { - gblocAors := models.GblocAors{ - JppsoRegionID: jppsoRegion.ID, - OconusRateAreaID: oconusRateArea.ID, - DepartmentIndicator: models.StringPointer(departmentIndicator.String()), - } - suite.MustSave(&gblocAors) - } + gblocAors, err := models.FetchGblocAorsByJppsoCodeRateAreaDept(suite.DB(), jppsoRegion.ID, oconusRateArea.ID, models.DepartmentIndicatorAIRANDSPACEFORCE.String()) + suite.NotNil(gblocAors) + suite.Nil(err) // loop through and make sure all branches are using it's own dedicated GBLOC and not default - for _, affiliation := range serviceAffiliations { - serviceMember := setupServiceMember(affiliation) - appCtx := suite.AppContextWithSessionForTest(&auth.Session{ - ServiceMemberID: serviceMember.ID, - }) - suite.Nil(err) - departmentIndictor, err := findOconusGblocDepartmentIndicator(appCtx, dutylocation) - suite.NotNil(departmentIndictor) - suite.Nil(err) - suite.NotNil(departmentIndictor.DepartmentIndicator) - if match, ok := expectedAffiliationToDepartmentIndicatorMap[affiliation.String()]; ok { - // verify service member's affiliation matches on specific departmentIndicator mapping record - suite.Equal(match, *departmentIndictor.DepartmentIndicator) - } else { - suite.Fail(fmt.Sprintf("key does not exist for %s", affiliation.String())) - } - suite.Equal(expected_gbloc, departmentIndictor.Gbloc) - } - }) - - suite.Run("failure -- returns error when there are default and no department specific GBLOC", func() { - contract, err := createContract(suite.AppContextForTest(), testContractCode, testContractName) - suite.NotNil(contract) - suite.FatalNoError(err) - - _, _, _, dutylocation := setupDataForOconusSearchCounselingOffice(*contract, testPostalCode, testGbloc, testTransportationName) - - // No specific departmentAffiliation mapping or default were created. - // Expect an error response when nothing is found. serviceMember := setupServiceMember(models.AffiliationAIRFORCE) appCtx := suite.AppContextWithSessionForTest(&auth.Session{ ServiceMemberID: serviceMember.ID, }) suite.Nil(err) departmentIndictor, err := findOconusGblocDepartmentIndicator(appCtx, dutylocation) - suite.Nil(departmentIndictor) - suite.NotNil(err) + suite.NotNil(departmentIndictor) + suite.Nil(err) + suite.NotNil(departmentIndictor.DepartmentIndicator) + suite.Equal(models.DepartmentIndicatorAIRANDSPACEFORCE.String(), *departmentIndictor.DepartmentIndicator) + suite.Equal("MBFL", departmentIndictor.Gbloc) }) suite.Run("failure - findOconusGblocDepartmentIndicator - returns error when find service member ID fails", func() { - contract, err := createContract(suite.AppContextForTest(), testContractCode, testContractName) - suite.NotNil(contract) - suite.FatalNoError(err) - - _, _, _, dutylocation := setupDataForOconusSearchCounselingOffice(*contract, testPostalCode, testGbloc, testTransportationName) + _, _, _, dutylocation := setupDataForOconusSearchCounselingOffice("99714", "JEAT") appCtx := suite.AppContextWithSessionForTest(&auth.Session{ // create fake service member ID to raise NOT found error ServiceMemberID: uuid.Must(uuid.NewV4()), }) - suite.Nil(err) departmentIndictor, err := findOconusGblocDepartmentIndicator(appCtx, dutylocation) suite.Nil(departmentIndictor) suite.NotNil(err) @@ -489,33 +352,16 @@ func (suite *TransportationOfficeServiceSuite) Test_Oconus_AK_FindCounselingOffi }) suite.Run("success - offices using default departmentIndicator mapping", func() { - contract, err := createContract(suite.AppContextForTest(), testContractCode, testContractName) - suite.NotNil(contract) - suite.FatalNoError(err) - - _, oconusRateArea, _, dutylocation := setupDataForOconusSearchCounselingOffice(*contract, testPostalCode, testGbloc, testTransportationName) + _, oconusRateArea, _, dutylocation := setupDataForOconusSearchCounselingOffice("99619", "MAPS") // setup department affiliation to GBLOC mappings - jppsoRegion := models.JppsoRegions{ - Code: testGbloc, - Name: "TEST PPM", - } - suite.MustSave(&jppsoRegion) - - gblocAors := models.GblocAors{ - JppsoRegionID: jppsoRegion.ID, - OconusRateAreaID: oconusRateArea.ID, - // DepartmentIndicator is nil, - } - suite.MustSave(&gblocAors) + jppsoRegion, err := models.FetchJppsoRegionByCode(suite.DB(), "MAPS") + suite.NotNil(jppsoRegion) + suite.Nil(err) - postalCodeToGBLOC := models.PostalCodeToGBLOC{ - PostalCode: testPostalCode, - GBLOC: testGbloc, - CreatedAt: time.Now(), - UpdatedAt: time.Now(), - } - suite.MustSave(&postalCodeToGBLOC) + gblocAors, err := models.FetchGblocAorsByJppsoCodeRateAreaDept(suite.DB(), jppsoRegion.ID, oconusRateArea.ID, models.DepartmentIndicatorARMY.String()) + suite.NotNil(gblocAors) + suite.Nil(err) serviceMember := setupServiceMember(models.AffiliationAIRFORCE) appCtx := suite.AppContextWithSessionForTest(&auth.Session{ @@ -527,15 +373,15 @@ func (suite *TransportationOfficeServiceSuite) Test_Oconus_AK_FindCounselingOffi suite.NotNil(offices) suite.Nil(err) suite.Equal(1, len(offices)) - suite.Equal(testTransportationName, offices[0].Name) + suite.Equal("TEST - PPO", offices[0].Name) // add another transportation office factory.BuildTransportationOffice(suite.DB(), []factory.Customization{ { Model: models.TransportationOffice{ - Name: testTransportationName2, + Name: "TEST - PPO2", ProvidesCloseout: true, - Gbloc: testGbloc, + Gbloc: "MAPS", }, }, }, nil) @@ -545,100 +391,6 @@ func (suite *TransportationOfficeServiceSuite) Test_Oconus_AK_FindCounselingOffi suite.Equal(2, len(offices)) }) - suite.Run("success - returns correct office based on service affiliation -- simulate Zone 2 scenerio", func() { - contract, err := createContract(suite.AppContextForTest(), testContractCode, testContractName) - suite.NotNil(contract) - suite.FatalNoError(err) - - _, oconusRateArea, _, dutylocation := setupDataForOconusSearchCounselingOffice(*contract, testPostalCode, testGbloc, testTransportationName) - - // ****************************************************************************** - // setup department affiliation to GBLOC mappings for AF/SF - // ****************************************************************************** - jppsoRegion_AFSF := models.JppsoRegions{ - Code: testGbloc, - Name: "TEST PPO", - } - suite.MustSave(&jppsoRegion_AFSF) - - gblocAors_AFSF := models.GblocAors{ - JppsoRegionID: jppsoRegion_AFSF.ID, - OconusRateAreaID: oconusRateArea.ID, - DepartmentIndicator: models.StringPointer(models.DepartmentIndicatorAIRANDSPACEFORCE.String()), - } - suite.MustSave(&gblocAors_AFSF) - - postalCodeToGBLOC_AFSF := models.PostalCodeToGBLOC{ - PostalCode: testPostalCode, - GBLOC: testGbloc, - CreatedAt: time.Now(), - UpdatedAt: time.Now(), - } - suite.MustSave(&postalCodeToGBLOC_AFSF) - // ****************************************************************************** - - // ****************************************************************************** - // setup department affiliation to GBLOC mappings for other branches NOT AF/SF - // ****************************************************************************** - jppsoRegion_not_AFSF := models.JppsoRegions{ - Code: testGbloc2, - Name: "TEST PPO 2", - } - suite.MustSave(&jppsoRegion_not_AFSF) - - gblocAors_not_AFSF := models.GblocAors{ - JppsoRegionID: jppsoRegion_not_AFSF.ID, - OconusRateAreaID: oconusRateArea.ID, - } - suite.MustSave(&gblocAors_not_AFSF) - - postalCodeToGBLOC_not_AFSF := models.PostalCodeToGBLOC{ - PostalCode: testPostalCode2, - GBLOC: testGbloc2, - CreatedAt: time.Now(), - UpdatedAt: time.Now(), - } - suite.MustSave(&postalCodeToGBLOC_not_AFSF) - - // add transportation office for other branches not AF/SF - factory.BuildTransportationOffice(suite.DB(), []factory.Customization{ - { - Model: models.TransportationOffice{ - Name: testTransportationName2, - ProvidesCloseout: true, - Gbloc: testGbloc2, - }, - }, - }, nil) - // ****************************************************************************** - - for _, affiliation := range serviceAffiliations { - serviceMember := setupServiceMember(affiliation) - if affiliation == models.AffiliationAIRFORCE || affiliation == models.AffiliationSPACEFORCE { - appCtx := suite.AppContextWithSessionForTest(&auth.Session{ - ServiceMemberID: serviceMember.ID, - }) - offices, err := findCounselingOffice(appCtx, dutylocation.ID) - suite.NotNil(offices) - suite.Nil(err) - suite.Equal(1, len(offices)) - // verify expected office is for AF/SF and not for Navy ..etc.. - suite.Equal(testTransportationName, offices[0].Name) - suite.NotEqual(testTransportationName2, offices[0].Name) - } else { - appCtx := suite.AppContextWithSessionForTest(&auth.Session{ - ServiceMemberID: serviceMember.ID, - }) - offices, err := findCounselingOffice(appCtx, dutylocation.ID) - suite.NotNil(offices) - suite.Nil(err) - suite.Equal(1, len(offices)) - // verify expected office is for Navy ..etc.. and not AF/SF - suite.Equal(testTransportationName2, offices[0].Name) - suite.NotEqual(testTransportationName, offices[0].Name) - } - } - }) } func (suite *TransportationOfficeServiceSuite) Test_GetTransportationOffice() {