github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/go-xorm/xorm/statement_test.go (about) 1 package xorm 2 3 import ( 4 "reflect" 5 "sync" 6 "testing" 7 "time" 8 9 "strings" 10 11 "github.com/insionng/yougam/libraries/go-xorm/core" 12 ) 13 14 var colStrTests = []struct { 15 omitColumn string 16 onlyToDBColumnNdx int 17 expected string 18 }{ 19 {"", -1, "`ID`, `IsDeleted`, `Caption`, `Code1`, `Code2`, `Code3`, `ParentID`, `Latitude`, `Longitude`"}, 20 {"Code2", -1, "`ID`, `IsDeleted`, `Caption`, `Code1`, `Code3`, `ParentID`, `Latitude`, `Longitude`"}, 21 {"", 1, "`ID`, `Caption`, `Code1`, `Code2`, `Code3`, `ParentID`, `Latitude`, `Longitude`"}, 22 {"Code3", 1, "`ID`, `Caption`, `Code1`, `Code2`, `ParentID`, `Latitude`, `Longitude`"}, 23 {"Longitude", 1, "`ID`, `Caption`, `Code1`, `Code2`, `Code3`, `ParentID`, `Latitude`"}, 24 {"", 8, "`ID`, `IsDeleted`, `Caption`, `Code1`, `Code2`, `Code3`, `ParentID`, `Latitude`"}, 25 } 26 27 // !nemec784! Only for Statement object creation 28 const driverName = "mysql" 29 const dataSourceName = "Server=TestServer;Database=TestDB;Uid=testUser;Pwd=testPassword;" 30 31 func init() { 32 core.RegisterDriver(driverName, &mysqlDriver{}) 33 } 34 35 func TestColumnsStringGeneration(t *testing.T) { 36 37 var statement *Statement 38 39 for ndx, testCase := range colStrTests { 40 41 statement = createTestStatement() 42 43 if testCase.omitColumn != "" { 44 statement.Omit(testCase.omitColumn) // !nemec784! Column must be skipped 45 } 46 47 if testCase.onlyToDBColumnNdx >= 0 { 48 columns := statement.RefTable.Columns() 49 columns[testCase.onlyToDBColumnNdx].MapType = core.ONLYTODB // !nemec784! Column must be skipped 50 } 51 52 actual := statement.genColumnStr() 53 54 if actual != testCase.expected { 55 t.Errorf("[test #%d] Unexpected columns string:\nwant:\t%s\nhave:\t%s", ndx, testCase.expected, actual) 56 } 57 } 58 } 59 60 func BenchmarkColumnsStringGeneration(b *testing.B) { 61 62 b.StopTimer() 63 64 statement := createTestStatement() 65 66 testCase := colStrTests[0] 67 68 if testCase.omitColumn != "" { 69 statement.Omit(testCase.omitColumn) // !nemec784! Column must be skipped 70 } 71 72 if testCase.onlyToDBColumnNdx >= 0 { 73 columns := statement.RefTable.Columns() 74 columns[testCase.onlyToDBColumnNdx].MapType = core.ONLYTODB // !nemec784! Column must be skipped 75 } 76 77 b.StartTimer() 78 79 for i := 0; i < b.N; i++ { 80 actual := statement.genColumnStr() 81 82 if actual != testCase.expected { 83 b.Errorf("Unexpected columns string:\nwant:\t%s\nhave:\t%s", testCase.expected, actual) 84 } 85 } 86 } 87 88 func BenchmarkGetFlagForColumnWithICKey_ContainsKey(b *testing.B) { 89 90 b.StopTimer() 91 92 mapCols := make(map[string]bool) 93 cols := []*core.Column{ 94 &core.Column{Name: `ID`}, 95 &core.Column{Name: `IsDeleted`}, 96 &core.Column{Name: `Caption`}, 97 &core.Column{Name: `Code1`}, 98 &core.Column{Name: `Code2`}, 99 &core.Column{Name: `Code3`}, 100 &core.Column{Name: `ParentID`}, 101 &core.Column{Name: `Latitude`}, 102 &core.Column{Name: `Longitude`}, 103 } 104 105 for _, col := range cols { 106 mapCols[strings.ToLower(col.Name)] = true 107 } 108 109 b.StartTimer() 110 111 for i := 0; i < b.N; i++ { 112 113 for _, col := range cols { 114 115 if _, ok := getFlagForColumn(mapCols, col); !ok { 116 b.Fatal("Unexpected result") 117 } 118 } 119 } 120 } 121 122 func BenchmarkGetFlagForColumnWithICKey_EmptyMap(b *testing.B) { 123 124 b.StopTimer() 125 126 mapCols := make(map[string]bool) 127 cols := []*core.Column{ 128 &core.Column{Name: `ID`}, 129 &core.Column{Name: `IsDeleted`}, 130 &core.Column{Name: `Caption`}, 131 &core.Column{Name: `Code1`}, 132 &core.Column{Name: `Code2`}, 133 &core.Column{Name: `Code3`}, 134 &core.Column{Name: `ParentID`}, 135 &core.Column{Name: `Latitude`}, 136 &core.Column{Name: `Longitude`}, 137 } 138 139 b.StartTimer() 140 141 for i := 0; i < b.N; i++ { 142 143 for _, col := range cols { 144 145 if _, ok := getFlagForColumn(mapCols, col); ok { 146 b.Fatal("Unexpected result") 147 } 148 } 149 } 150 } 151 152 type TestType struct { 153 ID int64 `xorm:"ID PK"` 154 IsDeleted bool `xorm:"IsDeleted"` 155 Caption string `xorm:"Caption"` 156 Code1 string `xorm:"Code1"` 157 Code2 string `xorm:"Code2"` 158 Code3 string `xorm:"Code3"` 159 ParentID int64 `xorm:"ParentID"` 160 Latitude float64 `xorm:"Latitude"` 161 Longitude float64 `xorm:"Longitude"` 162 } 163 164 func (TestType) TableName() string { 165 return "TestTable" 166 } 167 168 func createTestStatement() *Statement { 169 170 engine := createTestEngine() 171 172 statement := &Statement{} 173 statement.Init() 174 statement.Engine = engine 175 statement.setRefValue(reflect.ValueOf(TestType{})) 176 177 return statement 178 } 179 180 func createTestEngine() *Engine { 181 driver := core.QueryDriver(driverName) 182 uri, err := driver.Parse(driverName, dataSourceName) 183 184 if err != nil { 185 panic(err) 186 } 187 188 dialect := &mysql{} 189 err = dialect.Init(nil, uri, driverName, dataSourceName) 190 191 if err != nil { 192 panic(err) 193 } 194 195 engine := &Engine{ 196 dialect: dialect, 197 Tables: make(map[reflect.Type]*core.Table), 198 mutex: &sync.RWMutex{}, 199 TagIdentifier: "xorm", 200 TZLocation: time.Local, 201 } 202 engine.SetMapper(core.NewCacheMapper(new(core.SnakeMapper))) 203 204 return engine 205 }