Skip to content

Commit

Permalink
Fix some exports and paths
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Mar 23, 2024
1 parent 81f90f2 commit 587933b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
4 changes: 3 additions & 1 deletion cmd/tf2bdd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"github.com/leighmacdonald/tf2bdd/tf2bdd"
"log/slog"
"net/http"
"os"
Expand All @@ -13,6 +12,9 @@ import (
"time"

"github.com/leighmacdonald/steamid/v4/steamid"
"github.com/leighmacdonald/tf2bdd/tf2bdd"
_ "github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
)

var version = "dev"
Expand Down
4 changes: 2 additions & 2 deletions tf2bdd/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func addEntry(ctx context.Context, database *sql.DB, sid steamid.SteamID, msg []
SteamID: sid,
}

if err := addPlayer(ctx, database, player, author); err != nil {
if err := AddPlayer(ctx, database, player, author); err != nil {
if err.Error() == "UNIQUE constraint failed: player.steamid" {
return "", fmt.Errorf("duplicate steam id: %s", sid.String())
}
Expand Down Expand Up @@ -247,7 +247,7 @@ func loadAttachment(ctx context.Context, client *http.Client, database *sql.DB,

added := 0
for _, p := range toAdd {
if errAdd := addPlayer(ctx, database, p, author); errAdd != nil {
if errAdd := AddPlayer(ctx, database, p, author); errAdd != nil {
slog.Error("failed to add new entry", slog.String("error", errAdd.Error()))

continue
Expand Down
4 changes: 1 addition & 3 deletions tf2bdd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"time"

"github.com/leighmacdonald/steamid/v4/steamid"
_ "github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
)

func OpenDB(dbPath string) (*sql.DB, error) {
Expand Down Expand Up @@ -129,7 +127,7 @@ func getPlayers(ctx context.Context, db *sql.DB) ([]Player, error) {
return players, nil
}

func addPlayer(ctx context.Context, db *sql.DB, player Player, author int64) error {
func AddPlayer(ctx context.Context, db *sql.DB, player Player, author int64) error {
const query = `
INSERT INTO player (steamid, attributes, last_seen, last_name, author, created_on)
VALUES(?, ?, ?, ?, ?, ?)`
Expand Down
23 changes: 13 additions & 10 deletions tf2bdd/server_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tf2bdd
package tf2bdd_test

import (
"context"
Expand All @@ -10,20 +10,23 @@ import (
"testing"

"github.com/leighmacdonald/steamid/v4/steamid"
"github.com/leighmacdonald/tf2bdd/tf2bdd"
_ "github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/stretchr/testify/require"
)

func newTestDB(ctx context.Context) (*sql.DB, error) {
db, errDB := OpenDB(":memory:")
db, errDB := tf2bdd.OpenDB(":memory:")
if errDB != nil {
return nil, errDB
}

return db, SetupDB(ctx, db)
return db, tf2bdd.SetupDB(ctx, db)
}

func TestHandleGetSteamIDS(t *testing.T) {
testConfig := Config{
testConfig := tf2bdd.Config{
SteamKey: "",
DiscordClientID: "",
DiscordBotToken: "",
Expand All @@ -48,27 +51,27 @@ func TestHandleGetSteamIDS(t *testing.T) {
database, errApp := newTestDB(ctx)
require.NoError(t, errApp)

localPlayers := []Player{
localPlayers := []tf2bdd.Player{
{
SteamID: steamid.New(76561198237337976),
Attributes: []string{"cheater"},
LastSeen: LastSeen{},
LastSeen: tf2bdd.LastSeen{},
},
{
SteamID: steamid.New(76561198834913692),
Attributes: []string{"cheater"},
LastSeen: LastSeen{},
LastSeen: tf2bdd.LastSeen{},
},
}

for _, p := range localPlayers {
require.NoError(t, addPlayer(ctx, database, p, 0))
require.NoError(t, tf2bdd.AddPlayer(ctx, database, p, 0))
}

CreateRouter(database, testConfig).ServeHTTP(recorder, req)
tf2bdd.CreateRouter(database, testConfig).ServeHTTP(recorder, req)
require.Equal(t, http.StatusOK, recorder.Code)

var players PlayerListRoot
var players tf2bdd.PlayerListRoot
require.NoError(t, json.NewDecoder(recorder.Body).Decode(&players))
require.Equal(t, len(localPlayers), len(players.Players))
}
Expand Down

0 comments on commit 587933b

Please sign in to comment.