This is an automated email from the ASF dual-hosted git repository.

ocket8888 pushed a commit to branch 5.0.x
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git

commit d07150a62de6c1aa2026a43a03f01d02ba2c4e75
Author: Myles B <[email protected]>
AuthorDate: Wed Nov 25 13:20:05 2020 -0700

    Improve validation error message for regions with no division name (#5314)
    
    * expanded TORegion.Validate and copied+updated a test from divisions
    
    Fixed `TestValidation` in `regions_test.go`
    
    - Syntax errors resolved
    - Wrapped up tc.Region structs in TORegion structs
    - Added logging to ensure expected error message is displayed
    
    removed comment and reflect usage in TestValidate
    
    updated regions validation error message, updated test
    
    added division id to pseudo-fixture in regions_test
    
    sort imports in regions_test.go
    
    reordered imports
    
    i dont know exactly what gofmt is complaining about here
    
    gofmt on imports
    
    * Fix import order
    
    * removed divisionName from validation
    
    * s/are//
    
    Co-authored-by: ocket8888 <[email protected]>
    (cherry picked from commit 82df69b064029dad824ff90df59e739a8368bfa3)
---
 traffic_ops/traffic_ops_golang/region/regions.go   |  5 +++-
 .../traffic_ops_golang/region/regions_test.go      | 34 ++++++++++++++++++++--
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/traffic_ops/traffic_ops_golang/region/regions.go 
b/traffic_ops/traffic_ops_golang/region/regions.go
index 600471e..3661515 100644
--- a/traffic_ops/traffic_ops_golang/region/regions.go
+++ b/traffic_ops/traffic_ops_golang/region/regions.go
@@ -95,7 +95,10 @@ func (region *TORegion) GetType() string {
 
 func (region *TORegion) Validate() error {
        if len(region.Name) < 1 {
-               return errors.New(`Region 'name' is required.`)
+               return errors.New(`region 'name' is required`)
+       }
+       if region.Division == 0 {
+               return errors.New(`region 'division' is required`)
        }
        return nil
 }
diff --git a/traffic_ops/traffic_ops_golang/region/regions_test.go 
b/traffic_ops/traffic_ops_golang/region/regions_test.go
index 79431ce..c9165c8 100644
--- a/traffic_ops/traffic_ops_golang/region/regions_test.go
+++ b/traffic_ops/traffic_ops_golang/region/regions_test.go
@@ -23,11 +23,12 @@ import (
        "testing"
        "time"
 
+       "github.com/jmoiron/sqlx"
+       sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
+
        "github.com/apache/trafficcontrol/lib/go-tc"
        "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
        "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/test"
-       "github.com/jmoiron/sqlx"
-       sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
 )
 
 func getTestRegions() []tc.Region {
@@ -46,7 +47,6 @@ func getTestRegions() []tc.Region {
 
        return regions
 }
-
 func TestReadRegions(t *testing.T) {
 
        mockDB, mock, err := sqlmock.New()
@@ -109,3 +109,31 @@ func TestInterfaces(t *testing.T) {
                t.Errorf("Region must be Identifier")
        }
 }
+func TestValidation(t *testing.T) {
+       testRegion := tc.Region{
+               DivisionName: "west",
+               Division:     77,
+               ID:           1,
+               Name:         "region1",
+               LastUpdated:  tc.TimeNoMod{Time: time.Now()},
+       }
+       testTORegion := TORegion{Region: testRegion}
+       errs := test.SortErrors(test.SplitErrors(testTORegion.Validate()))
+
+       if len(errs) > 0 {
+               t.Errorf(`expected no errors,  got %v`, errs)
+       }
+
+       testRegionNoDivision := tc.Region{
+               ID:          1,
+               Name:        "region1",
+               LastUpdated: tc.TimeNoMod{Time: time.Now()},
+       }
+       testTORegionNoDivision := TORegion{Region: testRegionNoDivision}
+       errs = 
test.SortErrors(test.SplitErrors(testTORegionNoDivision.Validate()))
+       if len(errs) == 0 {
+               t.Errorf(`expected an error with a nil division id, received no 
error`)
+       } else {
+               t.Logf(`Got expected error validating region with no division: 
%s`, errs[0].Error())
+       }
+}

Reply via email to