github.com/aeternity/aepp-sdk-go/v6@v6.0.0/cmd/chain_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/aeternity/aepp-sdk-go/v6/naet"
     9  )
    10  
    11  // Prefixing each test with Example makes go-test check the stdout
    12  // For now, just verify that none of the commands segfault.
    13  
    14  func init() {
    15  	flag.BoolVar(&online, "online", false, "Run tests that need a running node on localhost:3013, Network ID ae_docker")
    16  	flag.Parse()
    17  	setPrivateNetParams()
    18  }
    19  
    20  func Test_topFunc(t *testing.T) {
    21  	type args struct {
    22  		conn naet.GetTopBlocker
    23  		args []string
    24  	}
    25  	tests := []struct {
    26  		name    string
    27  		args    args
    28  		wantErr bool
    29  		online  bool
    30  	}{
    31  		{
    32  			name: "Normal KeyBlockOrMicroBlockHeader",
    33  			args: args{
    34  				conn: &mockGetTopBlocker{
    35  					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}}`,
    36  				},
    37  				args: []string{},
    38  			},
    39  			wantErr: false,
    40  			online:  false,
    41  		},
    42  		{
    43  			name: "Online Test",
    44  			args: args{
    45  				conn: newAeNode(),
    46  				args: []string{},
    47  			},
    48  			wantErr: false,
    49  			online:  true,
    50  		},
    51  	}
    52  	for _, tt := range tests {
    53  		t.Run(tt.name, func(t *testing.T) {
    54  			if !online && tt.online {
    55  				t.Skip("Skipping online test")
    56  			}
    57  			if err := topFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
    58  				t.Errorf("topFunc() error = %v, wantErr %v", err, tt.wantErr)
    59  			}
    60  		})
    61  	}
    62  }
    63  
    64  func Test_broadcastFunc(t *testing.T) {
    65  	type args struct {
    66  		conn naet.PostTransactioner
    67  		args []string
    68  	}
    69  	tests := []struct {
    70  		name    string
    71  		args    args
    72  		wantErr bool
    73  	}{
    74  		{
    75  			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.
    76  			args: args{
    77  				conn: &mockPostTransactioner{},
    78  				args: []string{"tx_+KgLAfhCuEAPX1l3BdFOcLeduH3PPwPV25mETXZE8IBDe6PGuasSEKJeB/cDDm+kW05Cdp38+mpvVSTTPMx7trL/7qxfUr8IuGD4XhYBoQHOp63kcMn5nZ1OQAiAqG8dSbtES2LxGp67ZLvP63P+8wGTcXVlcnkgU3BlY2lmaWNhdGlvbpZyZXNwb25zZSBTcGVjaWZpY2F0aW9uAABkhrXmIPSAAIIB9AHdGxXf"},
    79  			},
    80  			wantErr: false,
    81  		},
    82  	}
    83  	for _, tt := range tests {
    84  		t.Run(tt.name, func(t *testing.T) {
    85  			if err := broadcastFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
    86  				t.Errorf("broadcastFunc() error = %v, wantErr %v", err, tt.wantErr)
    87  			}
    88  		})
    89  	}
    90  }
    91  
    92  func Test_statusFunc(t *testing.T) {
    93  	type args struct {
    94  		conn naet.GetStatuser
    95  		args []string
    96  	}
    97  	tests := []struct {
    98  		name    string
    99  		args    args
   100  		wantErr bool
   101  		online  bool
   102  	}{
   103  		{
   104  			name: "Normal Status",
   105  			args: args{
   106  				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}`},
   107  				args: []string{},
   108  			},
   109  			wantErr: false,
   110  			online:  false,
   111  		},
   112  		{
   113  			name: "Online Status",
   114  			args: args{
   115  				conn: newAeNode(),
   116  				args: []string{},
   117  			},
   118  			wantErr: false,
   119  			online:  true,
   120  		},
   121  	}
   122  	for _, tt := range tests {
   123  		t.Run(tt.name, func(t *testing.T) {
   124  			if !online && tt.online {
   125  				t.Skip("Skipping online test")
   126  			}
   127  			if err := statusFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
   128  				t.Errorf("statusFunc() error = %v, wantErr %v", err, tt.wantErr)
   129  			}
   130  		})
   131  	}
   132  }
   133  
   134  func Test_ttlFunc(t *testing.T) {
   135  	type args struct {
   136  		conn naet.GetHeighter
   137  		args []string
   138  	}
   139  	tests := []struct {
   140  		name    string
   141  		args    args
   142  		wantErr bool
   143  	}{
   144  		{
   145  			name: "Height is 1337",
   146  			args: args{
   147  				conn: &mockGetHeighter{h: 1337},
   148  				args: []string{},
   149  			},
   150  			wantErr: false,
   151  		},
   152  	}
   153  	for _, tt := range tests {
   154  		t.Run(tt.name, func(t *testing.T) {
   155  			if err := ttlFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
   156  				t.Errorf("ttlFunc() error = %v, wantErr %v", err, tt.wantErr)
   157  			}
   158  		})
   159  	}
   160  }
   161  
   162  func Test_networkIDFunc(t *testing.T) {
   163  	type args struct {
   164  		conn naet.GetStatuser
   165  		args []string
   166  	}
   167  	tests := []struct {
   168  		name    string
   169  		args    args
   170  		wantErr bool
   171  		online  bool
   172  	}{
   173  		{
   174  			name: "Status from ae_docker node",
   175  			args: args{
   176  				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}`},
   177  				args: []string{},
   178  			},
   179  			wantErr: false,
   180  			online:  false,
   181  		},
   182  		{
   183  			name: "Online Test",
   184  			args: args{
   185  				conn: newAeNode(),
   186  				args: []string{},
   187  			},
   188  			wantErr: false,
   189  			online:  true,
   190  		},
   191  	}
   192  	for _, tt := range tests {
   193  		t.Run(tt.name, func(t *testing.T) {
   194  			fmt.Println(online, tt.online)
   195  			if !online && tt.online {
   196  				t.Skip("Skipping online test")
   197  			}
   198  			if err := networkIDFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
   199  				t.Errorf("networkIDFunc() error = %v, wantErr %v", err, tt.wantErr)
   200  			}
   201  		})
   202  	}
   203  }