Skip to content

Commit ed2f6d9

Browse files
committed
Improve tests
1 parent 7fdb94d commit ed2f6d9

File tree

1 file changed

+174
-89
lines changed

1 file changed

+174
-89
lines changed

migrate_test.go

Lines changed: 174 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -217,150 +217,235 @@ func TestMigration(t *testing.T) {
217217
t.Fatalf("Migration failed: %v", err)
218218
}
219219

220-
// Verify SQLite data
221-
sqliteRawDB, err := sql.Open("sqlite3", sqlitePath)
222-
if err != nil {
223-
t.Fatalf("Failed to open SQLite: %v", err)
224-
}
225-
defer sqliteRawDB.Close()
226-
220+
// Verify SQLite data using GORM
227221
// Verify user1
228-
var u1UUID string
229-
var u1LastLogin sql.NullTime
230-
var u1MaxUSN int
231-
if err := sqliteRawDB.QueryRow("SELECT uuid, last_login_at, max_usn FROM users WHERE id = ?", user1.ID).Scan(&u1UUID, &u1LastLogin, &u1MaxUSN); err != nil {
222+
var sqliteUser1 SqliteUser
223+
if err := sqliteDB.First(&sqliteUser1, user1.ID).Error; err != nil {
232224
t.Fatalf("Failed to query user1: %v", err)
233225
}
234-
if u1UUID != user1.UUID {
235-
t.Errorf("User1 UUID: expected %s, got %s", user1.UUID, u1UUID)
226+
if sqliteUser1.ID != user1.ID {
227+
t.Errorf("User1 ID: expected %d, got %d", user1.ID, sqliteUser1.ID)
228+
}
229+
if sqliteUser1.UUID != user1.UUID {
230+
t.Errorf("User1 UUID: expected %s, got %s", user1.UUID, sqliteUser1.UUID)
236231
}
237-
if !u1LastLogin.Valid {
238-
t.Errorf("User1 LastLoginAt should be valid")
232+
if sqliteUser1.CreatedAt.Unix() != user1.CreatedAt.Unix() {
233+
t.Errorf("User1 CreatedAt: expected %v, got %v", user1.CreatedAt, sqliteUser1.CreatedAt)
239234
}
240-
if u1MaxUSN != user1.MaxUSN {
241-
t.Errorf("User1 MaxUSN: expected %d, got %d", user1.MaxUSN, u1MaxUSN)
235+
if sqliteUser1.UpdatedAt.Unix() != user1.UpdatedAt.Unix() {
236+
t.Errorf("User1 UpdatedAt: expected %v, got %v", user1.UpdatedAt, sqliteUser1.UpdatedAt)
237+
}
238+
if sqliteUser1.LastLoginAt == nil || user1.LastLoginAt == nil {
239+
if sqliteUser1.LastLoginAt != user1.LastLoginAt {
240+
t.Errorf("User1 LastLoginAt: one is nil, other is not")
241+
}
242+
} else if sqliteUser1.LastLoginAt.Unix() != user1.LastLoginAt.Unix() {
243+
t.Errorf("User1 LastLoginAt: expected %v, got %v", user1.LastLoginAt, sqliteUser1.LastLoginAt)
244+
}
245+
if sqliteUser1.MaxUSN != user1.MaxUSN {
246+
t.Errorf("User1 MaxUSN: expected %d, got %d", user1.MaxUSN, sqliteUser1.MaxUSN)
242247
}
243248

244249
// Verify user2
245-
var u2UUID string
246-
var u2LastLogin sql.NullTime
247-
var u2MaxUSN int
248-
if err := sqliteRawDB.QueryRow("SELECT uuid, last_login_at, max_usn FROM users WHERE id = ?", user2.ID).Scan(&u2UUID, &u2LastLogin, &u2MaxUSN); err != nil {
250+
var sqliteUser2 SqliteUser
251+
if err := sqliteDB.First(&sqliteUser2, user2.ID).Error; err != nil {
249252
t.Fatalf("Failed to query user2: %v", err)
250253
}
251-
if u2UUID != user2.UUID {
252-
t.Errorf("User2 UUID: expected %s, got %s", user2.UUID, u2UUID)
254+
if sqliteUser2.ID != user2.ID {
255+
t.Errorf("User2 ID: expected %d, got %d", user2.ID, sqliteUser2.ID)
256+
}
257+
if sqliteUser2.UUID != user2.UUID {
258+
t.Errorf("User2 UUID: expected %s, got %s", user2.UUID, sqliteUser2.UUID)
253259
}
254-
if u2LastLogin.Valid {
255-
t.Errorf("User2 LastLoginAt should be null")
260+
if sqliteUser2.LastLoginAt != nil {
261+
t.Errorf("User2 LastLoginAt should be nil, got %v", sqliteUser2.LastLoginAt)
256262
}
257-
if u2MaxUSN != user2.MaxUSN {
258-
t.Errorf("User2 MaxUSN: expected %d, got %d", user2.MaxUSN, u2MaxUSN)
263+
if sqliteUser2.MaxUSN != user2.MaxUSN {
264+
t.Errorf("User2 MaxUSN: expected %d, got %d", user2.MaxUSN, sqliteUser2.MaxUSN)
259265
}
260266

261267
// Verify account1
262-
var a1Email sql.NullString
263-
var a1EmailVerified bool
264-
var a1Password sql.NullString
265-
if err := sqliteRawDB.QueryRow("SELECT email, email_verified, password FROM accounts WHERE user_id = ?", user1.ID).Scan(&a1Email, &a1EmailVerified, &a1Password); err != nil {
268+
var sqliteAccount1 SqliteAccount
269+
if err := sqliteDB.Where("user_id = ?", user1.ID).First(&sqliteAccount1).Error; err != nil {
266270
t.Fatalf("Failed to query account1: %v", err)
267271
}
268-
if a1Email.String != "user1@example.com" {
269-
t.Errorf("Account1 Email: expected 'user1@example.com', got '%s'", a1Email.String)
272+
if sqliteAccount1.ID != account1.ID {
273+
t.Errorf("Account1 ID: expected %d, got %d", account1.ID, sqliteAccount1.ID)
274+
}
275+
if sqliteAccount1.UserID != account1.UserID {
276+
t.Errorf("Account1 UserID: expected %d, got %d", account1.UserID, sqliteAccount1.UserID)
277+
}
278+
if sqliteAccount1.Email.String != account1.Email.String {
279+
t.Errorf("Account1 Email: expected %s, got %s", account1.Email.String, sqliteAccount1.Email.String)
270280
}
271-
if !a1EmailVerified {
272-
t.Errorf("Account1 EmailVerified: expected true, got false")
281+
if sqliteAccount1.EmailVerified != account1.EmailVerified {
282+
t.Errorf("Account1 EmailVerified: expected %v, got %v", account1.EmailVerified, sqliteAccount1.EmailVerified)
273283
}
274-
if a1Password.String != "hashedpassword1" {
275-
t.Errorf("Account1 Password: expected 'hashedpassword1', got '%s'", a1Password.String)
284+
if sqliteAccount1.Password.String != account1.Password.String {
285+
t.Errorf("Account1 Password: expected %s, got %s", account1.Password.String, sqliteAccount1.Password.String)
286+
}
287+
if sqliteAccount1.CreatedAt.Unix() != account1.CreatedAt.Unix() {
288+
t.Errorf("Account1 CreatedAt: expected %v, got %v", account1.CreatedAt, sqliteAccount1.CreatedAt)
289+
}
290+
if sqliteAccount1.UpdatedAt.Unix() != account1.UpdatedAt.Unix() {
291+
t.Errorf("Account1 UpdatedAt: expected %v, got %v", account1.UpdatedAt, sqliteAccount1.UpdatedAt)
276292
}
277293

278294
// Verify account2
279-
var a2Email sql.NullString
280-
var a2EmailVerified bool
281-
var a2Password sql.NullString
282-
if err := sqliteRawDB.QueryRow("SELECT email, email_verified, password FROM accounts WHERE user_id = ?", user2.ID).Scan(&a2Email, &a2EmailVerified, &a2Password); err != nil {
295+
var sqliteAccount2 SqliteAccount
296+
if err := sqliteDB.Where("user_id = ?", user2.ID).First(&sqliteAccount2).Error; err != nil {
283297
t.Fatalf("Failed to query account2: %v", err)
284298
}
285-
if a2Email.String != "user2@example.com" {
286-
t.Errorf("Account2 Email: expected 'user2@example.com', got '%s'", a2Email.String)
299+
if sqliteAccount2.Email.String != account2.Email.String {
300+
t.Errorf("Account2 Email: expected %s, got %s", account2.Email.String, sqliteAccount2.Email.String)
287301
}
288-
if a2EmailVerified {
289-
t.Errorf("Account2 EmailVerified: expected false, got true")
302+
if sqliteAccount2.EmailVerified != account2.EmailVerified {
303+
t.Errorf("Account2 EmailVerified: expected %v, got %v", account2.EmailVerified, sqliteAccount2.EmailVerified)
290304
}
291305

292306
// Verify book1
293-
var b1UUID, b1Label string
294-
var b1AddedOn, b1EditedOn int64
295-
var b1USN int
296-
var b1Deleted, b1Encrypted bool
297-
if err := sqliteRawDB.QueryRow("SELECT uuid, label, added_on, edited_on, usn, deleted, encrypted FROM books WHERE user_id = ?", user1.ID).Scan(&b1UUID, &b1Label, &b1AddedOn, &b1EditedOn, &b1USN, &b1Deleted, &b1Encrypted); err != nil {
307+
var sqliteBook1 SqliteBook
308+
if err := sqliteDB.Where("user_id = ?", user1.ID).First(&sqliteBook1).Error; err != nil {
298309
t.Fatalf("Failed to query book1: %v", err)
299310
}
300-
if b1UUID != book1.UUID {
301-
t.Errorf("Book1 UUID: expected %s, got %s", book1.UUID, b1UUID)
311+
if sqliteBook1.ID != book1.ID {
312+
t.Errorf("Book1 ID: expected %d, got %d", book1.ID, sqliteBook1.ID)
313+
}
314+
if sqliteBook1.UUID != book1.UUID {
315+
t.Errorf("Book1 UUID: expected %s, got %s", book1.UUID, sqliteBook1.UUID)
316+
}
317+
if sqliteBook1.UserID != book1.UserID {
318+
t.Errorf("Book1 UserID: expected %d, got %d", book1.UserID, sqliteBook1.UserID)
319+
}
320+
if sqliteBook1.Label != book1.Label {
321+
t.Errorf("Book1 Label: expected %s, got %s", book1.Label, sqliteBook1.Label)
302322
}
303-
if b1Label != "golang" {
304-
t.Errorf("Book1 Label: expected 'golang', got '%s'", b1Label)
323+
if sqliteBook1.AddedOn != book1.AddedOn {
324+
t.Errorf("Book1 AddedOn: expected %d, got %d", book1.AddedOn, sqliteBook1.AddedOn)
305325
}
306-
if b1AddedOn != book1.AddedOn {
307-
t.Errorf("Book1 AddedOn: expected %d, got %d", book1.AddedOn, b1AddedOn)
326+
if sqliteBook1.EditedOn != book1.EditedOn {
327+
t.Errorf("Book1 EditedOn: expected %d, got %d", book1.EditedOn, sqliteBook1.EditedOn)
308328
}
309-
if b1USN != book1.USN {
310-
t.Errorf("Book1 USN: expected %d, got %d", book1.USN, b1USN)
329+
if sqliteBook1.USN != book1.USN {
330+
t.Errorf("Book1 USN: expected %d, got %d", book1.USN, sqliteBook1.USN)
311331
}
312-
if b1Deleted != book1.Deleted {
313-
t.Errorf("Book1 Deleted: expected %v, got %v", book1.Deleted, b1Deleted)
332+
if sqliteBook1.Deleted != book1.Deleted {
333+
t.Errorf("Book1 Deleted: expected %v, got %v", book1.Deleted, sqliteBook1.Deleted)
334+
}
335+
if sqliteBook1.Encrypted != book1.Encrypted {
336+
t.Errorf("Book1 Encrypted: expected %v, got %v", book1.Encrypted, sqliteBook1.Encrypted)
337+
}
338+
if sqliteBook1.CreatedAt.Unix() != book1.CreatedAt.Unix() {
339+
t.Errorf("Book1 CreatedAt: expected %v, got %v", book1.CreatedAt, sqliteBook1.CreatedAt)
340+
}
341+
if sqliteBook1.UpdatedAt.Unix() != book1.UpdatedAt.Unix() {
342+
t.Errorf("Book1 UpdatedAt: expected %v, got %v", book1.UpdatedAt, sqliteBook1.UpdatedAt)
314343
}
315344

316345
// Verify note1
317-
var n1UUID, n1BookUUID, n1Body, n1Client string
318-
var n1AddedOn, n1EditedOn int64
319-
var n1Public, n1Deleted, n1Encrypted bool
320-
var n1USN int
321-
if err := sqliteRawDB.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, public, usn, deleted, encrypted, client FROM notes WHERE user_id = ?", user1.ID).Scan(&n1UUID, &n1BookUUID, &n1Body, &n1AddedOn, &n1EditedOn, &n1Public, &n1USN, &n1Deleted, &n1Encrypted, &n1Client); err != nil {
346+
var sqliteNote1 SqliteNote
347+
if err := sqliteDB.Where("user_id = ?", user1.ID).First(&sqliteNote1).Error; err != nil {
322348
t.Fatalf("Failed to query note1: %v", err)
323349
}
324-
if n1UUID != note1.UUID {
325-
t.Errorf("Note1 UUID: expected %s, got %s", note1.UUID, n1UUID)
350+
if sqliteNote1.ID != note1.ID {
351+
t.Errorf("Note1 ID: expected %d, got %d", note1.ID, sqliteNote1.ID)
352+
}
353+
if sqliteNote1.UUID != note1.UUID {
354+
t.Errorf("Note1 UUID: expected %s, got %s", note1.UUID, sqliteNote1.UUID)
326355
}
327-
if n1BookUUID != book1.UUID {
328-
t.Errorf("Note1 BookUUID: expected %s, got %s", book1.UUID, n1BookUUID)
356+
if sqliteNote1.UserID != note1.UserID {
357+
t.Errorf("Note1 UserID: expected %d, got %d", note1.UserID, sqliteNote1.UserID)
329358
}
330-
if n1Body != "This is a test note about golang" {
331-
t.Errorf("Note1 Body: expected 'This is a test note about golang', got '%s'", n1Body)
359+
if sqliteNote1.BookUUID != note1.BookUUID {
360+
t.Errorf("Note1 BookUUID: expected %s, got %s", note1.BookUUID, sqliteNote1.BookUUID)
332361
}
333-
if !n1Public {
334-
t.Errorf("Note1 Public: expected true, got false")
362+
if sqliteNote1.Body != note1.Body {
363+
t.Errorf("Note1 Body: expected %s, got %s", note1.Body, sqliteNote1.Body)
335364
}
336-
if n1Client != "cli" {
337-
t.Errorf("Note1 Client: expected 'cli', got '%s'", n1Client)
365+
if sqliteNote1.AddedOn != note1.AddedOn {
366+
t.Errorf("Note1 AddedOn: expected %d, got %d", note1.AddedOn, sqliteNote1.AddedOn)
367+
}
368+
if sqliteNote1.EditedOn != note1.EditedOn {
369+
t.Errorf("Note1 EditedOn: expected %d, got %d", note1.EditedOn, sqliteNote1.EditedOn)
370+
}
371+
if sqliteNote1.Public != note1.Public {
372+
t.Errorf("Note1 Public: expected %v, got %v", note1.Public, sqliteNote1.Public)
373+
}
374+
if sqliteNote1.USN != note1.USN {
375+
t.Errorf("Note1 USN: expected %d, got %d", note1.USN, sqliteNote1.USN)
376+
}
377+
if sqliteNote1.Deleted != note1.Deleted {
378+
t.Errorf("Note1 Deleted: expected %v, got %v", note1.Deleted, sqliteNote1.Deleted)
379+
}
380+
if sqliteNote1.Encrypted != note1.Encrypted {
381+
t.Errorf("Note1 Encrypted: expected %v, got %v", note1.Encrypted, sqliteNote1.Encrypted)
382+
}
383+
if sqliteNote1.Client != note1.Client {
384+
t.Errorf("Note1 Client: expected %s, got %s", note1.Client, sqliteNote1.Client)
385+
}
386+
if sqliteNote1.CreatedAt.Unix() != note1.CreatedAt.Unix() {
387+
t.Errorf("Note1 CreatedAt: expected %v, got %v", note1.CreatedAt, sqliteNote1.CreatedAt)
388+
}
389+
if sqliteNote1.UpdatedAt.Unix() != note1.UpdatedAt.Unix() {
390+
t.Errorf("Note1 UpdatedAt: expected %v, got %v", note1.UpdatedAt, sqliteNote1.UpdatedAt)
338391
}
339392

340393
// Verify token1
341-
var t1Value, t1Type string
342-
var t1UsedAt sql.NullTime
343-
if err := sqliteRawDB.QueryRow("SELECT value, type, used_at FROM tokens WHERE user_id = ?", user1.ID).Scan(&t1Value, &t1Type, &t1UsedAt); err != nil {
394+
var sqliteToken1 SqliteToken
395+
if err := sqliteDB.Where("user_id = ?", user1.ID).First(&sqliteToken1).Error; err != nil {
344396
t.Fatalf("Failed to query token1: %v", err)
345397
}
346-
if t1Value != "token123" {
347-
t.Errorf("Token1 Value: expected 'token123', got '%s'", t1Value)
398+
if sqliteToken1.ID != token1.ID {
399+
t.Errorf("Token1 ID: expected %d, got %d", token1.ID, sqliteToken1.ID)
400+
}
401+
if sqliteToken1.UserID != token1.UserID {
402+
t.Errorf("Token1 UserID: expected %d, got %d", token1.UserID, sqliteToken1.UserID)
348403
}
349-
if t1Type != "access" {
350-
t.Errorf("Token1 Type: expected 'access', got '%s'", t1Type)
404+
if sqliteToken1.Value != token1.Value {
405+
t.Errorf("Token1 Value: expected %s, got %s", token1.Value, sqliteToken1.Value)
351406
}
352-
if !t1UsedAt.Valid {
353-
t.Errorf("Token1 UsedAt should be valid")
407+
if sqliteToken1.Type != token1.Type {
408+
t.Errorf("Token1 Type: expected %s, got %s", token1.Type, sqliteToken1.Type)
409+
}
410+
if sqliteToken1.UsedAt == nil || token1.UsedAt == nil {
411+
if sqliteToken1.UsedAt != token1.UsedAt {
412+
t.Errorf("Token1 UsedAt: one is nil, other is not")
413+
}
414+
} else if sqliteToken1.UsedAt.Unix() != token1.UsedAt.Unix() {
415+
t.Errorf("Token1 UsedAt: expected %v, got %v", token1.UsedAt, sqliteToken1.UsedAt)
416+
}
417+
if sqliteToken1.CreatedAt.Unix() != token1.CreatedAt.Unix() {
418+
t.Errorf("Token1 CreatedAt: expected %v, got %v", token1.CreatedAt, sqliteToken1.CreatedAt)
419+
}
420+
if sqliteToken1.UpdatedAt.Unix() != token1.UpdatedAt.Unix() {
421+
t.Errorf("Token1 UpdatedAt: expected %v, got %v", token1.UpdatedAt, sqliteToken1.UpdatedAt)
354422
}
355423

356424
// Verify session1
357-
var s1Key string
358-
var s1LastUsedAt, s1ExpiresAt time.Time
359-
if err := sqliteRawDB.QueryRow("SELECT key, last_used_at, expires_at FROM sessions WHERE user_id = ?", user1.ID).Scan(&s1Key, &s1LastUsedAt, &s1ExpiresAt); err != nil {
425+
var sqliteSession1 SqliteSession
426+
if err := sqliteDB.Where("user_id = ?", user1.ID).First(&sqliteSession1).Error; err != nil {
360427
t.Fatalf("Failed to query session1: %v", err)
361428
}
362-
if s1Key != "session123" {
363-
t.Errorf("Session1 Key: expected 'session123', got '%s'", s1Key)
429+
if sqliteSession1.ID != session1.ID {
430+
t.Errorf("Session1 ID: expected %d, got %d", session1.ID, sqliteSession1.ID)
431+
}
432+
if sqliteSession1.UserID != session1.UserID {
433+
t.Errorf("Session1 UserID: expected %d, got %d", session1.UserID, sqliteSession1.UserID)
434+
}
435+
if sqliteSession1.Key != session1.Key {
436+
t.Errorf("Session1 Key: expected %s, got %s", session1.Key, sqliteSession1.Key)
437+
}
438+
if sqliteSession1.LastUsedAt.Unix() != session1.LastUsedAt.Unix() {
439+
t.Errorf("Session1 LastUsedAt: expected %v, got %v", session1.LastUsedAt, sqliteSession1.LastUsedAt)
440+
}
441+
if sqliteSession1.ExpiresAt.Unix() != session1.ExpiresAt.Unix() {
442+
t.Errorf("Session1 ExpiresAt: expected %v, got %v", session1.ExpiresAt, sqliteSession1.ExpiresAt)
443+
}
444+
if sqliteSession1.CreatedAt.Unix() != session1.CreatedAt.Unix() {
445+
t.Errorf("Session1 CreatedAt: expected %v, got %v", session1.CreatedAt, sqliteSession1.CreatedAt)
446+
}
447+
if sqliteSession1.UpdatedAt.Unix() != session1.UpdatedAt.Unix() {
448+
t.Errorf("Session1 UpdatedAt: expected %v, got %v", session1.UpdatedAt, sqliteSession1.UpdatedAt)
364449
}
365450

366451
// Clean up

0 commit comments

Comments
 (0)