Skip to content

Commit

Permalink
tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-burghardt committed May 28, 2024
1 parent 65edfa1 commit eb02874
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/utils/ingestion_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func OperationID(ledgerNumber, txNumber, opNumber int32) int64 {
return toid.New(ledgerNumber, txNumber, opNumber).ToInt64()
}

func OperationResult(transaction ingest.LedgerTransaction, opNumber int) *xdr.OperationResultTr {
results, _ := transaction.Result.OperationResults()
func OperationResult(tx ingest.LedgerTransaction, opNumber int) *xdr.OperationResultTr {
results, _ := tx.Result.OperationResults()
tr := results[opNumber-1].MustTr()
return &tr
}
Expand Down
68 changes: 68 additions & 0 deletions internal/utils/ingestion_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package utils

import (
"crypto/sha256"
"testing"

"github.com/stellar/go/xdr"
"github.com/stretchr/testify/assert"
)

func TestMemo(t *testing.T) {
t.Run("type_none", func(t *testing.T) {
memo := xdr.Memo{
Type: xdr.MemoTypeMemoNone,
}

result := Memo(memo, "")
assert.Equal(t, "", result)
})

t.Run("type_text", func(t *testing.T) {
value := "test"
memo := xdr.Memo{
Type: xdr.MemoTypeMemoText,
Text: &value,
}

result := Memo(memo, "")
assert.Equal(t, "test", result)
})

t.Run("type_id", func(t *testing.T) {
value := xdr.Uint64(12345)
memo := xdr.Memo{
Type: xdr.MemoTypeMemoId,
Id: &value,
}

result := Memo(memo, "")
assert.Equal(t, "12345", result)
})

t.Run("type_hash", func(t *testing.T) {
hash := sha256.New()
hash.Write([]byte("test"))
value := xdr.Hash(hash.Sum(nil))
memo := xdr.Memo{
Type: xdr.MemoTypeMemoHash,
Hash: &value,
}

result := Memo(memo, "")
assert.Equal(t, value.HexString(), result)
})

t.Run("type_return", func(t *testing.T) {
hash := sha256.New()
hash.Write([]byte("test"))
value := xdr.Hash(hash.Sum(nil))
memo := xdr.Memo{
Type: xdr.MemoTypeMemoReturn,
RetHash: &value,
}

result := Memo(memo, "")
assert.Equal(t, value.HexString(), result)
})
}
12 changes: 12 additions & 0 deletions internal/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import (
"github.com/stretchr/testify/assert"
)

func TestSanitizeUTF8(t *testing.T) {
t.Run("non_utf8", func(t *testing.T) {
result := SanitizeUTF8("test\xF5")
assert.Equal(t, "test?", result)
})

t.Run("zero_byte", func(t *testing.T) {
result := SanitizeUTF8("test\x00\x00")
assert.Equal(t, "test", result)
})
}

func TestUnwrapInterfaceToPointer(t *testing.T) {
// Test with a string
strValue := "test"
Expand Down

0 comments on commit eb02874

Please sign in to comment.