Fix: unittests

This commit is contained in:
Grail Finder
2025-07-02 19:00:39 +03:00
parent a438d5b665
commit 130ed3763b
15 changed files with 227 additions and 245 deletions

View File

@ -16,9 +16,10 @@ type SessionsRepo interface {
}
func (p *RepoProvider) SessionByToken(ctx context.Context, token string) (*models.Session, error) {
db := getDB(ctx, p.DB)
session := &models.Session{}
// The lifetime in the DB is in seconds, but in the model it is in minutes.
err := p.DB.GetContext(ctx, session, `SELECT id, updated_at, lifetime / 60 as lifetime, cookie_token, username FROM sessions WHERE cookie_token = ?`, token)
err := sqlx.GetContext(ctx, db, session, `SELECT id, updated_at, lifetime / 60 as lifetime, cookie_token, username FROM sessions WHERE cookie_token = ?`, token)
if err != nil {
return nil, err
}
@ -26,36 +27,23 @@ func (p *RepoProvider) SessionByToken(ctx context.Context, token string) (*model
}
func (p *RepoProvider) SessionCreate(ctx context.Context, session *models.Session) error {
p.Ext = p.DB
tx, ok := ctx.Value("sqltx").(*sqlx.Tx)
if ok && tx != nil {
// how to know if it is a final exec in chain?
// or is it better to commit outside?
tocommit, ok := ctx.Value("tocommit").(bool)
if ok && tocommit {
defer func() {
if err := tx.Commit(); err != nil {
// log
// return err
}
}()
}
p.Ext = tx
}
db := getDB(ctx, p.DB)
// The lifetime in the model is in minutes, but in the DB it is in seconds.
_, err := p.Ext.Exec(`INSERT INTO sessions (updated_at, lifetime, cookie_token, username) VALUES (?, ?, ?, ?)`,
_, err := db.ExecContext(ctx, `INSERT INTO sessions (updated_at, lifetime, cookie_token, username) VALUES (?, ?, ?, ?)`,
time.Now(), session.Lifetime*60, session.CookieToken, session.Username)
return err
}
func (p *RepoProvider) SessionUpdate(ctx context.Context, session *models.Session) error {
db := getDB(ctx, p.DB)
// The lifetime in the model is in minutes, but in the DB it is in seconds.
_, err := p.DB.ExecContext(ctx, `UPDATE sessions SET updated_at = ?, lifetime = ? WHERE cookie_token = ?`,
_, err := db.ExecContext(ctx, `UPDATE sessions SET updated_at = ?, lifetime = ? WHERE cookie_token = ?`,
time.Now(), session.Lifetime*60, session.CookieToken)
return err
}
func (p *RepoProvider) SessionDelete(ctx context.Context, token string) error {
_, err := p.DB.ExecContext(ctx, `DELETE FROM sessions WHERE cookie_token = ?`, token)
db := getDB(ctx, p.DB)
_, err := db.ExecContext(ctx, `DELETE FROM sessions WHERE cookie_token = ?`, token)
return err
}