generated from kurtosis-tech/package-template-repo
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55a0d18
commit 600bd69
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package common | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestDiveFileHandler_ReadFile(t *testing.T) { | ||
// Create a temporary file for testing | ||
tempFile, err := os.CreateTemp("", "testfile.txt") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(tempFile.Name()) | ||
|
||
// Write some data to the temporary file | ||
testData := []byte("test data") | ||
_, err = tempFile.Write(testData) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Instantiate the diveFileHandler | ||
df := NewDiveFileHandler() | ||
|
||
// Test ReadFile function | ||
readData, err := df.ReadFile(tempFile.Name()) | ||
if err != nil { | ||
t.Fatalf("Error reading file: %v", err) | ||
} | ||
|
||
// Compare read data with the original data | ||
if string(readData) != string(testData) { | ||
t.Errorf("Read data doesn't match the original data. Expected: %s, Got: %s", testData, readData) | ||
} | ||
} | ||
|
||
func TestDiveFileHandler_ReadJson(t *testing.T) { | ||
// Create a temporary file for testing | ||
|
||
tempFile, err := os.Create("testfile.json") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(tempFile.Name()) | ||
|
||
// Write JSON data to the temporary file | ||
testData := map[string]string{"key": "value"} | ||
encodedData, err := json.Marshal(testData) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
_, err = tempFile.Write(encodedData) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Close the file before reading it | ||
if err := tempFile.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Instantiate the diveFileHandler | ||
|
||
df := NewDiveFileHandler() | ||
|
||
// Test ReadJson function | ||
var decodedData map[string]string | ||
err = df.ReadJson(tempFile.Name(), &decodedData) | ||
if err != nil { | ||
t.Fatalf("Error reading JSON file: %v", err) | ||
} | ||
|
||
// Compare decoded data with the original data | ||
if decodedData["key"] != testData["key"] { | ||
t.Errorf("Read JSON data doesn't match the original data. Expected: %v, Got: %v", testData, decodedData) | ||
} | ||
} |