kaspad/database/ffldb/ff/location_test.go
stasatdaglabs 2e2492cc5d
[NOD-849] Database tests (#695)
* [NOD-849] Cover ffldb/transaction with tests.

* [NOD-849] Cover cursor.go with tests.

* [NOD-849] Cover ldb/transaction with tests.

* [NOD-849] Cover location.go with tests.

* [NOD-849] Write TestFlatFileMultiFileRollback.

* [NOD-849] Fix merge errors.

* [NOD-849] Fix a comment.

* [NOD-849] Fix a comment.

* [NOD-849] Add a test that makes sure that files get deleted on rollback.

* [NOD-849] Add a test that makes sure that serializeLocation serialized to an expected value.

* [NOD-849] Improve TestFlatFileLocationDeserializationErrors.

* [NOD-849] Fix a copy+paste error.

* [NOD-849] Explain maxFileSize = 16.

* [NOD-849] Remove redundant RollbackUnlessClosed call.

* [NOD-849] Extract bucket to a variable in TestCursorSanity.

* [NOD-849] Rename TestKeyValueTransactionCommit to TestTransactionCommitForLevelDBMethods.

* [NOD-849] Extract prepareXXX into separate functions.

* [NOD-849] Simplify function calls in TestTransactionCloseErrors.

* [NOD-849] Extract validateCurrentCursorKeyAndValue to a separate function.

* [NOD-849] Add a comment over TestCursorSanity.

* [NOD-849] Add a comment over function in TestCursorCloseErrors.

* [NOD-849] Add a comment over function in TestTransactionCloseErrors.

* [NOD-849] Separate TestTransactionCloseErrors to TestTransactionCommitErrors and TestTransactionRollbackErrors.

* [NOD-849] Separate TestTransactionCloseErrors to TestTransactionCommitErrors and TestTransactionRollbackErrors.

* [NOD-849] Fix copy+paste error in comments.

* [NOD-849] Fix merge errors.

* [NOD-849] Merge TestTransactionCommitErrors and TestTransactionRollbackErrors into TestTransactionCloseErrors.

* [NOD-849] Move prepareDatabaseForTest into ffldb_test.go.

* [NOD-849] Add cursorKey to Value error messages in validateCurrentCursorKeyAndValue.
2020-05-03 12:19:09 +03:00

63 lines
2.0 KiB
Go

package ff
import (
"bytes"
"encoding/hex"
"reflect"
"strings"
"testing"
)
func TestFlatFileLocationSerialization(t *testing.T) {
location := &flatFileLocation{
fileNumber: 1,
fileOffset: 2,
dataLength: 3,
}
serializedLocation := serializeLocation(location)
expectedSerializedLocation := []byte{1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0}
if !bytes.Equal(serializedLocation, expectedSerializedLocation) {
t.Fatalf("TestFlatFileLocationSerialization: serializeLocation "+
"returned unexpected bytes. Want: %s, got: %s",
hex.EncodeToString(expectedSerializedLocation), hex.EncodeToString(serializedLocation))
}
deserializedLocation, err := deserializeLocation(serializedLocation)
if err != nil {
t.Fatalf("TestFlatFileLocationSerialization: deserializeLocation "+
"unexpectedly failed: %s", err)
}
if !reflect.DeepEqual(deserializedLocation, location) {
t.Fatalf("TestFlatFileLocationSerialization: original "+
"location and deserialized location aren't the same. Want: %v, "+
"got: %v", location, deserializedLocation)
}
}
func TestFlatFileLocationDeserializationErrors(t *testing.T) {
expectedError := "unexpected serializedLocation length"
tooShortSerializedLocation := []byte{0, 1, 2, 3, 4, 5}
_, err := deserializeLocation(tooShortSerializedLocation)
if err == nil {
t.Fatalf("TestFlatFileLocationSerialization: deserializeLocation " +
"unexpectedly succeeded")
}
if !strings.Contains(err.Error(), expectedError) {
t.Fatalf("TestFlatFileLocationSerialization: deserializeLocation "+
"returned unexpected error. Want: %s, got: %s", expectedError, err)
}
tooLongSerializedLocation := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
_, err = deserializeLocation(tooLongSerializedLocation)
if err == nil {
t.Fatalf("TestFlatFileLocationSerialization: deserializeLocation " +
"unexpectedly succeeded")
}
if !strings.Contains(err.Error(), expectedError) {
t.Fatalf("TestFlatFileLocationSerialization: deserializeLocation "+
"returned unexpected error. Want: %s, got: %s", expectedError, err)
}
}