Skip to content

Commit

Permalink
fix cassandra driver: always provide state hash (#28)
Browse files Browse the repository at this point in the history
* always provide state hash

* fix unit tests
  • Loading branch information
kcajmagic authored Jan 30, 2020
1 parent 1cf7df3 commit cc14d71
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion cassandra/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
26 changes: 21 additions & 5 deletions cassandra/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -173,6 +176,7 @@ func TestGetRecordsOfType(t *testing.T) {
}

func TestGetLatestHash(t *testing.T) {
goodUUID := gocql.TimeUUID()
tests := []struct {
description string
expectedHash string
Expand Down Expand Up @@ -210,16 +214,18 @@ 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,
},
}

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)
Expand All @@ -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)
}

})
}
Expand Down

0 comments on commit cc14d71

Please sign in to comment.