|
- package common
-
- import (
- "testing"
- )
-
- func TestForeignUserIDStart(t *testing.T) {
- if ForeignUserIDStart != 10000000 {
- t.Errorf("ForeignUserIDStart = %d, want 10000000", ForeignUserIDStart)
- }
- }
-
- func TestUserSourceConstants(t *testing.T) {
- if UserSourceLocal != "local" {
- t.Errorf("UserSourceLocal = %q, want %q", UserSourceLocal, "local")
- }
- if UserSourceSynced != "synced" {
- t.Errorf("UserSourceSynced = %q, want %q", UserSourceSynced, "synced")
- }
- }
-
- func TestIsSyncedUser(t *testing.T) {
- tests := []struct {
- name string
- source string
- id int
- want bool
- }{
- {"synced with normal id", UserSourceSynced, 123, true},
- {"synced with id just below threshold", UserSourceSynced, ForeignUserIDStart - 1, true},
- {"synced with id 0", UserSourceSynced, 0, false},
- {"synced with id at threshold", UserSourceSynced, ForeignUserIDStart, false},
- {"local with normal id", UserSourceLocal, 123, false},
- {"empty source with normal id", "", 123, false},
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if got := IsSyncedUser(tt.source, tt.id); got != tt.want {
- t.Errorf("IsSyncedUser(%q, %d) = %v, want %v", tt.source, tt.id, got, tt.want)
- }
- })
- }
- }
-
- func TestIsLocalUser(t *testing.T) {
- tests := []struct {
- name string
- source string
- id int
- want bool
- }{
- {"local at threshold", UserSourceLocal, ForeignUserIDStart, true},
- {"local above threshold", UserSourceLocal, ForeignUserIDStart + 100, true},
- {"local with id 0", UserSourceLocal, 0, true},
- {"synced with small id", UserSourceSynced, 123, false},
- {"empty source at threshold", "", ForeignUserIDStart, true},
- {"empty source with small id", "", 500, false},
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if got := IsLocalUser(tt.source, tt.id); got != tt.want {
- t.Errorf("IsLocalUser(%q, %d) = %v, want %v", tt.source, tt.id, got, tt.want)
- }
- })
- }
- }
-
- func TestValidateUserSource(t *testing.T) {
- tests := []struct {
- name string
- source string
- id int
- wantErr bool
- }{
- {"synced with valid id", UserSourceSynced, 100, false},
- {"synced at threshold", UserSourceSynced, ForeignUserIDStart, true},
- {"local at threshold", UserSourceLocal, ForeignUserIDStart, false},
- {"local with small non-zero id", UserSourceLocal, 500, true},
- {"local with id 0", UserSourceLocal, 0, false},
- {"empty source with id 0", "", 0, false},
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- err := ValidateUserSource(tt.source, tt.id)
- if (err != nil) != tt.wantErr {
- t.Errorf("ValidateUserSource(%q, %d) error = %v, wantErr %v", tt.source, tt.id, err, tt.wantErr)
- }
- })
- }
- }
|