Add tests for LoadLabelRules, LabelsFromFile, Labels and LoadModel functions

This commit is contained in:
Theresa Gresch 2019-07-17 10:48:23 +02:00
parent e782abd7e3
commit 4aa110396e

View file

@ -8,7 +8,27 @@ import (
"github.com/stretchr/testify/assert"
)
func TestTensorFlow_LoadLabelRules(t *testing.T) {
t.Run("labels.yml exists", func(t *testing.T) {
conf := config.NewTestConfig()
tensorFlow := NewTensorFlow(conf)
result := tensorFlow.loadLabelRules()
assert.Nil(t, result)
})
t.Run("labels.yml not existing in config path", func(t *testing.T) {
conf := config.NewTestErrorConfig()
tensorFlow := NewTensorFlow(conf)
result := tensorFlow.loadLabelRules()
assert.Contains(t, result.Error(), "label rules file not found")
})
}
func TestTensorFlow_LabelsFromFile(t *testing.T) {
t.Run("/chameleon_lime.jpg", func(t *testing.T) {
conf := config.TestConfig()
tensorFlow := NewTensorFlow(conf)
@ -31,6 +51,16 @@ func TestTensorFlow_LabelsFromFile(t *testing.T) {
assert.Equal(t, "chameleon", result[0].Name)
assert.Equal(t, 7, result[0].Uncertainty)
})
t.Run("not existing file", func(t *testing.T) {
conf := config.TestConfig()
tensorFlow := NewTensorFlow(conf)
result, err := tensorFlow.LabelsFromFile(conf.ExamplesPath() + "/notexisting.jpg")
assert.Contains(t, err.Error(), "no such file or directory")
assert.Empty(t, result)
})
}
func TestTensorFlow_Labels(t *testing.T) {
@ -38,6 +68,7 @@ func TestTensorFlow_Labels(t *testing.T) {
t.Skip("skipping test in short mode.")
}
t.Run("/chameleon_lime.jpg", func(t *testing.T) {
conf := config.TestConfig()
tensorFlow := NewTensorFlow(conf)
@ -59,13 +90,8 @@ func TestTensorFlow_Labels(t *testing.T) {
assert.Equal(t, 100-93, result[0].Uncertainty)
}
}
func TestTensorFlow_Labels_Dog(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
})
t.Run("/dog_orange.jpg", func(t *testing.T) {
conf := config.TestConfig()
tensorFlow := NewTensorFlow(conf)
@ -89,4 +115,50 @@ func TestTensorFlow_Labels_Dog(t *testing.T) {
assert.Equal(t, 34, result[0].Uncertainty)
assert.Equal(t, 91, result[1].Uncertainty)
}
})
t.Run("/Random.docx", func(t *testing.T) {
conf := config.TestConfig()
tensorFlow := NewTensorFlow(conf)
if imageBuffer, err := ioutil.ReadFile(conf.ExamplesPath() + "/Random.docx"); err != nil {
t.Error(err)
} else {
result, err := tensorFlow.Labels(imageBuffer)
assert.Empty(t, result)
assert.Contains(t, err.Error(), "invalid image")
}
})
t.Run("/6720px_white.jpg", func(t *testing.T) {
conf := config.TestConfig()
tensorFlow := NewTensorFlow(conf)
if imageBuffer, err := ioutil.ReadFile(conf.ExamplesPath() + "/6720px_white.jpg"); err != nil {
t.Error(err)
} else {
result, err := tensorFlow.Labels(imageBuffer)
assert.Empty(t, result)
assert.Nil(t, err)
}
})
}
func TestTensorFlow_LoadModel(t *testing.T) {
t.Run("model path exists", func(t *testing.T) {
conf := config.NewTestConfig()
tensorFlow := NewTensorFlow(conf)
result := tensorFlow.loadModel()
assert.Nil(t, result)
})
t.Run("model path does not exist", func(t *testing.T) {
conf := config.NewTestErrorConfig()
tensorFlow := NewTensorFlow(conf)
result := tensorFlow.loadModel()
assert.Contains(t, result.Error(), "Could not find SavedModel")
})
}