-
Notifications
You must be signed in to change notification settings - Fork 0
Implement Repository and InvestmentStore Interfaces for Database and Cache Operations #16
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1beb697
Add investment domain interfaces and MongoDB/Redis repositories
jyx0615 7d03bd9
Implement CreateOpportunity functionality and update related structures
jyx0615 1d04662
Rename package 'responde' to 'respond' for consistency across HTTP re…
jyx0615 a375767
Fix typo in MongoInvestmentRepository type name and update related me…
jyx0615 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package mongodb | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "go.mongodb.org/mongo-driver/bson" | ||
| "go.mongodb.org/mongo-driver/mongo" | ||
|
|
||
| "github.com/Financial-Partner/server/internal/entities" | ||
| ) | ||
|
|
||
| type MongoInvestmentRepository struct { | ||
| collection *mongo.Collection | ||
| } | ||
|
|
||
| func NewInvestmentRepository(db MongoClient) *MongoInvestmentRepository { | ||
| return &MongoInvestmentRepository{ | ||
| collection: db.Collection("investments"), | ||
| } | ||
| } | ||
|
|
||
| func (r *MongoInvestmentRepository) CreateInvestment(ctx context.Context, entity *entities.Investment) (*entities.Investment, error) { | ||
| _, err := r.collection.InsertOne(ctx, entity) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return entity, nil | ||
| } | ||
|
|
||
| func (r *MongoInvestmentRepository) CreateOpportunity(ctx context.Context, entity *entities.Opportunity) (*entities.Opportunity, error) { | ||
| _, err := r.collection.InsertOne(ctx, entity) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return entity, nil | ||
| } | ||
|
|
||
| func (r *MongoInvestmentRepository) FindOpportunitiesByUserId(ctx context.Context, userID string) ([]entities.Opportunity, error) { | ||
| var opportunities []entities.Opportunity | ||
| cursor, err := r.collection.Find(ctx, bson.M{"user_id": userID}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer cursor.Close(ctx) | ||
|
|
||
| if err := cursor.All(ctx, &opportunities); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return opportunities, nil | ||
| } | ||
|
|
||
| func (r *MongoInvestmentRepository) FindInvestmentsByUserId(ctx context.Context, userID string) ([]entities.Investment, error) { | ||
| var investments []entities.Investment | ||
| cursor, err := r.collection.Find(ctx, bson.M{"user_id": userID}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer cursor.Close(ctx) | ||
|
|
||
| if err := cursor.All(ctx, &investments); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return investments, nil | ||
| } |
200 changes: 200 additions & 0 deletions
200
internal/infrastructure/persistence/mongodb/investment_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| package mongodb_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/Financial-Partner/server/internal/entities" | ||
| "github.com/Financial-Partner/server/internal/infrastructure/persistence/mongodb" | ||
| "github.com/stretchr/testify/assert" | ||
| "go.mongodb.org/mongo-driver/bson" | ||
| "go.mongodb.org/mongo-driver/bson/primitive" | ||
| "go.mongodb.org/mongo-driver/mongo/integration/mtest" | ||
| ) | ||
|
|
||
| func TestMongoInvestmentRepository(t *testing.T) { | ||
| mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock)) | ||
|
|
||
| testUserID := primitive.NewObjectID().Hex() | ||
|
|
||
| testInvestment := &entities.Investment{ | ||
| ID: primitive.NewObjectID(), | ||
| UserID: primitive.NewObjectID(), | ||
| OpportunityID: primitive.NewObjectID(), | ||
| Amount: 1000, | ||
| CreatedAt: time.Date(2023, time.January, 31, 0, 0, 0, 0, time.UTC), | ||
| UpdatedAt: time.Date(2023, time.January, 31, 0, 0, 0, 0, time.UTC), | ||
| } | ||
| testInvestments := []entities.Investment{ | ||
| *testInvestment, | ||
| } | ||
|
|
||
| testOpportunity := &entities.Opportunity{ | ||
| ID: primitive.NewObjectID(), | ||
| Title: "real estate", | ||
| Description: "Invest in real estate", | ||
| Tags: []string{"high risk", "long term"}, | ||
| IsIncrease: true, | ||
| Variation: 10, | ||
| Duration: "1 year", | ||
| MinAmount: 1000, | ||
| CreatedAt: time.Date(2023, time.January, 31, 0, 0, 0, 0, time.UTC), | ||
| UpdatedAt: time.Date(2023, time.January, 31, 0, 0, 0, 0, time.UTC), | ||
| } | ||
| testOpportunities := []entities.Opportunity{ | ||
| *testOpportunity, | ||
| } | ||
|
|
||
| var testInvestmentDocs []bson.D | ||
| for _, investment := range testInvestments { | ||
| investmentBSON, err := bson.Marshal(investment) | ||
| assert.NoError(t, err) | ||
| var investmentDoc bson.D | ||
| err = bson.Unmarshal(investmentBSON, &investmentDoc) | ||
| assert.NoError(t, err) | ||
| testInvestmentDocs = append(testInvestmentDocs, investmentDoc) | ||
| } | ||
|
|
||
| var testOpportunityDocs []bson.D | ||
| for _, opportunity := range testOpportunities { | ||
| opportunityBSON, err := bson.Marshal(opportunity) | ||
| assert.NoError(t, err) | ||
| var opportunityDoc bson.D | ||
| err = bson.Unmarshal(opportunityBSON, &opportunityDoc) | ||
| assert.NoError(t, err) | ||
| testOpportunityDocs = append(testOpportunityDocs, opportunityDoc) | ||
| } | ||
|
|
||
| t.Run("CreateInvestment", func(t *testing.T) { | ||
| mt.Run("error", func(mt *mtest.T) { | ||
| mt.AddMockResponses( | ||
| mtest.CreateCommandErrorResponse(mtest.CommandError{ | ||
| Code: 11000, | ||
| Message: "Duplicate key error", | ||
| }), | ||
| ) | ||
|
|
||
| repo := mongodb.NewInvestmentRepository(mt.DB) | ||
| result, err := repo.CreateInvestment(context.Background(), testInvestment) | ||
| assert.Error(t, err) | ||
| assert.Nil(t, result) | ||
| }) | ||
|
|
||
| mt.Run("success", func(mt *mtest.T) { | ||
| mt.AddMockResponses( | ||
| mtest.CreateSuccessResponse(), | ||
| ) | ||
|
|
||
| repo := mongodb.NewInvestmentRepository(mt.DB) | ||
| result, err := repo.CreateInvestment(context.Background(), testInvestment) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, result) | ||
| assert.Equal(t, testInvestment, result) | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("CreateOpportunity", func(t *testing.T) { | ||
| mt.Run("error", func(mt *mtest.T) { | ||
| mt.AddMockResponses( | ||
| mtest.CreateCommandErrorResponse(mtest.CommandError{ | ||
| Code: 11000, | ||
| Message: "Duplicate key error", | ||
| }), | ||
| ) | ||
|
|
||
| repo := mongodb.NewInvestmentRepository(mt.DB) | ||
| result, err := repo.CreateOpportunity(context.Background(), testOpportunity) | ||
| assert.Error(t, err) | ||
| assert.Nil(t, result) | ||
| }) | ||
|
|
||
| mt.Run("success", func(mt *mtest.T) { | ||
| mt.AddMockResponses( | ||
| mtest.CreateSuccessResponse(), | ||
| ) | ||
|
|
||
| repo := mongodb.NewInvestmentRepository(mt.DB) | ||
| result, err := repo.CreateOpportunity(context.Background(), testOpportunity) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, result) | ||
| assert.Equal(t, testOpportunity, result) | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("FindOpportunitiesByUserId", func(t *testing.T) { | ||
| mt.Run("database error", func(mt *mtest.T) { | ||
| mt.AddMockResponses( | ||
| mtest.CreateCommandErrorResponse(mtest.CommandError{ | ||
| Code: 11000, | ||
| Message: "Database error", | ||
| }), | ||
| ) | ||
|
|
||
| repo := mongodb.NewInvestmentRepository(mt.DB) | ||
| result, err := repo.FindOpportunitiesByUserId(context.Background(), testUserID) | ||
| assert.Error(t, err) | ||
| assert.Nil(t, result) | ||
| }) | ||
| mt.Run("not found", func(mt *mtest.T) { | ||
| mt.AddMockResponses(mtest.CreateCursorResponse(0, "foo.bar", mtest.FirstBatch)) | ||
| repo := mongodb.NewInvestmentRepository(mt.DB) | ||
| result, err := repo.FindOpportunitiesByUserId(context.Background(), testUserID) | ||
| assert.NoError(t, err) | ||
| assert.Nil(t, result) | ||
| }) | ||
| mt.Run("success", func(mt *mtest.T) { | ||
| mt.AddMockResponses( | ||
| mtest.CreateCursorResponse(1, "foo.bar", mtest.FirstBatch, testOpportunityDocs...), | ||
| mtest.CreateCursorResponse(0, "foo.bar", mtest.NextBatch), | ||
| ) | ||
| repo := mongodb.NewInvestmentRepository(mt.Client.Database("testdb")) | ||
| result, err := repo.FindOpportunitiesByUserId(context.Background(), testUserID) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, result) | ||
| assert.Len(t, result, len(testOpportunityDocs)) | ||
| // Validate each investment | ||
| for i, opportunity := range result { | ||
| assert.Equal(t, testOpportunities[i], opportunity) | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("FindInvestmentsByUserId", func(t *testing.T) { | ||
| mt.Run("database error", func(mt *mtest.T) { | ||
| mt.AddMockResponses( | ||
| mtest.CreateCommandErrorResponse(mtest.CommandError{ | ||
| Code: 11000, | ||
| Message: "Database error", | ||
| }), | ||
| ) | ||
|
|
||
| repo := mongodb.NewInvestmentRepository(mt.DB) | ||
| result, err := repo.FindInvestmentsByUserId(context.Background(), testUserID) | ||
| assert.Error(t, err) | ||
| assert.Nil(t, result) | ||
| }) | ||
| mt.Run("not found", func(mt *mtest.T) { | ||
| mt.AddMockResponses(mtest.CreateCursorResponse(0, "foo.bar", mtest.FirstBatch)) | ||
| repo := mongodb.NewInvestmentRepository(mt.DB) | ||
| result, err := repo.FindInvestmentsByUserId(context.Background(), testUserID) | ||
| assert.NoError(t, err) | ||
| assert.Nil(t, result) | ||
| }) | ||
| mt.Run("success", func(mt *mtest.T) { | ||
| mt.AddMockResponses( | ||
| mtest.CreateCursorResponse(1, "foo.bar", mtest.FirstBatch, testInvestmentDocs...), | ||
| mtest.CreateCursorResponse(0, "foo.bar", mtest.NextBatch), | ||
| ) | ||
| repo := mongodb.NewInvestmentRepository(mt.Client.Database("testdb")) | ||
| result, err := repo.FindInvestmentsByUserId(context.Background(), testUserID) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, result) | ||
| assert.Len(t, result, len(testInvestmentDocs)) | ||
| // Validate each investment | ||
| for i, investment := range result { | ||
| assert.Equal(t, testInvestments[i], investment) | ||
| } | ||
| }) | ||
| }) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.