Skip to content

Commit e075757

Browse files
Ihar YakimushIhar Yakimush
authored andcommitted
fix firstOrDefault
1 parent 8d891b8 commit e075757

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

SolrNet.Linq.IntegrationTests/EnumeratedTests.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using System.Threading.Tasks;
34
using Xunit;
45

@@ -66,14 +67,39 @@ public void OrDefault()
6667
Assert.Null(t2);
6768
}
6869

70+
[Fact]
71+
public void Throw()
72+
{
73+
Assert.Throws<InvalidOperationException>(() =>
74+
Product.SolrOperations.Value.AsQueryable().First(p => p.Id == "qwe"));
75+
Assert.Throws<InvalidOperationException>(() =>
76+
Product.SolrOperations.Value.AsQueryable().Single(p => p.Id == "qwe"));
77+
Assert.Throws<InvalidOperationException>(() => Product.SolrOperations.Value.AsQueryable().Single());
78+
}
79+
80+
[Fact]
81+
public async Task ThrowAsync()
82+
{
83+
await Assert.ThrowsAsync<InvalidOperationException>(() =>
84+
Product.SolrOperations.Value.AsQueryable().FirstAsync(p => p.Id == "qwe"));
85+
86+
await Assert.ThrowsAsync<InvalidOperationException>(() =>
87+
Product.SolrOperations.Value.AsQueryable().SingleAsync(p => p.Id == "qwe"));
88+
await Assert.ThrowsAsync<InvalidOperationException>(() => Product.SolrOperations.Value.AsQueryable().SingleAsync());
89+
}
90+
6991
[Fact]
7092
public async Task OrDefaultAsync()
7193
{
7294
Product t1 = await Product.SolrOperations.Value.AsQueryable().FirstOrDefaultAsync(p => p.Id == "qwe");
73-
Product t2 = await Product.SolrOperations.Value.AsQueryable().SingleOrDefaultAsync(p => p.Id == "qwe");
95+
Product t2 = await Product.SolrOperations.Value.AsQueryable().Where(p => p.Id == "qwe").FirstOrDefaultAsync();
96+
Product t3 = await Product.SolrOperations.Value.AsQueryable().SingleOrDefaultAsync(p => p.Id == "qwe");
97+
Product t4 = await Product.SolrOperations.Value.AsQueryable().Where(p => p.Id == "qwe").SingleOrDefaultAsync();
7498

7599
Assert.Null(t1);
76100
Assert.Null(t2);
101+
Assert.Null(t3);
102+
Assert.Null(t4);
77103
}
78104
}
79105
}

SolrNet.Linq/SolrLinqExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static async Task<TSource> FirstOrDefaultAsync<TSource>(this IQueryable<T
4848
null,
4949
GetMethod<TSource>(nameof(Queryable.FirstOrDefault), 1), query.Expression));
5050

51-
return result.First();
51+
return result.FirstOrDefault();
5252
}
5353

5454
return query.FirstOrDefault();
@@ -62,7 +62,7 @@ public static async Task<TSource> FirstOrDefaultAsync<TSource>(this IQueryable<T
6262
null,
6363
GetMethod<TSource>(nameof(Queryable.FirstOrDefault), 2), query.Expression, predicate));
6464

65-
return result.First();
65+
return result.FirstOrDefault();
6666
}
6767

6868
return query.FirstOrDefault(predicate);
@@ -104,7 +104,7 @@ public static async Task<TSource> SingleOrDefaultAsync<TSource>(this IQueryable<
104104
null,
105105
GetMethod<TSource>(nameof(Queryable.SingleOrDefault), 1), query.Expression));
106106

107-
return result.Single();
107+
return result.SingleOrDefault();
108108
}
109109

110110
return query.SingleOrDefault();
@@ -118,7 +118,7 @@ public static async Task<TSource> SingleOrDefaultAsync<TSource>(this IQueryable<
118118
null,
119119
GetMethod<TSource>(nameof(Queryable.SingleOrDefault), 2), query.Expression, predicate));
120120

121-
return result.Single();
121+
return result.SingleOrDefault();
122122
}
123123

124124
return query.SingleOrDefault(predicate);

SolrNet.Linq/SolrNet.Linq.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
<AssemblyOriginatorKeyFile>sgn.snk</AssemblyOriginatorKeyFile>
77
<Authors>IharYakimush</Authors>
88
<Company />
9-
<Description>SolrNet IQueryable provider. Extend SolrNet functionality by adding limited linq to solr support.</Description>
9+
<Description>SolrNet IQueryable provider. Extend SolrNet functionality by adding linq to solr support for basic methods.</Description>
1010
<PackageLicenseUrl>https://github.com/IharYakimush/solr-net-linq/blob/master/LICENSE</PackageLicenseUrl>
1111
<PackageProjectUrl>https://github.com/IharYakimush/solr-net-linq</PackageProjectUrl>
1212
<PackageTags>solr solrnet linq</PackageTags>
1313
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1414
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
1515
<Version>1.2.0</Version>
16-
<PackageReleaseNotes>support First(), FirstOrDefault(). Single(), SingleOrDefault() methods</PackageReleaseNotes>
16+
<PackageReleaseNotes>support First(), FirstOrDefault(). Single(), SingleOrDefault() methods and its async versions</PackageReleaseNotes>
1717
</PropertyGroup>
1818

1919
<ItemGroup>

0 commit comments

Comments
 (0)