Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query: testing with fake client #161

Open
dipankardas011 opened this issue Aug 5, 2023 · 1 comment
Open

query: testing with fake client #161

dipankardas011 opened this issue Aug 5, 2023 · 1 comment

Comments

@dipankardas011
Copy link

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
Copy link
Author

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)
		}
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant