github.com/Finschia/finschia-sdk@v0.49.1/x/foundation/client/testutil/query.go (about) 1 package testutil 2 3 import ( 4 "fmt" 5 6 ostcli "github.com/Finschia/ostracon/libs/cli" 7 "github.com/gogo/protobuf/proto" 8 9 "github.com/Finschia/finschia-sdk/client/flags" 10 clitestutil "github.com/Finschia/finschia-sdk/testutil/cli" 11 sdk "github.com/Finschia/finschia-sdk/types" 12 "github.com/Finschia/finschia-sdk/x/foundation" 13 "github.com/Finschia/finschia-sdk/x/foundation/client/cli" 14 ) 15 16 func (s *IntegrationTestSuite) TestNewQueryCmdParams() { 17 val := s.network.Validators[0] 18 commonArgs := []string{ 19 fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight), 20 fmt.Sprintf("--%s=json", ostcli.OutputFlag), 21 } 22 23 testCases := map[string]struct { 24 args []string 25 valid bool 26 expected proto.Message 27 }{ 28 "valid query": { 29 []string{}, 30 true, 31 &foundation.QueryParamsResponse{ 32 Params: foundation.Params{ 33 FoundationTax: sdk.MustNewDecFromStr("0.2"), 34 }, 35 }, 36 }, 37 "wrong number of args": { 38 []string{ 39 "extra", 40 }, 41 false, 42 nil, 43 }, 44 } 45 46 for name, tc := range testCases { 47 s.Run(name, func() { 48 cmd := cli.NewQueryCmdParams() 49 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 50 if !tc.valid { 51 s.Require().Error(err) 52 return 53 } 54 s.Require().NoError(err) 55 56 var actual foundation.QueryParamsResponse 57 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String()) 58 s.Require().Equal(tc.expected, &actual) 59 }) 60 } 61 } 62 63 func (s *IntegrationTestSuite) TestNewQueryCmdTreasury() { 64 val := s.network.Validators[0] 65 commonArgs := []string{ 66 fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight), 67 fmt.Sprintf("--%s=json", ostcli.OutputFlag), 68 } 69 70 testCases := map[string]struct { 71 args []string 72 valid bool 73 }{ 74 "valid query": { 75 []string{}, 76 true, 77 }, 78 "wrong number of args": { 79 []string{ 80 "extra", 81 }, 82 false, 83 }, 84 } 85 86 for name, tc := range testCases { 87 s.Run(name, func() { 88 cmd := cli.NewQueryCmdTreasury() 89 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 90 if !tc.valid { 91 s.Require().Error(err) 92 return 93 } 94 s.Require().NoError(err) 95 96 var actual foundation.QueryTreasuryResponse 97 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String()) 98 }) 99 } 100 } 101 102 func (s *IntegrationTestSuite) TestNewQueryCmdFoundationInfo() { 103 val := s.network.Validators[0] 104 commonArgs := []string{ 105 fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight), 106 fmt.Sprintf("--%s=json", ostcli.OutputFlag), 107 } 108 109 testCases := map[string]struct { 110 args []string 111 valid bool 112 }{ 113 "valid query": { 114 []string{}, 115 true, 116 }, 117 "wrong number of args": { 118 []string{ 119 "extra", 120 }, 121 false, 122 }, 123 } 124 125 for name, tc := range testCases { 126 s.Run(name, func() { 127 cmd := cli.NewQueryCmdFoundationInfo() 128 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 129 if !tc.valid { 130 s.Require().Error(err) 131 return 132 } 133 s.Require().NoError(err) 134 135 var actual foundation.QueryFoundationInfoResponse 136 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String()) 137 }) 138 } 139 } 140 141 func (s *IntegrationTestSuite) TestNewQueryCmdMember() { 142 val := s.network.Validators[0] 143 commonArgs := []string{ 144 fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight), 145 fmt.Sprintf("--%s=json", ostcli.OutputFlag), 146 } 147 148 testCases := map[string]struct { 149 args []string 150 valid bool 151 expected *foundation.Member 152 }{ 153 "valid query": { 154 []string{ 155 s.permanentMember.String(), 156 }, 157 true, 158 &foundation.Member{ 159 Address: s.permanentMember.String(), 160 Metadata: "permanent member", 161 }, 162 }, 163 "wrong number of args": { 164 []string{ 165 s.permanentMember.String(), 166 "extra", 167 }, 168 false, 169 nil, 170 }, 171 "invalid member": { 172 []string{ 173 "", 174 }, 175 false, 176 nil, 177 }, 178 } 179 180 for name, tc := range testCases { 181 s.Run(name, func() { 182 cmd := cli.NewQueryCmdMember() 183 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 184 if !tc.valid { 185 s.Require().Error(err) 186 return 187 } 188 s.Require().NoError(err) 189 190 var actual foundation.QueryMemberResponse 191 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String()) 192 s.Require().Equal(tc.expected, actual.Member) 193 }) 194 } 195 } 196 197 func (s *IntegrationTestSuite) TestNewQueryCmdMembers() { 198 val := s.network.Validators[0] 199 commonArgs := []string{ 200 fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight), 201 fmt.Sprintf("--%s=json", ostcli.OutputFlag), 202 } 203 204 testCases := map[string]struct { 205 args []string 206 valid bool 207 }{ 208 "valid query": { 209 []string{}, 210 true, 211 }, 212 "wrong number of args": { 213 []string{ 214 "extra", 215 }, 216 false, 217 }, 218 } 219 220 for name, tc := range testCases { 221 s.Run(name, func() { 222 cmd := cli.NewQueryCmdMembers() 223 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 224 if !tc.valid { 225 s.Require().Error(err) 226 return 227 } 228 s.Require().NoError(err) 229 230 var actual foundation.QueryMembersResponse 231 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String()) 232 }) 233 } 234 } 235 236 func (s *IntegrationTestSuite) TestNewQueryCmdProposal() { 237 val := s.network.Validators[0] 238 commonArgs := []string{ 239 fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight), 240 fmt.Sprintf("--%s=json", ostcli.OutputFlag), 241 } 242 243 testCases := map[string]struct { 244 args []string 245 valid bool 246 }{ 247 "valid query": { 248 []string{ 249 fmt.Sprintf("%d", s.proposalID), 250 }, 251 true, 252 }, 253 "wrong number of args": { 254 []string{ 255 fmt.Sprintf("%d", s.proposalID), 256 "extra", 257 }, 258 false, 259 }, 260 "invalid id": { 261 []string{ 262 fmt.Sprintf("%d", -1), 263 }, 264 false, 265 }, 266 } 267 268 for name, tc := range testCases { 269 s.Run(name, func() { 270 cmd := cli.NewQueryCmdProposal() 271 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 272 if !tc.valid { 273 s.Require().Error(err) 274 return 275 } 276 s.Require().NoError(err) 277 278 var actual foundation.QueryProposalResponse 279 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String()) 280 }) 281 } 282 } 283 284 func (s *IntegrationTestSuite) TestNewQueryCmdProposals() { 285 val := s.network.Validators[0] 286 commonArgs := []string{ 287 fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight), 288 fmt.Sprintf("--%s=json", ostcli.OutputFlag), 289 } 290 291 testCases := map[string]struct { 292 args []string 293 valid bool 294 }{ 295 "valid query": { 296 []string{}, 297 true, 298 }, 299 "wrong number of args": { 300 []string{ 301 "extra", 302 }, 303 false, 304 }, 305 } 306 307 for name, tc := range testCases { 308 s.Run(name, func() { 309 cmd := cli.NewQueryCmdProposals() 310 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 311 if !tc.valid { 312 s.Require().Error(err) 313 return 314 } 315 s.Require().NoError(err) 316 317 var actual foundation.QueryProposalsResponse 318 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String()) 319 }) 320 } 321 } 322 323 func (s *IntegrationTestSuite) TestNewQueryCmdVote() { 324 val := s.network.Validators[0] 325 commonArgs := []string{ 326 fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight), 327 fmt.Sprintf("--%s=json", ostcli.OutputFlag), 328 } 329 330 testCases := map[string]struct { 331 args []string 332 valid bool 333 }{ 334 "valid query": { 335 []string{ 336 fmt.Sprintf("%d", s.proposalID), 337 s.permanentMember.String(), 338 }, 339 true, 340 }, 341 "wrong number of args": { 342 []string{ 343 fmt.Sprintf("%d", s.proposalID), 344 s.permanentMember.String(), 345 "extra", 346 }, 347 false, 348 }, 349 "invalid proposal id": { 350 []string{ 351 fmt.Sprintf("%d", -1), 352 s.permanentMember.String(), 353 }, 354 false, 355 }, 356 "invalid voter": { 357 []string{ 358 fmt.Sprintf("%d", s.proposalID), 359 "", 360 }, 361 false, 362 }, 363 } 364 365 for name, tc := range testCases { 366 s.Run(name, func() { 367 cmd := cli.NewQueryCmdVote() 368 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 369 if !tc.valid { 370 s.Require().Error(err) 371 return 372 } 373 s.Require().NoError(err) 374 375 var actual foundation.QueryVoteResponse 376 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String()) 377 }) 378 } 379 } 380 381 func (s *IntegrationTestSuite) TestNewQueryCmdVotes() { 382 val := s.network.Validators[0] 383 commonArgs := []string{ 384 fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight), 385 fmt.Sprintf("--%s=json", ostcli.OutputFlag), 386 } 387 388 testCases := map[string]struct { 389 args []string 390 valid bool 391 }{ 392 "valid query": { 393 []string{ 394 fmt.Sprintf("%d", s.proposalID), 395 }, 396 true, 397 }, 398 "wrong number of args": { 399 []string{ 400 fmt.Sprintf("%d", s.proposalID), 401 "extra", 402 }, 403 false, 404 }, 405 "invalid proposal id": { 406 []string{ 407 fmt.Sprintf("%d", -1), 408 }, 409 false, 410 }, 411 } 412 413 for name, tc := range testCases { 414 s.Run(name, func() { 415 cmd := cli.NewQueryCmdVotes() 416 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 417 if !tc.valid { 418 s.Require().Error(err) 419 return 420 } 421 s.Require().NoError(err) 422 423 var actual foundation.QueryVotesResponse 424 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String()) 425 }) 426 } 427 } 428 429 func (s *IntegrationTestSuite) TestNewQueryCmdTallyResult() { 430 val := s.network.Validators[0] 431 commonArgs := []string{ 432 fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight), 433 fmt.Sprintf("--%s=json", ostcli.OutputFlag), 434 } 435 436 testCases := map[string]struct { 437 args []string 438 valid bool 439 }{ 440 "valid query": { 441 []string{ 442 fmt.Sprintf("%d", s.proposalID), 443 }, 444 true, 445 }, 446 "wrong number of args": { 447 []string{ 448 fmt.Sprintf("%d", s.proposalID), 449 "extra", 450 }, 451 false, 452 }, 453 "invalid proposal id": { 454 []string{ 455 fmt.Sprintf("%d", -1), 456 }, 457 false, 458 }, 459 } 460 461 for name, tc := range testCases { 462 s.Run(name, func() { 463 cmd := cli.NewQueryCmdTallyResult() 464 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 465 if !tc.valid { 466 s.Require().Error(err) 467 return 468 } 469 s.Require().NoError(err) 470 471 var actual foundation.QueryTallyResultResponse 472 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String()) 473 }) 474 } 475 } 476 477 func (s *IntegrationTestSuite) TestNewQueryCmdCensorships() { 478 val := s.network.Validators[0] 479 commonArgs := []string{ 480 fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight), 481 fmt.Sprintf("--%s=json", ostcli.OutputFlag), 482 } 483 484 testCases := map[string]struct { 485 args []string 486 valid bool 487 expected int 488 }{ 489 "valid query": { 490 []string{}, 491 true, 492 1, 493 }, 494 "wrong number of args": { 495 []string{ 496 "extra", 497 }, 498 false, 499 0, 500 }, 501 } 502 503 for name, tc := range testCases { 504 s.Run(name, func() { 505 cmd := cli.NewQueryCmdCensorships() 506 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 507 if !tc.valid { 508 s.Require().Error(err) 509 return 510 } 511 s.Require().NoError(err) 512 513 var actual foundation.QueryCensorshipsResponse 514 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String()) 515 s.Require().Len(actual.Censorships, tc.expected) 516 }) 517 } 518 } 519 520 func (s *IntegrationTestSuite) TestNewQueryCmdGrants() { 521 val := s.network.Validators[0] 522 commonArgs := []string{ 523 fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight), 524 fmt.Sprintf("--%s=json", ostcli.OutputFlag), 525 } 526 527 testCases := map[string]struct { 528 args []string 529 valid bool 530 expected int 531 }{ 532 "valid query": { 533 []string{ 534 s.stranger.String(), 535 foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), 536 }, 537 true, 538 1, 539 }, 540 "no msg type url": { 541 []string{ 542 s.stranger.String(), 543 }, 544 true, 545 1, 546 }, 547 "wrong number of args": { 548 []string{ 549 s.stranger.String(), 550 foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), 551 "extra", 552 }, 553 false, 554 0, 555 }, 556 "invalid grantee": { 557 []string{ 558 "", 559 foundation.ReceiveFromTreasuryAuthorization{}.MsgTypeURL(), 560 }, 561 false, 562 0, 563 }, 564 } 565 566 for name, tc := range testCases { 567 s.Run(name, func() { 568 cmd := cli.NewQueryCmdGrants() 569 out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...)) 570 if !tc.valid { 571 s.Require().Error(err) 572 return 573 } 574 s.Require().NoError(err) 575 576 var actual foundation.QueryGrantsResponse 577 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String()) 578 s.Require().Equal(tc.expected, len(actual.Authorizations)) 579 }) 580 } 581 }