farside/db/db_test.go
Ben Busby b5bad4defc
Rewrite project, add daily update of services list
The project was rewritten from Elixir to Go, primarily because:

- I don't write Elixir anymore and don't want to maintain a project in a
  language I no longer write
- I already write Go for other projects, including my day job, so it's
  a safer bet for a project that I want to maintain long term
- Go allows me to build portable executables that will make it easier
  for others to run farside on their own machines

The Go version of Farsside also has a built in task to fetch the latest
services{-full}.json file from the repo and ingest it, which makes
running a farside server a lot simpler.

It also automatically fetches the latest instance state from
https://farside.link unless configured as a primary farside node, which
will allow others to use farside without increasing traffic to all
instances that are queried by farside (just to the farside node itself).
2025-01-21 13:46:29 -07:00

61 lines
1.1 KiB
Go

package db
import (
"log"
"os"
"slices"
"testing"
)
func TestMain(m *testing.M) {
err := InitializeDB()
if err != nil {
log.Fatalln("Failed to initialize database")
}
exitCode := m.Run()
_ = CloseDB()
os.Exit(exitCode)
}
func TestDatabase(t *testing.T) {
var (
service = "test"
siteA = "a.com"
siteB = "b.com"
siteC = "c.com"
)
instances := []string{siteA, siteB, siteC}
err := SetInstances(service, instances)
if err != nil {
t.Fatalf("Failed to set instances: %v\n", err)
}
dbInstances, err := GetAllInstances(service)
if err != nil {
t.Fatalf("Failed to retrieve instances: %v\n", err)
}
for _, instance := range instances {
idx := slices.Index(dbInstances, instance)
if idx < 0 {
t.Fatalf("Failed to find instance in list")
}
}
firstInstance, err := GetInstance(service)
if err != nil {
t.Fatalf("Failed to fetch single instance: %v\n", err)
}
secondInstance, err := GetInstance(service)
if err != nil {
t.Fatalf("Failed to fetch single instance (second): %v\n", err)
} else if firstInstance == secondInstance {
t.Fatalf("Same instance was selected twice")
}
_ = CloseDB()
}