public void MassUpdate_UpdateNothing_InCaseOfAnyError() { var cmd = DbConnector.CreateDbCommand("select max(content_item_id) from content_item"); var maxId = (decimal)DbConnector.GetRealScalarData(cmd); var values = new List <Dictionary <string, string> >(); var article1 = new Dictionary <string, string> { [FieldName.ContentItemId] = BaseArticlesIds[0].ToString(), ["Title"] = "Name5" }; values.Add(article1); var article2 = new Dictionary <string, string> { [FieldName.ContentItemId] = BaseArticlesIds[1].ToString(), ["Title"] = "Name5" }; values.Add(article2); Assert.That(() => { DbConnector.MassUpdate(ContentId, values, 1); }, Throws.Exception); ; var maxIdAfter = (decimal)DbConnector.GetRealScalarData(cmd); var titles = Global.GetTitles(DbConnector, ContentId); Assert.That(titles, Does.Not.Contain("Name5"), "In case of any error the internal transaction should be rolled back"); Assert.That(maxId, Is.EqualTo(maxIdAfter), "No new content items"); }
public void AddFormToContent_ThrowsException_ValidateConstraintForDataConflict() { var titleName = DbConnector.FieldName(Global.SiteId, ContentName, "Title"); var numberName = DbConnector.FieldName(Global.SiteId, ContentName, "Number"); var fn = SqlQuerySyntaxHelper.FieldName(DbConnector.DatabaseType, "Title"); var cmd = DbConnector.CreateDbCommand($"select content_item_id from content_{ContentId}_united where {fn} <> 'Name2'"); var id = (int)(decimal)DbConnector.GetRealScalarData(cmd); var article1 = new Hashtable { [titleName] = "Name2", [numberName] = "9,5" }; Assert.That(() => { id = DbConnector.AddFormToContent(Global.SiteId, ContentName, "Published", ref article1, id); }, Throws.Exception.TypeOf <QpInvalidAttributeException>().And.Message.Contains("Unique constraint violation"), "Duplicate of test data should violate rules"); }
public void MassUpdate_ThrowsException_ValidateConstraintForDataConflict() { var values = new List <Dictionary <string, string> >(); var fn = SqlQuerySyntaxHelper.FieldName(DbConnector.DatabaseType, "Title"); var cmd = DbConnector.CreateDbCommand($"select content_item_id from content_{ContentId}_united where {fn} <> 'Name2'"); var id = (decimal)DbConnector.GetRealScalarData(cmd); var article1 = new Dictionary <string, string> { [FieldName.ContentItemId] = id.ToString(CultureInfo.InvariantCulture), ["Title"] = "Name2", ["Number"] = "9,5" }; values.Add(article1); Assert.That(() => DbConnector.MassUpdate(ContentId, values, 1), Throws.Exception.TypeOf <QpInvalidAttributeException>().And.Message.Contains("for content articles"), "Duplicate of test data should violate rules"); }
public DbCommand GetDbCommand() { int contentId; var siteId = 0; if (ContentId != 0) { contentId = ContentId; } else { siteId = DbConnector.GetSiteId(SiteName); if (siteId == 0) { throw new ApplicationException($"Site '{SiteName}' is not found"); } contentId = DbConnector.GetContentId(DbConnector.GetSiteId(SiteName), ContentName); if (contentId == 0) { throw new ApplicationException($"Content '{SiteName}.{ContentName}' is not found"); } } var select = GetSqlCommandSelect(contentId); var from = GetSqlCommandFrom(contentId); if (UseSecurity) { from = from.Replace(InsertKey, DbConnector.GetSecuritySql(contentId, UserId, GroupId, StartLevel, EndLevel)); } var cmd = DbConnector.CreateDbCommand(); var where = GetSqlCommandWhere(siteId, cmd); var orderBy = GetSqlCommandOrderBy(); var startRow = StartRowExpression <= 0 ? 1 : StartRowExpression; var endRow = new long[] { 0, int.MaxValue, int.MaxValue - 1 }.Contains(PageSizeExpression) ? 0 : startRow + PageSizeExpression - 1; CountSql = $"SELECT cast(COUNT(*) as bigint) FROM {from} WHERE {where}"; var sb = new StringBuilder(); sb.AppendLine($@"SELECT {select} FROM {from} WHERE {where} ORDER BY {orderBy}"); if (endRow > 0 || startRow > 1) { cmd.Parameters.AddWithValue("@startRow", startRow - 1); if (endRow != int.MaxValue) { cmd.Parameters.AddWithValue("@endRow", endRow); sb.AppendLine(DbConnector.DatabaseType == DatabaseType.SqlServer ? @"OFFSET @startRow ROWS FETCH NEXT @endRow - @startRow ROWS ONLY" : @"LIMIT @endRow - @startRow OFFSET @startRow" ); } else { if (startRow > 1) { sb.AppendLine(DbConnector.DatabaseType == DatabaseType.SqlServer ? @"OFFSET @startRow ROWS" : @"OFFSET @startRow" ); } } } if (Parameters != null) { foreach (var param in Parameters) { cmd.Parameters.Add(param); } } cmd.CommandText = sb.ToString(); return(cmd); }