Skip to content

Commit

Permalink
update go and test to comply with new linters
Browse files Browse the repository at this point in the history
  • Loading branch information
chicco785 committed Nov 28, 2024
1 parent 2f244b0 commit eb5b04b
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 28 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,6 @@ issues:
- goconst
- init
- gochecknoinits
- dupl
run:
timeout: 5m
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/zaphiro-technologies/protobuf

go 1.21
go 1.23

require (
github.com/google/uuid v1.6.0
Expand All @@ -9,6 +9,7 @@ require (
)

require (
github.com/ccoveille/go-safecast v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/ccoveille/go-safecast v1.2.0 h1:H4X7aosepsU1Mfk+098CTdKpsDH0cfYJ2RmwXFjgvfc=
github.com/ccoveille/go-safecast v1.2.0/go.mod h1:QqwNjxQ7DAqY0C721OIO9InMk9zCwcsO7tnRuHytad8=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
Expand Down
15 changes: 10 additions & 5 deletions go/grid/v1/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import (
"testing"
"time"

"github.com/ccoveille/go-safecast"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
)

func generateData(dataType int, value uint64, timestamp int64) *Data {
func generateData(dataType int32, value uint64, timestamp int64) *Data {
return &Data{
DataType: DataType(dataType),
Value: &value,
Expand All @@ -37,7 +38,7 @@ func generateDataSet(producerId string, data map[string]*Data) *DataSet {
}

func TestData(t *testing.T) {
for k := 0; k < 46; k++ {
for k := int32(0); k < 46; k++ {
test := generateData(k, rand.Uint64(), time.Now().UnixNano())
buf, err := proto.Marshal(test)
assert.NoError(t, err)
Expand All @@ -52,7 +53,7 @@ func TestData(t *testing.T) {

func TestDataSet(t *testing.T) {
dataMap := map[string]*Data{}
for k := 0; k < 46; k++ {
for k := int32(0); k < 46; k++ {
dataMap[uuid.New().String()] = generateData(k, rand.Uint64(), time.Now().UnixNano())
}
test := generateDataSet("myDataSet", dataMap)
Expand All @@ -66,7 +67,11 @@ func TestDataSet(t *testing.T) {
}

func BenchmarkDataSerialization(b *testing.B) {
test := generateData(rand.Intn(46), rand.Uint64(), time.Now().UnixNano())
randDataType, err := safecast.ToInt32(rand.Intn(46))
if err != nil {
b.Fatalf("Failed to convert int to int32: %v", err)
}
test := generateData(randDataType, rand.Uint64(), time.Now().UnixNano())
for i := 0; i < b.N; i++ {
buf, _ := proto.Marshal(test)
conf := &Data{}
Expand All @@ -76,7 +81,7 @@ func BenchmarkDataSerialization(b *testing.B) {

func BenchmarkDataSetSerialization(b *testing.B) {
dataMap := map[string]*Data{}
for k := 0; k < 46; k++ {
for k := int32(0); k < 46; k++ {
dataMap[uuid.New().String()] = generateData(k, rand.Uint64(), time.Now().UnixNano())
}
test := generateDataSet("myDataSet", dataMap)
Expand Down
19 changes: 14 additions & 5 deletions go/grid/v1/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"
"time"

"github.com/ccoveille/go-safecast"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
Expand All @@ -27,7 +28,7 @@ import (

func generateEvent(
eventId string,
eventSourceType int,
eventSourceType int32,
eventSource string,
occurredAt int64,
message string,
Expand All @@ -44,7 +45,7 @@ func generateEvent(
}

func TestEvent(t *testing.T) {
for k := 0; k < 4; k++ {
for k := int32(0); k < 4; k++ {
test := generateEvent(
uuid.NewString(),
k,
Expand All @@ -66,9 +67,13 @@ func TestEvent(t *testing.T) {
}

func BenchmarkEventSerialization(b *testing.B) {
randEventSourceType, err := safecast.ToInt32(rand.Intn(4))
if err != nil {
b.Fatalf("Failed to convert int to int32: %v", err)
}
test := generateEvent(
uuid.NewString(),
rand.Intn(4),
randEventSourceType,
uuid.NewString(),
time.Now().UnixNano(),
"my message benchmark event",
Expand Down Expand Up @@ -99,7 +104,7 @@ func generateGridEvent(
}

func TestGridEvent(t *testing.T) {
for k := 0; k < 5; k++ {
for k := int32(0); k < 5; k++ {
event := generateEvent(
uuid.NewString(),
k,
Expand Down Expand Up @@ -127,9 +132,13 @@ func TestGridEvent(t *testing.T) {
}

func BenchmarkGridEventSerialization(b *testing.B) {
randEventSourceType, err := safecast.ToInt32(rand.Intn(4))
if err != nil {
b.Fatalf("Failed to convert int to int32: %v", err)
}
event := generateEvent(
uuid.NewString(),
rand.Intn(4),
randEventSourceType,
uuid.NewString(),
time.Now().UnixNano(),
"my message benchmark grid event",
Expand Down
63 changes: 50 additions & 13 deletions go/grid/v1/fault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"
"time"

"github.com/ccoveille/go-safecast"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
Expand All @@ -27,7 +28,7 @@ import (

func generateFault(
faultId string,
faultKind, phaseCode int,
faultKind, phaseCode int32,
updatedAt int64,
faultyEquipmentId string,
) *Fault {
Expand All @@ -41,11 +42,15 @@ func generateFault(
}

func TestFault(t *testing.T) {
for k := 0; k < 5; k++ {
for k := int32(0); k < 5; k++ {
randPhaseCode, err := safecast.ToInt32(rand.Intn(26))
if err != nil {
t.Fatalf("Failed to convert int to int32: %v", err)
}
test := generateFault(
uuid.NewString(),
k,
rand.Intn(26),
randPhaseCode,
time.Now().UnixNano(),
uuid.NewString(),
)
Expand All @@ -62,10 +67,18 @@ func TestFault(t *testing.T) {
}

func BenchmarkFaultSerialization(b *testing.B) {
randFaultKind, err := safecast.ToInt32(rand.Intn(4))
if err != nil {
b.Fatalf("Failed to convert int to int32: %v", err)
}
randPhaseCode, err := safecast.ToInt32(rand.Intn(26))
if err != nil {
b.Fatalf("Failed to convert int to int32: %v", err)
}
test := generateFault(
uuid.NewString(),
rand.Intn(4),
rand.Intn(26),
randFaultKind,
randPhaseCode,
time.Now().UnixNano(),
uuid.NewString(),
)
Expand All @@ -87,8 +100,12 @@ func generateLineFault(
}

func TestLineFault(t *testing.T) {
for k := 0; k < 5; k++ {
fault := generateFault(uuid.NewString(), k, rand.Intn(26), time.Now().UnixNano(), "line1")
for k := int32(0); k < 5; k++ {
randPhaseCode, err := safecast.ToInt32(rand.Intn(26))
if err != nil {
t.Fatalf("Failed to convert int to int32: %v", err)
}
fault := generateFault(uuid.NewString(), k, randPhaseCode, time.Now().UnixNano(), "line1")
test := generateLineFault(fault, rand.Float32())
buf, err := proto.Marshal(test)
assert.NoError(t, err)
Expand All @@ -100,10 +117,18 @@ func TestLineFault(t *testing.T) {
}

func BenchmarkLineFaultSerialization(b *testing.B) {
randFaultKind, err := safecast.ToInt32(rand.Intn(4))
if err != nil {
b.Fatalf("Failed to convert int to int32: %v", err)
}
randPhaseCode, err := safecast.ToInt32(rand.Intn(26))
if err != nil {
b.Fatalf("Failed to convert int to int32: %v", err)
}
fault := generateFault(
uuid.NewString(),
rand.Intn(4),
rand.Intn(26),
randFaultKind,
randPhaseCode,
time.Now().UnixNano(),
"line1",
)
Expand All @@ -126,11 +151,15 @@ func generateEquipmentFault(
}

func TestEquipmentFault(t *testing.T) {
for k := 0; k < 5; k++ {
for k := int32(0); k < 5; k++ {
randPhaseCode, err := safecast.ToInt32(rand.Intn(26))
if err != nil {
t.Fatalf("Failed to convert int to int32: %v", err)
}
fault := generateFault(
uuid.NewString(),
k,
rand.Intn(26),
randPhaseCode,
time.Now().UnixNano(),
"equipment1",
)
Expand All @@ -145,10 +174,18 @@ func TestEquipmentFault(t *testing.T) {
}

func BenchmarkEquipmentFaultSerialization(b *testing.B) {
randFaultKind, err := safecast.ToInt32(rand.Intn(4))
if err != nil {
b.Fatalf("Failed to convert int to int32: %v", err)
}
randPhaseCode, err := safecast.ToInt32(rand.Intn(26))
if err != nil {
b.Fatalf("Failed to convert int to int32: %v", err)
}
fault := generateFault(
uuid.NewString(),
rand.Intn(4),
rand.Intn(26),
randFaultKind,
randPhaseCode,
time.Now().UnixNano(),
"equipment1",
)
Expand Down
17 changes: 13 additions & 4 deletions go/platform/v1/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"
"time"

"github.com/ccoveille/go-safecast"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
Expand All @@ -32,7 +33,7 @@ func generateTask(taskType TaskType, createdAt int64) *Task {
}

func TestTask(t *testing.T) {
for k := 0; k < 5; k++ {
for k := int32(0); k < 5; k++ {
test := generateTask(TaskType(k), time.Now().UnixNano())
buf, err := proto.Marshal(test)
assert.NoError(t, err)
Expand All @@ -58,7 +59,7 @@ func generateNotification(
}

func TestNotification(t *testing.T) {
for k := 0; k < 5; k++ {
for k := int32(0); k < 5; k++ {
test := generateNotification(
NotificationType(k),
time.Now().UnixNano(),
Expand Down Expand Up @@ -94,8 +95,12 @@ func TestTriggerNotification(t *testing.T) {
}

func BenchmarkNotificationSerialization(b *testing.B) {
randInt32, err := safecast.ToInt32(rand.Intn(4))
if err != nil {
b.Fatalf("Failed to convert int to int32: %v", err)
}
test := generateNotification(
NotificationType(rand.Intn(4)),
NotificationType(randInt32),
time.Now().UnixNano(),
"1",
)
Expand All @@ -107,7 +112,11 @@ func BenchmarkNotificationSerialization(b *testing.B) {
}

func BenchmarkTaskSerialization(b *testing.B) {
test := generateTask(TaskType(rand.Intn(5)), time.Now().UnixNano())
randInt32, err := safecast.ToInt32(rand.Intn(5))
if err != nil {
b.Fatalf("Failed to convert int to int32: %v", err)
}
test := generateTask(TaskType(randInt32), time.Now().UnixNano())
for i := 0; i < b.N; i++ {
buf, _ := proto.Marshal(test)
conf := &Task{}
Expand Down

0 comments on commit eb5b04b

Please sign in to comment.