This commit is contained in:
131
tests/statistics_test.go
Normal file
131
tests/statistics_test.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.techease.ru/Smart-search/smart-search-back/internal/model"
|
||||
"git.techease.ru/Smart-search/smart-search-back/internal/repository"
|
||||
userpb "git.techease.ru/Smart-search/smart-search-back/pkg/pb/user"
|
||||
)
|
||||
|
||||
func (s *IntegrationSuite) TestStatistics_CorrectCountWithMultipleUsers() {
|
||||
ctx := context.Background()
|
||||
|
||||
_, _, user1ID := s.createUniqueTestUser("stats_user1", 1000.0)
|
||||
_, _, user2ID := s.createUniqueTestUser("stats_user2", 1000.0)
|
||||
|
||||
requestRepo := repository.NewRequestRepository(s.pool)
|
||||
supplierRepo := repository.NewSupplierRepository(s.pool)
|
||||
|
||||
req1 := &model.Request{UserID: user1ID, RequestTxt: "Request 1 with TZ"}
|
||||
err := requestRepo.Create(ctx, req1)
|
||||
s.Require().NoError(err)
|
||||
|
||||
req2 := &model.Request{UserID: user1ID, RequestTxt: "Request 2 with TZ"}
|
||||
err = requestRepo.Create(ctx, req2)
|
||||
s.Require().NoError(err)
|
||||
|
||||
_, err = s.pool.Exec(ctx, `
|
||||
INSERT INTO requests_for_suppliers (user_id, request_txt, mailling_status_id)
|
||||
VALUES ($1, NULL, 1)
|
||||
`, user1ID)
|
||||
s.Require().NoError(err)
|
||||
|
||||
suppliers1 := []*model.Supplier{
|
||||
{Name: "Supplier 1", Email: "s1@test.com"},
|
||||
{Name: "Supplier 2", Email: "s2@test.com"},
|
||||
{Name: "Supplier 3", Email: "s3@test.com"},
|
||||
}
|
||||
err = supplierRepo.BulkInsert(ctx, req1.ID, suppliers1)
|
||||
s.Require().NoError(err)
|
||||
|
||||
suppliers2 := []*model.Supplier{
|
||||
{Name: "Supplier 4", Email: "s4@test.com"},
|
||||
{Name: "Supplier 5", Email: "s5@test.com"},
|
||||
}
|
||||
err = supplierRepo.BulkInsert(ctx, req2.ID, suppliers2)
|
||||
s.Require().NoError(err)
|
||||
|
||||
req4 := &model.Request{UserID: user2ID, RequestTxt: "User2 Request with TZ"}
|
||||
err = requestRepo.Create(ctx, req4)
|
||||
s.Require().NoError(err)
|
||||
|
||||
suppliers3 := []*model.Supplier{
|
||||
{Name: "Supplier 6", Email: "s6@test.com"},
|
||||
{Name: "Supplier 7", Email: "s7@test.com"},
|
||||
{Name: "Supplier 8", Email: "s8@test.com"},
|
||||
{Name: "Supplier 9", Email: "s9@test.com"},
|
||||
{Name: "Supplier 10", Email: "s10@test.com"},
|
||||
}
|
||||
err = supplierRepo.BulkInsert(ctx, req4.ID, suppliers3)
|
||||
s.Require().NoError(err)
|
||||
|
||||
resp, err := s.userClient.GetStatistics(ctx, &userpb.GetStatisticsRequest{
|
||||
UserId: int64(user1ID),
|
||||
})
|
||||
s.NoError(err)
|
||||
s.NotNil(resp)
|
||||
|
||||
s.Equal("3", resp.RequestsCount)
|
||||
s.Equal("5", resp.SuppliersCount)
|
||||
s.Equal("2", resp.CreatedTz)
|
||||
|
||||
resp2, err := s.userClient.GetStatistics(ctx, &userpb.GetStatisticsRequest{
|
||||
UserId: int64(user2ID),
|
||||
})
|
||||
s.NoError(err)
|
||||
s.NotNil(resp2)
|
||||
|
||||
s.Equal("1", resp2.RequestsCount)
|
||||
s.Equal("5", resp2.SuppliersCount)
|
||||
s.Equal("1", resp2.CreatedTz)
|
||||
}
|
||||
|
||||
func (s *IntegrationSuite) TestStatistics_CreatedTZNotMultipliedBySuppliers() {
|
||||
ctx := context.Background()
|
||||
|
||||
_, _, userID := s.createUniqueTestUser("stats_multiply", 1000.0)
|
||||
|
||||
requestRepo := repository.NewRequestRepository(s.pool)
|
||||
supplierRepo := repository.NewSupplierRepository(s.pool)
|
||||
|
||||
req := &model.Request{UserID: userID, RequestTxt: "Single request with TZ"}
|
||||
err := requestRepo.Create(ctx, req)
|
||||
s.Require().NoError(err)
|
||||
|
||||
suppliers := make([]*model.Supplier, 10)
|
||||
for i := 0; i < 10; i++ {
|
||||
suppliers[i] = &model.Supplier{
|
||||
Name: "Supplier",
|
||||
Email: "supplier@test.com",
|
||||
}
|
||||
}
|
||||
err = supplierRepo.BulkInsert(ctx, req.ID, suppliers)
|
||||
s.Require().NoError(err)
|
||||
|
||||
resp, err := s.userClient.GetStatistics(ctx, &userpb.GetStatisticsRequest{
|
||||
UserId: int64(userID),
|
||||
})
|
||||
s.NoError(err)
|
||||
s.NotNil(resp)
|
||||
|
||||
s.Equal("1", resp.RequestsCount)
|
||||
s.Equal("10", resp.SuppliersCount)
|
||||
s.Equal("1", resp.CreatedTz)
|
||||
}
|
||||
|
||||
func (s *IntegrationSuite) TestStatistics_EmptyForNewUser() {
|
||||
ctx := context.Background()
|
||||
|
||||
_, _, userID := s.createUniqueTestUser("stats_empty", 1000.0)
|
||||
|
||||
resp, err := s.userClient.GetStatistics(ctx, &userpb.GetStatisticsRequest{
|
||||
UserId: int64(userID),
|
||||
})
|
||||
s.NoError(err)
|
||||
s.NotNil(resp)
|
||||
|
||||
s.Equal("0", resp.RequestsCount)
|
||||
s.Equal("0", resp.SuppliersCount)
|
||||
s.Equal("0", resp.CreatedTz)
|
||||
}
|
||||
Reference in New Issue
Block a user