github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/ledger/util/couchdb/couchdbutil_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 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 couchdb
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric/common/ledger/testutil"
    24  	"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
    25  )
    26  
    27  //Unit test of couch db util functionality
    28  func TestCreateCouchDBConnectionAndDB(t *testing.T) {
    29  	if ledgerconfig.IsCouchDBEnabled() == true {
    30  
    31  		database := "testcreatecouchdbconnectionanddb"
    32  		cleanup(database)
    33  		defer cleanup(database)
    34  		//create a new connection
    35  		couchInstance, err := CreateCouchInstance(connectURL, "", "")
    36  		testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to CreateCouchInstance"))
    37  
    38  		_, err = CreateCouchDatabase(*couchInstance, database)
    39  		testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to CreateCouchDatabase"))
    40  	}
    41  
    42  }
    43  
    44  func TestDatabaseMapping(t *testing.T) {
    45  
    46  	//create a new instance and database object using a database name mixed case
    47  	databaseName, err := mapAndValidateDatabaseName("testDB")
    48  	testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to map database name"))
    49  	testutil.AssertEquals(t, databaseName, "testdb")
    50  
    51  	//create a new instance and database object using a database name with numerics
    52  	databaseName, err = mapAndValidateDatabaseName("test1234DB")
    53  	testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to map database name"))
    54  	testutil.AssertEquals(t, databaseName, "test1234db")
    55  
    56  	//create a new instance and database object using a database name with special characters
    57  	databaseName, err = mapAndValidateDatabaseName("test1234_$(),+-/~!@#%^&*[]{}.")
    58  	testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to map database name"))
    59  	testutil.AssertEquals(t, databaseName, "test1234_$(),+-/_____________")
    60  
    61  	//create a new instance and database object using a database name with special characters
    62  	databaseName, err = mapAndValidateDatabaseName("5test1234")
    63  	testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to map database name"))
    64  	testutil.AssertEquals(t, databaseName, "db_5test1234")
    65  
    66  	//create a new instance and database object using an empty string
    67  	_, err = mapAndValidateDatabaseName("")
    68  	testutil.AssertError(t, err, fmt.Sprintf("Error should have been thrown for an invalid name"))
    69  
    70  	_, err = mapAndValidateDatabaseName("A12345678901234567890123456789012345678901234" +
    71  		"56789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
    72  		"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456" +
    73  		"78901234567890123456789012345678901234567890")
    74  	testutil.AssertError(t, err, fmt.Sprintf("Error should have been thrown for an invalid name"))
    75  
    76  }