github.com/k0marov/go-socnet@v0.0.0-20220715154813-90d07867c782/features/profiles/store/sql_db/sql_db_test.go (about) 1 package sql_db_test 2 3 import ( 4 "github.com/k0marov/go-socnet/core/general/core_err" 5 . "github.com/k0marov/go-socnet/core/helpers/test_helpers" 6 "testing" 7 8 "github.com/k0marov/go-socnet/features/profiles/domain/models" 9 "github.com/k0marov/go-socnet/features/profiles/store" 10 "github.com/k0marov/go-socnet/features/profiles/store/sql_db" 11 12 _ "github.com/mattn/go-sqlite3" 13 ) 14 15 func TestSqlDB_ErrorHandling(t *testing.T) { 16 sql := OpenSqliteDB(t) 17 sut, err := sql_db.NewSqlDB(sql) 18 AssertNoError(t, err) 19 sql.Close() // this will force all calls to throw errors 20 t.Run("GetProfile", func(t *testing.T) { 21 _, err := sut.GetProfile(RandomString()) 22 AssertSomeError(t, err) 23 }) 24 t.Run("CreateProfile", func(t *testing.T) { 25 err := sut.CreateProfile(RandomProfileModel()) 26 AssertSomeError(t, err) 27 }) 28 t.Run("UpdateProfile", func(t *testing.T) { 29 err := sut.UpdateProfile(RandomString(), store.DBUpdateData{About: RandomString(), AvatarPath: RandomString()}) 30 AssertSomeError(t, err) 31 }) 32 } 33 34 func TestSqlDB(t *testing.T) { 35 t.Run("creating and reading profiles", func(t *testing.T) { 36 profileCount := 10 37 38 db, err := sql_db.NewSqlDB(OpenSqliteDB(t)) 39 AssertNoError(t, err) 40 41 // create 10 random profiles 42 profiles := []models.ProfileModel{} 43 for i := 0; i < profileCount; i++ { 44 profiles = append(profiles, RandomProfileModel()) 45 } 46 47 // add them to db 48 for _, profile := range profiles { 49 db.CreateProfile(profile) 50 } 51 52 // assert they can be found in the database 53 for _, profile := range profiles { 54 wantProfile := models.ProfileModel{ 55 Id: profile.Id, 56 Username: profile.Username, 57 About: profile.About, 58 AvatarPath: profile.AvatarPath, 59 } 60 gotProfile, err := db.GetProfile(profile.Id) 61 AssertNoError(t, err) 62 Assert(t, gotProfile, wantProfile, "profile stored in db") 63 } 64 65 // assert querying for unexisting profile returns ErrNotFound 66 _, err = db.GetProfile("9999") 67 AssertError(t, err, core_err.ErrNotFound) 68 }) 69 t.Run("updating profile", func(t *testing.T) { 70 newProfile1 := RandomProfileModel() 71 newProfile2 := RandomProfileModel() 72 73 db, err := sql_db.NewSqlDB(OpenSqliteDB(t)) 74 AssertNoError(t, err) 75 76 // insert both profiles into database 77 db.CreateProfile(newProfile1) 78 db.CreateProfile(newProfile2) 79 80 // update first profile 81 newAvatar := RandomString() 82 newAbout := RandomString() 83 err = db.UpdateProfile(newProfile1.Id, store.DBUpdateData{AvatarPath: newAvatar, About: newAbout}) 84 AssertNoError(t, err) 85 86 // update second profile with empty values (it shouldn't be updated) 87 err = db.UpdateProfile(newProfile2.Id, store.DBUpdateData{}) 88 AssertNoError(t, err) 89 90 // get the updated profile, it should be changed 91 wantUpdatedProfile1 := models.ProfileModel{ 92 Id: newProfile1.Id, 93 Username: newProfile1.Username, 94 About: newAbout, 95 AvatarPath: newAvatar, 96 } 97 updatedProfile1, err := db.GetProfile(newProfile1.Id) 98 AssertNoError(t, err) 99 Assert(t, updatedProfile1, wantUpdatedProfile1, "the updated profile") 100 101 // get the unaffected profile, it shouldn't be changed 102 gotProfile2, err := db.GetProfile(newProfile2.Id) 103 AssertNoError(t, err) 104 Assert(t, gotProfile2, newProfile2, "the unaffected profile") 105 }) 106 }