db_indexid_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (C) 2025 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. package sqlite
  7. import (
  8. "testing"
  9. "github.com/syncthing/syncthing/lib/protocol"
  10. )
  11. func TestIndexIDs(t *testing.T) {
  12. t.Parallel()
  13. db, err := Open(t.TempDir())
  14. if err != nil {
  15. t.Fatal()
  16. }
  17. t.Cleanup(func() {
  18. if err := db.Close(); err != nil {
  19. t.Fatal(err)
  20. }
  21. })
  22. t.Run("LocalDeviceID", func(t *testing.T) {
  23. t.Parallel()
  24. localID, err := db.GetIndexID("foo", protocol.LocalDeviceID)
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. if localID == 0 {
  29. t.Fatal("should have been generated")
  30. }
  31. again, err := db.GetIndexID("foo", protocol.LocalDeviceID)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. if again != localID {
  36. t.Fatal("should get same again")
  37. }
  38. other, err := db.GetIndexID("bar", protocol.LocalDeviceID)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. if other == localID {
  43. t.Fatal("should not get same for other folder")
  44. }
  45. })
  46. t.Run("OtherDeviceID", func(t *testing.T) {
  47. t.Parallel()
  48. localID, err := db.GetIndexID("foo", protocol.DeviceID{42})
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if localID != 0 {
  53. t.Fatal("should have been zero")
  54. }
  55. newID := protocol.NewIndexID()
  56. if err := db.SetIndexID("foo", protocol.DeviceID{42}, newID); err != nil {
  57. t.Fatal(err)
  58. }
  59. again, err := db.GetIndexID("foo", protocol.DeviceID{42})
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. if again != newID {
  64. t.Log(again, newID)
  65. t.Fatal("should get the ID we set")
  66. }
  67. })
  68. }