github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/peer/chaincode/query_test.go (about)

     1  /*
     2   Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4   Licensed under the Apache License, Version 2.0 (the "License");
     5   you may not use this file except in compliance with the License.
     6   You may obtain a copy of the License at
     7  
     8        http://www.apache.org/licenses/LICENSE-2.0
     9  
    10   Unless required by applicable law or agreed to in writing, software
    11   distributed under the License is distributed on an "AS IS" BASIS,
    12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   See the License for the specific language governing permissions and
    14   limitations under the License.
    15  */
    16  
    17  package chaincode
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestQueryCmd(t *testing.T) {
    26  	InitMSP()
    27  
    28  	mockCF, err := getMockChaincodeCmdFactory()
    29  	assert.NoError(t, err, "Error getting mock chaincode command factory")
    30  
    31  	// Success case: run query command with -r option
    32  	cmd := queryCmd(mockCF)
    33  	addFlags(cmd)
    34  	args := []string{"-r", "-n", "example02", "-c", "{\"Args\": [\"query\",\"a\"]}"}
    35  	cmd.SetArgs(args)
    36  	err = cmd.Execute()
    37  	assert.NoError(t, err, "Run chaincode query cmd error")
    38  
    39  	// Success case: run query command with -x option
    40  	cmd = queryCmd(mockCF)
    41  	addFlags(cmd)
    42  	args = []string{"-x", "-n", "example02", "-c", "{\"Args\": [\"query\",\"a\"]}"}
    43  	cmd.SetArgs(args)
    44  	err = cmd.Execute()
    45  	assert.NoError(t, err, "Run chaincode query cmd error")
    46  
    47  	// Success case: run query command with out -r or -x option
    48  	cmd = queryCmd(mockCF)
    49  	addFlags(cmd)
    50  	args = []string{"-n", "example02", "-c", "{\"Args\": [\"query\",\"a\"]}"}
    51  	cmd.SetArgs(args)
    52  	err = cmd.Execute()
    53  	assert.NoError(t, err, "Expected error executing query command")
    54  
    55  	// Failure case: run query command with both -x and -r options
    56  	cmd = queryCmd(mockCF)
    57  	addFlags(cmd)
    58  	args = []string{"-r", "-x", "-n", "example02", "-c", "{\"Args\": [\"query\",\"a\"]}"}
    59  	cmd.SetArgs(args)
    60  	err = cmd.Execute()
    61  	assert.Error(t, err, "Expected error executing query command with both -r and -x options")
    62  
    63  	// Failure case: run query command with mock chaincode cmd factory built to return error
    64  	mockCF, err = getMockChaincodeCmdFactoryWithErr()
    65  	assert.NoError(t, err, "Error getting mock chaincode command factory")
    66  	cmd = queryCmd(mockCF)
    67  	addFlags(cmd)
    68  	args = []string{"-r", "-n", "example02", "-c", "{\"Args\": [\"query\",\"a\"]}"}
    69  	cmd.SetArgs(args)
    70  	err = cmd.Execute()
    71  	assert.Error(t, err, "Expected error executing query command with both -r and -x options")
    72  }