// Licensed to the LF AI & Data foundation under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you 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 importutil import ( "encoding/binary" "io" "os" "testing" "github.com/milvus-io/milvus-proto/go-api/schemapb" "github.com/sbinet/npyio/npy" "github.com/stretchr/testify/assert" ) type MockReader struct { } func (r *MockReader) Read(p []byte) (n int, err error) { return 0, io.EOF } func Test_CreateNumpyFile(t *testing.T) { // directory doesn't exist data1 := []float32{1, 2, 3, 4, 5} err := CreateNumpyFile("/dummy_not_exist/dummy.npy", data1) assert.NotNil(t, err) // invalid data type data2 := make(map[string]int) err = CreateNumpyFile("/tmp/dummy.npy", data2) assert.NotNil(t, err) } func Test_CreateNumpyData(t *testing.T) { // directory doesn't exist data1 := []float32{1, 2, 3, 4, 5} buf, err := CreateNumpyData(data1) assert.NotNil(t, buf) assert.Nil(t, err) // invalid data type data2 := make(map[string]int) buf, err = CreateNumpyData(data2) assert.NotNil(t, err) assert.Nil(t, buf) } func Test_ConvertNumpyType(t *testing.T) { checkFunc := func(inputs []string, output schemapb.DataType) { for i := 0; i < len(inputs); i++ { dt, err := convertNumpyType(inputs[i]) assert.Nil(t, err) assert.Equal(t, output, dt) } } checkFunc([]string{"b1", "i1", "int8"}, schemapb.DataType_Int8) checkFunc([]string{"i2", "i2", "int16"}, schemapb.DataType_Int16) checkFunc([]string{"i4", "i4", "int32"}, schemapb.DataType_Int32) checkFunc([]string{"i8", "i8", "int64"}, schemapb.DataType_Int64) checkFunc([]string{"f4", "f4", "float32"}, schemapb.DataType_Float) checkFunc([]string{"f8", "f8", "float64"}, schemapb.DataType_Double) dt, err := convertNumpyType("dummy") assert.NotNil(t, err) assert.Equal(t, schemapb.DataType_None, dt) } func Test_StringLen(t *testing.T) { len, utf, err := stringLen("S1") assert.Equal(t, 1, len) assert.False(t, utf) assert.Nil(t, err) len, utf, err = stringLen("2S") assert.Equal(t, 2, len) assert.False(t, utf) assert.Nil(t, err) len, utf, err = stringLen("4U") assert.Equal(t, 4, len) assert.True(t, utf) assert.Nil(t, err) len, utf, err = stringLen("dummy") assert.NotNil(t, err) assert.Equal(t, 0, len) assert.False(t, utf) } func Test_NumpyAdapterSetByteOrder(t *testing.T) { adapter := &NumpyAdapter{ reader: nil, npyReader: &npy.Reader{}, } assert.Nil(t, adapter.Reader()) assert.NotNil(t, adapter.NpyReader()) adapter.npyReader.Header.Descr.Type = "