feat: Adding very preliminar and broken test harness.

This commit is contained in:
James Wells 2023-04-03 18:39:01 -07:00
parent ac8c225752
commit 7f9a5777bd
Signed by: jwells
GPG key ID: 73196D10B8E65666
14 changed files with 678 additions and 10 deletions

View file

@ -0,0 +1,65 @@
package chaindb
import (
"bytes"
"os"
"testing"
"time"
gstructs "github.com/dragonheim/gagent/internal/gstructs"
)
const testChainDBPath = "test_chaindb.hcl"
func TestGagentDb(t *testing.T) {
// Create a new GagentDb
db := NewGagentDb()
// Add a row to the database
row := &GagentDbRow{
DBName: "testDB",
Agent: gstructs.AgentDetails{
Client: "testAgent",
Shasum: "v1.0.0",
},
}
db.AddRow(row)
// Check if the row was added correctly
if len(db.ChainRow) != 1 {
t.Errorf("Expected length of ChainRow to be 1, but got %d", len(db.ChainRow))
}
// Check if the timestamp was set correctly
if db.ChainRow[0].Timestamp.After(time.Now()) {
t.Error("Timestamp is incorrectly set in the future")
}
// Write the database to an HCL file
err := db.WriteHCL(testChainDBPath)
if err != nil {
t.Errorf("Error writing HCL file: %v", err)
}
// Load the database from the HCL file
loadedDb := NewGagentDb()
err = loadedDb.LoadHCL(testChainDBPath)
if err != nil {
t.Errorf("Error loading HCL file: %v", err)
}
// Check if the loaded database is the same as the original one
if !bytes.Equal(loadedDb.ChainRow[0].DbCurrHash[:], db.ChainRow[0].DbCurrHash[:]) {
t.Error("Loaded database has a different current hash than the original one")
}
if !bytes.Equal(loadedDb.ChainRow[0].DbPrevHash[:], db.ChainRow[0].DbPrevHash[:]) {
t.Error("Loaded database has a different previous hash than the original one")
}
// Clean up the test HCL file
err = os.Remove(testChainDBPath)
if err != nil {
t.Errorf("Error cleaning up test HCL file: %v", err)
}
}