123 lines
3.3 KiB
Go
123 lines
3.3 KiB
Go
package access
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Helper function to create a mock card
|
|
func mockCard(uid string, cardType string) *Card {
|
|
return &Card{
|
|
UID: uid,
|
|
Type: cardType,
|
|
}
|
|
}
|
|
|
|
// TestNewCardTimeSeries tests the NewCardTimeSeries constructor.
|
|
func TestNewCardTimeSeries(t *testing.T) {
|
|
cts := NewCardTimeSeries()
|
|
if cts.Map == nil || cts.crossRef == nil || cts.mu == nil {
|
|
t.Error("NewCardTimeSeries() failed to initialize all fields")
|
|
}
|
|
}
|
|
|
|
// TestCardTimeSeries_CheckIn tests the CheckIn method.
|
|
func TestCardTimeSeries_CheckIn(t *testing.T) {
|
|
cts := NewCardTimeSeries()
|
|
card := mockCard("12345", "TestType")
|
|
cts.CheckIn(card)
|
|
|
|
if _, ok := cts.Map[cts.last]; !ok {
|
|
t.Errorf("CheckIn() failed to add card to Map")
|
|
}
|
|
|
|
if _, ok := cts.crossRef[card]; !ok {
|
|
t.Errorf("CheckIn() failed to add card to crossRef")
|
|
}
|
|
}
|
|
|
|
// TestCardTimeSeries_Get tests the Get method.
|
|
func TestCardTimeSeries_Get(t *testing.T) {
|
|
cts := NewCardTimeSeries()
|
|
card := mockCard("12345", "TestType")
|
|
now := time.Now()
|
|
cts.Map[now] = card
|
|
|
|
if got := cts.Get(now); !reflect.DeepEqual(got, card) {
|
|
t.Errorf("Get() = %v, want %v", got, card)
|
|
}
|
|
}
|
|
|
|
// TestCardTimeSeries_GetClosest tests the GetClosest method.
|
|
func TestCardTimeSeries_GetClosest(t *testing.T) {
|
|
cts := NewCardTimeSeries()
|
|
card1 := mockCard("12345", "TestType")
|
|
card2 := mockCard("67890", "TestType")
|
|
now := time.Now()
|
|
cts.Map[now] = card1
|
|
cts.Map[now.Add(-1*time.Hour)] = card2
|
|
|
|
if got := cts.GetClosest(now.Add(-30 * time.Minute)); !reflect.DeepEqual(got, card2) {
|
|
t.Errorf("GetClosest() = %v, want %v", got, card2)
|
|
}
|
|
}
|
|
|
|
// TestCardTimeSeries_GetLast tests the GetLast method.
|
|
func TestCardTimeSeries_GetLast(t *testing.T) {
|
|
cts := NewCardTimeSeries()
|
|
card := mockCard("12345", "TestType")
|
|
cts.CheckIn(card)
|
|
|
|
if got := cts.GetLast(); !reflect.DeepEqual(got, card) {
|
|
t.Errorf("GetLast() = %v, want %v", got, card)
|
|
}
|
|
}
|
|
|
|
// TestKeys_ListenForTags tests the ListenForTags method.
|
|
func TestKeys_ListenForTags(t *testing.T) {
|
|
// This test may require mocking of the NFC reader and its methods
|
|
// Assuming a mock implementation is available, this test would simulate the behavior of the ListenForTags method
|
|
}
|
|
|
|
// TestKeys_ListenForOneTag tests the ListenForOneTag method.
|
|
func TestKeys_ListenForOneTag(t *testing.T) {
|
|
connString := os.Getenv("TEST_RFID")
|
|
if connString == "" {
|
|
t.Skip("TEST_RFID environment variable not set")
|
|
}
|
|
|
|
// Assuming NewRFID function and related dependencies are properly mocked
|
|
rfid, err := NewRFID(connString, SPI) // Or I2C, depending on the test setup
|
|
if err != nil {
|
|
t.Fatalf("Failed to create RFID instance: %v", err)
|
|
}
|
|
defer rfid.Close()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
card, err := rfid.ListenForOneTag(ctx)
|
|
if err != nil {
|
|
t.Errorf("ListenForOneTag() returned an error: %v", err)
|
|
}
|
|
|
|
if card == nil {
|
|
t.Errorf("ListenForOneTag() returned nil, expected a card")
|
|
}
|
|
|
|
t.Run("CheckIn", func(t *testing.T) {
|
|
if rfid.AccessLog.GetLast() != card {
|
|
t.Errorf("ListenForOneTag() failed to check card into timeseries log")
|
|
}
|
|
if rfid.AccessLog.GetClosest(time.Now()) != card {
|
|
t.Errorf("ListenForOneTag() failed to check card into timeseries log")
|
|
}
|
|
})
|
|
|
|
}
|
|
|
|
// Note: Additional mocking and setup may be required to test methods that interact with external dependencies like NFC reader.
|