Skip to content
Permalink
Browse files

Update should return 0 when columns is empty (#552)

  • Loading branch information...
asherber authored and pleb committed Sep 26, 2019
1 parent 696a1cd commit a562455a04815659d6dfdd894d86f030c921f113
Showing with 107 additions and 51 deletions.
  1. +67 −0 PetaPoco.Tests.Integration/Databases/BaseUpdateTests.cs
  2. +21 −0 PetaPoco.Tests.Unit/DatabaseTests.cs
  3. +19 −51 PetaPoco/Database.cs
@@ -144,6 +144,23 @@ public void Update_GivenPocoPrimaryKeyAndColumns_ShouldBeValid()
personOther.Height.ShouldNotBe(_person.Height);
}

[Fact]
public void Update_GivenPocoAndEmptyColumns_ShouldBeValid()
{
DB.Insert(_person);

var personOther = DB.Single<Person>(_person.Id);
UpdateProperties(personOther);
DB.Update(personOther, new string[0]).ShouldBe(0);
personOther = DB.Single<Person>(_person.Id);

personOther.Id.ShouldBe(_person.Id);
personOther.Age.ShouldBe(_person.Age);
personOther.Dob.ShouldBe(_person.Dob);
personOther.Name.ShouldBe(_person.Name);
personOther.Height.ShouldBe(_person.Height);
}

[Fact]
public void Update_GivenTablePrimaryKeyNamePocoAndPrimaryKeyValue_ShouldBeValid()
{
@@ -187,6 +204,23 @@ public void Update_GivenTablePrimaryKeyNamePocoAndPrimaryKeyValueAndColumns_Shou
personOther.Height.ShouldNotBe(_person.Height);
}

[Fact]
public void Update_GivenTablePrimaryKeyNamePocoAndPrimaryKeyValueAndEmptyColumns_ShouldBeValid()
{
DB.Insert("SpecificPeople", "Id", false, _person);

var personOther = SinglePersonOther(_person.Id);
UpdateProperties(personOther);
DB.Update("SpecificPeople", "Id", personOther, _person.Id, new string[0]).ShouldBe(0);
personOther = SinglePersonOther(_person.Id);

personOther.Id.ShouldBe(_person.Id);
personOther.Age.ShouldBe(_person.Age);
personOther.Dob.ShouldBe(_person.Dob);
personOther.Name.ShouldBe(_person.Name);
personOther.Height.ShouldBe(_person.Height);
}

[Fact]
public void Update_GivenSqlAndParameters_ShouldBeValid()
{
@@ -354,6 +388,22 @@ public async Task UpdateAsync_GivenPocoPrimaryKeyAndColumns_ShouldBeValid()
personOther.Name.ShouldNotBe(_person.Name);
personOther.Height.ShouldNotBe(_person.Height);
}
[Fact]
public async Task UpdateAsync_GivenPocoAndEmptyColumns_ShouldBeValid()
{
await DB.InsertAsync(_person);

var personOther = await DB.SingleAsync<Person>(_person.Id);
UpdateProperties(personOther);
(await DB.UpdateAsync(personOther, new string[0])).ShouldBe(0);
personOther = await DB.SingleAsync<Person>(_person.Id);

personOther.Id.ShouldBe(_person.Id);
personOther.Age.ShouldBe(_person.Age);
personOther.Dob.ShouldBe(_person.Dob);
personOther.Name.ShouldBe(_person.Name);
personOther.Height.ShouldBe(_person.Height);
}

[Fact]
public async Task UpdateAsync_GivenTablePrimaryKeyNamePocoAndPrimaryKeyValue_ShouldBeValid()
@@ -398,6 +448,23 @@ public async Task UpdateAsync_GivenTablePrimaryKeyNamePocoAndPrimaryKeyValueAndC
personOther.Height.ShouldNotBe(_person.Height);
}

[Fact]
public async Task UpdateAsync_GivenTablePrimaryKeyNamePocoAndPrimaryKeyValueAndEmptyColumns_ShouldBeValid()
{
await DB.InsertAsync("SpecificPeople", "Id", false, _person);

var personOther = await SinglePersonOtherAsync(_person.Id);
UpdateProperties(personOther);
(await DB.UpdateAsync("SpecificPeople", "Id", personOther, _person.Id, new string[0])).ShouldBe(0);
personOther = await SinglePersonOtherAsync(_person.Id);

personOther.Id.ShouldBe(_person.Id);
personOther.Age.ShouldBe(_person.Age);
personOther.Dob.ShouldBe(_person.Dob);
personOther.Name.ShouldBe(_person.Name);
personOther.Height.ShouldBe(_person.Height);
}

[Fact]
public async Task UpdateAsync_GivenSqlAndParameters_ShouldBeValid()
{
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Dynamic;
@@ -121,6 +122,26 @@ public void Update_GivenInvalidArguments_ShouldThrow()
Should.Throw<ArgumentNullException>(() => DB.Update<Person>((Sql) null));
}

[Fact]
public void Update_GivenPocoAndEmptyColumns_ShouldReturn_Zero()
{
var person = new Person() { Id = Guid.NewGuid() };
var columns = new List<string>();

var output = DB.Update(person, columns);
output.ShouldBe(0);
}

[Fact]
public void Update_GivenTableNameAndEmptyColumns_ShouldReturn_Zero()
{
var person = new Person() { Id = Guid.NewGuid() };
var columns = new List<string>();

var output = DB.Update("person", "id", person, columns);
output.ShouldBe(0);
}

[Fact]
public void Insert_GivenInvalidArguments_ShouldThrow()
{
@@ -1969,24 +1969,13 @@ public Task<object> InsertAsync(CancellationToken cancellationToken, object poco

#endif

#endregion
#endregion

#region operation: Update
#region operation: Update

/// <inheritdoc />
public int Update(string tableName, string primaryKeyName, object poco, object primaryKeyValue)
{
if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException(nameof(tableName));

if (string.IsNullOrEmpty(primaryKeyName))
throw new ArgumentNullException(nameof(primaryKeyName));

if (poco == null)
throw new ArgumentNullException(nameof(poco));

return ExecuteUpdate(tableName, primaryKeyName, poco, primaryKeyValue, null);
}
=> Update(tableName, primaryKeyName, poco, primaryKeyValue, null);

/// <inheritdoc />
public int Update(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable<string> columns)
@@ -2000,6 +1989,9 @@ public int Update(string tableName, string primaryKeyName, object poco, object p
if (poco == null)
throw new ArgumentNullException(nameof(poco));

if (columns?.Any() == false)
return 0;

return ExecuteUpdate(tableName, primaryKeyName, poco, primaryKeyValue, columns);
}

@@ -2009,19 +2001,8 @@ public int Update(string tableName, string primaryKeyName, object poco)

/// <inheritdoc />
public int Update(string tableName, string primaryKeyName, object poco, IEnumerable<string> columns)
{
if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException(nameof(tableName));

if (string.IsNullOrEmpty(primaryKeyName))
throw new ArgumentNullException(nameof(primaryKeyName));

if (poco == null)
throw new ArgumentNullException(nameof(poco));

return ExecuteUpdate(tableName, primaryKeyName, poco, null, columns);
}

=> Update(tableName, primaryKeyName, poco, null, columns);

/// <inheritdoc />
public int Update(object poco, IEnumerable<string> columns)
=> Update(poco, null, columns);
@@ -2040,6 +2021,9 @@ public int Update(object poco, object primaryKeyValue, IEnumerable<string> colum
if (poco == null)
throw new ArgumentNullException(nameof(poco));

if (columns?.Any() == false)
return 0;

var pd = PocoData.ForType(poco.GetType(), _defaultMapper);
return ExecuteUpdate(pd.TableInfo.TableName, pd.TableInfo.PrimaryKey, poco, primaryKeyValue, columns);
}
@@ -2167,18 +2151,7 @@ public Task<int> UpdateAsync(string tableName, string primaryKeyName, object poc

/// <inheritdoc />
public Task<int> UpdateAsync(CancellationToken cancellationToken, string tableName, string primaryKeyName, object poco, object primaryKeyValue)
{
if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException(nameof(tableName));

if (string.IsNullOrEmpty(primaryKeyName))
throw new ArgumentNullException(nameof(primaryKeyName));

if (poco == null)
throw new ArgumentNullException(nameof(poco));

return ExecuteUpdateAsync(cancellationToken, tableName, primaryKeyName, poco, primaryKeyValue, null);
}
=> UpdateAsync(cancellationToken, tableName, primaryKeyName, poco, primaryKeyValue, null);

/// <inheritdoc />
public Task<int> UpdateAsync(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable<string> columns)
@@ -2197,6 +2170,9 @@ public Task<int> UpdateAsync(string tableName, string primaryKeyName, object poc
if (poco == null)
throw new ArgumentNullException(nameof(poco));

if (columns?.Any() == false)
return Task.FromResult(0);

return ExecuteUpdateAsync(cancellationToken, tableName, primaryKeyName, poco, primaryKeyValue, columns);
}

@@ -2214,18 +2190,7 @@ public Task<int> UpdateAsync(string tableName, string primaryKeyName, object poc

/// <inheritdoc />
public Task<int> UpdateAsync(CancellationToken cancellationToken, string tableName, string primaryKeyName, object poco, IEnumerable<string> columns)
{
if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException(nameof(tableName));

if (string.IsNullOrEmpty(primaryKeyName))
throw new ArgumentNullException(nameof(primaryKeyName));

if (poco == null)
throw new ArgumentNullException(nameof(poco));

return ExecuteUpdateAsync(cancellationToken, tableName, primaryKeyName, poco, null, columns);
}
=> UpdateAsync(cancellationToken, tableName, primaryKeyName, poco, null, columns);

/// <inheritdoc />
public Task<int> UpdateAsync(object poco, IEnumerable<string> columns)
@@ -2261,6 +2226,9 @@ public Task<int> UpdateAsync(CancellationToken cancellationToken, object poco, o
if (poco == null)
throw new ArgumentNullException(nameof(poco));

if (columns?.Any() == false)
return Task.FromResult(0);

var pd = PocoData.ForType(poco.GetType(), _defaultMapper);
return ExecuteUpdateAsync(cancellationToken, pd.TableInfo.TableName, pd.TableInfo.PrimaryKey, poco, primaryKeyValue, columns);
}

0 comments on commit a562455

Please sign in to comment.
You can’t perform that action at this time.