civogo icon indicating copy to clipboard operation
civogo copied to clipboard

query: testing with fake client

Open dipankardas011 opened this issue 1 year ago • 1 comments

currently I am using the civogo client to get all the valid regions to validate users input.

// IsValidRegionCIVO validates the region code for CIVO
func isValidRegion(reg string) error {
	regions, err := civoClient.ListRegions()
	if err != nil {
		return err
	}
	for _, region := range regions {
		if region.Code == reg {
			return nil
		}
	}
	return fmt.Errorf("INVALID REGION")
}

now i want to test this function, via fakeCLient but it is returning fake output [{FAKE1 Fake testing region false {false false false false false false false false false} true}] regions. and using the original client requires creds

func (*civogo.Client).ListRegions() ([]civogo.Region, error)

ListRegions returns all load balancers owned by the calling API account

[`(civogo.Client).ListRegions` on pkg.go.dev](https://pkg.go.dev/github.com/civo/[email protected]#Client.ListRegions)

dipankardas011 avatar Aug 05 '23 15:08 dipankardas011

Here is the test function for more context

func TestIsValidRegion(t *testing.T) {
	testSet := map[string]error{
		"LON1": nil,
		"FRA1": nil,
		"NYC1": nil,
		"Lon!": errors.New(""),
		"":     errors.New(""),
	}
	for region, expected := range testSet {
		// FIXME: want to use fakeClient but it uses real client
		if err := isValidRegion(region); (expected != nil && err == nil) || (expected == nil && err != nil) {
			t.Fatalf("Region code mismatch %s\n", region)
		}
	}
}

dipankardas011 avatar Aug 05 '23 15:08 dipankardas011