Skip to content

Instantly share code, notes, and snippets.

@Marvin9
Last active August 21, 2020 10:15
Show Gist options
  • Save Marvin9/3b17febea5c245b1f712df274327b8a5 to your computer and use it in GitHub Desktop.
Save Marvin9/3b17febea5c245b1f712df274327b8a5 to your computer and use it in GitHub Desktop.
package database_test
import (
"go-mock-postgre/database"
"go-mock-postgre/database/mock"
"testing"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
func TestAddUsers(t *testing.T) {
mock.MockedDB(mock.CREATE)
defer mock.MockedDB(mock.DROP)
newUser := "fooo"
database.AddUser(newUser)
/*
As we know ConnectDB() will connect database named DATABASE_NAME defined in .env,
make sure to change that before running tests OR we have better approach in next section.
*/
db, err := database.ConnectDB()
if err != nil {
t.Errorf("Error connecting database in %v\n%v", t.Name(), err)
}
defer db.Close()
var newUserInDB database.Users
notFound := db.Where("username = ?", newUser).First(&newUserInDB).RecordNotFound()
if notFound {
t.Errorf("%v was added but was not retrieved.", newUser)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment