Skip to content

Commit

Permalink
test: add unit test for model and digest (#2538)
Browse files Browse the repository at this point in the history
Signed-off-by: MinH-09 <2107139596@qq.com>
  • Loading branch information
MinH-09 authored Jul 11, 2023
1 parent 01287f4 commit d0b9bbe
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 2 deletions.
4 changes: 2 additions & 2 deletions manager/types/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ type ModelEvaluation struct {
MAE float64 `json:"mae" binding:"omitempty,gte=0"`
}

// MakeModelName returns model name of GNN.
// MakeGNNModelName returns model name of GNN.
func MakeGNNModelName(hostname, ip string, clusterID uint64) string {
return fmt.Sprintf("%s_%s_%s_%s", ip, hostname, fmt.Sprint(clusterID), GNNModelNameSuffix)
}

// MakeModelName returns model name of MLP.
// MakeMLPModelName returns model name of MLP.
func MakeMLPModelName(hostname, ip string, clusterID uint64) string {
return fmt.Sprintf("%s_%s_%s_%s", ip, hostname, fmt.Sprint(clusterID), MLPModelNameSuffix)
}
Expand Down
199 changes: 199 additions & 0 deletions manager/types/model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* Copyright 2023 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package types

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_MakeGNNModelName(t *testing.T) {
tests := []struct {
name string
hostname string
ip string
clusterID uint64
expect func(t *testing.T, s string)
}{
{
name: "make gnn model name",
hostname: "foo",
ip: "127.0.0.1",
clusterID: uint64(1),
expect: func(t *testing.T, s string) {
assert := assert.New(t)
assert.Equal(s, "127.0.0.1_foo_1_gnn")
},
},
{
name: "hostname is empty",
hostname: "",
ip: "127.0.0.1",
clusterID: uint64(1),
expect: func(t *testing.T, s string) {
assert := assert.New(t)
assert.Equal(s, "127.0.0.1__1_gnn")
},
},
{
name: "ip is empty",
hostname: "foo",
ip: "",
clusterID: uint64(1),
expect: func(t *testing.T, s string) {
assert := assert.New(t)
assert.Equal(s, "_foo_1_gnn")
},
},
{
name: "hostname and ip are empty",
hostname: "",
ip: "",
clusterID: uint64(1),
expect: func(t *testing.T, s string) {
assert := assert.New(t)
assert.Equal(s, "__1_gnn")
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.expect(t, MakeGNNModelName(tc.hostname, tc.ip, tc.clusterID))
})
}
}

func Test_MakeMLPModelName(t *testing.T) {
tests := []struct {
name string
hostname string
ip string
clusterID uint64
expect func(t *testing.T, s string)
}{
{
name: "make mlp model name",
hostname: "foo",
ip: "127.0.0.1",
clusterID: uint64(1),
expect: func(t *testing.T, s string) {
assert := assert.New(t)
assert.Equal(s, "127.0.0.1_foo_1_mlp")
},
},
{
name: "hostname is empty",
hostname: "",
ip: "127.0.0.1",
clusterID: uint64(1),
expect: func(t *testing.T, s string) {
assert := assert.New(t)
assert.Equal(s, "127.0.0.1__1_mlp")
},
},
{
name: "ip is empty",
hostname: "foo",
ip: "",
clusterID: uint64(1),
expect: func(t *testing.T, s string) {
assert := assert.New(t)
assert.Equal(s, "_foo_1_mlp")
},
},
{
name: "hostname and ip are empty",
hostname: "",
ip: "",
clusterID: uint64(1),
expect: func(t *testing.T, s string) {
assert := assert.New(t)
assert.Equal(s, "__1_mlp")
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.expect(t, MakeMLPModelName(tc.hostname, tc.ip, tc.clusterID))
})
}
}

func Test_MakeObjectKeyOfModelFile(t *testing.T) {
tests := []struct {
name string
modelName string
version int
expect func(t *testing.T, s string)
}{
{
name: "make objectKey of model file",
modelName: "foo",
version: 1,
expect: func(t *testing.T, s string) {
assert := assert.New(t)
assert.Equal(s, "foo/1/model.graphdef")
},
},
{
name: "modelName is empty",
modelName: "",
version: 1,
expect: func(t *testing.T, s string) {
assert := assert.New(t)
assert.Equal(s, "/1/model.graphdef")
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.expect(t, MakeObjectKeyOfModelFile(tc.modelName, tc.version))
})
}
}

func Test_MakeObjectKeyOfModelConfigFile(t *testing.T) {
tests := []struct {
name string
modelName string
version int
expect func(t *testing.T, s string)
}{
{
name: "make objectKey of model file",
modelName: "foo",
expect: func(t *testing.T, s string) {
assert := assert.New(t)
assert.Equal(s, "foo/config.pbtxt")
},
},
{
name: "modelName is empty",
modelName: "",
expect: func(t *testing.T, s string) {
assert := assert.New(t)
assert.Equal(s, "/config.pbtxt")
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.expect(t, MakeObjectKeyOfModelConfigFile(tc.modelName))
})
}
}
4 changes: 4 additions & 0 deletions pkg/digest/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,7 @@ func TestDigest_MD5FromBytes(t *testing.T) {
func TestDigest_SHA256FromStrings(t *testing.T) {
assert.Equal(t, "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", SHA256FromStrings("hello"))
}

func TestDigest_SHA256FromBytes(t *testing.T) {
assert.Equal(t, "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", SHA256FromBytes([]byte("hello")))
}

0 comments on commit d0b9bbe

Please sign in to comment.