From b5bad4defc6c75b9b969658229ce5fd2f3a46107 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Tue, 21 Jan 2025 13:46:29 -0700 Subject: 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). --- db/db_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 db/db_test.go (limited to 'db/db_test.go') diff --git a/db/db_test.go b/db/db_test.go new file mode 100644 index 0000000..7bc92ca --- /dev/null +++ b/db/db_test.go @@ -0,0 +1,60 @@ +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() +} -- cgit v1.2.3