Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement integration tests #12

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions nearnet/Dockerfile

This file was deleted.

15 changes: 0 additions & 15 deletions nearnet/docker-compose.yml

This file was deleted.

17 changes: 0 additions & 17 deletions nearnet/entrypoint.sh

This file was deleted.

5 changes: 0 additions & 5 deletions nearnet/get_creds.sh

This file was deleted.

4 changes: 0 additions & 4 deletions nearnet/tail_logs.sh

This file was deleted.

81 changes: 81 additions & 0 deletions nearnet/testlib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package nearnet

import (
"context"
"crypto/rand"
"errors"
"fmt"

"github.com/eteu-technologies/near-api-go/pkg/client"
"github.com/eteu-technologies/near-api-go/pkg/config"
"github.com/eteu-technologies/near-api-go/pkg/types"
"github.com/eteu-technologies/near-api-go/pkg/types/action"
"github.com/eteu-technologies/near-api-go/pkg/types/key"
)

var (
chainAccountFn client.TransactionOpt
)

func main() {
if err := entrypoint(); err != nil {
panic(err)
}
}

func entrypoint() (err error) {
ctx := context.Background()

var rpc client.Client
cfg := config.Networks["testnet"].NodeURL
if rpc, err = client.NewClient(cfg); err != nil {
return
}

// TODO: load key from node directory
var chainKeyPair key.KeyPair
if chainKeyPair, err = key.GenerateKeyPair(key.KeyTypeED25519, rand.Reader); err != nil {
return
}

chainAccountFn = client.WithKeyPair(chainKeyPair)

// TODO: generate random valid name for users
accountA := "integtest-a-.near"
accountB := "integtest-b-.near"

var runnables []Runnable = []Runnable{
CreateAccount("", accountA, types.NEARToYocto(10)),
CreateAccount(accountA, accountB, types.NEARToYocto(1)),
AssertThat("accountB has less NEAR than accountA", func(ctx context.Context, rpc client.Client) (err error) {
// TODO!
return errors.New("not implemented")
}),
}

_ = rpc
_ = ctx
_ = runnables

return
}

type Runnable func(ctx context.Context, rpc client.Client) error

func CreateAccount(creator, name types.AccountID, balance types.Balance) Runnable {
return func(ctx context.Context, rpc client.Client) (err error) {
_, err = rpc.TransactionSendAwait(ctx, creator, name, []action.Action{
action.NewTransfer(balance),
}, chainAccountFn, client.WithLatestBlock())
return
}
}

func AssertThat(what string, fn Runnable /* reusing type because lazy */) Runnable {
return func(ctx context.Context, rpc client.Client) (err error) {
if err = fn(ctx, rpc); err != nil {
return fmt.Errorf("Assertion %s failed! %w", what, err)
}
return
}
}