323 lines
8.3 KiB
Go
323 lines
8.3 KiB
Go
package jsondb
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"git.0x0001f346.de/andreas/utils"
|
|
)
|
|
|
|
func TestIsValidClusterName(t *testing.T) {
|
|
data := map[string]bool{
|
|
"": false,
|
|
"new/cluster": false,
|
|
"newCluster": true,
|
|
"test": true,
|
|
"1337": true,
|
|
}
|
|
|
|
for d, expectedResult := range data {
|
|
result := isValidName(d)
|
|
if result != expectedResult {
|
|
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSetLocationOfCluster(t *testing.T) {
|
|
newLocation := "/tmp"
|
|
SetLocationOfCluster(newLocation)
|
|
|
|
if locationOfCluster != newLocation {
|
|
t.Errorf("\ngot: %q\nwanted: %q", locationOfCluster, newLocation)
|
|
}
|
|
}
|
|
|
|
func TestGetLocationOfCluster(t *testing.T) {
|
|
newLocation := "/tmp"
|
|
SetLocationOfCluster(newLocation)
|
|
result := GetLocationOfCluster()
|
|
|
|
if result != newLocation {
|
|
t.Errorf("\ngot: %q\nwanted: %q", result, newLocation)
|
|
}
|
|
}
|
|
|
|
func TestSetNameOfCluster(t *testing.T) {
|
|
newName := "autos"
|
|
SetNameOfCluster(newName)
|
|
|
|
if nameOfCluster != newName {
|
|
t.Errorf("\ngot: %q\nwanted: %q", nameOfCluster, newName)
|
|
}
|
|
}
|
|
|
|
func TestGetNameOfCluster(t *testing.T) {
|
|
newName := "autos"
|
|
SetNameOfCluster(newName)
|
|
result := GetNameOfCluster()
|
|
|
|
if result != newName {
|
|
t.Errorf("\ngot: %q\nwanted: %q", result, newName)
|
|
}
|
|
}
|
|
|
|
func TestLoadCluster(t *testing.T) {
|
|
SetLocationOfCluster("/tmp")
|
|
SetNameOfCluster("jsondbTesting")
|
|
LoadCluster()
|
|
|
|
err := LoadCluster()
|
|
if err != nil {
|
|
t.Errorf("\ngot: %q\nwanted: %v", err, nil)
|
|
}
|
|
|
|
expectedResult, _ := json.Marshal(Cluster{})
|
|
|
|
result, _ := json.Marshal(cluster)
|
|
|
|
if string(result) != string(expectedResult) {
|
|
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", string(result), string(expectedResult), getFullPathToDatabase("autos"))
|
|
}
|
|
}
|
|
|
|
func TestCreateDatabase(t *testing.T) {
|
|
SetLocationOfCluster("/tmp")
|
|
SetNameOfCluster("jsondbTesting")
|
|
|
|
err := CreateDatabase("autos")
|
|
if err != nil {
|
|
t.Errorf("\ngot: %q\nwanted: %v", err, nil)
|
|
}
|
|
|
|
if !utils.DoesFileExist(getFullPathToDatabase("autos")) {
|
|
t.Errorf("\nFile %q does not exist", getFullPathToDatabase("autos"))
|
|
}
|
|
}
|
|
|
|
func TestSetKeyInDatabase(t *testing.T) {
|
|
SetLocationOfCluster("/tmp")
|
|
SetNameOfCluster("jsondbTesting")
|
|
LoadCluster()
|
|
|
|
err := SetKeyInDatabase("autos", "hello", "world")
|
|
if err != nil {
|
|
t.Errorf("\ngot: %q\nwanted: %v", err, nil)
|
|
}
|
|
|
|
expectedResult := "{\n\t\"hello\": \"world\"\n}\n"
|
|
result, _ := utils.LoadStringFromFile(getFullPathToDatabase("autos"))
|
|
if result != expectedResult {
|
|
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, getFullPathToDatabase("autos"))
|
|
}
|
|
}
|
|
|
|
func TestGetKeyFromDatabase(t *testing.T) {
|
|
SetLocationOfCluster("/tmp")
|
|
SetNameOfCluster("jsondbTesting")
|
|
LoadCluster()
|
|
SetKeyInDatabase("autos", "Age", int64(18))
|
|
SetKeyInDatabase("autos", "John", "Wick")
|
|
|
|
data := map[string]interface{}{
|
|
"hello": "world",
|
|
"John": "Wick",
|
|
}
|
|
|
|
for d, expectedResult := range data {
|
|
result, _ := GetKeyFromDatabase("autos", d)
|
|
if result != expectedResult {
|
|
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, d)
|
|
}
|
|
}
|
|
|
|
data = map[string]interface{}{
|
|
"Age": int64(18),
|
|
}
|
|
|
|
for d, expectedResult := range data {
|
|
result, _ := GetKeyFromDatabase("autos", d)
|
|
if int64(result.(float64)) != int64(expectedResult.(int64)) {
|
|
t.Errorf("\ngot: %d\nwanted: %d\nfor: %q", int64(result.(float64)), int64(expectedResult.(float64)), d)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDeleteKeyFromDatabase(t *testing.T) {
|
|
SetLocationOfCluster("/tmp")
|
|
SetNameOfCluster("jsondbTesting")
|
|
LoadCluster()
|
|
|
|
err := DeleteKeyFromDatabase("autos", "Age")
|
|
if err != nil {
|
|
t.Errorf("\ngot: %q\nwanted: %v", err, nil)
|
|
}
|
|
|
|
_, err = GetKeyFromDatabase("autos", "Age")
|
|
expectedError := fmt.Errorf("key does not exist")
|
|
if errors.Is(err, expectedError) {
|
|
t.Errorf("\ngot: %q\nwanted: %q", err, expectedError)
|
|
}
|
|
|
|
expectedResult := "{\n\t\"John\": \"Wick\",\n\t\"hello\": \"world\"\n}\n"
|
|
result, _ := utils.LoadStringFromFile(getFullPathToDatabase("autos"))
|
|
if result != expectedResult {
|
|
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, getFullPathToDatabase("autos"))
|
|
}
|
|
}
|
|
|
|
func TestGetDatabase(t *testing.T) {
|
|
SetLocationOfCluster("/tmp")
|
|
SetNameOfCluster("jsondbTesting")
|
|
LoadCluster()
|
|
|
|
expectedResult, _ := json.Marshal(
|
|
Database{
|
|
"John": "Wick",
|
|
"hello": "world",
|
|
},
|
|
)
|
|
|
|
database, _ := GetDatabase("autos")
|
|
result, _ := json.Marshal(database)
|
|
|
|
if string(result) != string(expectedResult) {
|
|
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", string(result), string(expectedResult), getFullPathToDatabase("autos"))
|
|
}
|
|
}
|
|
|
|
func TestSetDatabase(t *testing.T) {
|
|
SetLocationOfCluster("/tmp")
|
|
SetNameOfCluster("jsondbTesting")
|
|
LoadCluster()
|
|
|
|
database := Database{
|
|
"Timon": "Pumba",
|
|
}
|
|
err := SetDatabase("autos", database)
|
|
if err != nil {
|
|
t.Errorf("\ngot: %q\nwanted: %v", err, nil)
|
|
}
|
|
|
|
expectedResult, _ := json.Marshal(
|
|
Database{"Timon": "Pumba"},
|
|
)
|
|
|
|
database, _ = GetDatabase("autos")
|
|
result, _ := json.Marshal(database)
|
|
|
|
if string(result) != string(expectedResult) {
|
|
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", string(result), string(expectedResult), getFullPathToDatabase("autos"))
|
|
}
|
|
|
|
expectedStringResult := "{\n\t\"Timon\": \"Pumba\"\n}\n"
|
|
stringResult, _ := utils.LoadStringFromFile(getFullPathToDatabase("autos"))
|
|
if stringResult != expectedStringResult {
|
|
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", stringResult, expectedStringResult, getFullPathToDatabase("autos"))
|
|
}
|
|
}
|
|
|
|
func TestReloadDatabase(t *testing.T) {
|
|
SetLocationOfCluster("/tmp")
|
|
SetNameOfCluster("jsondbTesting")
|
|
LoadCluster()
|
|
|
|
database := Database{
|
|
"The One": "Neo",
|
|
}
|
|
err := safeDatabase("autos", database)
|
|
if err != nil {
|
|
t.Errorf("\ngot: %q\nwanted: %v", err, nil)
|
|
}
|
|
expectedResult := "{\"The One\":\"Neo\"}"
|
|
|
|
err = ReloadDatabase("autos")
|
|
if err != nil {
|
|
t.Errorf("\ngot: %q\nwanted: %v", err, nil)
|
|
}
|
|
database, _ = GetDatabase("autos")
|
|
result, _ := json.Marshal(database)
|
|
|
|
if string(result) != expectedResult {
|
|
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", string(result), expectedResult, getFullPathToDatabase("autos"))
|
|
}
|
|
}
|
|
|
|
func TestRenameKeyInDatabase(t *testing.T) {
|
|
SetLocationOfCluster("/tmp")
|
|
SetNameOfCluster("jsondbTesting")
|
|
LoadCluster()
|
|
|
|
err := RenameKeyInDatabase("autos", "The One", "Matrix")
|
|
if err != nil {
|
|
t.Errorf("\ngot: %q\nwanted: %v", err, nil)
|
|
}
|
|
|
|
database, _ := GetDatabase("autos")
|
|
result, _ := json.Marshal(database)
|
|
expectedResult := "{\"Matrix\":\"Neo\"}"
|
|
if string(result) != expectedResult {
|
|
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", string(result), expectedResult, getFullPathToDatabase("autos"))
|
|
}
|
|
|
|
expectedStringResult := "{\n\t\"Matrix\": \"Neo\"\n}\n"
|
|
stringResult, _ := utils.LoadStringFromFile(getFullPathToDatabase("autos"))
|
|
if stringResult != expectedStringResult {
|
|
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", stringResult, expectedStringResult, getFullPathToDatabase("autos"))
|
|
}
|
|
}
|
|
|
|
func TestGetDeepCopyOfDatabase(t *testing.T) {
|
|
SetLocationOfCluster("/tmp")
|
|
SetNameOfCluster("jsondbTesting")
|
|
LoadCluster()
|
|
|
|
expectedResult, _ := json.Marshal(
|
|
Database{"Matrix": "Neo"},
|
|
)
|
|
expectedResultAsString := string(expectedResult)
|
|
|
|
database, _ := GetDatabase("autos")
|
|
database["Matrix"] = "Morpheus"
|
|
|
|
result, _ := json.Marshal(cluster["autos"])
|
|
resultAsString := string(result)
|
|
|
|
if expectedResultAsString != resultAsString {
|
|
t.Errorf("\ngot: %q\nwanted: %q", resultAsString, expectedResultAsString)
|
|
}
|
|
}
|
|
|
|
func TestRenameDatabase(t *testing.T) {
|
|
SetLocationOfCluster("/tmp")
|
|
SetNameOfCluster("jsondbTesting")
|
|
LoadCluster()
|
|
|
|
err := RenameDatabase("autos", "schiffe")
|
|
if err != nil {
|
|
t.Errorf("\ngot: %q\nwanted: %v", err, nil)
|
|
}
|
|
|
|
expectedResult, _ := json.Marshal(
|
|
Cluster{
|
|
"schiffe": Database{"Matrix": "Neo"},
|
|
},
|
|
)
|
|
expectedResultAsString := string(expectedResult)
|
|
|
|
result, _ := json.Marshal(cluster)
|
|
resultAsString := string(result)
|
|
|
|
if expectedResultAsString != resultAsString {
|
|
t.Errorf("\ngot: %q\nwanted: %q", resultAsString, expectedResultAsString)
|
|
}
|
|
|
|
expectedStringResult := "{\n\t\"Matrix\": \"Neo\"\n}\n"
|
|
stringResult, _ := utils.LoadStringFromFile(getFullPathToDatabase("schiffe"))
|
|
if stringResult != expectedStringResult {
|
|
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", stringResult, expectedStringResult, getFullPathToDatabase("autos"))
|
|
}
|
|
}
|