Skip to content

Commit

Permalink
Bug: Operations are failing on the Unorganized Table - by Christian F…
Browse files Browse the repository at this point in the history
…ranck #262
  • Loading branch information
mikependon committed Jun 8, 2019
1 parent 5d7103e commit 5217208
Show file tree
Hide file tree
Showing 26 changed files with 1,309 additions and 72 deletions.
42 changes: 42 additions & 0 deletions RepoDb.Core/RepoDb.Tests/RepoDb.IntegrationTests/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,48 @@ public static TypeLevelMappedForStringEnumCompleteTable CreateTypeLevelMappedFor

#endregion

#region UnorganizedTable

/// <summary>
/// Creates a list of <see cref="UnorganizedTable"/> objects.
/// </summary>
/// <param name="count">The number of rows.</param>
/// <returns>A list of <see cref="UnorganizedTable"/> objects.</returns>
public static List<UnorganizedTable> CreateUnorganizedTables(int count)
{
var tables = new List<UnorganizedTable>();
for (var i = 0; i < count; i++)
{
var index = i + 1;
tables.Add(new UnorganizedTable
{
SessionId = Guid.NewGuid(),
ColumnDateTime2 = DateTime.UtcNow,
ColumnInt = index,
ColumnNVarChar = $"NVARCHAR{index}"
});
}
return tables;
}

/// <summary>
/// Creates an instance of <see cref="UnorganizedTable"/> object.
/// </summary>
/// <returns>A new created instance of <see cref="UnorganizedTable"/> object.</returns>
public static UnorganizedTable CreateUnorganizedTable()
{
var random = new Random();
return new UnorganizedTable
{
SessionId = Guid.NewGuid(),
ColumnDateTime2 = DateTime.UtcNow,
ColumnInt = random.Next(int.MinValue, int.MaxValue),
ColumnNVarChar = Guid.NewGuid().ToString()
};
}

#endregion

#region Dynamics

#region IdentityTable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using RepoDb.Attributes;
using System;

namespace RepoDb.IntegrationTests.Models
{
[Map("[dbo].[Unorganized Table]")]
public class UnorganizedTable
{
public long Id { get; set; }
[Map("Session Id")]
public Guid SessionId { get; set; }
[Map("Column Int")]
public int? ColumnInt { get; set; }
[Map("Column/NVarChar")]
public string ColumnNVarChar { get; set; }
[Map("Column.DateTime")]
public DateTime? ColumnDateTime2 { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RepoDb.IntegrationTests.Models;
using RepoDb.IntegrationTests.Setup;
using System;
using System.Data.SqlClient;
using System.Linq;

namespace RepoDb.IntegrationTests
{
[TestClass]
public class ObjectQuotationTest
{
[TestInitialize]
public void Initialize()
{
Database.Initialize();
Cleanup();
}

[TestCleanup]
public void Cleanup()
{
Database.Cleanup();
}

#region Delete

[TestMethod]
public void TestDeleteObjectQuotation()
{
// Setup
var entities = Helper.CreateUnorganizedTables(10);
var last = entities.Last();

using (var connection = new SqlConnection(Database.ConnectionStringForRepoDb))
{
// Act
var rowsInserted = connection.InsertAll<UnorganizedTable>(entities);
var deleteResult = connection.Delete<UnorganizedTable>(last.Id);

// Assert
Assert.AreEqual(1, deleteResult);
}
}

[TestMethod]
public void TestDeleteObjectQuotationViaNonAlphaNumericField()
{
// Setup
var entities = Helper.CreateUnorganizedTables(10);
var last = entities.Last();

using (var connection = new SqlConnection(Database.ConnectionStringForRepoDb))
{
// Act
var rowsInserted = connection.InsertAll<UnorganizedTable>(entities);
var deleteResult = connection.Delete<UnorganizedTable>(e => e.SessionId == last.SessionId);

// Assert
Assert.AreEqual(1, deleteResult);
}
}

#endregion

#region Insert

[TestMethod]
public void TestInsertObjectQuotation()
{
// Setup
var entity = Helper.CreateUnorganizedTable();

using (var connection = new SqlConnection(Database.ConnectionStringForRepoDb))
{
// Act
var id = connection.Insert<UnorganizedTable, long>(entity);

// Assert
Assert.IsNotNull(id);
Assert.IsTrue(id > 0);
Assert.AreEqual(1, connection.CountAll<UnorganizedTable>());
}
}

#endregion

#region InsertAll

[TestMethod]
public void TestInsertAllObjectQuotation()
{
// Setup
var entities = Helper.CreateUnorganizedTables(10);

using (var connection = new SqlConnection(Database.ConnectionStringForRepoDb))
{
// Act
var rowsInserted = connection.InsertAll<UnorganizedTable>(entities);

// Assert
Assert.AreEqual(entities.Count, rowsInserted);
Assert.AreEqual(entities.Count, connection.CountAll<UnorganizedTable>());
}
}

#endregion

#region Merge

[TestMethod]
public void TestMergeObjectQuotation()
{
// Setup
var entity = Helper.CreateUnorganizedTable();

using (var connection = new SqlConnection(Database.ConnectionStringForRepoDb))
{
// Act
var id = connection.Merge<UnorganizedTable, long>(entity);

// Assert
Assert.IsNotNull(id);
Assert.IsTrue(id > 0);
Assert.AreEqual(1, connection.CountAll<UnorganizedTable>());

// Setup
entity.ColumnDateTime2 = DateTime.UtcNow;
entity.ColumnInt = 2;
entity.ColumnNVarChar = Guid.NewGuid().ToString();

// Act
id = connection.Merge<UnorganizedTable, long>(entity);
var queryResult = connection.Query<UnorganizedTable>(id).First();

// Assert
Helper.AssertPropertiesEquality(entity, queryResult);
}
}

#endregion

#region MergeAll

[TestMethod]
public void TestMergeAllObjectQuotation()
{
// Setup
var entities = Helper.CreateUnorganizedTables(10);

using (var connection = new SqlConnection(Database.ConnectionStringForRepoDb))
{
// Act
var rowsAffected = connection.MergeAll<UnorganizedTable>(entities);

// Assert
Assert.AreEqual(entities.Count, rowsAffected);
Assert.AreEqual(entities.Count, connection.CountAll<UnorganizedTable>());

// Setup
entities.ForEach(entity =>
{
entity.ColumnDateTime2 = DateTime.UtcNow;
entity.ColumnInt = 2;
entity.ColumnNVarChar = Guid.NewGuid().ToString();
});

// Act
rowsAffected = connection.MergeAll<UnorganizedTable>(entities);

// Assert
Assert.AreEqual(entities.Count, rowsAffected);

// Act
var queryAllResult = connection.QueryAll<UnorganizedTable>();

// Assert
entities.ForEach(entity => Helper.AssertPropertiesEquality(entity, queryAllResult.First(item => item.Id == entity.Id)));
}
}

#endregion

#region Query

[TestMethod]
public void TestQueryObjectQuotation()
{
// Setup
var entities = Helper.CreateUnorganizedTables(10);
var last = entities.Last();

using (var connection = new SqlConnection(Database.ConnectionStringForRepoDb))
{
// Act
var rowsInserted = connection.InsertAll<UnorganizedTable>(entities);
var queryResult = connection.Query<UnorganizedTable>(last.Id).FirstOrDefault();

// Assert
Assert.IsNotNull(queryResult);
Helper.AssertPropertiesEquality(last, queryResult);
}
}

[TestMethod]
public void TestQueryObjectQuotationViaNonAlphaNumericField()
{
// Setup
var entities = Helper.CreateUnorganizedTables(10);
var last = entities.Last();

using (var connection = new SqlConnection(Database.ConnectionStringForRepoDb))
{
// Act
var rowsInserted = connection.InsertAll<UnorganizedTable>(entities);
var queryResult = connection.Query<UnorganizedTable>(e => e.SessionId == last.SessionId).FirstOrDefault();

// Assert
Assert.IsNotNull(queryResult);
Helper.AssertPropertiesEquality(last, queryResult);
}
}

#endregion

#region QueryAll

[TestMethod]
public void TestQueryAllObjectQuotation()
{
// Setup
var entities = Helper.CreateUnorganizedTables(10);

using (var connection = new SqlConnection(Database.ConnectionStringForRepoDb))
{
// Act
var rowsInserted = connection.InsertAll<UnorganizedTable>(entities);
var queryAllResult = connection.QueryAll<UnorganizedTable>();

// Assert
Assert.AreEqual(entities.Count, queryAllResult.Count());
entities.ForEach(entity => Helper.AssertPropertiesEquality(entity, queryAllResult.First(item => item.Id == entity.Id)));
}
}

#endregion

#region Update

[TestMethod]
public void TestUpdateObjectQuotation()
{
// Setup
var entity = Helper.CreateUnorganizedTable();

using (var connection = new SqlConnection(Database.ConnectionStringForRepoDb))
{
// Act
var id = connection.Insert<UnorganizedTable, long>(entity);

// Setup
entity.ColumnDateTime2 = DateTime.UtcNow;
entity.ColumnInt = 2;
entity.ColumnNVarChar = Guid.NewGuid().ToString();

// Act
var updateReuslt = connection.Update<UnorganizedTable>(entity);
var queryResult = connection.Query<UnorganizedTable>(id).First();

// Assert
Assert.AreEqual(1, updateReuslt);
Helper.AssertPropertiesEquality(entity, queryResult);
}
}

#endregion

#region UpdateAll

[TestMethod]
public void TestUpdateAllObjectQuotation()
{
// Setup
var entities = Helper.CreateUnorganizedTables(10);

using (var connection = new SqlConnection(Database.ConnectionStringForRepoDb))
{
// Act
var rowsAffected = connection.InsertAll<UnorganizedTable>(entities);

// Setup
entities.ForEach(entity =>
{
entity.ColumnDateTime2 = DateTime.UtcNow;
entity.ColumnInt = 2;
entity.ColumnNVarChar = Guid.NewGuid().ToString();
});

// Act
rowsAffected = connection.UpdateAll<UnorganizedTable>(entities);

// Assert
Assert.AreEqual(entities.Count, rowsAffected);

// Act
var queryAllResult = connection.QueryAll<UnorganizedTable>();

// Assert
entities.ForEach(entity => Helper.AssertPropertiesEquality(entity, queryAllResult.First(item => item.Id == entity.Id)));
}
}

#endregion
}
}
Loading

0 comments on commit 5217208

Please sign in to comment.