25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

94 satır
2.6 KiB

  1. package common
  2. import (
  3. "testing"
  4. )
  5. func TestForeignUserIDStart(t *testing.T) {
  6. if ForeignUserIDStart != 10000000 {
  7. t.Errorf("ForeignUserIDStart = %d, want 10000000", ForeignUserIDStart)
  8. }
  9. }
  10. func TestUserSourceConstants(t *testing.T) {
  11. if UserSourceLocal != "local" {
  12. t.Errorf("UserSourceLocal = %q, want %q", UserSourceLocal, "local")
  13. }
  14. if UserSourceSynced != "synced" {
  15. t.Errorf("UserSourceSynced = %q, want %q", UserSourceSynced, "synced")
  16. }
  17. }
  18. func TestIsSyncedUser(t *testing.T) {
  19. tests := []struct {
  20. name string
  21. source string
  22. id int
  23. want bool
  24. }{
  25. {"synced with normal id", UserSourceSynced, 123, true},
  26. {"synced with id just below threshold", UserSourceSynced, ForeignUserIDStart - 1, true},
  27. {"synced with id 0", UserSourceSynced, 0, false},
  28. {"synced with id at threshold", UserSourceSynced, ForeignUserIDStart, false},
  29. {"local with normal id", UserSourceLocal, 123, false},
  30. {"empty source with normal id", "", 123, false},
  31. }
  32. for _, tt := range tests {
  33. t.Run(tt.name, func(t *testing.T) {
  34. if got := IsSyncedUser(tt.source, tt.id); got != tt.want {
  35. t.Errorf("IsSyncedUser(%q, %d) = %v, want %v", tt.source, tt.id, got, tt.want)
  36. }
  37. })
  38. }
  39. }
  40. func TestIsLocalUser(t *testing.T) {
  41. tests := []struct {
  42. name string
  43. source string
  44. id int
  45. want bool
  46. }{
  47. {"local at threshold", UserSourceLocal, ForeignUserIDStart, true},
  48. {"local above threshold", UserSourceLocal, ForeignUserIDStart + 100, true},
  49. {"local with id 0", UserSourceLocal, 0, true},
  50. {"synced with small id", UserSourceSynced, 123, false},
  51. {"empty source at threshold", "", ForeignUserIDStart, true},
  52. {"empty source with small id", "", 500, false},
  53. }
  54. for _, tt := range tests {
  55. t.Run(tt.name, func(t *testing.T) {
  56. if got := IsLocalUser(tt.source, tt.id); got != tt.want {
  57. t.Errorf("IsLocalUser(%q, %d) = %v, want %v", tt.source, tt.id, got, tt.want)
  58. }
  59. })
  60. }
  61. }
  62. func TestValidateUserSource(t *testing.T) {
  63. tests := []struct {
  64. name string
  65. source string
  66. id int
  67. wantErr bool
  68. }{
  69. {"synced with valid id", UserSourceSynced, 100, false},
  70. {"synced at threshold", UserSourceSynced, ForeignUserIDStart, true},
  71. {"local at threshold", UserSourceLocal, ForeignUserIDStart, false},
  72. {"local with small non-zero id", UserSourceLocal, 500, true},
  73. {"local with id 0", UserSourceLocal, 0, false},
  74. {"empty source with id 0", "", 0, false},
  75. }
  76. for _, tt := range tests {
  77. t.Run(tt.name, func(t *testing.T) {
  78. err := ValidateUserSource(tt.source, tt.id)
  79. if (err != nil) != tt.wantErr {
  80. t.Errorf("ValidateUserSource(%q, %d) error = %v, wantErr %v", tt.source, tt.id, err, tt.wantErr)
  81. }
  82. })
  83. }
  84. }