Skip to content

Commit

Permalink
Making examples.go become an automated test (#68)
Browse files Browse the repository at this point in the history
* Make example runnable with `go test`

* Updated workspacePath

* Adding readme showing how to run test
  • Loading branch information
jimmychu0807 authored Jun 13, 2023
1 parent 5ef9cd8 commit f547802
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 27 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,24 @@ The go sdk implementation of the CESS network, which provides RPC calls, status
If you find out any vulnerability, Please send an email to frode@cess.one, we are happy to communicate with you.

## Installation

To get the package use the standard:
```

```sh
go get -u "github.com/CESSProject/sdk-go"
```

## Testing

To test,

1. Run a [CESS node](https://github.com/CESSProject/cess) locally.
2. Run the command

```sh
go test -v
```

## Documentation & Examples
Please refer to https://pkg.go.dev/github.com/CESSProject/sdk-go

Expand Down
72 changes: 46 additions & 26 deletions examples.go → example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
SPDX-License-Identifier: Apache-2.0
*/

package sdkgo
package sdkgo_test

import (
"testing"
"context"
"log"
"time"

p2pgo "github.com/CESSProject/p2p-go"
cess "github.com/CESSProject/sdk-go"
"github.com/CESSProject/sdk-go/config"
)

Expand All @@ -37,77 +39,95 @@ var P2pBootstrapNodes = []string{
"/ip4/221.122.79.3/tcp/10010/p2p/12D3KooWAdyc4qPWFHsxMtXvSrm7CXNFhUmKPQdoXuKQXki69qBo",
}

// Tmp files will be downloaded
var testWorkspacePath = "/tmp"

const P2pCommunicationPort = 4001

func Example_newClient() {
cli, err := New(
// To run these examples, please run a [CESS node](https://github.com/cessProject/cess) locally
// as well.

func TestNewClient(t *testing.T) {
cli, err := cess.New(
config.CharacterName_Client,
ConnectRpcAddrs(testnets),
Mnemonic(MNEMONIC),
TransactionTimeout(time.Duration(time.Second*15)),
cess.ConnectRpcAddrs(localNode),
cess.Mnemonic(MNEMONIC),
cess.TransactionTimeout(time.Duration(time.Second*15)),
)

if err != nil {
panic(err)
t.Errorf("Error: %s", err)
t.FailNow()
}

blockhright, _ := cli.QueryBlockHeight("")

log.Printf("Successfully created SDK client, latest block height: %d\n", blockhright)
}

func Example_RegisterDeoss() {
cli, err := New(
func TestRegisterDeOSS(t *testing.T) {
cli, err := cess.New(
config.CharacterName_Deoss,
ConnectRpcAddrs(localNode),
Mnemonic(MNEMONIC),
TransactionTimeout(time.Duration(time.Second*15)),
cess.ConnectRpcAddrs(localNode),
cess.Mnemonic(MNEMONIC),
cess.TransactionTimeout(time.Duration(time.Second*15)),
)
if err != nil {
panic(err)
t.Errorf("Error: %s", err)
t.FailNow()
}

p2p, err := p2pgo.New(
context.Background(),
p2pgo.ListenPort(P2pCommunicationPort),
p2pgo.Workspace("/"),
p2pgo.Workspace(testWorkspacePath),
p2pgo.BootPeers(P2pBootstrapNodes),
)

if err != nil {
panic(err)
t.Errorf("Error: %s", err)
t.FailNow()
}

txhash, _, err := cli.Register(cli.GetRoleName(), p2p.GetPeerPublickey(), "", 0)

if err != nil {
panic(err)
t.Errorf("Error: %s", err)
t.FailNow()
}

log.Printf("Deoss registration successful, transaction hash is %s\n", txhash)
}

func Example_RegisterStorageNode() {
cli, err := New(
func TestRegisterStorageNode(t *testing.T) {
cli, err := cess.New(
config.CharacterName_Bucket,
ConnectRpcAddrs(localNode),
Mnemonic(MNEMONIC),
TransactionTimeout(time.Duration(time.Second*10)),
cess.ConnectRpcAddrs(localNode),
cess.Mnemonic(MNEMONIC),
cess.TransactionTimeout(time.Duration(time.Second*10)),
)

if err != nil {
panic(err)
t.Errorf("Error: %s", err)
t.FailNow()
}

p2p, err := p2pgo.New(
context.Background(),
p2pgo.ListenPort(P2pCommunicationPort),
p2pgo.Workspace("/"),
p2pgo.Workspace(testWorkspacePath),
p2pgo.BootPeers(P2pBootstrapNodes),
)

if err != nil {
panic(err)
t.Errorf("Error: %s", err)
t.FailNow()
}

txhash, _, err := cli.Register(cli.GetRoleName(), p2p.GetPeerPublickey(), AliceAddr, 2000)

if err != nil {
panic(err)
t.Errorf("Error: %s", err)
t.FailNow()
}

log.Printf("Storage node registration successful, transaction hash is %s\n", txhash)
Expand Down

0 comments on commit f547802

Please sign in to comment.