Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshtin committed Sep 2, 2022
1 parent bf9516a commit b95ff74
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions common/searchattribute/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (t *TestMapper) GetAlias(fieldName string, namespace string) (string, error
if namespace == "error-namespace" {
return "", serviceerror.NewInternal("mapper error")
} else if namespace == "test-namespace" {
if fieldName == "pass-through" {
return fieldName, nil
}

return "alias_of_" + fieldName, nil
}

Expand All @@ -61,6 +65,9 @@ func (t *TestMapper) GetFieldName(alias string, namespace string) (string, error
if namespace == "error-namespace" {
return "", serviceerror.NewInternal("mapper error")
} else if namespace == "test-namespace" {
if alias == "pass-through" {
return alias, nil
}
return strings.TrimPrefix(alias, "alias_of_"), nil
}
return "", serviceerror.NewInvalidArgument("unknown namespace")
Expand All @@ -73,7 +80,7 @@ func Test_ApplyAliases(t *testing.T) {
"wrong_field": {Data: []byte("data23")}, // Wrong unknown name must be ignored.
},
}
sa, err := ApplyAliases(&TestMapper{}, sa, "error-namespace")
_, err := ApplyAliases(&TestMapper{}, sa, "error-namespace")
assert.Error(t, err)
var internalErr *serviceerror.Internal
assert.ErrorAs(t, err, &internalErr)
Expand All @@ -86,7 +93,7 @@ func Test_ApplyAliases(t *testing.T) {
}
sa, err = ApplyAliases(&TestMapper{}, sa, "unknown-namespace")
assert.NoError(t, err)
assert.Len(t, sa.GetIndexedFields(), 0)
assert.Nil(t, sa)

sa = &commonpb.SearchAttributes{
IndexedFields: map[string]*commonpb.Payload{
Expand All @@ -97,6 +104,7 @@ func Test_ApplyAliases(t *testing.T) {
}
sa, err = ApplyAliases(&TestMapper{}, sa, "test-namespace")
assert.NoError(t, err)
assert.NotNil(t, sa)
assert.Len(t, sa.GetIndexedFields(), 2)
assert.EqualValues(t, "data1", sa.GetIndexedFields()["alias_of_field1"].GetData())
assert.EqualValues(t, "data2", sa.GetIndexedFields()["alias_of_field2"].GetData())
Expand All @@ -107,8 +115,20 @@ func Test_ApplyAliases(t *testing.T) {
}
sa, err = ApplyAliases(&TestMapper{}, sa, "error-namespace")
assert.NoError(t, err)
assert.Nil(t, sa)
sa, err = ApplyAliases(&TestMapper{}, sa, "unknown-namespace")
assert.NoError(t, err)
assert.Nil(t, sa)

// Pass through search attributes are not mapped.
sa = &commonpb.SearchAttributes{
IndexedFields: map[string]*commonpb.Payload{
"pass-through": {Data: []byte("data1")},
},
}
sa, err = ApplyAliases(&TestMapper{}, sa, "test-namespace")
assert.NoError(t, err)
assert.Nil(t, sa)
}

func Test_SubstituteAliases(t *testing.T) {
Expand All @@ -117,7 +137,7 @@ func Test_SubstituteAliases(t *testing.T) {
"alias_of_field1": {Data: []byte("data1")},
},
}
sa, err := SubstituteAliases(&TestMapper{}, sa, "error-namespace")
_, err := SubstituteAliases(&TestMapper{}, sa, "error-namespace")
assert.Error(t, err)
var internalErr *serviceerror.Internal
assert.ErrorAs(t, err, &internalErr)
Expand All @@ -128,7 +148,7 @@ func Test_SubstituteAliases(t *testing.T) {
"alias_of_field2": {Data: []byte("data2")},
},
}
sa, err = SubstituteAliases(&TestMapper{}, sa, "unknown-namespace")
_, err = SubstituteAliases(&TestMapper{}, sa, "unknown-namespace")
assert.Error(t, err)
var invalidArgumentErr *serviceerror.InvalidArgument
assert.ErrorAs(t, err, &invalidArgumentErr)
Expand All @@ -141,6 +161,7 @@ func Test_SubstituteAliases(t *testing.T) {
}
sa, err = SubstituteAliases(&TestMapper{}, sa, "test-namespace")
assert.NoError(t, err)
assert.NotNil(t, sa)
assert.Len(t, sa.GetIndexedFields(), 2)
assert.EqualValues(t, "data1", sa.GetIndexedFields()["field1"].GetData())
assert.EqualValues(t, "data2", sa.GetIndexedFields()["field2"].GetData())
Expand All @@ -152,7 +173,7 @@ func Test_SubstituteAliases(t *testing.T) {
"wrong_alias": {Data: []byte("data3")},
},
}
sa, err = SubstituteAliases(&TestMapper{}, sa, "test-namespace")
_, err = SubstituteAliases(&TestMapper{}, sa, "test-namespace")
assert.Error(t, err)
assert.ErrorAs(t, err, &invalidArgumentErr)

Expand All @@ -162,6 +183,18 @@ func Test_SubstituteAliases(t *testing.T) {
}
sa, err = SubstituteAliases(&TestMapper{}, sa, "error-namespace")
assert.NoError(t, err)
assert.Nil(t, sa)
sa, err = SubstituteAliases(&TestMapper{}, sa, "unknown-namespace")
assert.NoError(t, err)
assert.Nil(t, sa)

// Pass through aliases are not substituted.
sa = &commonpb.SearchAttributes{
IndexedFields: map[string]*commonpb.Payload{
"pass-through": {Data: []byte("data1")},
},
}
sa, err = SubstituteAliases(&TestMapper{}, sa, "test-namespace")
assert.NoError(t, err)
assert.Nil(t, sa)
}

0 comments on commit b95ff74

Please sign in to comment.