Skip to content

Commit

Permalink
fix: do not limit and offset count
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Jun 21, 2024
1 parent 8740247 commit e613d15
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 7 deletions.
10 changes: 6 additions & 4 deletions db/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ func ConvertModFilter(query *ent.ModQuery, filter *models.ModFilter, count bool,
} else if len(filter.References) > 0 {
query = query.Where(mod.ModReferenceIn(filter.References...))
} else if filter != nil {
query = query.
Limit(*filter.Limit).
Offset(*filter.Offset)
if !count {
query = query.
Limit(*filter.Limit).
Offset(*filter.Offset)
}

if *filter.OrderBy != generated.ModFieldsSearch {
if filter.OrderBy != nil && *filter.OrderBy != generated.ModFieldsSearch {
if string(*filter.OrderBy) == "last_version_date" {
query = query.Modify(func(s *sql.Selector) {
s.OrderExpr(sql.ExprP("case when last_version_date is null then 1 else 0 end, last_version_date"))
Expand Down
8 changes: 5 additions & 3 deletions gql/resolver_mods.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ func (r *mutationResolver) CreateMod(ctx context.Context, newMod generated.NewMo
return nil, err
}

currentAvailable := float64(4)
currentAvailable := float64(util.ModsPer24h)
lastModTime := time.Now()
for _, mod := range existingMods {
currentAvailable--
if mod.CreatedAt.After(lastModTime) {
diff := mod.CreatedAt.Sub(lastModTime)
currentAvailable = math.Min(4, currentAvailable+diff.Hours()/6)
currentAvailable = math.Min(float64(util.ModsPer24h), currentAvailable+diff.Hours()/6)
}
lastModTime = mod.CreatedAt
}
Expand Down Expand Up @@ -396,6 +396,7 @@ func (r *getModsResolver) Mods(ctx context.Context, _ *generated.GetMods) ([]*ge

result, err := query.All(ctx)
if err != nil {
slox.Error(ctx, "failed querying mods", slog.Any("err", err))
return nil, err
}

Expand All @@ -412,10 +413,11 @@ func (r *getModsResolver) Count(ctx context.Context, _ *generated.GetMods) (int,
}

query := db.From(ctx).Mod.Query()
query = db.ConvertModFilter(query, modFilter, false, unapproved)
query = db.ConvertModFilter(query, modFilter, true, unapproved)

result, err := query.Count(ctx)
if err != nil {
slox.Error(ctx, "failed querying mod count", slog.Any("err", err))
return 0, err
}

Expand Down
52 changes: 52 additions & 0 deletions tests/get_mod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package tests

import (
"testing"

"github.com/MarvinJWendt/testza"
"github.com/satisfactorymodding/smr-api/generated"
)

const getQuery = `query GetMods($offset: Int!, $limit: Int!, $search: String, $order: Order, $orderBy: ModFields, $tagIDs: [TagID!]) {
getMods(
filter: {limit: $limit, offset: $offset, search: $search, order: $order, order_by: $orderBy, tagIDs: $tagIDs}
) {
count
mods {
mod_reference
tags {
id
}
}
}
}`

func TestGetModLimitOffset(t *testing.T) {
ctx, client, stop := setup()
defer stop()

token, _, err := makeUser(ctx)
testza.AssertNoError(t, err)

tags := seedTags(t, ctx, token, client)
seedMods(t, ctx, token, client, tags[0])

getRequest := authRequest(getQuery, token)

getRequest.Var("offset", "4")
getRequest.Var("limit", "2")
getRequest.Var("order", "asc")
getRequest.Var("orderBy", "created_at")

var getResponse struct {
GetMods generated.GetMods
}
testza.AssertNoError(t, client.Run(ctx, getRequest, &getResponse))
testza.AssertEqual(t, 10, getResponse.GetMods.Count)
testza.AssertEqual(t, 2, len(getResponse.GetMods.Mods))
testza.AssertEqual(t, "resource_overhaul", getResponse.GetMods.Mods[0].ModReference)
testza.AssertEqual(t, "automated_defense", getResponse.GetMods.Mods[1].ModReference)
testza.AssertEqual(t, 0, len(getResponse.GetMods.Mods[0].Tags))
testza.AssertEqual(t, 1, len(getResponse.GetMods.Mods[1].Tags))
testza.AssertEqual(t, tags[0], getResponse.GetMods.Mods[1].Tags[0].ID)
}
130 changes: 130 additions & 0 deletions tests/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package tests

import (
"context"
"testing"

"github.com/MarvinJWendt/testza"
"github.com/machinebox/graphql"
"github.com/satisfactorymodding/smr-api/generated"
"github.com/satisfactorymodding/smr-api/util"
)

func seedTags(t *testing.T, ctx context.Context, token string, client *graphql.Client) []string {

Check failure on line 13 in tests/utils_test.go

View workflow job for this annotation

GitHub Actions / Lint

context-as-argument: context.Context should be the first parameter of a function (revive)
tags := []string{
"hello",
"foo",
}

ids := make([]string, len(tags))
for i, tag := range tags {
createRequest := authRequest(`mutation CreateTag($name: TagName!) {
createTag(tagName: $name, description: "N/A") {
id
}
}`, token)
createRequest.Var("name", tag)

var createResponse struct {
CreateTag generated.Tag
}
testza.AssertNoError(t, client.Run(ctx, createRequest, &createResponse))
testza.AssertNotEqual(t, "", createResponse.CreateTag.ID)

ids[i] = createResponse.CreateTag.ID
}

return ids
}

type testMod struct {
Name string `json:"name"`
ShortDescription string `json:"short_description"`
FullDescription string `json:"full_description"`
ModReference string `json:"mod_reference"`
TagIDs []string `json:"tagIDs"`
}

func seedMods(t *testing.T, ctx context.Context, token string, client *graphql.Client, tagID string) []string {

Check failure on line 48 in tests/utils_test.go

View workflow job for this annotation

GitHub Actions / Lint

context-as-argument: context.Context should be the first parameter of a function (revive)
mods := []testMod{
{
Name: "Advanced Robotics",
ShortDescription: "Enhances robot efficiency and adds new automation features.",
ModReference: "advanced_robotics",
},
{
Name: "Eco-Friendly Power",
ShortDescription: "Introduces sustainable energy sources and eco-friendly power management.",
ModReference: "eco_friendly_power",
},
{
Name: "Quantum Transport",
ShortDescription: "Allows instantaneous item transport using quantum entanglement.",
ModReference: "quantum_transport",
},
{
Name: "Mega Factory",
ShortDescription: "Expands factory building limits and adds new large-scale production tools.",
ModReference: "mega_factory",
},
{
Name: "Resource Overhaul",
ShortDescription: "Revamps resource extraction and processing for more efficiency.",
ModReference: "resource_overhaul",
},
{
Name: "Automated Defense",
ShortDescription: "Adds advanced automated defense systems to protect your factory.",
ModReference: "automated_defense",
TagIDs: []string{tagID},
},
{
Name: "AI Assistant",
ShortDescription: "Introduces an AI assistant to help manage and optimize your factory.",
ModReference: "ai_assistant",
TagIDs: []string{tagID},
},
{
Name: "Fusion Reactors",
ShortDescription: "Adds fusion reactors as a high-efficiency power source.",
ModReference: "fusion_reactors",
TagIDs: []string{tagID},
},
{
Name: "Modular Production",
ShortDescription: "Allows modular production units for flexible factory layouts.",
ModReference: "modular_production",
TagIDs: []string{tagID},
},
{
Name: "Nanotech Manufacturing",
ShortDescription: "Incorporates nanotechnology for ultra-precise manufacturing processes.",
ModReference: "nanotech_manufacturing",
TagIDs: []string{tagID},
},
}

util.ModsPer24h = len(mods)

ids := make([]string, len(mods))
for i, mod := range mods {
mod.FullDescription = "N/A"

createRequest := authRequest(`mutation CreateMod($mod: NewMod!) {
createMod(mod: $mod) {
id
}
}`, token)
createRequest.Var("mod", mod)

var createResponse struct {
CreateMod generated.Mod
}
testza.AssertNoError(t, client.Run(ctx, createRequest, &createResponse))
testza.AssertNotEqual(t, "", createResponse.CreateMod.ID)

ids[i] = createResponse.CreateMod.ID
}

return ids
}
3 changes: 3 additions & 0 deletions util/ratelimits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package util

var ModsPer24h = 4

0 comments on commit e613d15

Please sign in to comment.