Skip to content

Commit

Permalink
test: Created a new test file to improve code coverage for utils func…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
Valkyrie00 committed Nov 15, 2023
1 parent 901103c commit dda9582
Show file tree
Hide file tree
Showing 2 changed files with 599 additions and 31 deletions.
64 changes: 33 additions & 31 deletions pkg/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func splitFlagString(flag string) (string, string) {
if len(splittedFlag) == 2 {
return splittedFlag[0], splittedFlag[1]
}

return splittedFlag[0], ""
}

Expand All @@ -24,7 +25,6 @@ func extractPropertyValue(path string, values map[string]interface{}) (interface
}

firstPartAndRest := strings.SplitN(path, ".", 2)

if len(firstPartAndRest) == 1 {
value := values[firstPartAndRest[0]]
return value, nil
Expand All @@ -34,6 +34,7 @@ func extractPropertyValue(path string, values map[string]interface{}) (interface
if ok {
return extractPropertyValue(firstPartAndRest[1], childMap)
}

return false, fmt.Errorf("unable to find property in path %s", path)
}

Expand All @@ -43,10 +44,8 @@ func getTypeForPath(schema map[string]interface{}, path string) (reflect.Kind, e
}

firstPartAndRest := strings.SplitN(path, ".", 2)

if len(firstPartAndRest) == 1 {
value, ok := schema[firstPartAndRest[0]].(map[string]interface{})

if !ok {
return 0, fmt.Errorf("schema was not in the expected format")
}
Expand Down Expand Up @@ -76,6 +75,7 @@ func getTypeForPath(schema map[string]interface{}, path string) (reflect.Kind, e
structSchema, _ := structMap.(map[string]interface{})["schema"].(map[string]interface{})
return getTypeForPath(structSchema, firstPartAndRest[1])
}

return 0, fmt.Errorf("unable to find property in schema %s", path)
}

Expand Down Expand Up @@ -106,7 +106,6 @@ func processResolvedFlag(resolvedFlag resolvedFlag, defaultValue interface{},
}

actualKind, schemaErr := getTypeForPath(resolvedFlag.FlagSchema.Schema, propertyPath)

if schemaErr != nil || actualKind != expectedKind {
return openfeature.InterfaceResolutionDetail{
Value: defaultValue,
Expand All @@ -123,7 +122,6 @@ func processResolvedFlag(resolvedFlag resolvedFlag, defaultValue interface{},
}

extractedValue, extractValueError := extractPropertyValue(propertyPath, updatedMap)

if extractValueError != nil {
return typeMismatchError(defaultValue)
}
Expand All @@ -140,46 +138,42 @@ func replaceNumbers(basePath string, input map[string]interface{},
updatedMap := make(map[string]interface{})
for key, value := range input {
kind, typeErr := getTypeForPath(schema, fmt.Sprintf("%s%s", basePath, key))

if typeErr != nil {
return updatedMap, fmt.Errorf("unable to get type for path %w", typeErr)
}

switch kind {
case reflect.Float64:
{
floatValue, err := value.(json.Number).Float64()
if err != nil {
return updatedMap, fmt.Errorf("unable to convert to float")
}
updatedMap[key] = floatValue
floatValue, err := value.(json.Number).Float64()
if err != nil {
return updatedMap, fmt.Errorf("unable to convert to float")
}

updatedMap[key] = floatValue
case reflect.Int64:
{
intValue, err := value.(json.Number).Int64()
if err != nil {
return updatedMap, fmt.Errorf("unable to convert to int")
}
updatedMap[key] = intValue
intValue, err := value.(json.Number).Int64()
if err != nil {
return updatedMap, fmt.Errorf("unable to convert to int")
}

updatedMap[key] = intValue
case reflect.Map:
{
asMap, ok := value.(map[string]interface{})
if !ok {
return updatedMap, fmt.Errorf("unable to convert map")
}
childMap, err := replaceNumbers(fmt.Sprintf("%s.", key), asMap, schema)
if err != nil {
return updatedMap, fmt.Errorf("unable to convert map")
}
updatedMap[key] = childMap
asMap, ok := value.(map[string]interface{})
if !ok {
return updatedMap, fmt.Errorf("unable to convert map")
}
default:
{
updatedMap[key] = value

childMap, err := replaceNumbers(fmt.Sprintf("%s.", key), asMap, schema)
if err != nil {
return updatedMap, fmt.Errorf("unable to convert map")
}

updatedMap[key] = childMap
default:
updatedMap[key] = value
}
}

return updatedMap, nil
}

Expand All @@ -202,6 +196,7 @@ func toBoolResolutionDetail(res openfeature.InterfaceResolutionDetail,
ProviderResolutionDetail: res.ProviderResolutionDetail,
}
}

return openfeature.BoolResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: openfeature.ProviderResolutionDetail{
Expand All @@ -210,6 +205,7 @@ func toBoolResolutionDetail(res openfeature.InterfaceResolutionDetail,
},
}
}

return openfeature.BoolResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: res.ProviderResolutionDetail,
Expand All @@ -226,6 +222,7 @@ func toStringResolutionDetail(res openfeature.InterfaceResolutionDetail,
ProviderResolutionDetail: res.ProviderResolutionDetail,
}
}

return openfeature.StringResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: openfeature.ProviderResolutionDetail{
Expand All @@ -234,6 +231,7 @@ func toStringResolutionDetail(res openfeature.InterfaceResolutionDetail,
},
}
}

return openfeature.StringResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: res.ProviderResolutionDetail,
Expand All @@ -250,6 +248,7 @@ func toFloatResolutionDetail(res openfeature.InterfaceResolutionDetail,
ProviderResolutionDetail: res.ProviderResolutionDetail,
}
}

return openfeature.FloatResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: openfeature.ProviderResolutionDetail{
Expand All @@ -258,6 +257,7 @@ func toFloatResolutionDetail(res openfeature.InterfaceResolutionDetail,
},
}
}

return openfeature.FloatResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: res.ProviderResolutionDetail,
Expand All @@ -274,6 +274,7 @@ func toIntResolutionDetail(res openfeature.InterfaceResolutionDetail,
ProviderResolutionDetail: res.ProviderResolutionDetail,
}
}

return openfeature.IntResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: openfeature.ProviderResolutionDetail{
Expand All @@ -282,6 +283,7 @@ func toIntResolutionDetail(res openfeature.InterfaceResolutionDetail,
},
}
}

return openfeature.IntResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: res.ProviderResolutionDetail,
Expand Down
Loading

0 comments on commit dda9582

Please sign in to comment.