github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/access/rest/routes/router_test.go (about)

     1  package routes
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestParseURL(t *testing.T) {
    12  	tests := []struct {
    13  		name     string
    14  		url      string
    15  		expected string
    16  	}{
    17  		{
    18  			name:     "/v1/transactions",
    19  			url:      "/v1/transactions",
    20  			expected: "createTransaction",
    21  		},
    22  		{
    23  			name:     "/v1/transactions/{id}",
    24  			url:      "/v1/transactions/53730d3f3d2d2f46cb910b16db817d3a62adaaa72fdb3a92ee373c37c5b55a76",
    25  			expected: "getTransactionByID",
    26  		},
    27  		{
    28  			name:     "/v1/transaction_results/{id}",
    29  			url:      "/v1/transaction_results/53730d3f3d2d2f46cb910b16db817d3a62adaaa72fdb3a92ee373c37c5b55a76",
    30  			expected: "getTransactionResultByID",
    31  		},
    32  		{
    33  			name:     "/v1/blocks",
    34  			url:      "/v1/blocks",
    35  			expected: "getBlocksByHeight",
    36  		},
    37  		{
    38  			name:     "/v1/blocks/{id}",
    39  			url:      "/v1/blocks/53730d3f3d2d2f46cb910b16db817d3a62adaaa72fdb3a92ee373c37c5b55a76",
    40  			expected: "getBlocksByIDs",
    41  		},
    42  		{
    43  			name:     "/v1/blocks/{id}/payload",
    44  			url:      "/v1/blocks/53730d3f3d2d2f46cb910b16db817d3a62adaaa72fdb3a92ee373c37c5b55a76/payload",
    45  			expected: "getBlockPayloadByID",
    46  		},
    47  		{
    48  			name:     "/v1/execution_results/{id}",
    49  			url:      "/v1/execution_results/53730d3f3d2d2f46cb910b16db817d3a62adaaa72fdb3a92ee373c37c5b55a76",
    50  			expected: "getExecutionResultByID",
    51  		},
    52  		{
    53  			name:     "/v1/execution_results",
    54  			url:      "/v1/execution_results",
    55  			expected: "getExecutionResultByBlockID",
    56  		},
    57  		{
    58  			name:     "/v1/collections/{id}",
    59  			url:      "/v1/collections/53730d3f3d2d2f46cb910b16db817d3a62adaaa72fdb3a92ee373c37c5b55a76",
    60  			expected: "getCollectionByID",
    61  		},
    62  		{
    63  			name:     "/v1/scripts",
    64  			url:      "/v1/scripts",
    65  			expected: "executeScript",
    66  		},
    67  		{
    68  			name:     "/v1/accounts/{address}",
    69  			url:      "/v1/accounts/6a587be304c1224c",
    70  			expected: "getAccount",
    71  		},
    72  		{
    73  			name:     "/v1/accounts/{address}/keys/{index}",
    74  			url:      "/v1/accounts/6a587be304c1224c/keys/0",
    75  			expected: "getAccountKeyByIndex",
    76  		},
    77  		{
    78  			name:     "/v1/events",
    79  			url:      "/v1/events",
    80  			expected: "getEvents",
    81  		},
    82  		{
    83  			name:     "/v1/network/parameters",
    84  			url:      "/v1/network/parameters",
    85  			expected: "getNetworkParameters",
    86  		},
    87  		{
    88  			name:     "/v1/node_version_info",
    89  			url:      "/v1/node_version_info",
    90  			expected: "getNodeVersionInfo",
    91  		},
    92  		{
    93  			name:     "/v1/subscribe_events",
    94  			url:      "/v1/subscribe_events",
    95  			expected: "subscribeEvents",
    96  		},
    97  	}
    98  
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			got, err := URLToRoute(tt.url)
   102  			require.NoError(t, err)
   103  			assert.Equal(t, tt.expected, got)
   104  		})
   105  	}
   106  }
   107  
   108  func TestBenchmarkParseURL(t *testing.T) {
   109  	tests := []struct {
   110  		name     string
   111  		url      string
   112  		expected string
   113  	}{
   114  		{
   115  			name:     "/v1/transactions",
   116  			url:      "/v1/transactions",
   117  			expected: "createTransaction",
   118  		},
   119  		{
   120  			name:     "/v1/transactions/{id}",
   121  			url:      "/v1/transactions/53730d3f3d2d2f46cb910b16db817d3a62adaaa72fdb3a92ee373c37c5b55a76",
   122  			expected: "getTransactionByID",
   123  		},
   124  		{
   125  			name:     "/v1/transaction_results/{id}",
   126  			url:      "/v1/transaction_results/53730d3f3d2d2f46cb910b16db817d3a62adaaa72fdb3a92ee373c37c5b55a76",
   127  			expected: "getTransactionResultByID",
   128  		},
   129  		{
   130  			name:     "/v1/blocks",
   131  			url:      "/v1/blocks",
   132  			expected: "getBlocksByHeight",
   133  		},
   134  		{
   135  			name:     "/v1/blocks/{id}",
   136  			url:      "/v1/blocks/53730d3f3d2d2f46cb910b16db817d3a62adaaa72fdb3a92ee373c37c5b55a76",
   137  			expected: "getBlocksByIDs",
   138  		},
   139  		{
   140  			name:     "/v1/blocks/{id}/payload",
   141  			url:      "/v1/blocks/53730d3f3d2d2f46cb910b16db817d3a62adaaa72fdb3a92ee373c37c5b55a76/payload",
   142  			expected: "getBlockPayloadByID",
   143  		},
   144  		{
   145  			name:     "/v1/execution_results/{id}",
   146  			url:      "/v1/execution_results/53730d3f3d2d2f46cb910b16db817d3a62adaaa72fdb3a92ee373c37c5b55a76",
   147  			expected: "getExecutionResultByID",
   148  		},
   149  		{
   150  			name:     "/v1/execution_results",
   151  			url:      "/v1/execution_results",
   152  			expected: "getExecutionResultByBlockID",
   153  		},
   154  		{
   155  			name:     "/v1/collections/{id}",
   156  			url:      "/v1/collections/53730d3f3d2d2f46cb910b16db817d3a62adaaa72fdb3a92ee373c37c5b55a76",
   157  			expected: "getCollectionByID",
   158  		},
   159  		{
   160  			name:     "/v1/scripts",
   161  			url:      "/v1/scripts",
   162  			expected: "executeScript",
   163  		},
   164  		{
   165  			name:     "/v1/accounts/{address}",
   166  			url:      "/v1/accounts/6a587be304c1224c",
   167  			expected: "getAccount",
   168  		},
   169  		{
   170  			name:     "/v1/accounts/{address}/keys/{index}",
   171  			url:      "/v1/accounts/6a587be304c1224c/keys/0",
   172  			expected: "getAccountKeyByIndex",
   173  		},
   174  		{
   175  			name:     "/v1/events",
   176  			url:      "/v1/events",
   177  			expected: "getEvents",
   178  		},
   179  		{
   180  			name:     "/v1/network/parameters",
   181  			url:      "/v1/network/parameters",
   182  			expected: "getNetworkParameters",
   183  		},
   184  		{
   185  			name:     "/v1/node_version_info",
   186  			url:      "/v1/node_version_info",
   187  			expected: "getNodeVersionInfo",
   188  		},
   189  		{
   190  			name:     "/v1/subscribe_events",
   191  			url:      "/v1/subscribe_events",
   192  			expected: "subscribeEvents",
   193  		},
   194  	}
   195  
   196  	for _, tt := range tests {
   197  		t.Run(tt.name, func(t *testing.T) {
   198  			start := time.Now()
   199  			for i := 0; i < 100_000; i++ {
   200  				_, _ = URLToRoute(tt.url)
   201  			}
   202  			t.Logf("%s: %v", tt.name, time.Since(start)/100_000)
   203  		})
   204  	}
   205  }