Skip to content

Commit

Permalink
fix tests: elasticsearch and postgres.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Jul 23, 2024
1 parent a4b88d6 commit d49dc05
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 154 deletions.
2 changes: 1 addition & 1 deletion elasticsearch/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestQuery(t *testing.T) {
now := nostr.Now()
yesterday := now - 60*60*24
filter := &nostr.Filter{
filter := nostr.Filter{
IDs: []string{"abc", "123", "971b9489b4fd4e41a85951607922b982d981fa9d55318bc304f21f390721404c"},
Kinds: []int{0, 1},
Tags: nostr.TagMap{
Expand Down
19 changes: 14 additions & 5 deletions postgresql/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postgresql
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -62,14 +63,22 @@ func makePlaceHolders(n int) string {
return strings.TrimRight(strings.Repeat("?,", n), ",")
}

var (
TooManyIDs = errors.New("too many ids")
TooManyAuthors = errors.New("too many authors")
TooManyKinds = errors.New("too many kinds")
TooManyTagValues = errors.New("too many tag values")
EmptyTagSet = errors.New("empty tag set")
)

func (b PostgresBackend) queryEventsSql(filter nostr.Filter, doCount bool) (string, []any, error) {
var conditions []string
var params []any

if len(filter.IDs) > 0 {
if len(filter.IDs) > b.QueryIDsLimit {
// too many ids, fail everything
return "", nil, nil
return "", nil, TooManyIDs
}

for _, v := range filter.IDs {
Expand All @@ -81,7 +90,7 @@ func (b PostgresBackend) queryEventsSql(filter nostr.Filter, doCount bool) (stri
if len(filter.Authors) > 0 {
if len(filter.Authors) > b.QueryAuthorsLimit {
// too many authors, fail everything
return "", nil, nil
return "", nil, TooManyAuthors
}

for _, v := range filter.Authors {
Expand All @@ -93,7 +102,7 @@ func (b PostgresBackend) queryEventsSql(filter nostr.Filter, doCount bool) (stri
if len(filter.Kinds) > 0 {
if len(filter.Kinds) > b.QueryKindsLimit {
// too many kinds, fail everything
return "", nil, nil
return "", nil, TooManyKinds
}

for _, v := range filter.Kinds {
Expand All @@ -106,15 +115,15 @@ func (b PostgresBackend) queryEventsSql(filter nostr.Filter, doCount bool) (stri
for _, values := range filter.Tags {
if len(values) == 0 {
// any tag set to [] is wrong
return "", nil, nil
return "", nil, EmptyTagSet
}

// add these tags to the query
tagQuery = append(tagQuery, values...)

if len(tagQuery) > b.QueryTagsLimit {
// too many tags, fail everything
return "", nil, nil
return "", nil, TooManyTagValues
}
}

Expand Down
Loading

0 comments on commit d49dc05

Please sign in to comment.