github.com/condensat/bank-core@v0.1.0/database/model/accountstate_test.go (about) 1 // Copyright 2020 Condensat Tech. All rights reserved. 2 // Use of this source code is governed by a MIT 3 // license that can be found in the LICENSE file. 4 5 package model 6 7 import ( 8 "reflect" 9 "testing" 10 ) 11 12 func TestAccountStatus_Valid(t *testing.T) { 13 t.Parallel() 14 15 tests := []struct { 16 name string 17 p AccountStatus 18 want bool 19 }{ 20 {"Default", AccountStatus(""), false}, 21 {"Invalid", AccountStatus("invalid"), false}, 22 {"NotValid", AccountStatus("not-valid"), false}, 23 24 {"Created", AccountStatus("created"), true}, 25 {"Normal", AccountStatus("normal"), true}, 26 {"Locked", AccountStatus("locked"), true}, 27 {"Disabled", AccountStatus("disabled"), true}, 28 29 {"Random", AccountStatus("R4nD0m"), false}, 30 } 31 for _, tt := range tests { 32 tt := tt // capture range variable 33 t.Run(tt.name, func(t *testing.T) { 34 if got := tt.p.Valid(); got != tt.want { 35 t.Errorf("AccountStatus.Valid() = %v, want %v", got, tt.want) 36 } 37 }) 38 } 39 } 40 41 func TestParseAccountStatus(t *testing.T) { 42 t.Parallel() 43 44 type args struct { 45 str string 46 } 47 tests := []struct { 48 name string 49 args args 50 want AccountStatus 51 }{ 52 {"Default", args{""}, AccountStatusInvalid}, 53 {"Invalid", args{"invalid"}, AccountStatusInvalid}, 54 {"NotValid", args{"not-valid"}, AccountStatusInvalid}, 55 56 {"Created", args{"created"}, AccountStatusCreated}, 57 {"Normal", args{"normal"}, AccountStatusNormal}, 58 {"Locked", args{"locked"}, AccountStatusLocked}, 59 {"Disabled", args{"disabled"}, AccountStatusDisabled}, 60 61 {"Random", args{"R4nD0m"}, AccountStatusInvalid}, 62 } 63 for _, tt := range tests { 64 tt := tt // capture range variable 65 t.Run(tt.name, func(t *testing.T) { 66 if got := ParseAccountStatus(tt.args.str); got != tt.want { 67 t.Errorf("ParseAccountStatus() = %v, want %v", got, tt.want) 68 } 69 }) 70 } 71 } 72 73 func TestAccountStatus_String(t *testing.T) { 74 t.Parallel() 75 76 tests := []struct { 77 name string 78 p AccountStatus 79 want string 80 }{ 81 {"Default", AccountStatus(""), ""}, 82 {"Invalid", AccountStatus("invalid"), ""}, 83 {"NotValid", AccountStatus("not-valid"), ""}, 84 85 {"Created", AccountStatusCreated, "created"}, 86 {"Normal", AccountStatusNormal, "normal"}, 87 {"Locked", AccountStatusLocked, "locked"}, 88 {"Disabled", AccountStatusDisabled, "disabled"}, 89 90 {"Random", AccountStatus("R4nD0m"), ""}, 91 } 92 for _, tt := range tests { 93 tt := tt // capture range variable 94 t.Run(tt.name, func(t *testing.T) { 95 if got := tt.p.String(); got != tt.want { 96 t.Errorf("AccountStatus.String() = %v, want %v", got, tt.want) 97 } 98 }) 99 } 100 } 101 102 func Test_knownAccountStatus(t *testing.T) { 103 t.Parallel() 104 105 // do not use const here 106 // keep order 107 knownEnums := []string{ 108 "", // AccountStatusInvalid 109 110 "created", // AccountStatusCreated 111 "normal", // AccountStatusNormal 112 "locked", // AccountStatusAsyncLocked 113 "disabled", // AccountStatusDisabled 114 } 115 var want []AccountStatus 116 for _, enum := range knownEnums { 117 want = append(want, ParseAccountStatus(enum)) 118 } 119 120 if got := knownAccountStatus(); !reflect.DeepEqual(got, want) { 121 t.Errorf("knownAccountStatus() = %v, want %v", got, want) 122 } 123 }