github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/go-xorm/core/table_test.go (about)

     1  package core
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  var testsGetColumn = []struct {
     9  	name string
    10  	idx  int
    11  }{
    12  	{"Id", 0},
    13  	{"Deleted", 0},
    14  	{"Caption", 0},
    15  	{"Code_1", 0},
    16  	{"Code_2", 0},
    17  	{"Code_3", 0},
    18  	{"Parent_Id", 0},
    19  	{"Latitude", 0},
    20  	{"Longitude", 0},
    21  }
    22  
    23  var table *Table
    24  
    25  func init() {
    26  
    27  	table = NewEmptyTable()
    28  
    29  	var name string
    30  
    31  	for i := 0; i < len(testsGetColumn); i++ {
    32  		// as in Table.AddColumn func
    33  		name = strings.ToLower(testsGetColumn[i].name)
    34  
    35  		table.columnsMap[name] = append(table.columnsMap[name], &Column{})
    36  	}
    37  }
    38  
    39  func TestGetColumn(t *testing.T) {
    40  
    41  	for _, test := range testsGetColumn {
    42  		if table.GetColumn(test.name) == nil {
    43  			t.Error("Column not found!")
    44  		}
    45  	}
    46  }
    47  
    48  func TestGetColumnIdx(t *testing.T) {
    49  
    50  	for _, test := range testsGetColumn {
    51  		if table.GetColumnIdx(test.name, test.idx) == nil {
    52  			t.Errorf("Column %s with idx %d not found!", test.name, test.idx)
    53  		}
    54  	}
    55  }
    56  
    57  func BenchmarkGetColumnWithToLower(b *testing.B) {
    58  
    59  	for i := 0; i < b.N; i++ {
    60  		for _, test := range testsGetColumn {
    61  
    62  			if _, ok := table.columnsMap[strings.ToLower(test.name)]; !ok {
    63  				b.Errorf("Column not found:%s", test.name)
    64  			}
    65  		}
    66  	}
    67  }
    68  
    69  func BenchmarkGetColumnIdxWithToLower(b *testing.B) {
    70  
    71  	for i := 0; i < b.N; i++ {
    72  		for _, test := range testsGetColumn {
    73  
    74  			if c, ok := table.columnsMap[strings.ToLower(test.name)]; ok {
    75  				if test.idx < len(c) {
    76  					continue
    77  				} else {
    78  					b.Errorf("Bad idx in: %s, %d", test.name, test.idx)
    79  				}
    80  			} else {
    81  				b.Errorf("Column not found: %s, %d", test.name, test.idx)
    82  			}
    83  		}
    84  	}
    85  }
    86  
    87  func BenchmarkGetColumn(b *testing.B) {
    88  
    89  	for i := 0; i < b.N; i++ {
    90  		for _, test := range testsGetColumn {
    91  			if table.GetColumn(test.name) == nil {
    92  				b.Errorf("Column not found:%s", test.name)
    93  			}
    94  		}
    95  	}
    96  }
    97  
    98  func BenchmarkGetColumnIdx(b *testing.B) {
    99  
   100  	for i := 0; i < b.N; i++ {
   101  		for _, test := range testsGetColumn {
   102  			if table.GetColumnIdx(test.name, test.idx) == nil {
   103  				b.Errorf("Column not found:%s, %d", test.name, test.idx)
   104  			}
   105  		}
   106  	}
   107  }