Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix service tests #142

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a7b9f59
✨ feat(mongo-repo): add mongodb adapter to store data >>> ⏰ 3h
logicalangel Jun 21, 2024
3ef6cf0
🐛 fix(fixes): some fixes and lint problem >>> ⏰ 30m
logicalangel Jun 21, 2024
0a9d1b3
🐛 fix(errors): fix error check of linters >>> ⏰ 5m
logicalangel Jun 21, 2024
573ef5d
🐛 fix(lint): lint >>> ⏰ 2m
logicalangel Jun 21, 2024
dcb856c
✨ feat(postgres): rewrite repositories with gorm - remove gql - sepra…
logicalangel Jun 23, 2024
9a798a0
Merge branch 'feature-postgres' into feature-mongodb-139
logicalangel Jun 23, 2024
ca4d924
🐛 fix(mod): fix unused deps >>> ⏰ 20m
logicalangel Jun 24, 2024
b2d0cfc
🐛 fix(linter): fix errors >>> ⏰ 10m
logicalangel Jun 24, 2024
3517dfd
✨ feat(mongo): complete mongo repositories >>> ⏰ 3h
logicalangel Jun 27, 2024
5fd64e0
🐛 fix(linters): fix linter problems >>> ⏰ 10m
logicalangel Jun 27, 2024
2cff013
✨ feat(postgres): fix problem of modeling postgres tables >>> ⏰ 6h
logicalangel Jun 29, 2024
ba77a0d
🐛 fix(pubsub): fix problem of removing unused channels >>> ⏰ 1h
logicalangel Jun 30, 2024
8ef71c7
🐛 fix(linter): fix errors >>> ⏰ 1m
logicalangel Jun 30, 2024
45824d5
fix-service-tests
logicalangel Jul 6, 2024
61c605b
Merge branch 'feature-mongodb-139' into fix-service-tests
logicalangel Jul 6, 2024
d237076
✨ feat(correctness): add correctness service tests and some fixes >>>…
logicalangel Jul 11, 2024
87212c9
✨ feat(emv-record)!: some fix and add unit-test for emvlog service >>…
logicalangel Jul 14, 2024
13a5610
✨ feat(evmlog-rpc): add process unit test for evmlog and some changes…
logicalangel Jul 14, 2024
7b1694d
✨ feat(uniswap): add unit test for uniswap record >>> ⏰ 4h
logicalangel Jul 15, 2024
c5241d6
🐛 fix(linters): fix problem of linters >>> ⏰ 30m
logicalangel Jul 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
🐛 fix(fixes): some fixes and lint problem >>> ⏰ 30m
  • Loading branch information
logicalangel committed Jun 21, 2024
commit 3ef6cf0c7385e430798b557ad27f2b63fdd03558
2 changes: 1 addition & 1 deletion internal/app/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func Consumer() {

ethRPC := ethereum.New()
pos := pos.New(ethRPC)
db := postgres.New()

db := postgres.New()
eventLogRepo := postgresRepo.NewEventLog(db)
signerRepo := postgresRepo.NewSigner(db)
assetPrice := postgresRepo.NewAssetPrice(db)
Expand Down
2 changes: 1 addition & 1 deletion internal/config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type Network struct {
}

type Mongo struct {
URL string `env:"Mongo_URL" yaml:"url"`
URL string `env:"Mongo_URL" yaml:"url"`
Database string `env:"Mongo_Database" yaml:"database"`
}

Expand Down
5 changes: 3 additions & 2 deletions internal/repository/mongo/assetprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package mongo

import (
"context"

"github.com/TimeleapLabs/unchained/internal/config"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"log"

"github.com/TimeleapLabs/unchained/internal/consts"
"github.com/TimeleapLabs/unchained/internal/ent"
Expand Down Expand Up @@ -80,7 +80,8 @@ func (a AssetPriceRepo) Find(ctx context.Context, block uint64, chain string, na
var result ent.AssetPrice
err := cursor.Decode(&result)
if err != nil {
log.Fatal(err)
utils.Logger.With("err", err).Error("Cant decode signer record")
return nil, err
}

currentRecords = append(currentRecords, &result)
Expand Down
9 changes: 5 additions & 4 deletions internal/repository/mongo/assetprice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package mongo

import (
"context"
"log"
"math/big"
"runtime"
"testing"

"github.com/TimeleapLabs/unchained/internal/config"
"github.com/TimeleapLabs/unchained/internal/model"
"github.com/TimeleapLabs/unchained/internal/repository"
"github.com/TimeleapLabs/unchained/internal/transport/database/mongo"
"github.com/TimeleapLabs/unchained/internal/utils"
"github.com/stretchr/testify/suite"
"github.com/tryvium-travels/memongo"
"log"
"math/big"
"runtime"
"testing"
)

var SampleAssetPrice = model.AssetPrice{
Expand Down
19 changes: 4 additions & 15 deletions internal/repository/mongo/correctness.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package mongo

import (
"context"

"github.com/TimeleapLabs/unchained/internal/config"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"log"

"github.com/TimeleapLabs/unchained/internal/consts"
"github.com/TimeleapLabs/unchained/internal/ent"
Expand All @@ -20,8 +20,6 @@ type CorrectnessRepo struct {
}

func (c CorrectnessRepo) Find(ctx context.Context, hash []byte, topic []byte, timestamp uint64) ([]*ent.CorrectnessReport, error) {
currentRecords := []*ent.CorrectnessReport{}

cursor, err := c.client.
GetConnection().
Database(config.App.Mongo.Database).
Expand All @@ -37,18 +35,9 @@ func (c CorrectnessRepo) Find(ctx context.Context, hash []byte, topic []byte, ti
return nil, consts.ErrInternalError
}

defer cursor.Close(ctx)
for cursor.Next(ctx) {
var result ent.CorrectnessReport
err := cursor.Decode(&result)
if err != nil {
log.Fatal(err)
}

currentRecords = append(currentRecords, &result)
}
if err := cursor.Err(); err != nil {
utils.Logger.With("err", err).Error("Cant fetch asset price records from database")
currentRecords, err := CursorToList[*ent.CorrectnessReport](ctx, cursor)
if err != nil {
utils.Logger.With("err", err).Error("Cant fetch correctness reports from database")
return nil, consts.ErrInternalError
}

Expand Down
16 changes: 9 additions & 7 deletions internal/repository/mongo/correctness_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package mongo

import (
"context"
"log"
"math/big"
"runtime"
"testing"

"github.com/TimeleapLabs/unchained/internal/config"
"github.com/TimeleapLabs/unchained/internal/model"
"github.com/TimeleapLabs/unchained/internal/repository"
"github.com/TimeleapLabs/unchained/internal/transport/database/mongo"
"github.com/TimeleapLabs/unchained/internal/utils"
"github.com/stretchr/testify/suite"
"github.com/tryvium-travels/memongo"
"log"
"math/big"
"runtime"
"testing"
)

var sampleCorrectness = model.Correctness{
Expand Down Expand Up @@ -57,15 +59,15 @@ func (s *CorrectnessRepositoryTestSuite) SetupTest() {
}

func (s *CorrectnessRepositoryTestSuite) TestUpsert() {
err := s.repo.Upsert(nil, sampleCorrectness)
err := s.repo.Upsert(context.TODO(), sampleCorrectness)
s.NoError(err)
}

func (s *CorrectnessRepositoryTestSuite) TestFind() {
err := s.repo.Upsert(nil, sampleCorrectness)
err := s.repo.Upsert(context.TODO(), sampleCorrectness)
s.NoError(err)

result, err := s.repo.Find(nil, sampleCorrectness.Hash, sampleCorrectness.Topic[:], sampleCorrectness.Timestamp)
result, err := s.repo.Find(context.TODO(), sampleCorrectness.Hash, sampleCorrectness.Topic[:], sampleCorrectness.Timestamp)
s.NoError(err)
s.Len(result, 1)
}
Expand Down
17 changes: 3 additions & 14 deletions internal/repository/mongo/eventlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package mongo

import (
"context"

"github.com/TimeleapLabs/unchained/internal/config"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"log"

"github.com/TimeleapLabs/unchained/internal/consts"
"github.com/TimeleapLabs/unchained/internal/ent"
Expand All @@ -20,8 +20,6 @@ type EventLogRepo struct {
}

func (r EventLogRepo) Find(ctx context.Context, block uint64, hash []byte, index uint64) ([]*ent.EventLog, error) {
currentRecords := []*ent.EventLog{}

cursor, err := r.client.
GetConnection().
Database(config.App.Mongo.Database).
Expand All @@ -37,17 +35,8 @@ func (r EventLogRepo) Find(ctx context.Context, block uint64, hash []byte, index
return nil, consts.ErrInternalError
}

defer cursor.Close(ctx)
for cursor.Next(ctx) {
var result ent.EventLog
err := cursor.Decode(&result)
if err != nil {
log.Fatal(err)
}

currentRecords = append(currentRecords, &result)
}
if err := cursor.Err(); err != nil {
currentRecords, err := CursorToList[*ent.EventLog](ctx, cursor)
if err != nil {
utils.Logger.With("err", err).Error("Cant fetch event log records from database")
return nil, consts.ErrInternalError
}
Expand Down
15 changes: 9 additions & 6 deletions internal/repository/mongo/eventlog_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package mongo

import (
"log"
"runtime"
"testing"

"golang.org/x/net/context"

"github.com/TimeleapLabs/unchained/internal/config"
"github.com/TimeleapLabs/unchained/internal/model"
"github.com/TimeleapLabs/unchained/internal/repository"
"github.com/TimeleapLabs/unchained/internal/transport/database/mongo"
"github.com/TimeleapLabs/unchained/internal/utils"
"github.com/stretchr/testify/suite"
"github.com/tryvium-travels/memongo"
"log"
"runtime"
"testing"
)

var sampleEventLog = model.EventLog{
Expand Down Expand Up @@ -59,15 +62,15 @@ func (s *EventLogRepositoryTestSuite) SetupTest() {
}

func (s *EventLogRepositoryTestSuite) TestUpsert() {
err := s.repo.Upsert(nil, sampleEventLog)
err := s.repo.Upsert(context.TODO(), sampleEventLog)
s.NoError(err)
}

func (s *EventLogRepositoryTestSuite) TestFind() {
err := s.repo.Upsert(nil, sampleEventLog)
err := s.repo.Upsert(context.TODO(), sampleEventLog)
s.NoError(err)

result, err := s.repo.Find(nil, sampleEventLog.Block, sampleEventLog.TxHash[:], sampleEventLog.LogIndex)
result, err := s.repo.Find(context.TODO(), sampleEventLog.Block, sampleEventLog.TxHash[:], sampleEventLog.LogIndex)
s.NoError(err)
s.Len(result, 1)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/repository/mongo/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mongo

import (
"context"

"github.com/TimeleapLabs/unchained/internal/config"
"github.com/TimeleapLabs/unchained/internal/consts"
"github.com/TimeleapLabs/unchained/internal/ent"
Expand All @@ -11,15 +12,13 @@ import (
"github.com/TimeleapLabs/unchained/internal/utils"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
)

type signerRepo struct {
client database.MongoDatabase
}

func (s signerRepo) CreateSigners(ctx context.Context, signers []model.Signer) error {

for _, singer := range signers {
opt := options.Update().SetUpsert(true)

Expand Down Expand Up @@ -72,7 +71,8 @@ func (s signerRepo) GetSingerIDsByKeys(ctx context.Context, keys [][]byte) ([]in
var result ent.Signer
err := cursor.Decode(&result)
if err != nil {
log.Fatal(err)
utils.Logger.With("err", err).Error("Cant decode signer record")
return nil, err
}

ids = append(ids, result.ID)
Expand Down
10 changes: 6 additions & 4 deletions internal/repository/mongo/signer_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package mongo

import (
"context"
"log"
"runtime"
"testing"

"github.com/TimeleapLabs/unchained/internal/config"
"github.com/TimeleapLabs/unchained/internal/model"
"github.com/TimeleapLabs/unchained/internal/repository"
"github.com/TimeleapLabs/unchained/internal/transport/database/mongo"
"github.com/TimeleapLabs/unchained/internal/utils"
"github.com/stretchr/testify/suite"
"github.com/tryvium-travels/memongo"
"log"
"runtime"
"testing"
)

var sampleSigner = model.Signer{}
Expand Down Expand Up @@ -46,7 +48,7 @@ func (s *SignerRepositoryTestSuite) SetupTest() {
}

func (s *SignerRepositoryTestSuite) TestUpsert() {
err := s.repo.CreateSigners(nil, []model.Signer{sampleSigner})
err := s.repo.CreateSigners(context.TODO(), []model.Signer{sampleSigner})
s.Require().NoError(err)
}

Expand Down
30 changes: 30 additions & 0 deletions internal/repository/mongo/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mongo

import (
"context"

"github.com/TimeleapLabs/unchained/internal/consts"
"github.com/TimeleapLabs/unchained/internal/utils"
"go.mongodb.org/mongo-driver/mongo"
)

func CursorToList[T any](ctx context.Context, cursor *mongo.Cursor) ([]T, error) {
defer cursor.Close(ctx)

result := []T{}
for cursor.Next(ctx) {
var item T
err := cursor.Decode(&item)
if err != nil {
return nil, err
}
result = append(result, item)
}

if err := cursor.Err(); err != nil {
utils.Logger.With("err", err).Error("Cant fetch asset price records from database")
return nil, consts.ErrInternalError
}

return result, nil
}
1 change: 1 addition & 0 deletions internal/transport/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"context"

"go.mongodb.org/mongo-driver/mongo"

"github.com/TimeleapLabs/unchained/internal/ent"
Expand Down
1 change: 1 addition & 0 deletions internal/transport/database/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mongo

import (
"context"

"github.com/TimeleapLabs/unchained/internal/config"
"github.com/TimeleapLabs/unchained/internal/transport/database"
"github.com/TimeleapLabs/unchained/internal/utils"
Expand Down
Loading