Skip to content
This repository was archived by the owner on Feb 10, 2024. It is now read-only.

Commit

Permalink
Adds AllowMissing testing.
Browse files Browse the repository at this point in the history
* See issue #1.
  • Loading branch information
Alexandre Leblanc committed Jul 16, 2018
1 parent 974548c commit d5c34e5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DbAutoFillNextUnitTest/Dataset/BasicObject.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using DbAutoFillStandard.Types;
using System.Collections.Generic;

namespace DbAutoFillNextCoreUnitTest.Dataset
namespace DbAutoFillStandardUnitTest.Dataset
{
[DbAutoFill(AllowMissing = false)]
class BasicObject
Expand Down
2 changes: 1 addition & 1 deletion DbAutoFillNextUnitTest/Dataset/ComplexObject.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using DbAutoFillStandard.Types;

namespace DbAutoFillNextCoreUnitTest.Dataset
namespace DbAutoFillStandardUnitTest.Dataset
{
[DbAutoFill(AllowMissing = false,
ParameterPrefix = "p_")]
Expand Down
2 changes: 1 addition & 1 deletion DbAutoFillNextUnitTest/Dataset/DatasetGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Data;

namespace DbAutoFillNextCoreUnitTest.Dataset
namespace DbAutoFillStandardUnitTest.Dataset
{
internal static class DatasetGenerator
{
Expand Down
13 changes: 13 additions & 0 deletions DbAutoFillNextUnitTest/Dataset/SampleObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using DbAutoFillStandard.Types;

namespace DbAutoFillStandardUnitTest.Dataset
{
[DbAutoFill]
public class SampleObject
{
[DbAutoFill(AllowMissing = true)]
public int MissingField { get; set; }

public int Mandatory { get; set; }
}
}
46 changes: 41 additions & 5 deletions DbAutoFillNextUnitTest/DbAutoFillHelperTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using DbAutoFillNextCoreUnitTest.Dataset;
using DbAutoFillStandard;
using DbAutoFillStandard.Types;
using DbAutoFillStandardUnitTest.Dataset;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Data;
Expand Down Expand Up @@ -103,7 +103,7 @@ public void DbResultsToBasicObject()
const string intPropName = "IntField";

DataTable dt = DatasetGenerator.CreateNewBasicDataTable(
new string[] { stringPropName, intPropName },
new string[] { stringPropName, intPropName },
new Type[] { typeof(string), typeof(int) });

using (IDataReader dr = DatasetGenerator.CreateBasicDataReader(dt, expectedStringValue, expectedIntValue))
Expand Down Expand Up @@ -136,9 +136,9 @@ public void DbResultToComplexObject()
new string[] { nameINField, toDbUuidField, fromDbIdField, expectedAliasedColumnName },
new Type[] { typeof(string), typeof(int), typeof(int), typeof(string) });

using (IDataReader dr = DatasetGenerator.CreateBasicDataReader(dt,
expectedNameINValue,
unexpectedToDbUuidValue,
using (IDataReader dr = DatasetGenerator.CreateBasicDataReader(dt,
expectedNameINValue,
unexpectedToDbUuidValue,
expectedFromDbUuid,
expectedAliaseColumnValue))
{
Expand All @@ -157,6 +157,42 @@ public void DbResultToComplexObject()
}
}

[TestMethod]
public void DbResultToSampleObjectAllowMissing()
{
const string mandatoryField = "Mandatory";
const int expectedMandatoryValue = 890;

{
DataTable dt = DatasetGenerator.CreateNewBasicDataTable(
new string[] { mandatoryField },
new Type[] { typeof(int) });

using (IDataReader dr = DatasetGenerator.CreateBasicDataReader(dt, expectedMandatoryValue))
{
dr.Read();

SampleObject obj = new SampleObject();

DbAutoFillHelper.FillObjectFromDataReader(dr, obj);

Assert.AreEqual(expectedMandatoryValue, obj.Mandatory);
}
}

{
DataTable dt = DatasetGenerator.CreateNewBasicDataTable(new string[] { }, new Type[] { });

using (IDataReader dr = DatasetGenerator.CreateBasicDataReader(dt, new object[] { }))
{
dr.Read();
SampleObject obj = new SampleObject();

Assert.ThrowsException<MissingFieldException>(() => { DbAutoFillHelper.FillObjectFromDataReader(dr, obj); });
}
}
}

[TestCleanup]
public void TestCleanp()
{
Expand Down

0 comments on commit d5c34e5

Please sign in to comment.