github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/load_file_test.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package unary 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/matrixorigin/matrixone/pkg/common/moerr" 22 "github.com/matrixorigin/matrixone/pkg/container/vector" 23 "github.com/matrixorigin/matrixone/pkg/fileservice" 24 "github.com/matrixorigin/matrixone/pkg/testutil" 25 "github.com/smartystreets/goconvey/convey" 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func TestLoadFile(t *testing.T) { 30 dir := t.TempDir() 31 proc := testutil.NewProc() 32 ctx := context.Background() 33 filepath := dir + "test" 34 fs, readPath, err := fileservice.GetForETL(proc.FileService, filepath) 35 err = fs.Write(ctx, fileservice.IOVector{ 36 FilePath: readPath, 37 Entries: []fileservice.IOEntry{ 38 { 39 Offset: 0, 40 Size: 4, 41 Data: []byte("1234"), 42 }, 43 { 44 Offset: 4, 45 Size: 4, 46 Data: []byte("5678"), 47 }, 48 }, 49 }) 50 assert.Nil(t, err) 51 52 cases1 := []struct { 53 name string 54 filename string 55 want []byte 56 }{ 57 { 58 name: "File Case", 59 filename: filepath, 60 want: []byte("12345678"), 61 }, 62 } 63 for _, c := range cases1 { 64 convey.Convey(c.name, t, func() { 65 var inStrs []string 66 inStrs = append(inStrs, c.filename) 67 inVector := testutil.MakeVarcharVector(inStrs, nil) 68 res, err := LoadFile([]*vector.Vector{inVector}, proc) 69 convey.So(err, convey.ShouldBeNil) 70 convey.So(res.GetBytes(0), convey.ShouldResemble, c.want) 71 }) 72 } 73 74 //Test empty file 75 filepath = dir + "emptyfile" 76 fs, readPath, err = fileservice.GetForETL(proc.FileService, filepath) 77 err = fs.Write(ctx, fileservice.IOVector{ 78 FilePath: readPath, 79 Entries: []fileservice.IOEntry{ 80 { 81 Offset: 0, 82 Size: 0, 83 Data: []byte(""), 84 }, 85 }, 86 }) 87 assert.Nil(t, err) 88 89 cases2 := []struct { 90 name string 91 filename string 92 want []byte 93 }{ 94 { 95 name: "Empty File Case", 96 filename: filepath, 97 want: []byte{}, 98 }, 99 } 100 for _, c := range cases2 { 101 convey.Convey(c.name, t, func() { 102 var inStrs []string 103 inStrs = append(inStrs, c.filename) 104 inVector := testutil.MakeVarcharVector(inStrs, nil) 105 res, err := LoadFile([]*vector.Vector{inVector}, proc) 106 convey.So(err, convey.ShouldBeNil) 107 convey.So(res.GetBytes(0), convey.ShouldResemble, c.want) 108 }) 109 } 110 111 // Test Error 112 size := 1024 * 1024 * 1 113 data := make([]byte, size) 114 filepath = dir + "bigfile" 115 fs, readPath, err = fileservice.GetForETL(proc.FileService, filepath) 116 err = fs.Write(ctx, fileservice.IOVector{ 117 FilePath: readPath, 118 Entries: []fileservice.IOEntry{ 119 { 120 Offset: 0, 121 Size: int64(size), 122 Data: data, 123 }, 124 }, 125 }) 126 assert.Nil(t, err) 127 128 cases3 := []struct { 129 name string 130 filename string 131 want error 132 }{ 133 { 134 name: "Error Case", 135 filename: filepath, 136 want: moerr.NewInternalError(ctx, "Data too long for blob"), 137 }, 138 } 139 for _, c := range cases3 { 140 convey.Convey(c.name, t, func() { 141 var inStrs []string 142 inStrs = append(inStrs, c.filename) 143 inVector := testutil.MakeVarcharVector(inStrs, nil) 144 _, err := LoadFile([]*vector.Vector{inVector}, proc) 145 convey.So(err, convey.ShouldNotBeNil) 146 convey.So(err, convey.ShouldResemble, c.want) 147 }) 148 } 149 }