Skip to content

Commit 93dafa7

Browse files
committed
Improve null handling
1 parent bceb1d3 commit 93dafa7

File tree

3 files changed

+76
-10
lines changed

3 files changed

+76
-10
lines changed

PetaPoco.SqlKata.Tests/AutoGenerateTests.cs

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,52 @@ public void Generate_T_SimpleClass_HasSelect()
180180
Compare(q, expected);
181181
}
182182

183+
[Fact]
184+
public void ForType_NullQuery_Throws()
185+
{
186+
Query query = null;
187+
Action act = () => query.ForType<MyClass>();
188+
act.Should().Throw<ArgumentNullException>();
189+
}
190+
191+
[Fact]
192+
public void GenerateSelect_NullQuery_Throws()
193+
{
194+
Query query = null;
195+
Action act = () => query.GenerateSelect<MyClass>();
196+
act.Should().Throw<ArgumentNullException>();
197+
}
198+
199+
[Fact]
200+
public void ForType_NullMapper_Throws()
201+
{
202+
try
203+
{
204+
SqlKataExtensions.DefaultMapper = null;
205+
Action act = () => new Query().ForType<MyClass>(null);
206+
act.Should().Throw<ArgumentNullException>();
207+
}
208+
finally
209+
{
210+
SqlKataExtensions.DefaultMapper = new ConventionMapper();
211+
}
212+
}
213+
214+
[Fact]
215+
public void GenerateSelect_NullMapper_Throws()
216+
{
217+
try
218+
{
219+
SqlKataExtensions.DefaultMapper = null;
220+
Action act = () => new Query().GenerateSelect<MyClass>(null);
221+
act.Should().Throw<ArgumentNullException>();
222+
}
223+
finally
224+
{
225+
SqlKataExtensions.DefaultMapper = new ConventionMapper();
226+
}
227+
}
228+
183229
[Theory]
184230
[MemberData(nameof(Mappers))]
185231
public void Different_Mappers(IMapper mapper, string tableName, params string[] fieldNames)
@@ -200,24 +246,28 @@ public void Different_Mappers(IMapper mapper, string tableName, params string[]
200246
[MemberData(nameof(Mappers))]
201247
public void Different_Default_Mappers(IMapper mapper, string tableName, params string[] fieldNames)
202248
{
203-
try
249+
if (mapper != null)
204250
{
205-
SqlKataExtensions.DefaultMapper = mapper;
206-
var q = new Query().GenerateSelect<MyOtherClass>();
207-
var expected = new Query(tableName).Select(fieldNames);
208-
Compare(q, expected);
209-
}
210-
finally
211-
{
212-
SqlKataExtensions.DefaultMapper = new ConventionMapper();
213-
PetaPoco.Mappers.RevokeAll();
251+
try
252+
{
253+
SqlKataExtensions.DefaultMapper = mapper;
254+
var q = new Query().GenerateSelect<MyOtherClass>();
255+
var expected = new Query(tableName).Select(fieldNames);
256+
Compare(q, expected);
257+
}
258+
finally
259+
{
260+
SqlKataExtensions.DefaultMapper = new ConventionMapper();
261+
PetaPoco.Mappers.RevokeAll();
262+
}
214263
}
215264
}
216265

217266
public static IEnumerable<object[]> Mappers => new[]
218267
{
219268
new object[] { new UnderscoreMapper(), "my_other_class", "other_id", "other_name" },
220269
new object[] { new ConventionMapper(), "MyOtherClass", "OtherID", "OtherName" },
270+
new object[] { null, "MyOtherClass", "OtherID", "OtherName" },
221271
};
222272
}
223273

PetaPoco.SqlKata.Tests/ToSqlTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,12 @@ public void HasSelect_NoSelect_IsFalse()
174174
query.HasSelect().Should().BeFalse();
175175
}
176176

177+
[Fact]
178+
public void NullQuery_ShouldThrow()
179+
{
180+
Query query = null;
181+
Action act = () => query.ToSql();
182+
act.Should().Throw<ArgumentNullException>();
183+
}
177184
}
178185
}

PetaPoco.SqlKata/PetaPoco.SqlKata.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public static class SqlKataExtensions
5656
/// </summary>
5757
public static IMapper DefaultMapper { get; set; } = new ConventionMapper();
5858

59+
60+
5961
/// <summary>
6062
/// Convert a <seealso cref="Query"/> object to a <seealso cref="Sql" /> object,
6163
/// using a <seealso cref="SqlServerCompiler"/>.
@@ -72,6 +74,8 @@ public static class SqlKataExtensions
7274
/// <returns></returns>
7375
public static Sql ToSql(this Query query, CompilerType compilerType)
7476
{
77+
query = query ?? throw new ArgumentNullException(nameof(query));
78+
7579
var compiler = _compilers[compilerType].Value;
7680
var compiled = compiler.Compile(query);
7781
var ppSql = Helper.ReplaceAll(compiled.RawSql, "?", x => "@" + x);
@@ -113,6 +117,9 @@ public static Sql ToSql(this Query query, CompilerType compilerType)
113117
/// <returns></returns>
114118
public static Query ForType(this Query query, Type type, IMapper mapper)
115119
{
120+
query = query ?? throw new ArgumentNullException(nameof(query));
121+
mapper = mapper ?? DefaultMapper ?? throw new ArgumentNullException(nameof(mapper));
122+
116123
var tableInfo = mapper.GetTableInfo(type);
117124
return query.From(tableInfo.TableName);
118125
}
@@ -187,6 +194,8 @@ public static Query GenerateSelect(this Query query, object poco, IMapper mapper
187194
/// <returns></returns>
188195
public static Query GenerateSelect(this Query query, Type type, IMapper mapper)
189196
{
197+
query = query ?? throw new ArgumentNullException(nameof(query));
198+
190199
if (!query.HasSelect())
191200
{
192201
mapper = mapper ?? DefaultMapper ?? throw new ArgumentNullException(nameof(mapper));

0 commit comments

Comments
 (0)