github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/cmd/contract_tests/main.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/line/ostracon/cmd/contract_tests/unmarshaler"
     9  
    10  	"github.com/snikch/goodman/hooks"
    11  	"github.com/snikch/goodman/transaction"
    12  )
    13  
    14  func main() {
    15  	// This must be compiled beforehand and given to dredd as parameter, in the meantime the server should be running
    16  	h := hooks.NewHooks()
    17  	server := hooks.NewServer(hooks.NewHooksRunner(h))
    18  	h.BeforeAll(func(t []*transaction.Transaction) {
    19  		fmt.Println(t[0].Name)
    20  	})
    21  	h.BeforeEach(func(t *transaction.Transaction) {
    22  		if t.Expected.StatusCode != "200" {
    23  			t.Skip = true
    24  		} else if strings.HasPrefix(t.Name, "Tx") ||
    25  			// We need a proper example of evidence to broadcast
    26  			strings.HasPrefix(t.Name, "/broadcast_evidence >") ||
    27  			// We need a proper example of path and data
    28  			strings.HasPrefix(t.Name, "/abci_query >") ||
    29  			// We need to find a way to make a transaction before starting the tests,
    30  			// that hash should replace the dummy one in the openapi file
    31  			strings.HasPrefix(t.Name, "/tx >") {
    32  			t.Skip = true
    33  		}
    34  	})
    35  
    36  	// TODO This check need to remove if dredd is updated to check optional
    37  	// dredd can not validate optional items
    38  	h.Before("/genesis > Get Genesis > 200 > application/json", func(t *transaction.Transaction) {
    39  		removeOptionalFieldsOfExpected(t, []string{"result.genesis.app_state"})
    40  	})
    41  	h.Before("/broadcast_tx_async > Returns right away, with no response. "+
    42  		"Does not wait for CheckTx nor DeliverTx results. > 200 > application/json", func(t *transaction.Transaction) {
    43  		removeOptionalFieldsOfExpected(t, []string{"error"})
    44  	})
    45  	h.Before("/broadcast_tx_sync > Returns with the response from CheckTx. "+
    46  		"Does not wait for DeliverTx result. > 200 > application/json", func(t *transaction.Transaction) {
    47  		removeOptionalFieldsOfExpected(t, []string{"error"})
    48  	})
    49  	h.Before("/broadcast_tx_commit > Returns with the responses from CheckTx and DeliverTx. "+
    50  		"> 200 > application/json", func(t *transaction.Transaction) {
    51  		removeOptionalFieldsOfExpected(t, []string{"error"})
    52  	})
    53  	h.Before("/block_results > Get block results at a specified height > 200 > application/json",
    54  		func(t *transaction.Transaction) {
    55  			removeOptionalFieldsOfExpected(t, []string{
    56  				"result.txs_results",
    57  				"result.begin_block_events",
    58  				"result.end_block",
    59  				"result.end_block_events",
    60  				"result.validator_updates",
    61  				"result.consensus_param_updates"})
    62  		})
    63  
    64  	server.Serve()
    65  	defer server.Listener.Close()
    66  }
    67  
    68  func removeOptionalFieldsOfExpected(t *transaction.Transaction, paths []string) {
    69  	expected := unmarshaler.UnmarshalJSON(&t.Expected.Body)
    70  	for _, path := range paths {
    71  		expected.DeleteProperty(strings.Split(path, ".")...)
    72  	}
    73  	newBody, err := json.Marshal(expected.Body)
    74  	if err != nil {
    75  		panic(fmt.Sprintf("fail to marshal expected body with %s", err))
    76  	}
    77  	t.Expected.Body = string(newBody)
    78  }