github.com/decred/politeia@v1.4.0/politeiad/backendv2/tstorebe/plugins/comments/timestamp_test.go (about)

     1  // Copyright (c) 2022 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package comments
     6  
     7  import (
     8  	"encoding/hex"
     9  	"testing"
    10  )
    11  
    12  func TestGetTimestampKey(t *testing.T) {
    13  	token := "45154fb45664714b"
    14  
    15  	// Setup tests
    16  	tests := []struct {
    17  		name        string
    18  		commentID   uint32
    19  		token       string
    20  		shouldError bool
    21  		cacheKey    string
    22  	}{
    23  		{
    24  			name:        "success case",
    25  			commentID:   8,
    26  			token:       token,
    27  			shouldError: false,
    28  			cacheKey:    "timestamp-45154fb-8",
    29  		},
    30  		{
    31  			name:        "invalid token",
    32  			commentID:   1,
    33  			token:       "",
    34  			shouldError: true,
    35  			cacheKey:    "",
    36  		},
    37  	}
    38  
    39  	// Run tests
    40  	for _, tc := range tests {
    41  		t.Run(tc.name, func(t *testing.T) {
    42  			// Convert token to []byte if set
    43  			var (
    44  				tokenb []byte
    45  				err    error
    46  			)
    47  			if tc.token != "" {
    48  				tokenb, err = hex.DecodeString(tc.token)
    49  				if err != nil {
    50  					t.Fatal(err)
    51  				}
    52  			}
    53  			cacheKey, err := getTimestampKey(tokenb, tc.commentID)
    54  			switch {
    55  			case tc.shouldError && err == nil:
    56  				// Wanted an error but didn't get one
    57  				t.Errorf("want error got nil")
    58  				return
    59  
    60  			case !tc.shouldError && err != nil:
    61  				// Wanted success but got an error
    62  				t.Errorf("want error nil, got '%v'", err)
    63  				return
    64  
    65  			case !tc.shouldError && err == nil:
    66  				// Verify result
    67  				if cacheKey != tc.cacheKey {
    68  					// Expected key was not found, error
    69  					t.Errorf("unexpected cache key; want: %v, got: %v", tc.cacheKey,
    70  						cacheKey)
    71  				}
    72  				return
    73  			}
    74  		})
    75  	}
    76  }
    77  
    78  func TestParseTimestampKey(t *testing.T) {
    79  	// Setup tests
    80  	tests := []struct {
    81  		name        string
    82  		cacheKey    string
    83  		shouldError bool
    84  		commentID   uint32
    85  	}{
    86  		{
    87  			name:        "success case",
    88  			cacheKey:    "timestamp-45154fb-8",
    89  			shouldError: false,
    90  			commentID:   8,
    91  		},
    92  		{
    93  			name:        "invalid key",
    94  			cacheKey:    "--",
    95  			shouldError: true,
    96  			commentID:   0,
    97  		},
    98  	}
    99  
   100  	// Run tests
   101  	for _, tc := range tests {
   102  		t.Run(tc.name, func(t *testing.T) {
   103  			cid, err := parseTimestampKey(tc.cacheKey)
   104  			switch {
   105  			case tc.shouldError && err == nil:
   106  				// Wanted an error but didn't get one
   107  				t.Errorf("want error got nil")
   108  				return
   109  
   110  			case !tc.shouldError && err != nil:
   111  				// Wanted success but got an error
   112  				t.Errorf("want error nil, got '%v'", err)
   113  				return
   114  
   115  			case !tc.shouldError && err == nil:
   116  				// Verify result
   117  				if cid != tc.commentID {
   118  					// Expected key was not found, error
   119  					t.Errorf("unexpected comment ID; want: %v, got: %v", tc.commentID,
   120  						cid)
   121  				}
   122  				return
   123  			}
   124  		})
   125  	}
   126  }