Skip to content

Commit

Permalink
database: fix test (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-dionysos authored Dec 19, 2024
1 parent 24b765e commit 4eb8eed
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 47 deletions.
6 changes: 3 additions & 3 deletions database/query/client_insert_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package query
import (
"context"
"database/sql"
"math/rand/v2"
"os"
"sync"
"sync/atomic"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/cockroachdb/errors"
"github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/require"
"pgregory.net/rand"

"github.com/ice-blockchain/subzero/model"
)
Expand Down Expand Up @@ -106,11 +106,11 @@ func BenchmarkEventInsert(b *testing.B) {
ev.PubKey = generateHexString()
ev.CreatedAt = model.Timestamp(generateCreatedAt())
ev.Kind = generateKind()
ev.Content = generateRandomString(rand.Intn(1024))
ev.Content = generateRandomString(rand.IntN(1024))
ev.Tags = []model.Tag{
{"e", generateHexString()},
{"p", generateHexString()},
{"d", generateHexString(), generateRandomString(rand.Intn(10))},
{"d", generateHexString(), generateRandomString(rand.IntN(10))},
}
start := time.Now()
db.AcceptEvents(context.Background(), &ev)
Expand Down
21 changes: 8 additions & 13 deletions database/query/query_count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,29 @@ import (

"github.com/nbd-wtf/go-nostr"
"github.com/stretchr/testify/require"
"pgregory.net/rand"

"github.com/ice-blockchain/subzero/model"
)

func TestQueryEventsCount(t *testing.T) {
t.Parallel()

const totalEvents = int64(200)
db := helperNewDatabase(t)
db, data := helperEnsureDatabaseWithData(t)
defer db.Close()

authors := make(map[string]int64)
ids := make(map[string]model.Event, totalEvents)
events := make([]model.Event, 0, totalEvents)
ids := make(map[string]*model.Event, len(data.Events))

t.Run("Generate", func(t *testing.T) {
for range totalEvents {
event := helperGenerateEvent(t, db, true)
for _, event := range data.Events {
authors[event.PubKey]++
ids[event.ID] = event
events = append(events, event)
}
})
t.Run("CountAll", func(t *testing.T) {
count, err := db.CountEvents(context.TODO())
require.NoError(t, err)
require.Equal(t, totalEvents, count)
require.EqualValues(t, len(data.Events), count)
})
t.Run("CountByAuthor", func(t *testing.T) {
for author, expectedCount := range authors {
Expand All @@ -50,7 +45,7 @@ func TestQueryEventsCount(t *testing.T) {
})
t.Run("RandomTag", func(t *testing.T) {
for range 10 {
ev := events[rand.Int31n(int32(len(events)))]
ev := data.Random(t)
count, err := db.CountEvents(context.TODO(), model.Filter{
Tags: model.TagMap{}.SetLiterals(ev.Tags[0][0], ev.Tags[0][1:]...),
})
Expand All @@ -59,9 +54,9 @@ func TestQueryEventsCount(t *testing.T) {
}
})
t.Run("EventsOR", func(t *testing.T) {
ev1 := events[rand.Int31n(int32(len(events)))]
ev2 := events[rand.Int31n(int32(len(events)))]
ev3 := events[rand.Int31n(int32(len(events)))]
ev1 := data.Random(t)
ev2 := data.Random(t)
ev3 := data.Random(t)
count, err := db.CountEvents(context.TODO(),
model.Filter{
IDs: []string{ev1.ID},
Expand Down
8 changes: 4 additions & 4 deletions database/query/query_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package query

import (
"context"
"math/rand/v2"
"reflect"
"slices"
"strings"
Expand All @@ -12,7 +13,6 @@ import (
combinations "github.com/mxschmitt/golang-combinations"
"github.com/nbd-wtf/go-nostr"
"github.com/stretchr/testify/require"
"pgregory.net/rand"

"github.com/ice-blockchain/subzero/model"
)
Expand Down Expand Up @@ -67,7 +67,7 @@ func helperParseFilterStruct(t *testing.T, typ reflect.Type, parent *structEleme
fields = append(fields, el)
if field.Type.Kind() == reflect.Slice {
next := el.Clone()
next.NumElem = int(rand.Int31n(6)) + 1
next.NumElem = int(rand.Int32N(6)) + 1
fields = append(fields, next)
}

Expand All @@ -87,7 +87,7 @@ func helperParseFilterStruct(t *testing.T, typ reflect.Type, parent *structEleme
func helperRandomBool(t *testing.T) string {
t.Helper()

if rand.Int63n(100)%2 == 0 {
if rand.Int64N(100)%2 == 0 {
return "true"
}

Expand Down Expand Up @@ -125,7 +125,7 @@ func helperNewFilterFromElements(t *testing.T, fields []*structElement) model.Fi
value.Set(reflect.ValueOf(m))

case "Limit":
l := int(rand.Int63n(100))
l := int(rand.Int64N(100))
value.Set(reflect.ValueOf(l))

case "Until", "Since":
Expand Down
4 changes: 2 additions & 2 deletions database/query/query_where_builder_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package query

import (
"context"
"math/rand/v2"
"os"
"strconv"
"sync"
Expand All @@ -12,7 +13,6 @@ import (

"github.com/jamiealquiza/tachymeter"
"github.com/stretchr/testify/require"
"pgregory.net/rand"

"github.com/ice-blockchain/subzero/model"
)
Expand Down Expand Up @@ -62,7 +62,7 @@ func helperBenchEnsureDatabase(t interface {
func helperBenchRandomEvent(t interface{ Helper() }) *model.Event {
t.Helper()

return benchData.Events[rand.Int31n(int32(len(benchData.Events)))]
return benchData.Events[rand.Int32N(int32(len(benchData.Events)))]
}

func helperBenchSelectBy(t interface{ Helper() }, db *dbClient, meter *tachymeter.Tachymeter, filters []model.Filter) {
Expand Down
37 changes: 19 additions & 18 deletions database/query/query_where_builder_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ package query

import (
"context"
crand "crypto/rand"
"database/sql"
"encoding/hex"
"fmt"
"math/rand/v2"
"os"
"slices"
"strconv"
Expand All @@ -18,7 +20,6 @@ import (
"github.com/nbd-wtf/go-nostr"
"github.com/schollz/progressbar/v3"
"github.com/stretchr/testify/require"
"pgregory.net/rand"

"github.com/ice-blockchain/subzero/model"
)
Expand All @@ -30,14 +31,14 @@ type testEvents struct {
func (te *testEvents) Random(h interface{ Helper() }) *model.Event {
h.Helper()

idx := int(rand.Int31n(int32(len(te.Events))))
idx := int(rand.IntN(len(te.Events)))
ev := te.Events[idx]
te.Events = slices.Delete(te.Events, idx, idx+1)

return ev
}

func helperEnsureDatabase(t *testing.T) (*dbClient, *testEvents) {
func helperEnsureDatabaseWithData(t *testing.T) (*dbClient, *testEvents) {
t.Helper()

const eventCount = 100
Expand Down Expand Up @@ -88,7 +89,7 @@ limit 1000`
return nil
})
require.NoError(t, err)
rand.ShuffleSlice(nil, events)
rand.Shuffle(len(events), func(i, j int) { events[i], events[j] = events[j], events[i] })

return events
}
Expand All @@ -97,7 +98,7 @@ func generateHexString() string {
// The ids, authors, #e and #p filter lists MUST contain exact 64-character lowercase hex values.
var buf [64]byte

if _, err := rand.Read(buf[:]); err != nil {
if _, err := crand.Read(buf[:]); err != nil {
panic(err)
}

Expand Down Expand Up @@ -152,7 +153,7 @@ func generateKind() int {
nostr.KindSimpleGroupMembers,
}

return kinds[rand.Intn(len(kinds))]
return kinds[rand.IntN(len(kinds))]
}

func generateRandomString(n int) string {
Expand All @@ -164,7 +165,7 @@ func generateRandomString(n int) string {

b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
b[i] = letters[rand.IntN(len(letters))]
}

return string(b)
Expand All @@ -176,7 +177,7 @@ func generateCreatedAt() int64 {
end = 1740375055
)

return rand.Int63n(end-start) + start
return rand.Int64N(end-start) + start
}

func helperGenerateEvent(
Expand All @@ -195,13 +196,13 @@ func helperGenerateEvent(
ev.PubKey = generateHexString()
ev.CreatedAt = model.Timestamp(generateCreatedAt())
ev.Kind = generateKind()
ev.Content = generateRandomString(rand.Intn(1024))
ev.Content = generateRandomString(rand.IntN(1024))

if withTags {
ev.Tags = []model.Tag{
{"#e", generateHexString(), generateRandomString(rand.Intn(20)), generateRandomString(rand.Intn(30))},
{"#e", generateHexString(), generateRandomString(rand.IntN(20)), generateRandomString(rand.IntN(30))},
{"#p", generateHexString()},
{"#d", generateHexString(), generateRandomString(rand.Intn(10))},
{"#d", generateHexString(), generateRandomString(rand.IntN(10))},
}
}

Expand Down Expand Up @@ -237,7 +238,7 @@ func helperFillDatabase(t *testing.T, db *dbClient, size int) {
func TestWhereBuilderByAuthor(t *testing.T) {
t.Parallel()

db, ev := helperEnsureDatabase(t)
db, ev := helperEnsureDatabaseWithData(t)
defer db.Close()
events := helperSelectEvents(t, db,
model.Filter{
Expand All @@ -253,7 +254,7 @@ func TestWhereBuilderByAuthor(t *testing.T) {
func TestWhereBuilderByID(t *testing.T) {
t.Parallel()

db, ev := helperEnsureDatabase(t)
db, ev := helperEnsureDatabaseWithData(t)
defer db.Close()
events := helperSelectEvents(t, db,
model.Filter{
Expand All @@ -269,7 +270,7 @@ func TestWhereBuilderByID(t *testing.T) {
func TestWhereBuilderByMany(t *testing.T) {
t.Parallel()

db, ev := helperEnsureDatabase(t)
db, ev := helperEnsureDatabaseWithData(t)
defer db.Close()
ev1 := ev.Random(t)
ev2 := ev.Random(t)
Expand All @@ -293,7 +294,7 @@ func TestWhereBuilderByMany(t *testing.T) {
func TestWhereBuilderByTagsNoValuesSingle(t *testing.T) {
t.Parallel()

db, ev := helperEnsureDatabase(t)
db, ev := helperEnsureDatabaseWithData(t)
defer db.Close()
event := ev.Random(t)
filter := model.Filter{
Expand Down Expand Up @@ -356,7 +357,7 @@ func TestWhereBuilderByTagsSingle(t *testing.T) {
func TestWhereBuilderByTagsOnlySingle(t *testing.T) {
t.Parallel()

db, ev := helperEnsureDatabase(t)
db, ev := helperEnsureDatabaseWithData(t)
defer db.Close()
event := ev.Random(t)

Expand Down Expand Up @@ -629,7 +630,7 @@ func helperCountExpiredEvents(t *testing.T, db *dbClient) int {
func TestSelectEventsExpiration(t *testing.T) {
t.Parallel()

db, events := helperEnsureDatabase(t)
db, events := helperEnsureDatabaseWithData(t)
defer db.Close()

t.Run("Fill", func(t *testing.T) {
Expand Down Expand Up @@ -730,7 +731,7 @@ func TestSelectEventsExpiration(t *testing.T) {
func TestSelectWithExtensions(t *testing.T) {
t.Parallel()

db, events := helperEnsureDatabase(t)
db, events := helperEnsureDatabaseWithData(t)
defer db.Close()

t.Run("Fill", func(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ require (
github.com/xssnick/tonutils-storage v0.6.5
go.uber.org/goleak v1.3.0
golang.org/x/net v0.33.0
pgregory.net/rand v1.0.2
)

require (
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@ github.com/u2takey/go-utils v0.3.1 h1:TaQTgmEZZeDHQFYfd+AdUT1cT4QJgJn/XVPELhHw4y
github.com/u2takey/go-utils v0.3.1/go.mod h1:6e+v5vEZ/6gu12w/DC2ixZdZtCrNokVxD0JUklcqdCs=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8=
github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
Expand Down Expand Up @@ -448,8 +446,4 @@ gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
pgregory.net/rand v1.0.2 h1:ASEbkvwOmY/UPF2evJPBJ8XZg71xdKWYdByqKapI7Vw=
pgregory.net/rand v1.0.2/go.mod h1:EyNx8APnDE3Svi8sWgUZ5lOiz60cNZUPPBTyzOUpPl4=
pgregory.net/rapid v0.4.8 h1:d+5SGZWUbJPbl3ss6tmPFqnNeQR6VDOFly+eTjwPiEw=
pgregory.net/rapid v0.4.8/go.mod h1:Z5PbWqjvWR1I3UGjvboUuan4fe4ZYEYNLNQLExzCoUs=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=

0 comments on commit 4eb8eed

Please sign in to comment.