2022-12-14 18:53:55 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func assertTxns(t *testing.T, gotEventToTxn map[string]string, wantEventToTxn map[string]string) {
|
|
|
|
t.Helper()
|
|
|
|
if len(gotEventToTxn) != len(wantEventToTxn) {
|
|
|
|
t.Errorf("got %d results, want %d", len(gotEventToTxn), len(wantEventToTxn))
|
|
|
|
}
|
|
|
|
for wantEventID, wantTxnID := range wantEventToTxn {
|
|
|
|
gotTxnID, ok := gotEventToTxn[wantEventID]
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("txn ID for event %v is missing", wantEventID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if gotTxnID != wantTxnID {
|
|
|
|
t.Errorf("event %v got txn ID %v want %v", wantEventID, gotTxnID, wantTxnID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransactionTable(t *testing.T) {
|
2023-01-18 14:54:26 +00:00
|
|
|
db, close := connectToDB(t)
|
|
|
|
defer close()
|
2022-12-14 18:53:55 +00:00
|
|
|
userID := "@alice:txns"
|
2023-05-02 14:56:59 +01:00
|
|
|
deviceID := "alice_phone"
|
2022-12-14 18:53:55 +00:00
|
|
|
eventA := "$A"
|
|
|
|
eventB := "$B"
|
|
|
|
txnIDA := "txn_A"
|
|
|
|
txnIDB := "txn_B"
|
|
|
|
table := NewTransactionsTable(db)
|
|
|
|
// empty table select
|
2023-05-02 14:56:59 +01:00
|
|
|
gotTxns, err := table.Select(userID, deviceID, []string{eventA})
|
2022-12-14 18:53:55 +00:00
|
|
|
assertNoError(t, err)
|
|
|
|
assertTxns(t, gotTxns, nil)
|
|
|
|
|
|
|
|
// basic insert and select
|
2023-05-02 14:56:59 +01:00
|
|
|
err = table.Insert(userID, deviceID, map[string]string{
|
2022-12-14 18:53:55 +00:00
|
|
|
eventA: txnIDA,
|
|
|
|
})
|
|
|
|
assertNoError(t, err)
|
2023-05-02 14:56:59 +01:00
|
|
|
gotTxns, err = table.Select(userID, deviceID, []string{eventA})
|
2022-12-14 18:53:55 +00:00
|
|
|
assertNoError(t, err)
|
|
|
|
assertTxns(t, gotTxns, map[string]string{
|
|
|
|
eventA: txnIDA,
|
|
|
|
})
|
|
|
|
|
|
|
|
// multiple txns
|
2023-05-02 14:56:59 +01:00
|
|
|
err = table.Insert(userID, deviceID, map[string]string{
|
2022-12-14 18:53:55 +00:00
|
|
|
eventB: txnIDB,
|
|
|
|
})
|
|
|
|
assertNoError(t, err)
|
2023-05-02 14:56:59 +01:00
|
|
|
gotTxns, err = table.Select(userID, deviceID, []string{eventA, eventB})
|
2022-12-14 18:53:55 +00:00
|
|
|
assertNoError(t, err)
|
|
|
|
assertTxns(t, gotTxns, map[string]string{
|
|
|
|
eventA: txnIDA,
|
|
|
|
eventB: txnIDB,
|
|
|
|
})
|
|
|
|
|
|
|
|
// different user select
|
2023-05-02 14:56:59 +01:00
|
|
|
gotTxns, err = table.Select("@another", "another_device", []string{eventA, eventB})
|
2022-12-14 18:53:55 +00:00
|
|
|
assertNoError(t, err)
|
|
|
|
assertTxns(t, gotTxns, nil)
|
|
|
|
|
|
|
|
// no-op cleanup
|
|
|
|
err = table.Clean(time.Now().Add(-1 * time.Minute))
|
|
|
|
assertNoError(t, err)
|
2023-05-02 14:56:59 +01:00
|
|
|
gotTxns, err = table.Select(userID, deviceID, []string{eventA, eventB})
|
2022-12-14 18:53:55 +00:00
|
|
|
assertNoError(t, err)
|
|
|
|
assertTxns(t, gotTxns, map[string]string{
|
|
|
|
eventA: txnIDA,
|
|
|
|
eventB: txnIDB,
|
|
|
|
})
|
|
|
|
|
|
|
|
// real cleanup
|
|
|
|
err = table.Clean(time.Now())
|
|
|
|
assertNoError(t, err)
|
2023-05-02 14:56:59 +01:00
|
|
|
gotTxns, err = table.Select(userID, deviceID, []string{eventA, eventB})
|
2022-12-14 18:53:55 +00:00
|
|
|
assertNoError(t, err)
|
|
|
|
assertTxns(t, gotTxns, nil)
|
|
|
|
|
|
|
|
}
|