github.com/cobalt77/jfrog-client-go@v0.14.5/utils/io/content/contentreader_test.go (about) 1 package content 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/cobalt77/jfrog-client-go/utils/io/fileutils" 9 "github.com/cobalt77/jfrog-client-go/utils/log" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 const ( 14 searchResult = "SearchResult.json" 15 emptySearchResult = "EmptySearchResult.json" 16 ) 17 18 type inputRecord struct { 19 IntKey int `json:"intKey"` 20 StrKey string `json:"strKey"` 21 BoolKey bool `json:"boolKey"` 22 ArrayKey []ArrayValue `json:"arrayKey"` 23 } 24 25 type ArrayValue struct { 26 Key string `json:"key"` 27 Value string `json:"value"` 28 } 29 30 func init() { 31 log.SetLogger(log.NewLogger(log.DEBUG, nil)) 32 } 33 34 func TestContentReaderPath(t *testing.T) { 35 searchResultPath := filepath.Join(getTestDataPath(), searchResult) 36 reader := NewContentReader(searchResultPath, DefaultKey) 37 assert.Equal(t, reader.GetFilePath(), searchResultPath) 38 } 39 40 func TestContentReaderNextRecord(t *testing.T) { 41 searchResultPath := filepath.Join(getTestDataPath(), searchResult) 42 reader := NewContentReader(searchResultPath, DefaultKey) 43 // Read the same file two times 44 for i := 0; i < 2; i++ { 45 var rSlice []inputRecord 46 for item := new(inputRecord); reader.NextRecord(item) == nil; item = new(inputRecord) { 47 rSlice = append(rSlice, *item) 48 } 49 assert.NoError(t, reader.GetError()) 50 // First element 51 assert.Equal(t, 1, rSlice[0].IntKey) 52 assert.Equal(t, "A", rSlice[0].StrKey) 53 assert.Equal(t, true, rSlice[0].BoolKey) 54 assert.ElementsMatch(t, rSlice[0].ArrayKey, []ArrayValue{{Key: "build.number", Value: "6"}}) 55 // Second element 56 assert.Equal(t, 2, rSlice[1].IntKey) 57 assert.Equal(t, "B", rSlice[1].StrKey) 58 assert.Equal(t, false, rSlice[1].BoolKey) 59 assert.Empty(t, rSlice[1].ArrayKey) 60 // Length validation 61 len, err := reader.Length() 62 assert.NoError(t, err) 63 assert.Equal(t, 2, len) 64 reader.Reset() 65 } 66 } 67 68 func TestContentReaderEmptyResult(t *testing.T) { 69 searchResultPath := filepath.Join(getTestDataPath(), emptySearchResult) 70 reader := NewContentReader(searchResultPath, DefaultKey) 71 for item := new(inputRecord); reader.NextRecord(item) == nil; item = new(inputRecord) { 72 t.Error("Can't loop over empty file") 73 } 74 assert.NoError(t, reader.GetError()) 75 } 76 77 func getTestDataPath() string { 78 dir, _ := os.Getwd() 79 return filepath.Join(dir, "..", "..", "..", "tests", "testdata", "contentreaderwriter") 80 } 81 82 func TestCloseReader(t *testing.T) { 83 // Create a file. 84 fd, err := fileutils.CreateTempFile() 85 assert.NoError(t, err) 86 fd.Close() 87 filePathToBeDeleted := fd.Name() 88 89 // Load file to reader 90 reader := NewContentReader(filePathToBeDeleted, DefaultKey) 91 92 // Check file exists 93 _, err = os.Stat(filePathToBeDeleted) 94 assert.NoError(t, err) 95 96 // Check if the file got deleted 97 assert.NoError(t, reader.Close()) 98 _, err = os.Stat(filePathToBeDeleted) 99 assert.True(t, os.IsNotExist(err)) 100 } 101 102 func TestLengthCount(t *testing.T) { 103 searchResultPath := filepath.Join(getTestDataPath(), searchResult) 104 reader := NewContentReader(searchResultPath, DefaultKey) 105 len, err := reader.Length() 106 assert.NoError(t, err) 107 assert.Equal(t, len, 2) 108 // Check cache works with no Reset() being called. 109 len, err = reader.Length() 110 assert.NoError(t, err) 111 assert.Equal(t, len, 2) 112 }