/x/ecocredit/params_test.go

https://github.com/regen-network/regen-ledger · Go · 231 lines · 220 code · 11 blank · 0 comment · 17 complexity · 45f8596c874ede62066298ed4b245a43 MD5 · raw file

  1. package ecocredit
  2. import (
  3. "fmt"
  4. "testing"
  5. sdk "github.com/cosmos/cosmos-sdk/types"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestDefaultParams(t *testing.T) {
  9. expected := Params{
  10. CreditClassFee: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, DefaultCreditClassFeeTokens)),
  11. AllowedClassCreators: []string{},
  12. AllowlistEnabled: false,
  13. CreditTypes: []*CreditType{
  14. {
  15. Name: "carbon",
  16. Abbreviation: "C",
  17. Unit: "metric ton CO2 equivalent",
  18. Precision: PRECISION,
  19. },
  20. },
  21. }
  22. df := DefaultParams()
  23. require.Equal(t, df.String(), expected.String())
  24. }
  25. func Test_validateAllowedClassCreators(t *testing.T) {
  26. genAddrs := make([]string, 0, 3)
  27. for i := 0; i < 3; i++ {
  28. genAddrs = append(genAddrs, sdk.MustBech32ifyAddressBytes(sdk.Bech32MainPrefix, []byte(fmt.Sprintf("testaddr-%d", i))))
  29. }
  30. tests := []struct {
  31. name string
  32. args interface{}
  33. wantErr bool
  34. }{
  35. {
  36. name: "valid creator list",
  37. args: genAddrs,
  38. wantErr: false,
  39. },
  40. {
  41. name: "invalid creator list",
  42. args: []string{"bogus", "superbogus", "megabogus"},
  43. wantErr: true,
  44. },
  45. {
  46. name: "mix of invalid/valid",
  47. args: []string{"bogus", genAddrs[0]},
  48. wantErr: true,
  49. },
  50. {
  51. name: "wrong type",
  52. args: []int{1, 2, 3, 4, 5},
  53. wantErr: true,
  54. },
  55. {
  56. name: "not even an array",
  57. args: "bruh",
  58. wantErr: true,
  59. },
  60. }
  61. for _, tt := range tests {
  62. t.Run(tt.name, func(t *testing.T) {
  63. if err := validateAllowedClassCreators(tt.args); (err != nil) != tt.wantErr {
  64. t.Errorf("validateAllowedClassCreators() error = %v, wantErr %v", err, tt.wantErr)
  65. }
  66. })
  67. }
  68. }
  69. func Test_validateAllowlistEnabled(t *testing.T) {
  70. tests := []struct {
  71. name string
  72. args interface{}
  73. wantErr bool
  74. }{
  75. {
  76. name: "valid boolean value",
  77. args: true,
  78. wantErr: false,
  79. },
  80. {
  81. name: "valid boolean v2",
  82. args: false,
  83. wantErr: false,
  84. },
  85. {
  86. name: "invalid type",
  87. args: []string{"lol", "rofl"},
  88. wantErr: true,
  89. },
  90. {
  91. name: "seems valid but its not",
  92. args: 0,
  93. wantErr: true,
  94. },
  95. }
  96. for _, tt := range tests {
  97. t.Run(tt.name, func(t *testing.T) {
  98. if err := validateAllowlistEnabled(tt.args); (err != nil) != tt.wantErr {
  99. t.Errorf("validateAllowlistEnabled() error = %v, wantErr %v", err, tt.wantErr)
  100. }
  101. })
  102. }
  103. }
  104. func Test_validateCreditClassFee(t *testing.T) {
  105. tests := []struct {
  106. name string
  107. args interface{}
  108. wantErr bool
  109. }{
  110. {
  111. name: "valid credit fee",
  112. args: sdk.NewCoins(sdk.NewCoin("regen", sdk.NewInt(45)), sdk.NewCoin("osmo", sdk.NewInt(32))),
  113. wantErr: false,
  114. },
  115. {
  116. name: "no fee is valid",
  117. args: sdk.NewCoins(),
  118. wantErr: false,
  119. },
  120. {
  121. name: "invalid type",
  122. args: 45,
  123. wantErr: true,
  124. },
  125. }
  126. for _, tt := range tests {
  127. t.Run(tt.name, func(t *testing.T) {
  128. if err := validateCreditClassFee(tt.args); (err != nil) != tt.wantErr {
  129. t.Errorf("validateCreditClassFee() error = %v, wantErr %v", err, tt.wantErr)
  130. }
  131. })
  132. }
  133. }
  134. func Test_validateCreditTypes(t *testing.T) {
  135. tests := []struct {
  136. name string
  137. args interface{}
  138. wantErr bool
  139. }{
  140. {
  141. name: "valid credit types",
  142. args: []*CreditType{
  143. {Name: "carbon", Abbreviation: "C", Unit: "ton", Precision: 6},
  144. {Name: "biodiversity", Abbreviation: "BIO", Unit: "mi", Precision: 6},
  145. },
  146. wantErr: false,
  147. },
  148. {
  149. name: "wrong type",
  150. args: []*ClassInfo{
  151. {
  152. ClassId: "foo",
  153. Admin: "0xdeadbeef",
  154. Issuers: []string{"not", "an", "address"},
  155. Metadata: nil,
  156. CreditType: nil,
  157. },
  158. },
  159. wantErr: true,
  160. },
  161. {
  162. name: "cant have duplicate names",
  163. args: []*CreditType{
  164. {Name: "carbon", Abbreviation: "C", Unit: "ton", Precision: 6},
  165. {Name: "carbon", Abbreviation: "CAR", Unit: "ton", Precision: 6},
  166. },
  167. wantErr: true,
  168. },
  169. {
  170. name: "cant use non-normalized credit type name",
  171. args: []*CreditType{{Name: "biODiVerSitY", Abbreviation: "BIO", Unit: "ton", Precision: 6}},
  172. wantErr: true,
  173. },
  174. {
  175. name: "cant use empty name",
  176. args: []*CreditType{{Name: "", Abbreviation: "C", Unit: "ton", Precision: 6}},
  177. wantErr: true,
  178. },
  179. {
  180. name: "cant have duplicate abbreviations",
  181. args: []*CreditType{
  182. {Name: "carbon", Abbreviation: "C", Unit: "ton", Precision: 6},
  183. {Name: "carbonic-acid", Abbreviation: "C", Unit: "ton", Precision: 6},
  184. },
  185. wantErr: true,
  186. },
  187. {
  188. name: "cant use empty abbreviation",
  189. args: []*CreditType{{Name: "carbon", Unit: "ton", Precision: 6}},
  190. wantErr: true,
  191. },
  192. {
  193. name: "cant use lowercase abbreviation",
  194. args: []*CreditType{{Name: "carbon", Abbreviation: "c", Unit: "ton", Precision: 6}},
  195. wantErr: true,
  196. },
  197. {
  198. name: "cant use longer than 3 letter abbreviation",
  199. args: []*CreditType{{Name: "carbon", Abbreviation: "CARB", Unit: "ton", Precision: 6}},
  200. wantErr: true,
  201. },
  202. {
  203. name: "cant use precision other than 6",
  204. args: []*CreditType{{Name: "carbon", Abbreviation: "C", Unit: "ton", Precision: 0}},
  205. wantErr: true,
  206. },
  207. {
  208. name: "cant use empty units",
  209. args: []*CreditType{{Name: "carbon", Abbreviation: "C", Unit: "", Precision: 6}},
  210. wantErr: true,
  211. },
  212. }
  213. for _, tt := range tests {
  214. t.Run(tt.name, func(t *testing.T) {
  215. if err := validateCreditTypes(tt.args); (err != nil) != tt.wantErr {
  216. t.Errorf("validateCreditTypes() error = %v, wantErr %v", err, tt.wantErr)
  217. }
  218. })
  219. }
  220. }