-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a couple of tests for toml utils package
- Loading branch information
Showing
2 changed files
with
94 additions
and
3 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
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,89 @@ | ||
package toml | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/chainlink-testing-framework/utils/ptr" | ||
) | ||
|
||
func TestItSavesStructToTomlAndReadsIt(t *testing.T) { | ||
type someStruct struct { | ||
Hello string `toml:"hello"` | ||
Yolo map[string]string `toml:"yolo"` | ||
Laika *string `toml:"laika"` | ||
Mir *string `toml:"mir"` | ||
} | ||
testStruct := someStruct{ | ||
Hello: "World", | ||
Yolo: map[string]string{ | ||
"yolo": "swag", | ||
}, | ||
Mir: ptr.Ptr("Mir"), | ||
} | ||
|
||
t.Cleanup(func() { | ||
os.Remove("test/test.toml") | ||
os.Remove("test") | ||
os.Remove("test.toml") | ||
}) | ||
|
||
filePath, err := SaveStructAsToml(testStruct, "test", "test") | ||
require.NoError(t, err, "failed to save struct as toml") | ||
|
||
var readStruct someStruct | ||
err = OpenTomlFileAsStruct(filePath, &readStruct) | ||
require.NoError(t, err, "failed to read toml file") | ||
|
||
require.Equal(t, testStruct, readStruct, "read struct is not equal to original struct") | ||
|
||
//save to current folder | ||
filePath, err = SaveStructAsToml(testStruct, "", "test") | ||
require.NoError(t, err, "failed to save struct as toml") | ||
|
||
err = OpenTomlFileAsStruct(filePath, &readStruct) | ||
require.NoError(t, err, "failed to read toml file") | ||
|
||
require.Equal(t, testStruct, readStruct, "read struct is not equal to original struct") | ||
} | ||
|
||
func TestItOverridesExistingFile(t *testing.T) { | ||
type someStruct struct { | ||
Hello string `toml:"hello"` | ||
Yolo map[string]string `toml:"yolo"` | ||
Laika *string `toml:"laika"` | ||
Mir *string `toml:"mir"` | ||
} | ||
testStruct := someStruct{ | ||
Hello: "World", | ||
Yolo: map[string]string{ | ||
"yolo": "swag", | ||
}, | ||
Mir: ptr.Ptr("Mir"), | ||
} | ||
|
||
t.Cleanup(func() { | ||
os.Remove("test.toml") | ||
}) | ||
|
||
err := os.WriteFile("test.toml", []byte("mietek"), 0600) | ||
require.NoError(t, err, "failed to create test file") | ||
|
||
filePath, err := SaveStructAsToml(testStruct, "", "test") | ||
require.NoError(t, err, "failed to save struct as toml") | ||
|
||
var readStruct someStruct | ||
err = OpenTomlFileAsStruct(filePath, &readStruct) | ||
require.NoError(t, err, "failed to read toml file") | ||
|
||
require.Equal(t, testStruct, readStruct, "read struct is not equal to original struct") | ||
} | ||
|
||
func TestItReturnsErrorWhenOpeningNonExistent(t *testing.T) { | ||
var readStruct struct{} | ||
err := OpenTomlFileAsStruct("nonexistent.toml", &readStruct) | ||
require.Error(t, err, "should return error when opening nonexistent file") | ||
require.Contains(t, err.Error(), "no such file or directory", "should return correct error when opening nonexistent file") | ||
} |