stats_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (C) 2020 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. // The existence of this file means we get 0% test coverage rather than no
  7. // test coverage at all. Remove when implementing an actual test.
  8. package stats
  9. import (
  10. "testing"
  11. "time"
  12. "github.com/syncthing/syncthing/internal/db"
  13. "github.com/syncthing/syncthing/internal/db/sqlite"
  14. )
  15. func TestDeviceStat(t *testing.T) {
  16. sdb, err := sqlite.Open(t.TempDir())
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. t.Cleanup(func() {
  21. sdb.Close()
  22. })
  23. sr := NewDeviceStatisticsReference(db.NewTyped(sdb, "devstatref"))
  24. if err := sr.WasSeen(); err != nil {
  25. t.Fatal(err)
  26. }
  27. if err := sr.LastConnectionDuration(42 * time.Second); err != nil {
  28. t.Fatal(err)
  29. }
  30. stat, err := sr.GetStatistics()
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. if d := time.Since(stat.LastSeen); d > 5*time.Second {
  35. t.Error("Last seen far in the past:", d)
  36. }
  37. if d := stat.LastConnectionDurationS; d != 42 {
  38. t.Error("Bad last duration:", d)
  39. }
  40. }