-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from darcys22/dev
Dev
- Loading branch information
Showing
28 changed files
with
489 additions
and
240 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package core | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func init() { | ||
} | ||
|
||
func TestTransaction(t *testing.T) { | ||
user, err := NewUser("Tester") | ||
assert.NoError(t, err) | ||
|
||
txn, err := NewTransaction(user) | ||
assert.NoError(t, err) | ||
|
||
cash, err := NewAccount("1", "cash") | ||
assert.NoError(t, err) | ||
income, err := NewAccount("2", "income") | ||
assert.NoError(t, err) | ||
aud, err := NewCurrency("AUD", 2) | ||
assert.NoError(t, err) | ||
|
||
amountDR := big.NewInt(10) | ||
|
||
spl1, err := NewSplit(time.Now(), []byte("Cash Income"), []*Account{cash}, aud, amountDR) | ||
assert.NoError(t, err) | ||
|
||
err = txn.AppendSplit(spl1) | ||
assert.NoError(t, err) | ||
|
||
amountCR := big.NewInt(-10) | ||
spl2, err := NewSplit(time.Now(), []byte("Cash Income"), []*Account{income}, aud, amountCR) | ||
assert.NoError(t, err) | ||
|
||
err = txn.AppendSplit(spl2) | ||
assert.NoError(t, err) | ||
|
||
total, txnBalances := txn.Balance() | ||
assert.True(t, txnBalances) | ||
assert.Equal(t, total.Cmp(big.NewInt(0)), 0) | ||
|
||
assert.Equal(t, txn.Splits[0].Amount, amountDR) | ||
assert.Equal(t, txn.Splits[0].Accounts[0].Name, "cash") | ||
assert.Equal(t, txn.Splits[1].Amount, amountCR) | ||
assert.Equal(t, txn.Splits[1].Accounts[0].Name, "income") | ||
|
||
//func ReverseTransaction(originalTxn *Transaction, usr *User) (*Transaction, error) { | ||
reversedTxn, err := ReverseTransaction(txn, user) | ||
assert.NoError(t, err) | ||
|
||
total, txnBalances = reversedTxn.Balance() | ||
assert.True(t, txnBalances) | ||
assert.Equal(t, total.Cmp(big.NewInt(0)), 0) | ||
|
||
assert.Equal(t, reversedTxn.Splits[0].Amount, amountCR) | ||
assert.Equal(t, reversedTxn.Splits[0].Accounts[0].Name, "cash") | ||
assert.Equal(t, reversedTxn.Splits[1].Amount, amountDR) | ||
assert.Equal(t, reversedTxn.Splits[1].Accounts[0].Name, "income") | ||
|
||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package node | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"math/big" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/darcys22/godbledger/godbledger/cmd" | ||
"github.com/darcys22/godbledger/godbledger/ledger" | ||
"github.com/darcys22/godbledger/shared" | ||
|
||
"github.com/sirupsen/logrus" | ||
logTest "github.com/sirupsen/logrus/hooks/test" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
const ( | ||
maxPollingWaitTime = 1 * time.Second | ||
) | ||
|
||
func init() { | ||
logrus.SetLevel(logrus.DebugLevel) | ||
logrus.SetOutput(ioutil.Discard) | ||
} | ||
|
||
// Test that godbledger node can close. | ||
func TestNodeClose_OK(t *testing.T) { | ||
hook := logTest.NewGlobal() | ||
|
||
app := cli.App{} | ||
set := flag.NewFlagSet("test", 0) | ||
set.String("config", "", "doc") | ||
|
||
ctx := cli.NewContext(&app, set, nil) | ||
|
||
node, err := New(ctx) | ||
assert.NoError(t, err) | ||
|
||
node.Close() | ||
|
||
shared.LogsContain(t.Fatalf, hook, "Stopping ledger node", true) | ||
} | ||
|
||
// TestClearDB tests clearing the database | ||
func TestClearDB(t *testing.T) { | ||
hook := logTest.NewGlobal() | ||
|
||
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000)) | ||
assert.NoError(t, err, "Could not generate random number for file path") | ||
tmp := filepath.Join(os.TempDir(), fmt.Sprintf("datadirtest%d", randPath)) | ||
assert.NoError(t, os.RemoveAll(tmp)) | ||
|
||
app := cli.App{} | ||
set := flag.NewFlagSet("test", 0) | ||
set.Bool(cmd.ClearDB.Name, true, "") | ||
|
||
ctx := cli.NewContext(&app, set, nil) | ||
assert.NoError(t, err) | ||
err, cfg := cmd.MakeConfig(ctx) | ||
assert.NoError(t, err) | ||
cfg.DatabaseType = "memorydb" | ||
cfg.DataDirectory = tmp | ||
|
||
node, err := New(ctx) | ||
assert.NoError(t, err) | ||
|
||
ledger, err := ledger.New(ctx, cfg) | ||
assert.NoError(t, err) | ||
|
||
node.Register(ledger) | ||
go node.Start() | ||
d := time.Now().Add(maxPollingWaitTime) | ||
contextWithDeadline, cancel := context.WithDeadline(context.Background(), d) | ||
defer cancel() | ||
<-contextWithDeadline.Done() | ||
shared.LogsContain(t.Fatalf, hook, "Clearing SQLite3 DB", true) | ||
//case <-contextWithDeadline.Done(): | ||
|
||
node.Close() | ||
assert.NoError(t, os.RemoveAll(tmp)) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/darcys22/godbledger/godbledger/cmd" | ||
"github.com/darcys22/godbledger/godbledger/ledger" | ||
"github.com/darcys22/godbledger/shared" | ||
|
||
"github.com/sirupsen/logrus" | ||
logTest "github.com/sirupsen/logrus/hooks/test" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func init() { | ||
logrus.SetLevel(logrus.DebugLevel) | ||
logrus.SetOutput(ioutil.Discard) | ||
} | ||
|
||
func TestLifecycle_OK(t *testing.T) { | ||
hook := logTest.NewGlobal() | ||
|
||
set := flag.NewFlagSet("test", 0) | ||
set.String("config", "", "doc") | ||
ctx := cli.NewContext(nil, set, nil) | ||
err, cfg := cmd.MakeConfig(ctx) | ||
assert.NoError(t, err) | ||
|
||
cfg.DatabaseType = "memorydb" | ||
cfg.Host = "127.0.0.1" | ||
cfg.RPCPort = "7348" | ||
cfg.CACert = "bob.crt" | ||
cfg.Cert = "alice.crt" | ||
cfg.Key = "alice.key" | ||
|
||
ledger, err := ledger.New(ctx, cfg) | ||
assert.NoError(t, err) | ||
|
||
rpcService := NewRPCService(context.Background(), &Config{ | ||
Host: cfg.Host, | ||
Port: cfg.RPCPort, | ||
CACertFlag: cfg.CACert, | ||
CertFlag: cfg.Cert, | ||
KeyFlag: cfg.Key, | ||
}, ledger) | ||
|
||
rpcService.Start() | ||
|
||
shared.LogsContain(t.Fatalf, hook, "GRPC Listening on port", true) | ||
assert.NoError(t, rpcService.Stop()) | ||
} | ||
|
||
func TestStatus_CredentialError(t *testing.T) { | ||
credentialErr := errors.New("credentialError") | ||
s := &Service{credentialError: credentialErr} | ||
|
||
assert.Contains(t, s.credentialError.Error(), s.Status().Error()) | ||
} | ||
|
||
func TestRPC_InsecureEndpoint(t *testing.T) { | ||
hook := logTest.NewGlobal() | ||
|
||
set := flag.NewFlagSet("test", 0) | ||
set.String("config", "", "doc") | ||
ctx := cli.NewContext(nil, set, nil) | ||
err, cfg := cmd.MakeConfig(ctx) | ||
assert.NoError(t, err) | ||
|
||
cfg.DatabaseType = "memorydb" | ||
cfg.Host = "127.0.0.1" | ||
cfg.RPCPort = "7777" | ||
|
||
ledger, err := ledger.New(ctx, cfg) | ||
assert.NoError(t, err) | ||
|
||
rpcService := NewRPCService(context.Background(), &Config{ | ||
Host: cfg.Host, | ||
Port: cfg.RPCPort, | ||
}, ledger) | ||
|
||
rpcService.Start() | ||
|
||
shared.LogsContain(t.Fatalf, hook, "GRPC Listening on port", true) | ||
shared.LogsContain(t.Fatalf, hook, "You are using an insecure gRPC server", true) | ||
assert.NoError(t, rpcService.Stop()) | ||
} |
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
Oops, something went wrong.