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

Unit tests for stats #132

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions internal/app/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package app
import "github.com/capymind/internal/helpers"

func handleTotalUserCount(session *Session) {
message := helpers.GetTotalUserCount(session.Context, session.Locale())
message := helpers.GetTotalUserCount(session.Context, session.Locale(), adminStorage)
if message != nil {
setOutputText(*message, session)
}
}

func handleTotalActiveUserCount(session *Session) {
message := helpers.GetTotalActiveUserCount(session.Context, session.Locale())
message := helpers.GetTotalActiveUserCount(session.Context, session.Locale(), adminStorage)
if message != nil {
setOutputText(*message, session)
}
}

func handleTotalNoteCount(session *Session) {
message := helpers.GetTotalNoteCount(session.Context, session.Locale())
message := helpers.GetTotalNoteCount(session.Context, session.Locale(), adminStorage)
if message != nil {
setOutputText(*message, session)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/app/feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package app
import "github.com/capymind/internal/helpers"

func handleFeedbackLastWeek(session *Session) {
array := helpers.PrepareFeedback(session.Context, session.Locale())
array := helpers.PrepareFeedback(session.Context, session.Locale(), feedbackStorage)
for _, item := range array {
setOutputText(item, session)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/app/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func handleStats(session *Session) {
stats := helpers.GetStats(session.Context, session.Locale())
stats := helpers.GetStats(session.Context, session.Locale(), adminStorage, feedbackStorage)

var finalString string
for _, stat := range stats {
Expand Down
3 changes: 2 additions & 1 deletion internal/app/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ var aiService openai.OpenAI
// Use Firestore for the database
var db firestore.Firestore
var userStorage firestore.UserStorage
var feedbackStorage firestore.FeedbackStorage
var noteStorage firestore.NoteStorage
var adminStorage firestore.AdminStorage
var feedbackStorage firestore.FeedbackStorage
49 changes: 29 additions & 20 deletions internal/helpers/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@ import (
"log"
"sync"

"github.com/capymind/internal/database"
"github.com/capymind/internal/translator"
"github.com/capymind/third_party/firestore"
)

type statFunc func(ctx *context.Context, locale translator.Locale) *string
type feedbackFunc func(ctx *context.Context, locale translator.Locale) []string
type statFunc func(ctx *context.Context, locale translator.Locale, adminStorage database.AdminStorage) *string
type feedbackFunc func(ctx *context.Context, locale translator.Locale, feedbackStorage database.FeedbackStorage) []string

var wg sync.WaitGroup

var adminStorage firestore.AdminStorage
var feedbackStorage firestore.FeedbackStorage

func GetTotalUserCount(ctx *context.Context, locale translator.Locale) *string {
func GetTotalUserCount(ctx *context.Context, locale translator.Locale, adminStorage database.AdminStorage) *string {
count, err := adminStorage.GetTotalUserCount(ctx)
if err != nil {
log.Printf("[Admin] Error during fetching total user count: %v", err)
Expand All @@ -28,7 +25,7 @@ func GetTotalUserCount(ctx *context.Context, locale translator.Locale) *string {
return &message
}

func GetTotalActiveUserCount(ctx *context.Context, locale translator.Locale) *string {
func GetTotalActiveUserCount(ctx *context.Context, locale translator.Locale, adminStorage database.AdminStorage) *string {
count, err := adminStorage.GetActiveUserCount(ctx)
if err != nil {
log.Printf("[Admin] Error during fetching active user count: %v", err)
Expand All @@ -38,7 +35,7 @@ func GetTotalActiveUserCount(ctx *context.Context, locale translator.Locale) *st
return &message
}

func GetTotalNoteCount(ctx *context.Context, locale translator.Locale) *string {
func GetTotalNoteCount(ctx *context.Context, locale translator.Locale, adminStorage database.AdminStorage) *string {
count, err := adminStorage.GetTotalNoteCount(ctx)
if err != nil {
log.Printf("[Admin] Error during fetching total note count: %v", err)
Expand All @@ -48,11 +45,11 @@ func GetTotalNoteCount(ctx *context.Context, locale translator.Locale) *string {
return &message
}

func GetStats(ctx *context.Context, locale translator.Locale) []string {
totalUserCount := waitForStatFunction(GetTotalUserCount, ctx, locale)
totalActiveUserCount := waitForStatFunction(GetTotalActiveUserCount, ctx, locale)
totalNoteCount := waitForStatFunction(GetTotalNoteCount, ctx, locale)
feedback := waitForFeedback(PrepareFeedback, ctx, locale)
func GetStats(ctx *context.Context, locale translator.Locale, adminStorage database.AdminStorage, feedbackStorage database.FeedbackStorage) []string {
totalUserCount := waitForStatFunction(GetTotalUserCount, ctx, locale, adminStorage)
totalActiveUserCount := waitForStatFunction(GetTotalActiveUserCount, ctx, locale, adminStorage)
totalNoteCount := waitForStatFunction(GetTotalNoteCount, ctx, locale, adminStorage)
feedback := waitForFeedback(PrepareFeedback, ctx, locale, feedbackStorage)

wg.Wait()

Expand All @@ -73,7 +70,7 @@ func GetStats(ctx *context.Context, locale translator.Locale) []string {
return array
}

func PrepareFeedback(ctx *context.Context, locale translator.Locale) []string {
func PrepareFeedback(ctx *context.Context, locale translator.Locale, feedbackStorage database.FeedbackStorage) []string {
var array []string

feedback, err := feedbackStorage.GetFeedbackForLastWeek(ctx)
Expand All @@ -92,30 +89,42 @@ func PrepareFeedback(ctx *context.Context, locale translator.Locale) []string {
array = append(array, "")

for _, f := range feedback {
array = append(array, *f.User.FirstName+" "+*f.User.LastName+":"+"\n"+f.Feedback.Text+"\n")
var hasName bool
if f.User.FirstName != nil {
array = append(array, *f.User.FirstName+" ")
hasName = true
}
if f.User.LastName != nil {
array = append(array, *f.User.LastName)
hasName = true
}
if hasName {
array = append(array, ":")
}
array = append(array, "\n"+f.Feedback.Text+"\n")
}

return array
}

func waitForStatFunction(statFunc statFunc, ctx *context.Context, locale translator.Locale) *string {
func waitForStatFunction(statFunc statFunc, ctx *context.Context, locale translator.Locale, adminStorage database.AdminStorage) *string {
wg.Add(1)
ch := make(chan *string)
go func() {
defer wg.Done()
result := statFunc(ctx, locale)
result := statFunc(ctx, locale, adminStorage)
ch <- result
}()
result := <-ch
return result
}

func waitForFeedback(feedbackFunc feedbackFunc, ctx *context.Context, locale translator.Locale) []string {
func waitForFeedback(feedbackFunc feedbackFunc, ctx *context.Context, locale translator.Locale, feedbackStorage database.FeedbackStorage) []string {
wg.Add(1)
ch := make(chan []string)
go func() {
defer wg.Done()
result := feedbackFunc(ctx, locale)
result := feedbackFunc(ctx, locale, feedbackStorage)
ch <- result
}()
result := <-ch
Expand Down
93 changes: 93 additions & 0 deletions internal/helpers/stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package helpers

import (
"context"
"testing"

"github.com/capymind/internal/database"
"github.com/capymind/internal/translator"
)

type adminStatsMock struct{}

func (storage adminStatsMock) GetTotalUserCount(ctx *context.Context) (int64, error) {
return 100, nil
}

func (storage adminStatsMock) GetActiveUserCount(ctx *context.Context) (int64, error) {
return 75, nil
}

func (storage adminStatsMock) GetTotalNoteCount(ctx *context.Context) (int64, error) {
return 999, nil
}

type feedbackStatsMock struct{}

func (storage feedbackStatsMock) GetFeedbackForLastWeek(ctx *context.Context) ([]database.UserFeedback, error) {
var array []database.UserFeedback

firstName := "John"
lastName := "Doe"
user := database.User{
ID: "1",
FirstName: &firstName,
LastName: &lastName,
}
feedback1 := database.Feedback{
Text: "Test feedback",
}
feedback2 := database.Feedback{
Text: "Test feedback 2",
}

userFeedback1 := database.UserFeedback{
User: user,
Feedback: feedback1,
}
userFeedback2 := database.UserFeedback{
User: user,
Feedback: feedback2,
}

array = append(array, userFeedback1)
array = append(array, userFeedback2)

return array, nil
}

func (storage feedbackStatsMock) NewFeedback(ctx *context.Context, user database.User, feedback database.Feedback) error {
return nil
}

func TestGetStats(t *testing.T) {
adminStorage := adminStatsMock{}
feedbackStorage := feedbackStatsMock{}

context := context.Background()
stats := GetStats(&context, translator.EN, adminStorage, feedbackStorage)

if len(stats) != 14 {
t.Error("Expected 14 stats, got", len(stats))
}

if stats[0] != "The total number of users is 100" {
t.Error("Expected The total number of users is 100, got", stats[0])
}

if stats[1] != "The total number of active users is 75" {
t.Error("Expected The total number of active users is 75, got", stats[1])
}

if stats[2] != "The total number of notes is 999" {
t.Error("Expected The total number of notes is 999, got", stats[2])
}

if stats[9] != "\nTest feedback\n" {
t.Error("Expected Test feedback, got", stats[9])
}

if stats[13] != "\nTest feedback 2\n" {
t.Error("Expected Test feedback 2, got", stats[9])
}
}
2 changes: 1 addition & 1 deletion internal/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func prepareMessage(user *database.User, ctx *context.Context, offset int, messa
if !database.IsAdmin(user.Role) {
return
}
stats := helpers.GetStats(ctx, userLocale)
stats := helpers.GetStats(ctx, userLocale, adminStorage, feedbackStorage)

var finalString string
for _, stat := range stats {
Expand Down
2 changes: 2 additions & 0 deletions internal/scheduler/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ var aiService openai.OpenAI
var db firestore.Firestore
var userStorage firestore.UserStorage
var noteStorage firestore.NoteStorage
var adminStorage firestore.AdminStorage
var feedbackStorage firestore.FeedbackStorage
Loading