github.com/aeternity/aepp-sdk-go/v7@v7.0.1/cmd/chain_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/aeternity/aepp-sdk-go/v7/naet"
    10  )
    11  
    12  // Prefixing each test with Example makes go-test check the stdout
    13  // For now, just verify that none of the commands segfault.
    14  
    15  func TestMain(m *testing.M) {
    16  	flag.BoolVar(&online, "online", false, "Run tests that need a running node on localhost:3013, Network ID ae_docker")
    17  	flag.Parse()
    18  	setPrivateNetParams()
    19  	os.Exit(m.Run())
    20  }
    21  
    22  func Test_topFunc(t *testing.T) {
    23  	type args struct {
    24  		conn naet.GetTopBlocker
    25  		args []string
    26  	}
    27  	tests := []struct {
    28  		name    string
    29  		args    args
    30  		wantErr bool
    31  		online  bool
    32  	}{
    33  		{
    34  			name: "Normal KeyBlockOrMicroBlockHeader",
    35  			args: args{
    36  				conn: &mockGetTopBlocker{
    37  					msg: `{"key_block":{"beneficiary":"ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi","hash":"kh_2H9nMAH8nLFtWeP6YEb4w19d48h6nhUZ2NubUg9Lo5KHLDHiem","height":119,"info":"cb_AAAAAfy4hFE=","miner":"ak_SC7dBmcXyG4i37aKjZM6whqHUU58bsiNths7ivknrG2Z5iF3g","nonce":13382994076605909652,"pow":[1625,2569,3697,6403,6886,7037,7992,9276,9408,10308,10497,10968,12664,12986,13110,13664,13691,14455,14515,18467,19918,20032,20108,20318,20965,22501,23617,23701,23813,24091,24835,24948,24961,26396,26900,27956,28857,29077,30615,30766,31045,32088],"prev_hash":"kh_2HkDM6Bbc3kkPPKWw1psepDH95JFE8LNKKoP9waeh4wWSE1sKh","prev_key_hash":"kh_2HkDM6Bbc3kkPPKWw1psepDH95JFE8LNKKoP9waeh4wWSE1sKh","state_hash":"bs_3sa2GSmv8RGaZfcnaQmjyjFshoBrs68VuUf874RzqCNob1huX","target":539127532,"time":1562686597558,"version":3}}`,
    38  				},
    39  				args: []string{},
    40  			},
    41  			wantErr: false,
    42  			online:  false,
    43  		},
    44  		{
    45  			name: "Online Test",
    46  			args: args{
    47  				conn: newAeNode(),
    48  				args: []string{},
    49  			},
    50  			wantErr: false,
    51  			online:  true,
    52  		},
    53  	}
    54  	for _, tt := range tests {
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			if !online && tt.online {
    57  				t.Skip("Skipping online test")
    58  			}
    59  			if err := topFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
    60  				t.Errorf("topFunc() error = %v, wantErr %v", err, tt.wantErr)
    61  			}
    62  		})
    63  	}
    64  }
    65  
    66  func Test_broadcastFunc(t *testing.T) {
    67  	type args struct {
    68  		conn naet.PostTransactioner
    69  		args []string
    70  	}
    71  	tests := []struct {
    72  		name    string
    73  		args    args
    74  		wantErr bool
    75  	}{
    76  		{
    77  			name: "Normal PostTransaction without error", // this looks like it tests nothing, but actually exercises hashing/cross checking code. the mock pretends that the node checked the hash and found that it matched.
    78  			args: args{
    79  				conn: &mockPostTransactioner{},
    80  				args: []string{"tx_+KgLAfhCuEAPX1l3BdFOcLeduH3PPwPV25mETXZE8IBDe6PGuasSEKJeB/cDDm+kW05Cdp38+mpvVSTTPMx7trL/7qxfUr8IuGD4XhYBoQHOp63kcMn5nZ1OQAiAqG8dSbtES2LxGp67ZLvP63P+8wGTcXVlcnkgU3BlY2lmaWNhdGlvbpZyZXNwb25zZSBTcGVjaWZpY2F0aW9uAABkhrXmIPSAAIIB9AHdGxXf"},
    81  			},
    82  			wantErr: false,
    83  		},
    84  	}
    85  	for _, tt := range tests {
    86  		t.Run(tt.name, func(t *testing.T) {
    87  			if err := broadcastFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
    88  				t.Errorf("broadcastFunc() error = %v, wantErr %v", err, tt.wantErr)
    89  			}
    90  		})
    91  	}
    92  }
    93  
    94  func Test_statusFunc(t *testing.T) {
    95  	type args struct {
    96  		conn naet.GetStatuser
    97  		args []string
    98  	}
    99  	tests := []struct {
   100  		name    string
   101  		args    args
   102  		wantErr bool
   103  		online  bool
   104  	}{
   105  		{
   106  			name: "Normal Status",
   107  			args: args{
   108  				conn: &mockGetStatuser{msg: `{"difficulty":21749349,"genesis_key_block_hash":"kh_2v3mQTSSiyTrhPZjtYcwfbwzt4d6SbFZ5LS9Q7qCosu2QR1beh","listening":true,"network_id":"ae_docker","node_revision":"93c2bd73ae273e3068fb16893024030cf49817b5","node_version":"3.1.0","peer_count":0,"pending_transactions_count":0,"protocols":[{"effective_at_height":1,"version":3},{"effective_at_height":0,"version":1}],"solutions":0,"sync_progress":100,"syncing":false}`},
   109  				args: []string{},
   110  			},
   111  			wantErr: false,
   112  			online:  false,
   113  		},
   114  		{
   115  			name: "Online Status",
   116  			args: args{
   117  				conn: newAeNode(),
   118  				args: []string{},
   119  			},
   120  			wantErr: false,
   121  			online:  true,
   122  		},
   123  	}
   124  	for _, tt := range tests {
   125  		t.Run(tt.name, func(t *testing.T) {
   126  			if !online && tt.online {
   127  				t.Skip("Skipping online test")
   128  			}
   129  			if err := statusFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
   130  				t.Errorf("statusFunc() error = %v, wantErr %v", err, tt.wantErr)
   131  			}
   132  		})
   133  	}
   134  }
   135  
   136  func Test_ttlFunc(t *testing.T) {
   137  	type args struct {
   138  		conn naet.GetHeighter
   139  		args []string
   140  	}
   141  	tests := []struct {
   142  		name    string
   143  		args    args
   144  		wantErr bool
   145  	}{
   146  		{
   147  			name: "Height is 1337",
   148  			args: args{
   149  				conn: &mockGetHeighter{h: 1337},
   150  				args: []string{},
   151  			},
   152  			wantErr: false,
   153  		},
   154  	}
   155  	for _, tt := range tests {
   156  		t.Run(tt.name, func(t *testing.T) {
   157  			if err := ttlFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
   158  				t.Errorf("ttlFunc() error = %v, wantErr %v", err, tt.wantErr)
   159  			}
   160  		})
   161  	}
   162  }
   163  
   164  func Test_networkIDFunc(t *testing.T) {
   165  	type args struct {
   166  		conn naet.GetStatuser
   167  		args []string
   168  	}
   169  	tests := []struct {
   170  		name    string
   171  		args    args
   172  		wantErr bool
   173  		online  bool
   174  	}{
   175  		{
   176  			name: "Status from ae_docker node",
   177  			args: args{
   178  				conn: &mockGetStatuser{msg: `{"difficulty":21749349,"genesis_key_block_hash":"kh_2v3mQTSSiyTrhPZjtYcwfbwzt4d6SbFZ5LS9Q7qCosu2QR1beh","listening":true,"network_id":"ae_docker","node_revision":"93c2bd73ae273e3068fb16893024030cf49817b5","node_version":"3.1.0","peer_count":0,"pending_transactions_count":0,"protocols":[{"effective_at_height":1,"version":3},{"effective_at_height":0,"version":1}],"solutions":0,"sync_progress":100,"syncing":false}`},
   179  				args: []string{},
   180  			},
   181  			wantErr: false,
   182  			online:  false,
   183  		},
   184  		{
   185  			name: "Online Test",
   186  			args: args{
   187  				conn: newAeNode(),
   188  				args: []string{},
   189  			},
   190  			wantErr: false,
   191  			online:  true,
   192  		},
   193  	}
   194  	for _, tt := range tests {
   195  		t.Run(tt.name, func(t *testing.T) {
   196  			fmt.Println(online, tt.online)
   197  			if !online && tt.online {
   198  				t.Skip("Skipping online test")
   199  			}
   200  			if err := networkIDFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
   201  				t.Errorf("networkIDFunc() error = %v, wantErr %v", err, tt.wantErr)
   202  			}
   203  		})
   204  	}
   205  }