Skip to content

Commit

Permalink
Test with MYSQL in travis
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed Jul 1, 2018
1 parent 40adade commit 5c63dc2
Show file tree
Hide file tree
Showing 30 changed files with 810 additions and 662 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ language: go

go_import_path: project-root

services:
- mysql

go:
- "1.8"
- "1.9"
- "1.10"
- "tip"

before_install:
- mysql -e 'CREATE DATABASE webapitest;'
- export GOPATH=$HOME/gopath/src/project-root
- export PATH=$HOME/gopath/src/project-root/bin:$PATH
- cd $GOPATH/src/app/webapi
Expand Down
41 changes: 41 additions & 0 deletions migration/tables-only.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
SET NAMES utf8 COLLATE 'utf8_unicode_ci';
SET foreign_key_checks = 1;
SET time_zone = '+00:00';
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
SET CHARACTER SET utf8;

CREATE TABLE IF NOT EXISTS user_status (
id TINYINT(1) UNSIGNED NOT NULL AUTO_INCREMENT,

status VARCHAR(25) NOT NULL,

created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
deleted TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,

PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS user (
id VARCHAR(36) NOT NULL,

first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
password CHAR(60) NOT NULL,

status_id TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,

created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
deleted_at TIMESTAMP DEFAULT 0,

UNIQUE KEY (email),
CONSTRAINT `f_user_status` FOREIGN KEY (`status_id`) REFERENCES `user_status` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,

PRIMARY KEY (id)
);

INSERT INTO `user_status` (`id`, `status`, `created_at`, `updated_at`, `deleted`) VALUES
(1, 'active', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0),
(2, 'inactive', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0);
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 26 additions & 2 deletions src/app/webapi/component/core_mock.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
package component

import (
"log"

"app/webapi/internal/bind"
"app/webapi/internal/response"
"app/webapi/internal/testutil"
"app/webapi/pkg/database"
"app/webapi/pkg/query"
)

// TestDatabase returns a test database.
func TestDatabase(dbSpecificDB bool) *database.DBW {
dbc := new(database.Connection)
dbc.Hostname = "127.0.0.1"
dbc.Port = 3306
dbc.Username = "root"
dbc.Password = ""
dbc.Database = "webapitest"
dbc.Parameter = "parseTime=true&allowNativePasswords=true"

connection, err := dbc.Connect(dbSpecificDB)
if err != nil {
log.Println("DB Error:", err)
}

dbw := database.New(connection)

return dbw
}

// NewCoreMock returns all mocked dependencies.
func NewCoreMock() (Core, *CoreMock) {
ml := new(testutil.MockLogger)
md := new(testutil.MockDatabase)
//md := new(testutil.MockDatabase)
md := TestDatabase(true)
mq := query.New(md)
mt := new(testutil.MockToken)
resp := response.New()
Expand All @@ -31,7 +55,7 @@ func NewCoreMock() (Core, *CoreMock) {
// CoreMock contains all the mocked dependencies.
type CoreMock struct {
Log *testutil.MockLogger
DB *testutil.MockDatabase
DB IDatabase
Q IQuery
Bind IBind
Reponse IResponse
Expand Down
84 changes: 0 additions & 84 deletions src/app/webapi/component/fake/fake.go

This file was deleted.

47 changes: 0 additions & 47 deletions src/app/webapi/component/fake/fake_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions src/app/webapi/component/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ type IDatabase interface {
Error(err error) error
AffectedRows(result sql.Result) int

PaginatedResults(results interface{}, fn func() (results interface{}, total int, err error)) (total int, err error)
/*PaginatedResults(results interface{}, fn func() (results interface{}, total int, err error)) (total int, err error)
RecordExistsInt(fn func() (exists bool, ID int64, err error)) (exists bool, ID int64, err error)
RecordExistsString(fn func() (exists bool, ID string, err error)) (exists bool, ID string, err error)
AddRecordInt(fn func() (ID int64, err error)) (ID int64, err error)
AddRecordString(fn func() (ID string, err error)) (ID string, err error)
AddRecordString(fn func() (ID string, err error)) (ID string, err error)*/
}

// IQuery provides default queries.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
66 changes: 66 additions & 0 deletions src/app/webapi/component/user/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package user

import (
"errors"
"net/http"

"app/webapi/store"
)

// Create .
// swagger:route POST /v1/user user UserCreate
//
// Create a user.
//
// Security:
// token:
//
// Responses:
// 201: CreatedResponse
// 400: BadRequestResponse
// 401: UnauthorizedResponse
// 500: InternalServerErrorResponse
func (p *Endpoint) Create(w http.ResponseWriter, r *http.Request) (int, error) {
// swagger:parameters UserCreate
type request struct {
// in: formData
// Required: true
FirstName string `json:"first_name" validate:"required"`
// in: formData
// Required: true
LastName string `json:"last_name" validate:"required"`
// in: formData
// Required: true
Email string `json:"email" validate:"required,email"`
// in: formData
// Required: true
Password string `json:"password" validate:"required"`
}

// Request validation.
req := new(request)
if err := p.Bind.FormUnmarshal(req, r); err != nil {
return http.StatusBadRequest, err
} else if err = p.Bind.Validate(req); err != nil {
return http.StatusBadRequest, err
}

// Create the store.
u := store.NewUser(p.DB, p.Q)

// Check for existing user.
exists, _, err := u.ExistsByField(u, "email", req.Email)
if err != nil {
return http.StatusInternalServerError, err
} else if exists {
return http.StatusBadRequest, errors.New("user already exists")
}

// Create the user in the database.
ID, err := u.Create(req.FirstName, req.LastName, req.Email, req.Password)
if err != nil {
return http.StatusInternalServerError, err
}

return p.Response.Created(w, ID)
}
Loading

0 comments on commit 5c63dc2

Please sign in to comment.