From cc14d71484509ce5ae384c7894220f43e75b2631 Mon Sep 17 00:00:00 2001 From: Jack Murdock Date: Thu, 30 Jan 2020 15:10:48 -0800 Subject: [PATCH] fix cassandra driver: always provide state hash (#28) * always provide state hash * fix unit tests --- CHANGELOG.md | 6 +++++- cassandra/db.go | 10 +++++++++- cassandra/db_test.go | 26 +++++++++++++++++++++----- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d6a3c5..e496518 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [v0.5.2] +- Always provide a state hash cassandra [#28](https://github.com/xmidt-org/codex-db/pull/28) + ## [v0.5.1] - Fixed get record of status type [#27](https://github.com/xmidt-org/codex-db/pull/27) - Improved cassandra connect logic [#27](https://github.com/xmidt-org/codex-db/pull/27) @@ -47,7 +50,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [v0.1.0] - Initial creation, moved from: https://github.com/xmidt-org/codex-deploy -[Unreleased]: https://github.com/xmidt-org/codex-db/compare/v0.5.1..HEAD +[Unreleased]: https://github.com/xmidt-org/codex-db/compare/v0.5.2..HEAD +[v0.5.2]: https://github.com/xmidt-org/codex-db/compare/v0.5.1..v0.5.2 [v0.5.1]: https://github.com/xmidt-org/codex-db/compare/v0.5.0..v0.5.1 [v0.5.0]: https://github.com/xmidt-org/codex-db/compare/v0.4.0..v0.5.0 [v0.4.0]: https://github.com/xmidt-org/codex-db/compare/v0.3.3..v0.4.0 diff --git a/cassandra/db.go b/cassandra/db.go index 61654ca..fb0a6e5 100644 --- a/cassandra/db.go +++ b/cassandra/db.go @@ -220,17 +220,25 @@ func (c *Connection) GetStateHash(records []db.Record) (string, error) { } original := gocql.UUIDFromTime(time.Time{}) latest := original + originalBirthDate := int64(0) + latestBirthDate := originalBirthDate for _, elem := range records { uuid, err := gocql.ParseUUID(elem.RowID) if err != nil { + if elem.BirthDate > latestBirthDate { + latestBirthDate = elem.BirthDate + } continue } if uuid.Time().UnixNano() > latest.Time().UnixNano() { latest = uuid } } + if latest == original && latestBirthDate == originalBirthDate { + return gocql.TimeUUID().String(), errors.New("no hash or birthdate found") + } if latest == original { - return "", errors.New("no hash found") + return gocql.UUIDFromTime(time.Unix(0, latestBirthDate)).String(), errors.New("no hash found") } return latest.String(), nil } diff --git a/cassandra/db_test.go b/cassandra/db_test.go index 5571110..3ca4c5e 100644 --- a/cassandra/db_test.go +++ b/cassandra/db_test.go @@ -20,11 +20,14 @@ package cassandra import ( "encoding/json" "errors" + "fmt" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" db "github.com/xmidt-org/codex-db" "github.com/xmidt-org/webpa-common/xmetrics/xmetricstest" "github.com/xmidt-org/wrp-go/wrp" + "github.com/yugabyte/gocql" "testing" "time" ) @@ -173,6 +176,7 @@ func TestGetRecordsOfType(t *testing.T) { } func TestGetLatestHash(t *testing.T) { + goodUUID := gocql.TimeUUID() tests := []struct { description string expectedHash string @@ -210,9 +214,9 @@ func TestGetLatestHash(t *testing.T) { hasError: false, }, { - description: "empty record record", - expectedHash: "", - records: []db.Record{{}}, + description: "empty record", + expectedHash: goodUUID.String(), + records: []db.Record{{BirthDate: goodUUID.Time().UnixNano()}}, hasError: true, }, } @@ -220,6 +224,8 @@ func TestGetLatestHash(t *testing.T) { for _, tc := range tests { t.Run(tc.description, func(t *testing.T) { assert := assert.New(t) + require := require.New(t) + mockObj := new(mockFinder) p := xmetricstest.NewProvider(nil, Metrics) m := NewMeasures(p) @@ -230,12 +236,22 @@ func TestGetLatestHash(t *testing.T) { hash, err := dbConnection.GetStateHash(tc.records) if tc.hasError { + fmt.Println(err) assert.Error(err) } else { assert.NoError(err) } - assert.Equal(tc.expectedHash, hash) - mockObj.AssertExpectations(t) + if hash != "" { + hashedTime, err := gocql.ParseUUID(hash) + require.NoError(err) + expectedTime, err := gocql.ParseUUID(tc.expectedHash) + require.NoError(err) + + assert.Equal(expectedTime.Time().UnixNano(), hashedTime.Time().UnixNano()) + mockObj.AssertExpectations(t) + } else { + assert.Equal(tc.expectedHash, hash) + } }) }