Last active
March 14, 2017 12:31
-
-
Save eamonnmcevoy/ce5d75527bc24632432847ccede6bb57 to your computer and use it in GitHub Desktop.
GetByUsername(username string)
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 mongo_test | |
import ( | |
"log" | |
"testing" | |
"go_rest_api/pkg" | |
"go_rest_api/pkg/mongo" | |
) | |
const ( | |
mongoUrl = "localhost:27017" | |
dbName = "test_db" | |
userCollectionName = "user" | |
) | |
func Test_UserService(t *testing.T) { | |
t.Run("CreateUser", createUser_should_insert_user_into_mongo) | |
} | |
func createUser_should_insert_user_into_mongo(t *testing.T) { | |
//Arrange | |
session, err := mongo.NewSession(mongoUrl) | |
if(err != nil) { | |
log.Fatalf("Unable to connect to mongo: %s", err) | |
} | |
defer session.Close() | |
userService := mongo.NewUserService(session.Copy(), dbName, userCollectionName) | |
testUsername := "integration_test_user" | |
testPassword := "integration_test_password" | |
user := root.User{ | |
Username: testUsername, | |
Password: testPassword } | |
//Act | |
err = userService.Create(&user) | |
//Assert | |
if(err != nil) { | |
t.Error("Unable to create user: %s", err) | |
} | |
var results []root.User | |
session.GetCollection(dbName,userCollectionName).Find(nil).All(&results) | |
count := len(results) | |
if(count != 1) { | |
t.Error("Incorrect number of results. Expected `1`, got: `%i`", count) | |
} | |
if(results[0].Username != user.Username) { | |
t.Error("Incorrect Username. Expected `%s`, Got: `%s`", testUsername, results[0].Username) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment