civo / civogo

Golang client to interact with Civo's API
https://www.civo.com/
MIT License
35 stars 19 forks source link

query: testing with fake client #161

Open dipankardas011 opened 1 year ago

dipankardas011 commented 1 year ago

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/civogo@v0.3.42#Client.ListRegions)
dipankardas011 commented 1 year ago

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