github.com/decred/politeia@v1.4.0/politeiawww/cmd/politeiavoter/politeiavoter_test.go (about) 1 // Copyright (c) 2021 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 main 6 7 import ( 8 "testing" 9 "time" 10 ) 11 12 func TestSetupVoteDuration(t *testing.T) { 13 // Setup piv context 14 p, cleanup := fakePiv(t, 0, 1) 15 defer cleanup() 16 17 // Setup tests 18 var tests = []struct { 19 name string 20 voteDuration time.Duration 21 hoursPrior time.Duration 22 timeLeftInVote time.Duration 23 wantErr bool 24 }{ 25 { 26 "provided vote duration exceeds remaining time", 27 2 * time.Hour, 28 0, 29 1 * time.Hour, 30 true, 31 }, 32 { 33 "calculated vote duration is under 24 hours", 34 0, 35 12 * time.Hour, 36 35 * time.Hour, 37 true, 38 }, 39 { 40 "vote duration provided success", 41 1 * time.Hour, 42 0, 43 2 * time.Hour, 44 false, 45 }, 46 { 47 "vote duration not provided success", 48 0, 49 12 * time.Hour, 50 36 * time.Hour, 51 false, 52 }, 53 } 54 55 // Run tests 56 for _, tc := range tests { 57 t.Run(tc.name, func(t *testing.T) { 58 // Setup piv config 59 p.cfg.voteDuration = tc.voteDuration 60 p.cfg.hoursPrior = tc.hoursPrior 61 62 // Run test 63 err := p.setupVoteDuration(tc.timeLeftInVote) 64 switch { 65 case err != nil && tc.wantErr: 66 // Test passes 67 return 68 case err == nil && !tc.wantErr: 69 // Test passes 70 return 71 default: 72 // Test fails 73 t.Errorf("got err %v, want err %v", 74 err == nil, tc.wantErr) 75 } 76 }) 77 } 78 }