Skip to content

Commit

Permalink
Increase tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
arikkfir committed Apr 29, 2020
1 parent 0a5d364 commit d46c8a4
Show file tree
Hide file tree
Showing 21 changed files with 415 additions and 542 deletions.
14 changes: 10 additions & 4 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"syscall"

"github.com/golangly/log"
"github.com/neo4j/neo4j-go-driver/neo4j"

"github.com/bluebudgetz/gate/internal/config"
)
Expand All @@ -21,8 +22,13 @@ var signals = []os.Signal{
}

type App struct {
cfg config.Config
servers []*http.Server
cfg config.Config
neo4jDriver neo4j.Driver
servers []*http.Server
}

func (app *App) Neo4jDriver() neo4j.Driver {
return app.neo4jDriver
}

// Run the application, until an error (or nil) is pushed to the given quit channel.
Expand Down Expand Up @@ -72,6 +78,6 @@ func (app *App) BuildURL(host string, path string, pathArgs ...interface{}) stri
return fmt.Sprintf("http://%s:%d%s", host, app.GetConfig().HTTP.Port, fmt.Sprintf(path, pathArgs...))
}

func NewApp(cfg config.Config, servers []*http.Server) (*App, error) {
return &App{cfg, servers}, nil
func NewApp(cfg config.Config, neo4jDriver neo4j.Driver, servers []*http.Server) (*App, error) {
return &App{cfg, neo4jDriver, servers}, nil
}
2 changes: 1 addition & 1 deletion internal/boot/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func InitializeApp(

servers := server.NewHTTPServers(configConfig, router)

application, err := app.NewApp(configConfig, servers)
application, err := app.NewApp(configConfig, neo4jDriver, servers)
if err != nil {
cleanup()
return nil, nil, err
Expand Down
4 changes: 2 additions & 2 deletions internal/server/handlers/assets_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions internal/server/handlers/delete_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ package handlers
import (
"net/http"
"testing"
"time"

"github.com/go-chi/chi"
"github.com/neo4j/neo4j-go-driver/neo4j"
"github.com/stretchr/testify/require"

"github.com/bluebudgetz/gate/internal/util"
"github.com/bluebudgetz/gate/internal/util/testfw"
)

func TestDeleteAccount(t *testing.T) {
app, cleanup := util.RunTestApp(t, func(neo4jDriver neo4j.Driver) func(chi.Router) { return NewRoutes(neo4jDriver) })
app, cleanup := testfw.Run(t, func(neo4jDriver neo4j.Driver) func(chi.Router) { return NewRoutes(neo4jDriver) })
defer cleanup()
url := app.BuildURL("localhost", "/accounts/%s", "company")

firstGetResp := util.Request(t, url, "GET", nil, func(*http.Request) {})
require.Equal(t, http.StatusOK, firstGetResp.StatusCode)
acc := util.ResponseBodyObject(t, firstGetResp, &GetAccountResponse{}).(*GetAccountResponse)
require.Equal(t, "company", acc.Account.ID)
a1 := testfw.NewAccount(app.Neo4jDriver(), "a1", "A1", time.Now(), nil)
a1GetResp := testfw.Request(t, app.BuildURL("localhost", "/accounts/%s", a1.ID), "GET", nil, func(*http.Request) {})
require.Equal(t, http.StatusOK, a1GetResp.StatusCode)
data := testfw.ReadResponseBody(t, a1GetResp, &GetAccountResponse{}).(*GetAccountResponse)
require.Equal(t, a1.ID, data.Account.ID)

deleteResp := util.Request(t, url, "DELETE", nil, func(*http.Request) {})
require.Equal(t, http.StatusNoContent, deleteResp.StatusCode)
a1DeleteResp := testfw.Request(t, app.BuildURL("localhost", "/accounts/%s", a1.ID), "DELETE", nil, func(*http.Request) {})
require.Equal(t, http.StatusNoContent, a1DeleteResp.StatusCode)

secondGetResp := util.Request(t, url, "GET", nil, func(*http.Request) {})
require.Equal(t, http.StatusNotFound, secondGetResp.StatusCode)
require.Equal(t, int64(0), secondGetResp.ContentLength)
a1GetResp2 := testfw.Request(t, app.BuildURL("localhost", "/accounts/%s", a1.ID), "GET", nil, func(*http.Request) {})
require.Equal(t, http.StatusNotFound, a1GetResp2.StatusCode)
}
18 changes: 10 additions & 8 deletions internal/server/handlers/delete_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@ package handlers
import (
"net/http"
"testing"
"time"

"github.com/go-chi/chi"
"github.com/neo4j/neo4j-go-driver/neo4j"
"github.com/stretchr/testify/assert"

"github.com/bluebudgetz/gate/internal/util"
"github.com/bluebudgetz/gate/internal/util/testfw"
)

func TestDeleteTx(t *testing.T) {
app, cleanup := util.RunTestApp(t, func(neo4jDriver neo4j.Driver) func(chi.Router) { return NewRoutes(neo4jDriver) })
app, cleanup := testfw.Run(t, func(neo4jDriver neo4j.Driver) func(chi.Router) { return NewRoutes(neo4jDriver) })
defer cleanup()
url := app.BuildURL("localhost", "/transactions/%s", "salary-2020-01-01")

firstGetResp := util.Request(t, url, "GET", nil, func(*http.Request) {})
a1 := testfw.NewAccount(app.Neo4jDriver(), "a1", "A1", time.Now(), nil)
a2 := testfw.NewAccount(app.Neo4jDriver(), "a2", "A2", time.Now(), nil)
t1 := a1.CreateTransaction(app.Neo4jDriver(), a2, "a1-to-a2", time.Now(), time.Now(), 19, "Testing transaction")

firstGetResp := testfw.Request(t, app.BuildURL("localhost", "/transactions/%s", t1.ID), "GET", nil, func(*http.Request) {})
assert.Equal(t, http.StatusOK, firstGetResp.StatusCode)
acc := util.ResponseBodyObject(t, firstGetResp, &GetTransactionResponse{}).(*GetTransactionResponse)
assert.Equal(t, "salary-2020-01-01", acc.Tx.ID)

deleteResp := util.Request(t, url, "DELETE", nil, func(*http.Request) {})
deleteResp := testfw.Request(t, app.BuildURL("localhost", "/transactions/%s", t1.ID), "DELETE", nil, func(*http.Request) {})
assert.Equal(t, http.StatusNoContent, deleteResp.StatusCode)

secondGetResp := util.Request(t, url, "GET", nil, func(*http.Request) {})
secondGetResp := testfw.Request(t, app.BuildURL("localhost", "/transactions/%s", t1.ID), "GET", nil, func(*http.Request) {})
assert.Equal(t, http.StatusNotFound, secondGetResp.StatusCode)
assert.Equal(t, int64(0), secondGetResp.ContentLength)
}
19 changes: 10 additions & 9 deletions internal/server/handlers/get_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ package handlers
import (
"net/http"
"testing"
"time"

"github.com/go-chi/chi"
"github.com/neo4j/neo4j-go-driver/neo4j"
"github.com/stretchr/testify/assert"

"github.com/bluebudgetz/gate/internal/util"
"github.com/bluebudgetz/gate/internal/util/testfw"
)

func TestGetAccount(t *testing.T) {
app, cleanup := util.RunTestApp(t, func(neo4jDriver neo4j.Driver) func(chi.Router) { return NewRoutes(neo4jDriver) })
app, cleanup := testfw.Run(t, func(neo4jDriver neo4j.Driver) func(chi.Router) { return NewRoutes(neo4jDriver) })
defer cleanup()
url := app.BuildURL("localhost", "/accounts/%s", "company")

resp := util.Request(t, url, "GET", nil, func(*http.Request) {})
a1 := testfw.NewAccount(app.Neo4jDriver(), "a1", "A1", time.Now(), nil)
resp := testfw.Request(t, app.BuildURL("localhost", "/accounts/%s", a1.ID), "GET", nil, func(*http.Request) {})
assert.Equal(t, http.StatusOK, resp.StatusCode)
acc := util.ResponseBodyObject(t, resp, &GetAccountResponse{}).(*GetAccountResponse)
assert.Equal(t, "company", acc.Account.ID)
assert.Equal(t, "Big Company", acc.Account.Name)
assert.Equal(t, util.MustParseTimeRFC3339("2007-06-05T04:03:02Z"), acc.Account.CreatedOn)
assert.Nil(t, acc.Account.UpdatedOn)
data := testfw.ReadResponseBody(t, resp, &GetAccountResponse{}).(*GetAccountResponse)
assert.Equal(t, a1.ID, data.Account.ID)
assert.Equal(t, a1.Name, data.Account.Name)
assert.Equal(t, a1.CreatedOn.UTC(), data.Account.CreatedOn.UTC())
assert.Nil(t, data.Account.UpdatedOn)
}
2 changes: 1 addition & 1 deletion internal/server/handlers/get_accounts_tree.cyp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ OPTIONAL MATCH p = (a)<-[:childOf*0..]-(:Account)<-[r:Paid]-(:Account)
WITH a, children, sum(r.amount) AS incoming, outgoing

// Pull everything together
RETURN a as account, children, incoming, outgoing, incoming - outgoing AS balance
RETURN a as account, children, toFloat(incoming) AS incoming, toFloat(outgoing) AS outgoing, toFloat(incoming - outgoing) AS balance
ORDER BY a.name
// TODO: support skip/limit
30 changes: 27 additions & 3 deletions internal/server/handlers/get_accounts_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"net/http"
"sort"
"time"

"github.com/golangly/errors"
Expand Down Expand Up @@ -43,16 +44,28 @@ func buildTransactionTree(result neo4j.Result) ([]*GetAccountTreeData, error) {
}

// Create account object, and register it in in id->account map
incoming, ok := rec.Get("incoming")
if !ok {
return nil, errors.New("incoming mismatch")
}
outgoing, ok := rec.Get("outgoing")
if !ok {
return nil, errors.New("outgoing mismatch")
}
balance, ok := rec.Get("balance")
if !ok {
return nil, errors.New("balance mismatch")
}
account := GetAccountTreeData{
GetAccountData: GetAccountData{
ID: node.Props()["id"].(string),
CreatedOn: node.Props()["createdOn"].(time.Time),
UpdatedOn: util.OptionalDateTime(node.Props()["updatedOn"]),
Name: node.Props()["name"].(string),
},
IncomingTx: node.Props()["incoming"].(float64),
OutgoingTx: node.Props()["outgoing"].(float64),
Balance: node.Props()["balance"].(float64),
IncomingTx: incoming.(float64),
OutgoingTx: outgoing.(float64),
Balance: balance.(float64),
}
nodes[account.ID] = &account

Expand Down Expand Up @@ -86,12 +99,23 @@ func buildTransactionTree(result neo4j.Result) ([]*GetAccountTreeData, error) {
}
nodes[accountID].Children = children
}
for _, account := range nodes {
sort.Slice(account.Children, func(i, j int) bool {
return sort.StringsAreSorted([]string{account.Children[i].Name, account.Children[j].Name})
})
}

// Map root accounts map to a list of root accounts
var accounts = make([]*GetAccountTreeData, 0)
for _, account := range roots {
accounts = append(accounts, account)
}
sort.Slice(accounts, func(i, j int) bool {
si := accounts[i].Name
sj := accounts[j].Name
sorted := sort.StringsAreSorted([]string{si, sj})
return sorted
})
return accounts, nil
}

Expand Down
50 changes: 50 additions & 0 deletions internal/server/handlers/get_accounts_tree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package handlers

import (
"fmt"
"net/http"
"testing"
"time"

"github.com/go-chi/chi"
"github.com/neo4j/neo4j-go-driver/neo4j"
"github.com/stretchr/testify/assert"

"github.com/bluebudgetz/gate/internal/util/testfw"
)

func TestGetAccountsTree(t *testing.T) {
app, cleanup := testfw.Run(t, func(neo4jDriver neo4j.Driver) func(chi.Router) { return NewRoutes(neo4jDriver) })
defer cleanup()

assertAccount := func(expected testfw.Account, actual *GetAccountTreeData) {
assert.Equal(t, expected.ID, actual.ID)
assert.Equal(t, expected.Name, actual.Name)
assert.Equal(t, expected.CreatedOn.UTC(), actual.CreatedOn.UTC())
assert.Nil(t, actual.UpdatedOn)
assert.Equal(t, float64(0), actual.Balance)
assert.Equal(t, float64(0), actual.IncomingTx)
assert.Equal(t, float64(0), actual.OutgoingTx)
}

a01 := testfw.NewAccount(app.Neo4jDriver(), "a01", "A01", time.Now(), nil)
a11 := testfw.NewAccount(app.Neo4jDriver(), "a11", "A11", time.Now(), a01)
a12 := testfw.NewAccount(app.Neo4jDriver(), "a12", "A12", time.Now(), a01)
a02 := testfw.NewAccount(app.Neo4jDriver(), "a02", "A02", time.Now(), nil)
a21 := testfw.NewAccount(app.Neo4jDriver(), "a23", "A21", time.Now(), a02)
fmt.Printf("%+v\n", a01)
fmt.Printf("%+v\n", a11)
fmt.Printf("%+v\n", a12)
fmt.Printf("%+v\n", a02)
fmt.Printf("%+v\n", a21)

resp := testfw.Request(t, app.BuildURL("localhost", "/accounts"), "GET", nil, func(r *http.Request) {})
assert.Equal(t, http.StatusOK, resp.StatusCode)
data := testfw.ReadResponseBody(t, resp, &GetAccountTreeResponse{}).(*GetAccountTreeResponse)
assert.Equal(t, 2, len(data.Accounts))
assertAccount(*a01, data.Accounts[0])
assertAccount(*a11, data.Accounts[0].Children[0])
assertAccount(*a12, data.Accounts[0].Children[1])
assertAccount(*a02, data.Accounts[1])
assertAccount(*a21, data.Accounts[1].Children[0])
}
22 changes: 11 additions & 11 deletions internal/server/handlers/get_tx_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ type (

func GetTransactionList() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
req := GetTransactionListRequest{}
req := GetTransactionListRequest{Paging: util.Paging{Page: 0, PageSize: 10}}
if err := webutil.Bind(r, &req); err != nil {
webutil.RenderWithStatusCode(w, r, http.StatusInternalServerError, err)
return
}

// Query
result, err := services.GetNeo4jSession(r.Context()).Run(getTxListQuery, map[string]interface{}{
"skip": (req.Paging.Page - 1) * req.Paging.PageSize,
"skip": req.Paging.Page * req.Paging.PageSize,
"limit": req.Paging.PageSize,
})
if err != nil {
Expand All @@ -60,23 +60,23 @@ func GetTransactionList() http.HandlerFunc {
} else if src, ok := rec.Get("src"); !ok {
webutil.RenderWithStatusCode(w, r, http.StatusInternalServerError, errors.New("failed getting src node"))
return
} else if dst, ok := rec.Get("src"); !ok {
} else if dst, ok := rec.Get("dst"); !ok {
webutil.RenderWithStatusCode(w, r, http.StatusInternalServerError, errors.New("failed getting dst node"))
return
} else {
txNode := tx.(neo4j.Node).Props()
txRel := tx.(neo4j.Relationship).Props()
srcNode := src.(neo4j.Node).Props()
dstNode := dst.(neo4j.Node).Props()
transactions = append(transactions, GetTransactionListItemData{
ID: txNode["id"].(string),
CreatedOn: txNode["createdOn"].(time.Time),
UpdatedOn: txNode["updatedOn"].(*time.Time),
IssuedOn: txNode["issuedOn"].(time.Time),
Origin: txNode["origin"].(string),
ID: txRel["id"].(string),
CreatedOn: txRel["createdOn"].(time.Time),
UpdatedOn: util.OptionalDateTime(txRel["updatedOn"]),
IssuedOn: txRel["issuedOn"].(time.Time),
Origin: txRel["origin"].(string),
SourceAccountID: srcNode["id"].(string),
TargetAccountID: dstNode["id"].(string),
Amount: txNode["amount"].(float64),
Comment: txNode["comment"].(string),
Amount: txRel["amount"].(float64),
Comment: txRel["comment"].(string),
})
}
}
Expand Down
Loading

0 comments on commit d46c8a4

Please sign in to comment.