func TestGetAttributeSelectSql(t *testing.T) { dbc := csdb.MustConnectTest() defer dbc.Close() dbrSess := dbc.NewSession() dbrSelect, err := eav.GetAttributeSelectSql(dbrSess, codegen.NewAddAttrTables(dbc.DB, "customer"), 1, 4) if err != nil { t.Fatal(err) } sql, _, err := dbrSelect.ToSql() assert.NoError(t, err) _, err = sqlbeautifier.FromString(sql) // check for syntax errors if err != nil { t.Fatalf("%s\n\n%s\n", err, sql) } if testWantGetAttributeSelectSql != sql { buf, err := sqlbeautifier.FromString(testWantGetAttributeSelectSql) if err != nil { t.Fatalf("%s\n%s\n", err, testWantGetAttributeSelectSql) } sql = sqlbeautifier.MustFromString(sql) println(diff.MustUnified(buf.String(), sql), "\n") t.Fatal(sql) } // @todo error is that we have column attribute_model in the select list but it should not occur // because in codegen it is defined that this column has no usage so we can skip it. }
func TestGetAttributeSelectSql(t *testing.T) { db := csdb.MustConnectTest() defer db.Close() dbrSess := dbr.NewConnection(db, nil).NewSession(nil) dbrSelect, err := eav.GetAttributeSelectSql(dbrSess, NewAddAttrTables(db, "customer"), 1, 4) if err != nil { t.Error(err) } sql, _ := dbrSelect.ToSql() assert.Equal(t, "SELECT `main_table`.`attribute_id`, `main_table`.`entity_type_id`, `main_table`.`attribute_code`, `main_table`.`backend_model`, `main_table`.`backend_type`, `main_table`.`backend_table`, `main_table`.`frontend_model`, `main_table`.`frontend_input`, `main_table`.`frontend_label`, `main_table`.`frontend_class`, `main_table`.`source_model`, `main_table`.`is_user_defined`, `main_table`.`is_unique`, `main_table`.`note`, `additional_table`.`input_filter`, `additional_table`.`validate_rules`, `additional_table`.`is_system`, `additional_table`.`sort_order`, `additional_table`.`data_model`, `additional_table`.`is_used_for_customer_segment`, IFNULL(`scope_table`.`is_visible`, `additional_table`.`is_visible`) AS `is_visible`, IFNULL(`scope_table`.`is_required`, `main_table`.`is_required`) AS `is_required`, IFNULL(`scope_table`.`default_value`, `main_table`.`default_value`) AS `default_value`, IFNULL(`scope_table`.`multiline_count`, `additional_table`.`multiline_count`) AS `multiline_count` FROM `eav_attribute` AS `main_table` INNER JOIN `customer_eav_attribute` AS `additional_table` ON (`additional_table`.`attribute_id` = `main_table`.`attribute_id`) AND (`main_table`.`entity_type_id` = ?) LEFT JOIN `customer_eav_attribute_website` AS `scope_table` ON (`scope_table`.`attribute_id` = `main_table`.`attribute_id`) AND (`scope_table`.`website_id` = ?)", sql) }
// depends on generated code func TestSQLQueryToColumnsToStruct(t *testing.T) { db := csdb.MustConnectTest() defer db.Close() dbrSess := dbr.NewConnection(db, nil).NewSession(nil) dbrSelect, err := eav.GetAttributeSelectSql(dbrSess, NewAddAttrTables(db, "catalog_product"), 4, 0) if err != nil { t.Error(err) } colSliceDbr, err := SQLQueryToColumns(db, dbrSelect) if err != nil { t.Error(err) } assert.Len(t, colSliceDbr, 35) for _, col := range colSliceDbr { assert.True(t, col.Field.Valid, fmt.Sprintf("%#v", col)) assert.True(t, col.Type.Valid, fmt.Sprintf("%#v", col)) } columns2, err2 := SQLQueryToColumns(db, nil, "SELECT * FROM `catalog_product_option`", " ORDER BY option_id DESC") if err2 != nil { t.Error(err2) } assert.Len(t, columns2, 10) for _, col := range columns2 { assert.True(t, col.Field.Valid, fmt.Sprintf("%#v", col)) assert.True(t, col.Type.Valid, fmt.Sprintf("%#v", col)) } colSliceDbr.MapSQLToGoDBRType() code, err := ColumnsToStructCode(nil, "testStruct", colSliceDbr) if err != nil { t.Error(err, "\n", string(code)) } checkContains := [][]byte{ []byte(`TeststructSlice`), []byte(`dbr.NullString`), []byte("`db:\"is_visible_in_advanced_search\"`"), } for _, s := range checkContains { if false == bytes.Contains(code, s) { t.Errorf("%s\ndoes not contain %s", code, s) } } }
func TestGetAttributeSelect(t *testing.T) { db := csdb.MustConnectTest() defer db.Close() dbrSess := dbr.NewConnection(db, nil).NewSession(nil) et, err := eav.GetEntityTypeCollection().GetByCode("catalog_product") if err != nil { t.Error(err) } websiteID := int64(1) dbrSelect, err := eav.GetAttributeSelectSql(dbrSess, et.AdditionalAttributeTable, et.EntityTypeID, websiteID) if err != nil { t.Error(err) } sql, _ := dbrSelect.ToSql() assert.Equal( t, "SELECT `main_table`.`attribute_id`, `main_table`.`entity_type_id`, `main_table`.`attribute_code`, `main_table`.`backend_model`, `main_table`.`backend_type`, `main_table`.`backend_table`, `main_table`.`frontend_model`, `main_table`.`frontend_input`, `main_table`.`frontend_label`, `main_table`.`frontend_class`, `main_table`.`source_model`, `main_table`.`is_required`, `main_table`.`is_user_defined`, `main_table`.`default_value`, `main_table`.`is_unique`, `main_table`.`note`, `additional_table`.`frontend_input_renderer`, `additional_table`.`is_global`, `additional_table`.`is_visible`, `additional_table`.`is_searchable`, `additional_table`.`is_filterable`, `additional_table`.`is_comparable`, `additional_table`.`is_visible_on_front`, `additional_table`.`is_html_allowed_on_front`, `additional_table`.`is_used_for_price_rules`, `additional_table`.`is_filterable_in_search`, `additional_table`.`used_in_product_listing`, `additional_table`.`used_for_sort_by`, `additional_table`.`is_configurable`, `additional_table`.`apply_to`, `additional_table`.`is_visible_in_advanced_search`, `additional_table`.`position`, `additional_table`.`is_wysiwyg_enabled`, `additional_table`.`is_used_for_promo_rules`, `additional_table`.`search_weight` FROM `eav_attribute` AS `main_table` INNER JOIN `catalog_eav_attribute` AS `additional_table` ON (`additional_table`.`attribute_id` = `main_table`.`attribute_id`) AND (`main_table`.`entity_type_id` = ?)", sql, ) et, err = eav.GetEntityTypeCollection().GetByCode("customer") if err != nil { t.Error(err) } dbrSelect, err = eav.GetAttributeSelectSql(dbrSess, et.AdditionalAttributeTable, et.EntityTypeID, websiteID) if err != nil { t.Error(err) } sql, _ = dbrSelect.ToSql() assert.Equal( t, "SELECT `main_table`.`attribute_id`, `main_table`.`entity_type_id`, `main_table`.`attribute_code`, `main_table`.`backend_model`, `main_table`.`backend_type`, `main_table`.`backend_table`, `main_table`.`frontend_model`, `main_table`.`frontend_input`, `main_table`.`frontend_label`, `main_table`.`frontend_class`, `main_table`.`source_model`, `main_table`.`is_required`, `main_table`.`is_user_defined`, `main_table`.`default_value`, `main_table`.`is_unique`, `main_table`.`note`, `additional_table`.`is_visible`, `additional_table`.`input_filter`, `additional_table`.`multiline_count`, `additional_table`.`validate_rules`, `additional_table`.`is_system`, `additional_table`.`sort_order`, `additional_table`.`data_model`, `additional_table`.`is_used_for_customer_segment`, `scope_table`.`is_visible` AS `scope_is_visible`, `scope_table`.`is_required` AS `scope_is_required`, `scope_table`.`default_value` AS `scope_default_value`, `scope_table`.`multiline_count` AS `scope_multiline_count` FROM `eav_attribute` AS `main_table` INNER JOIN `customer_eav_attribute` AS `additional_table` ON (`additional_table`.`attribute_id` = `main_table`.`attribute_id`) AND (`main_table`.`entity_type_id` = ?) LEFT JOIN `customer_eav_attribute_website` AS `scope_table` ON (`scope_table`.`attribute_id` = `main_table`.`attribute_id`) AND (`scope_table`.`website_id` = ?)", sql, ) }
func TestGetAttributeSelectSql(t *testing.T) { dbc := csdb.MustConnectTest() defer dbc.Close() dbrSess := dbc.NewSession() dbrSelect, err := eav.GetAttributeSelectSql(dbrSess, codegen.NewAddAttrTables(dbc.DB, "customer"), 1, 4) if err != nil { t.Error(err) } else { sql, _ := dbrSelect.ToSql() assert.Equal(t, "SELECT `main_table`.`attribute_id`, `main_table`.`entity_type_id`, `main_table`.`attribute_code`, `main_table`.`backend_model`, `main_table`.`backend_type`, `main_table`.`backend_table`, `main_table`.`frontend_model`, `main_table`.`frontend_input`, `main_table`.`frontend_label`, `main_table`.`frontend_class`, `main_table`.`source_model`, `main_table`.`is_user_defined`, `main_table`.`is_unique`, `main_table`.`note`, `additional_table`.`input_filter`, `additional_table`.`validate_rules`, `additional_table`.`is_system`, `additional_table`.`sort_order`, `additional_table`.`data_model`, IFNULL(`scope_table`.`is_visible`, `additional_table`.`is_visible`) AS `is_visible`, IFNULL(`scope_table`.`is_required`, `main_table`.`is_required`) AS `is_required`, IFNULL(`scope_table`.`default_value`, `main_table`.`default_value`) AS `default_value`, IFNULL(`scope_table`.`multiline_count`, `additional_table`.`multiline_count`) AS `multiline_count` FROM `eav_attribute` AS `main_table` INNER JOIN `customer_eav_attribute` AS `additional_table` ON (`additional_table`.`attribute_id` = `main_table`.`attribute_id`) AND (`main_table`.`entity_type_id` = ?) LEFT JOIN `customer_eav_attribute_website` AS `scope_table` ON (`scope_table`.`attribute_id` = `main_table`.`attribute_id`) AND (`scope_table`.`website_id` = ?)", sql) } // @todo error is that we have column attribute_model in the select list but it should not occur // because in codegen it is defined that this column has no usage so we can skip it. }
func getAttrSelect(ctx *context, websiteID int64) *dbr.SelectBuilder { dbrSelect, err := eav.GetAttributeSelectSql( ctx.dbrConn.NewSession(nil), ctx.aat, ctx.et.EntityTypeID, websiteID, ) codegen.LogFatal(err) dbrSelect.OrderDir(csdb.MainTable+".attribute_code", true) tew, err := ctx.aat.TableEavWebsite() codegen.LogFatal(err) if websiteID > 0 && tew != nil { // only here in codegen used to detect any changes if an attribute value will be overridden by a website ID dbrSelect.Where(csdb.ScopeTable + ".website_id IS NOT NULL") dbrSelect.Columns = append(dbrSelect.Columns, csdb.ScopeTable+".website_id") } return dbrSelect }
func TestGetSQLPrepareForTemplate(t *testing.T) { db := csdb.MustConnectTest() defer db.Close() resultSlice2, err := LoadStringEntities(db, nil, "SELECT * FROM `cataloginventory_stock` ", "ORDER BY stock_id") if err != nil { t.Error(err) } assert.Len(t, resultSlice2, 1) // 1 row for _, row := range resultSlice2 { assert.True(t, len(row["stock_id"]) > 0, "Incorrect length of stock_id", fmt.Sprintf("%#v", row)) } // advanced test dbrSess := dbr.NewConnection(db, nil).NewSession(nil) dbrSelect, err := eav.GetAttributeSelectSql(dbrSess, NewAddAttrTables(db, "catalog_product"), 4, 0) if err != nil { t.Error(err) } attributeResultSlice, err := LoadStringEntities(db, dbrSelect) if err != nil { t.Error(err) } assert.Len(t, attributeResultSlice, 110) // 110 rows for _, row := range attributeResultSlice { assert.True(t, len(row["attribute_id"]) > 0, "Incorrect length of attribute_id", fmt.Sprintf("%#v", row)) } colSliceDbr, err := SQLQueryToColumns(db, dbrSelect) if err != nil { t.Error(err) } for _, col := range colSliceDbr { assert.Empty(t, col.GoType) assert.Empty(t, col.GoName) } var unchanged = make(map[string]string) for _, s := range attributeResultSlice { assert.True(t, len(s["is_wysiwyg_enabled"]) == 1, "Should contain 0 or 1 as string: %s", s["is_wysiwyg_enabled"]) assert.True(t, len(s["used_in_product_listing"]) == 1, "Should contain 0 or 1 as string: %s", s["used_in_product_listing"]) assert.False(t, strings.ContainsRune(s["attribute_code"], '"'), "Should not contain double quotes for escaping: %s", s["attribute_code"]) unchanged[s["attribute_id"]] = s["entity_type_id"] } importPaths1 := PrepareForTemplate(colSliceDbr, attributeResultSlice, ConfigAttributeModel, "catalog") assert.True(t, len(importPaths1) > 1, "Should output multiple import paths: %#v", importPaths1) for _, s := range attributeResultSlice { assert.True(t, len(s["is_wysiwyg_enabled"]) >= 4, "Should contain false or true as string: %s", s["is_wysiwyg_enabled"]) assert.True(t, len(s["used_in_product_listing"]) >= 4, "Should contain false or true as string: %s", s["used_in_product_listing"]) assert.True(t, strings.ContainsRune(s["attribute_code"], '"'), "Should contain double quotes for escaping: %s", s["attribute_code"]) assert.Equal(t, unchanged[s["attribute_id"]], s["entity_type_id"], "Columns: %#v", s) assert.True(t, len(s["frontend_model"]) >= 3, "Should contain nil or a Go func: %s", s["frontend_model"]) assert.True(t, len(s["backend_model"]) >= 3, "Should contain nil or a Go func: %s", s["backend_model"]) assert.True(t, len(s["source_model"]) >= 3, "Should contain nil or a Go func: %s", s["source_model"]) } }