Last active
August 21, 2020 10:15
-
-
Save Marvin9/3b17febea5c245b1f712df274327b8a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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