Exemple #1
0
func TestGetColumns(t *testing.T) {
	// @todo fix test data retrieving from database ...
	dbc := csdb.MustConnectTest()
	defer dbc.Close()
	sess := dbc.NewSession()

	tests := []struct {
		table          string
		want           string
		wantErr        error
		wantJoinFields string
	}{
		{"core_config_data",
			"csdb.Column{Field:dbr.InitNullString(`config_id`, true), Type:dbr.InitNullString(`int(10) unsigned`, true), Null:dbr.InitNullString(`NO`, true), Key:dbr.InitNullString(`PRI`, true), Default:dbr.InitNullString(``, false), Extra:dbr.InitNullString(`auto_increment`, true)},\ncsdb.Column{Field:dbr.InitNullString(`scope`, true), Type:dbr.InitNullString(`varchar(8)`, true), Null:dbr.InitNullString(`NO`, true), Key:dbr.InitNullString(`MUL`, true), Default:dbr.InitNullString(`default`, true), Extra:dbr.InitNullString(``, true)},\ncsdb.Column{Field:dbr.InitNullString(`scope_id`, true), Type:dbr.InitNullString(`int(11)`, true), Null:dbr.InitNullString(`NO`, true), Key:dbr.InitNullString(``, true), Default:dbr.InitNullString(`0`, true), Extra:dbr.InitNullString(``, true)},\ncsdb.Column{Field:dbr.InitNullString(`path`, true), Type:dbr.InitNullString(`varchar(255)`, true), Null:dbr.InitNullString(`NO`, true), Key:dbr.InitNullString(``, true), Default:dbr.InitNullString(`general`, true), Extra:dbr.InitNullString(``, true)},\ncsdb.Column{Field:dbr.InitNullString(`value`, true), Type:dbr.InitNullString(`text`, true), Null:dbr.InitNullString(`YES`, true), Key:dbr.InitNullString(``, true), Default:dbr.InitNullString(``, false), Extra:dbr.InitNullString(``, true)}\n",
			nil,
			"config_id_scope_scope_id_path_value",
		},
		{"catalog_category_product",
			"csdb.Column{Field:dbr.InitNullString(`category_id`, true), Type:dbr.InitNullString(`int(10) unsigned`, true), Null:dbr.InitNullString(`NO`, true), Key:dbr.InitNullString(`PRI`, true), Default:dbr.InitNullString(`0`, true), Extra:dbr.InitNullString(``, true)},\ncsdb.Column{Field:dbr.InitNullString(`product_id`, true), Type:dbr.InitNullString(`int(10) unsigned`, true), Null:dbr.InitNullString(`NO`, true), Key:dbr.InitNullString(`PRI`, true), Default:dbr.InitNullString(`0`, true), Extra:dbr.InitNullString(``, true)},\ncsdb.Column{Field:dbr.InitNullString(`position`, true), Type:dbr.InitNullString(`int(11)`, true), Null:dbr.InitNullString(`NO`, true), Key:dbr.InitNullString(``, true), Default:dbr.InitNullString(`0`, true), Extra:dbr.InitNullString(``, true)}\n",
			nil,
			"category_id_product_id_position",
		},
		{"non_existent",
			"",
			errors.New("non_existent"),
			"",
		},
	}

	for _, test := range tests {
		cols1, err1 := csdb.GetColumns(sess, test.table)
		if test.wantErr != nil {
			assert.Error(t, err1)
			assert.Contains(t, err1.Error(), test.wantErr.Error())
			//t.Logf("%s\n%#v\n", err1.Error(), err1.(errgo.Locationer).Location())
		} else {
			assert.NoError(t, err1)
			assert.Equal(t, test.want, fmt.Sprintf("%#v\n", cols1))
			assert.Equal(t, test.wantJoinFields, cols1.JoinFields("_"))
		}
	}
}
Exemple #2
0
// BenchmarkGetColumns-4	    1000	   3376128 ns/op	   24198 B/op	     196 allocs/op
// BenchmarkGetColumns-4	    1000	   1185381 ns/op	   21861 B/op	     179 allocs/op
func BenchmarkGetColumns(b *testing.B) {
	dbc := csdb.MustConnectTest()
	defer dbc.Close()
	sess := dbc.NewSession()
	var err error
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		benchmarkGetColumns, err = csdb.GetColumns(sess, "eav_attribute")
		if err != nil {
			b.Error(err)
		}
	}
	hashHave, err := benchmarkGetColumns.Hash()
	if err != nil {
		b.Error(err)
	}
	if 0 != bytes.Compare(hashHave, benchmarkGetColumnsHashWant) {
		b.Errorf("\nHave %#v\nWant %#v\n", hashHave, benchmarkGetColumnsHashWant)
	}
	//	b.Log(benchmarkGetColumns.GoString())
}