go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/apputil/model_test.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package apputil 9 10 import ( 11 "context" 12 "testing" 13 "time" 14 15 "go.charczuk.com/sdk/assert" 16 ) 17 18 func Test_Model_DeleteExpiredSessions(t *testing.T) { 19 mgr, done := NewTest(t) 20 defer done() 21 22 u0 := CreateTestUser(t, mgr) 23 s0 := CreateTestSession(t, mgr, u0) 24 s1 := CreateTestSession(t, mgr, u0) 25 s2 := CreateTestSession(t, mgr, u0) 26 27 // update a couple of them to be expired 28 s0.ExpiresUTC = time.Now().UTC().Add(-6 * time.Hour) 29 updated, err := mgr.Invoke(context.Background()).Update(s0) 30 assert.ItsEqual(t, true, updated) 31 assert.ItsNil(t, err) 32 33 s1.ExpiresUTC = time.Now().UTC().Add(-5 * time.Hour) 34 updated, err = mgr.Invoke(context.Background()).Update(s1) 35 assert.ItsEqual(t, true, updated) 36 assert.ItsNil(t, err) 37 38 err = mgr.DeleteExpiredSessions(context.Background(), time.Now().UTC().Add(-time.Hour)) 39 assert.ItsNil(t, err) 40 41 var allSessions []Session 42 err = mgr.Invoke(context.Background()).All(&allSessions) 43 assert.ItsNil(t, err) 44 45 assert.ItsLen(t, allSessions, 1) 46 assert.ItsEqual(t, s2.SessionID, allSessions[0].SessionID) 47 }