diff --git a/grid-proxy/internal/explorer/db/postgres.go b/grid-proxy/internal/explorer/db/postgres.go index 0910f6fa..889b5e44 100644 --- a/grid-proxy/internal/explorer/db/postgres.go +++ b/grid-proxy/internal/explorer/db/postgres.go @@ -477,7 +477,7 @@ func (d *PostgresDatabase) GetFarms(ctx context.Context, filter types.FarmFilter } if filter.Region != nil { - nodeQuery = nodeQuery.Where("LOWER(node_location.continent) = LOWER(?)", *filter.Region) + nodeQuery = nodeQuery.Where("node_location.continent ILIKE '%' || ? || '%'", *filter.Region) } if len(filter.NodeStatus) != 0 { @@ -681,7 +681,7 @@ func (d *PostgresDatabase) GetNodes(ctx context.Context, filter types.NodeFilter q = q.Where("node.city ILIKE '%' || ? || '%'", *filter.CityContains) } if filter.Region != nil { - q = q.Where("LOWER(node_location.continent) = LOWER(?)", *filter.Region) + q = q.Where("node_location.continent ILIKE '%' || ? || '%'", *filter.Region) } if filter.NodeID != nil { q = q.Where("node.node_id = ?", *filter.NodeID) diff --git a/grid-proxy/tests/queries/farm_test.go b/grid-proxy/tests/queries/farm_test.go index 73d5e983..5ae652bb 100644 --- a/grid-proxy/tests/queries/farm_test.go +++ b/grid-proxy/tests/queries/farm_test.go @@ -62,27 +62,18 @@ var farmFilterRandomValueGenerator = map[string]func(agg FarmsAggregate) interfa return &country }, "Region": func(agg FarmsAggregate) interface{} { - c := changeCase(agg.regions[rand.Intn(len(agg.regions))]) - if len(c) == 0 { + value := getRandomItemSubstring(agg.regions) + if len(value) == 0 { return nil } - - return &c + return &value }, "NameContains": func(agg FarmsAggregate) interface{} { - c := agg.farmNames[rand.Intn(len(agg.farmNames))] - runesList := []rune(c) - a, b := rand.Intn(len(runesList)), rand.Intn(len(runesList)) - if a > b { - a, b = b, a - } - runesList = runesList[a : b+1] - c = string(runesList) - if len(c) == 0 { + name := getRandomItemSubstring(agg.farmNames) + if len(name) == 0 { return nil } - - return &c + return &name }, "CertificationType": func(agg FarmsAggregate) interface{} { return &agg.certifications[rand.Intn(len(agg.certifications))] diff --git a/grid-proxy/tests/queries/mock_client/farms.go b/grid-proxy/tests/queries/mock_client/farms.go index 5170e555..ac4f11ac 100644 --- a/grid-proxy/tests/queries/mock_client/farms.go +++ b/grid-proxy/tests/queries/mock_client/farms.go @@ -175,7 +175,7 @@ func (f *Farm) satisfyFarmNodesFilter(data *DBData, filter types.FarmFilter) boo continue } - if filter.Region != nil && !strings.EqualFold(*filter.Region, data.Regions[node.Country]) { + if filter.Region != nil && !stringMatch(data.Regions[node.Country], *filter.Region) { continue } diff --git a/grid-proxy/tests/queries/mock_client/nodes.go b/grid-proxy/tests/queries/mock_client/nodes.go index 1b217d2b..6dab1e94 100644 --- a/grid-proxy/tests/queries/mock_client/nodes.go +++ b/grid-proxy/tests/queries/mock_client/nodes.go @@ -363,7 +363,7 @@ func (n *Node) satisfies(f types.NodeFilter, data *DBData) bool { return false } - if f.Region != nil && !strings.EqualFold(*f.Region, data.Regions[n.Country]) { + if f.Region != nil && !stringMatch(data.Regions[n.Country], *f.Region) { return false } diff --git a/grid-proxy/tests/queries/node_test.go b/grid-proxy/tests/queries/node_test.go index c8a4487f..2ebacaca 100644 --- a/grid-proxy/tests/queries/node_test.go +++ b/grid-proxy/tests/queries/node_test.go @@ -125,26 +125,14 @@ var nodeFilterRandomValueGenerator = map[string]func(agg NodesAggregate) interfa return &country }, "Region": func(agg NodesAggregate) interface{} { - region := changeCase(agg.regions[rand.Intn(len(agg.regions))]) + region := getRandomItemSubstring(agg.regions) if len(region) == 0 { return nil } - return ®ion }, "CountryContains": func(agg NodesAggregate) interface{} { - c := agg.countries[rand.Intn(len(agg.countries))] - if len(c) == 0 { - return nil - } - - runesList := []rune(c) - a, b := rand.Intn(len(runesList)), rand.Intn(len(runesList)) - if a > b { - a, b = b, a - } - runesList = runesList[a : b+1] - c = string(runesList) + c := getRandomItemSubstring(agg.countries) if len(c) == 0 { return nil } @@ -160,18 +148,10 @@ var nodeFilterRandomValueGenerator = map[string]func(agg NodesAggregate) interfa return &city }, "CityContains": func(agg NodesAggregate) interface{} { - c := agg.cities[rand.Intn(len(agg.cities))] + c := getRandomItemSubstring(agg.cities) if len(c) == 0 { return nil } - - runesList := []rune(c) - a, b := rand.Intn(len(runesList)), rand.Intn(len(runesList)) - if a > b { - a, b = b, a - } - runesList = runesList[a : b+1] - c = string(runesList) return &c }, "FarmName": func(agg NodesAggregate) interface{} { @@ -183,18 +163,10 @@ var nodeFilterRandomValueGenerator = map[string]func(agg NodesAggregate) interfa return &name }, "FarmNameContains": func(agg NodesAggregate) interface{} { - c := agg.farmNames[rand.Intn(len(agg.farmNames))] + c := getRandomItemSubstring(agg.farmNames) if len(c) == 0 { return nil } - - runesList := []rune(c) - a, b := rand.Intn(len(runesList)), rand.Intn(len(runesList)) - if a > b { - a, b = b, a - } - runesList = runesList[a : b+1] - c = string(runesList) return &c }, "FarmIDs": func(agg NodesAggregate) interface{} { diff --git a/grid-proxy/tests/queries/utils.go b/grid-proxy/tests/queries/utils.go index 1f749fd9..ea14f80e 100644 --- a/grid-proxy/tests/queries/utils.go +++ b/grid-proxy/tests/queries/utils.go @@ -36,6 +36,29 @@ func min(a, b uint64) uint64 { return b } +// getRandomItemSubstring +// - selects a random item from input slice +// - transform case the selected item +// - extracts a random substring from the selected item +func getRandomItemSubstring(items []string) string { + if len(items) == 0 { + return "" + } + + item := items[rand.Intn(len(items))] + item = changeCase(item) + if len(item) == 0 { + return "" + } + + runesList := []rune(item) + a, b := rand.Intn(len(runesList)), rand.Intn(len(runesList)) + if a > b { + a, b = b, a + } + return string(runesList[a : b+1]) +} + func changeCase(s string) string { if len(s) == 0 { return s diff --git a/grid-proxy/tests/queries/utils_test.go b/grid-proxy/tests/queries/utils_test.go index 9419e7d0..2b72e40a 100644 --- a/grid-proxy/tests/queries/utils_test.go +++ b/grid-proxy/tests/queries/utils_test.go @@ -1,6 +1,9 @@ package test -import "testing" +import ( + "strings" + "testing" +) func TestGetRandomSliceLengthInts(t *testing.T) { inputSlice := []int{1, 2, 3, 4, 5} @@ -19,3 +22,32 @@ func TestGetRandomSliceLengthEmpty(t *testing.T) { t.Errorf("expected length %d, got %d", length, len(result)) } } + +func TestGetRandomItemSubstring(t *testing.T) { + testItems := []string{"egypt", "belgium", "united states"} + + for i := 0; i < 10; i++ { + result := getRandomItemSubstring(testItems) + if result == "" { + continue + } + + // is result a substring of any item in testItems? check case insensitive + found := false + for _, item := range testItems { + if strings.Contains(strings.ToLower(item), strings.ToLower(result)) { + found = true + break + } + } + + if !found { + t.Errorf("result '%s' is not a substring of any input item", result) + } + } + + emptyResult := getRandomItemSubstring([]string{}) + if emptyResult != "" { + t.Errorf("expected empty string, got '%s'", emptyResult) + } +}