Пример #1
0
func TestDBStorageMultipleStmt(t *testing.T) {
	debugLogBuf.Reset()
	defer debugLogBuf.Reset() // contains only data from the debug level, info level will be dumped to os.Stdout
	defer infoLogBuf.Reset()

	if testing.Short() {
		t.Skip("Test skipped in short mode")
	}
	dbc := csdb.MustConnectTest()
	defer func() { assert.NoError(t, dbc.Close()) }()

	sdb := config.MustNewDBStorage(dbc.DB)
	sdb.All.Idle = time.Second * 1
	sdb.Read.Idle = time.Second * 1
	sdb.Write.Idle = time.Second * 1

	sdb.Start()

	tests := []struct {
		key       string
		value     interface{}
		wantValue string
	}{
		{"websites/10/testDBStorage/secure/base_url", "http://corestore.io", "http://corestore.io"},
		{"websites/10/testDBStorage/log/active", 1, "1"},
		{"websites/20/testDBStorage/log/clean", 19.999, "19.999"},
		{"websites/20/testDBStorage/product/shipping", 29.999, "29.999"},
		{"default/0/testDBStorage/checkout/multishipping", false, "false"},
	}
	for i, test := range tests {
		sdb.Set(test.key, test.value)
		assert.Exactly(t, test.wantValue, sdb.Get(test.key), "Test: %v", test)
		if i < 2 {
			// last two iterations reopen a new statement, not closing it and reusing it
			time.Sleep(time.Millisecond * 1500) // trigger ticker to close statements
		}
	}

	for i, test := range tests {
		ak := util.StringSlice(sdb.AllKeys())
		assert.True(t, ak.Include(test.key), "Missing Key: %s", test.key)
		if i < 2 {
			time.Sleep(time.Millisecond * 1500) // trigger ticker to close statements
		}
	}
	assert.NoError(t, sdb.Stop())

	logStr := debugLogBuf.String()
	assert.Exactly(t, 3, strings.Count(logStr, `csdb.ResurrectStmt.stmt.Prepare SQL: "INSERT INTO`))
	assert.Exactly(t, 3, strings.Count(logStr, "csdb.ResurrectStmt.stmt.Prepare SQL: \"SELECT `value` FROM"))

	assert.Exactly(t, 4, strings.Count(logStr, `csdb.ResurrectStmt.stmt.Close SQL: "INSERT INTO`), "\n%s\n", logStr)
	assert.Exactly(t, 4, strings.Count(logStr, "csdb.ResurrectStmt.stmt.Close SQL: \"SELECT `value` FROM"))

	//println("\n", logStr, "\n")

	// 6 is: open close for iteration 0+1, open in iteration 2 and close in iteration 4
	assert.Exactly(t, 6, strings.Count(logStr, fmt.Sprintf("CONCAT(scope,'%s',scope_id,'%s',path) AS `fqpath`", scope.PS, scope.PS)))
}
Пример #2
0
func TestDBStorageOneStmt(t *testing.T) {
	debugLogBuf.Reset()
	defer debugLogBuf.Reset()
	defer infoLogBuf.Reset()

	dbc := csdb.MustConnectTest()
	defer func() { assert.NoError(t, dbc.Close()) }()

	sdb := config.MustNewDBStorage(dbc.DB).Start()

	// Stop() would only be called under rare circumstances on a production system
	defer func() { assert.NoError(t, sdb.Stop()) }()

	tests := []struct {
		key       string
		value     interface{}
		wantNil   bool
		wantValue string
	}{
		{"stores/1/testDBStorage/secure/base_url", "http://corestore.io", false, "http://corestore.io"},
		{"stores/1/testDBStorage/log/active", 1, false, "1"},
		{"stores/99999/testDBStorage/log/clean", 19.999, false, "19.999"},
		{"stores/99999/testDBStorage/log/clean", 29.999, false, "29.999"},
		{"default/1/testDBStorage/catalog/purge", true, false, "true"},
		{"default/1/testDBStorage/catalog/clean", 0, false, "0"},
	}
	for _, test := range tests {
		sdb.Set(test.key, test.value)
		if test.wantNil {
			assert.Nil(t, sdb.Get(test.key), "Test: %v", test)
		} else {
			assert.Exactly(t, test.wantValue, sdb.Get(test.key), "Test: %v", test)
		}
	}

	assert.Exactly(t, 1, strings.Count(debugLogBuf.String(), `csdb.ResurrectStmt.stmt.Prepare SQL: "INSERT INTO`))
	assert.Exactly(t, 1, strings.Count(debugLogBuf.String(), "csdb.ResurrectStmt.stmt.Prepare SQL: \"SELECT `value` FROM"))

	for _, test := range tests {
		ak := util.StringSlice(sdb.AllKeys()) // trigger many queries with one statement
		assert.True(t, ak.Include(test.key), "Missing Key: %s", test.key)
	}
	assert.Exactly(t, 1, strings.Count(debugLogBuf.String(), fmt.Sprintf("CONCAT(scope,'%s',scope_id,'%s',path) AS `fqpath`", scope.PS, scope.PS)))
}
Пример #3
0
// GetAttributeSelectSql generates the select query to retrieve full attribute configuration
// Implements the scope on a SQL query basis so that attribute functions does not need to deal with it.
// Tests see the tools package
// @see magento2/app/code/Magento/Eav/Model/Resource/Attribute/Collection.php::_initSelect()
func GetAttributeSelectSql(dbrSess dbr.SessionRunner, aat EntityTypeAdditionalAttributeTabler, entityTypeID, websiteID int64) (*dbr.SelectBuilder, error) {

	ta, err := TableCollection.Structure(TableIndexAttribute)
	if err != nil {
		if PkgLog.IsDebug() {
			PkgLog.Debug("eav.GetAttributeSelectSql.TableCollection.Structure", "err", err, "entityTypeID", entityTypeID, "websiteID", websiteID)
		}
		return nil, errgo.Mask(err)
	}
	taa, err := aat.TableAdditionalAttribute()
	if err != nil {
		if PkgLog.IsDebug() {
			PkgLog.Debug("eav.GetAttributeSelectSql.TableAdditionalAttribute", "err", err, "ta", ta, "entityTypeID", entityTypeID, "websiteID", websiteID)
		}
		return nil, errgo.Mask(err)
	}
	tew, err := aat.TableEavWebsite()
	if err != nil {
		if PkgLog.IsDebug() {
			PkgLog.Debug("eav.GetAttributeSelectSql.TableEavWebsite", "err", err, "ta", ta, "taa", taa, "entityTypeID", entityTypeID, "websiteID", websiteID)
		}
		return nil, errgo.Mask(err)
	}
	// tew table can now contains columns names which can occur in table eav_attribute and
	// or [catalog|customer|entity]_eav_attribute
	var (
		ifNull           []string
		tewAddedCols     []string
		taColumnsQuoted  = util.StringSlice(ta.AllColumnAliasQuote(csdb.MainTable))
		taaColumnsQuoted = util.StringSlice(taa.ColumnAliasQuote(csdb.AdditionalTable))
	)

	if tew != nil {
		ifNull = make([]string, len(tew.Columns.ColumnsNoPK().FieldNames()))
		for i, tewC := range tew.Columns.ColumnsNoPK().FieldNames() {
			t := ""
			switch {
			case ta.In(tewC):
				t = csdb.MainTable
				break
			case taa.In(tewC):
				t = csdb.AdditionalTable
				break
			default:
				err := errgo.Newf("Cannot find column name %s.%s neither in table %s nor in %s.", tew.Name, tewC, ta.Name, taa.Name)
				if PkgLog.IsDebug() {
					PkgLog.Debug("eav.GetAttributeSelectSql.Columns.FieldNames.default", "err", err, "ta", ta, "taa", taa, "entityTypeID", entityTypeID, "websiteID", websiteID)
				}
				return nil, err
			}
			ifNull[i] = dbr.IfNullAs(csdb.ScopeTable, tewC, t, tewC, tewC)
			tewAddedCols = append(tewAddedCols, tewC)
		}
		taColumnsQuoted.ReduceContains(tewAddedCols...)
		taaColumnsQuoted.ReduceContains(tewAddedCols...)
	}

	selectSql := dbrSess.
		Select(taColumnsQuoted...).
		From(ta.Name, csdb.MainTable).
		Join(
			dbr.JoinTable(taa.Name, csdb.AdditionalTable),
			taaColumnsQuoted,
			dbr.ConditionRaw(csdb.AdditionalTable+".attribute_id = "+csdb.MainTable+".attribute_id"),
			dbr.ConditionRaw(csdb.MainTable+".entity_type_id = ?", entityTypeID),
		)

	if len(tewAddedCols) > 0 {
		selectSql.
			LeftJoin(
				dbr.JoinTable(tew.Name, csdb.ScopeTable),
				ifNull,
				dbr.ConditionRaw(csdb.ScopeTable+"."+"attribute_id"+" = "+csdb.MainTable+"."+"attribute_id"),
				dbr.ConditionRaw(csdb.ScopeTable+"."+"website_id"+" = ?", websiteID),
			)
	}
	return selectSql, nil
}