Skip to content

Commit

Permalink
Add test commands, fix failing tests, standardize comments
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed Jul 11, 2018
1 parent a359e41 commit ca57db7
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 35 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ You must use Go 1.7 or newer because it uses the http context.

Use one of the following commands to start a MySQL container with Docker:

- Start MySQL without a password: `docker run -d -p 3306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql:5.7`
- Start MySQL with a password: `docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=somepassword mysql:5.7`
- Start MySQL without a password: `docker run -d --name=mysql57 -p 3306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql:5.7`
- Start MySQL with a password: `docker run -d --name=mysql57 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=somepassword mysql:5.7`

Start MySQL and import `migration/mysql.sql` to create the database and tables.

Expand Down Expand Up @@ -312,7 +312,20 @@ func (p *Endpoint) Index(w http.ResponseWriter, r *http.Request) (int, error) {

You can disable logging on the server by setting an environment variable: `WEBAPI_LOG_LEVEL=none`

## Test Coverage
## Testing

All the tests use a database called: `webapitest`. The quickest way to get it set up is:

```bash
# Launch MySQL in docker container.
docker run -d --name=mysql57 -p 3306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql:5.7

# Create the database via docker exec.
docker exec mysql57 sh -c 'exec mysql -uroot -e "CREATE DATABASE IF NOT EXISTS webapitest DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;"'

# Or create the database manually.
CREATE DATABASE webapitest DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
```

You can use these commands to run tests:

Expand Down
2 changes: 1 addition & 1 deletion src/app/webapi/component/user/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (p *Endpoint) Create(w http.ResponseWriter, r *http.Request) (int, error) {
// Create the DB store.
u := store.NewUser(p.DB, p.Q)

// Check for existing user.
// Check for existing item.
exists, _, err := u.ExistsByField(u, "email", req.Email)
if err != nil {
return http.StatusInternalServerError, err
Expand Down
2 changes: 1 addition & 1 deletion src/app/webapi/component/user/index.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package user

import (
"app/webapi/pkg/structcopy"
"net/http"

"app/webapi/model"
"app/webapi/pkg/structcopy"
"app/webapi/store"
)

Expand Down
2 changes: 1 addition & 1 deletion src/app/webapi/component/user/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (p *Endpoint) Show(w http.ResponseWriter, r *http.Request) (int, error) {
// Create the DB store.
u := store.NewUser(p.DB, p.Q)

// Get a user.
// Get an item by ID.
exists, err := u.FindOneByID(u, req.UserID)
if err != nil {
return http.StatusInternalServerError, err
Expand Down
2 changes: 1 addition & 1 deletion src/app/webapi/component/user/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (p *Endpoint) Update(w http.ResponseWriter, r *http.Request) (int, error) {
// Create the DB store.
u := store.NewUser(p.DB, p.Q)

// Determine if the user exists.
// Determine if the item exists.
exists, err := u.ExistsByID(u, req.UserID)
if err != nil {
return http.StatusInternalServerError, err
Expand Down
17 changes: 10 additions & 7 deletions src/app/webapi/internal/testutil/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ func ConnectDatabase(dbSpecificDB bool) *database.DBW {
return dbw
}

// LoadDatabase will set up the DB for the tests.
func LoadDatabase(t *testing.T) {
// ResetDatabase will drop and create the test database.
func ResetDatabase() {
db := ConnectDatabase(false)
db.Exec(`DROP DATABASE IF EXISTS webapitest`)
db.Exec(`CREATE DATABASE webapitest DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci`)
}

db = ConnectDatabase(true)
// LoadDatabase will set up the DB for the tests.
func LoadDatabase(t *testing.T) {
ResetDatabase()

db := ConnectDatabase(true)
b, err := ioutil.ReadFile("../../../../../migration/tables-only.sql")
if err != nil {
t.Error(err)
Expand All @@ -57,11 +62,9 @@ func LoadDatabase(t *testing.T) {

// LoadDatabaseFromFile will set up the DB for the tests.
func LoadDatabaseFromFile(file string) {
db := ConnectDatabase(false)
db.Exec(`DROP DATABASE IF EXISTS webapitest`)
db.Exec(`CREATE DATABASE webapitest DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci`)
ResetDatabase()

db = ConnectDatabase(true)
db := ConnectDatabase(true)
b, err := ioutil.ReadFile(file)
if err != nil {
log.Println(err)
Expand Down
4 changes: 4 additions & 0 deletions src/app/webapi/pkg/basemigrate/basemigrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ package basemigrate_test
import (
"testing"

"app/webapi/internal/testutil"
"app/webapi/pkg/basemigrate"

"github.com/stretchr/testify/assert"
)

func TestMigration(t *testing.T) {
testutil.ResetDatabase()
err := basemigrate.Migrate("testdata/success.sql", false)
assert.Nil(t, err)
}

func TestMigrationFailDuplicate(t *testing.T) {
testutil.ResetDatabase()
err := basemigrate.Migrate("testdata/fail-duplicate.sql", false)
assert.Contains(t, err.Error(), "checksum does not match")
}

func TestParse(t *testing.T) {
testutil.ResetDatabase()
arr, err := basemigrate.ParseFile("testdata/success.sql")
assert.Nil(t, err)
assert.Equal(t, 3, len(arr))
Expand Down
17 changes: 8 additions & 9 deletions src/app/webapi/pkg/structcopy/structcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ func ByTag(src interface{}, srcTag string, dst interface{}, dstTag string) (err
// Loop through each field.
keysSrc := vs.Type()
keysDst := vd.Type()
for jS := 0; jS < vs.NumField(); jS++ {
fieldS := vs.Field(jS)
tagS := keysSrc.Field(jS).Tag

for jD := 0; jD < vd.NumField(); jD++ {
fieldD := vd.Field(jD)
tagD := keysDst.Field(jD).Tag

// Set the "status" field.
for jD := 0; jD < vd.NumField(); jD++ {
fieldD := vd.Field(jD)
tagD := keysDst.Field(jD).Tag
for jS := 0; jS < vs.NumField(); jS++ {
fieldS := vs.Field(jS)
tagS := keysSrc.Field(jS).Tag

// If the tags match, copy the value from src to dst field.
if tagS.Get(srcTag) == tagD.Get(dstTag) {
if fieldS.Type() != fieldD.Type() {
return fmt.Errorf("field types do not match - src type '%v' for tag '%v' do not match dst type '%v' for tag '%v'",
Expand Down
13 changes: 1 addition & 12 deletions src/app/webapi/store/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,13 @@ func (x UserGroup) PrimaryKey() string {
return "id"
}

// *****************************************************************************
// Create
// *****************************************************************************

// Create adds a new user.
func (x *User) Create(firstName, lastName, email, password string) (string, error) {
// Generate a UUID.
uuid, err := securegen.UUID()
if err != nil {
return "", err
}

// Create the user.
_, err = x.db.Exec(`
INSERT INTO user
(id, first_name, last_name, email, password, status_id)
Expand All @@ -84,13 +78,8 @@ func (x *User) Create(firstName, lastName, email, password string) (string, erro
return uuid, err
}

// *****************************************************************************
// Update
// *****************************************************************************

// Update makes changes to one entity.
// Update makes changes to a user.
func (x *User) Update(ID, firstName, lastName, email, password string) (err error) {
// Update the entity.
_, err = x.db.Exec(`
UPDATE user
SET
Expand Down

0 comments on commit ca57db7

Please sign in to comment.