Skip to content

Commit

Permalink
fix firstOrDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihar Yakimush authored and Ihar Yakimush committed Aug 7, 2018
1 parent 8d891b8 commit e075757
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
30 changes: 28 additions & 2 deletions SolrNet.Linq.IntegrationTests/EnumeratedTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -66,14 +67,39 @@ public void OrDefault()
Assert.Null(t2);
}

[Fact]
public void Throw()
{
Assert.Throws<InvalidOperationException>(() =>
Product.SolrOperations.Value.AsQueryable().First(p => p.Id == "qwe"));
Assert.Throws<InvalidOperationException>(() =>
Product.SolrOperations.Value.AsQueryable().Single(p => p.Id == "qwe"));
Assert.Throws<InvalidOperationException>(() => Product.SolrOperations.Value.AsQueryable().Single());
}

[Fact]
public async Task ThrowAsync()
{
await Assert.ThrowsAsync<InvalidOperationException>(() =>
Product.SolrOperations.Value.AsQueryable().FirstAsync(p => p.Id == "qwe"));

await Assert.ThrowsAsync<InvalidOperationException>(() =>
Product.SolrOperations.Value.AsQueryable().SingleAsync(p => p.Id == "qwe"));
await Assert.ThrowsAsync<InvalidOperationException>(() => Product.SolrOperations.Value.AsQueryable().SingleAsync());
}

[Fact]
public async Task OrDefaultAsync()
{
Product t1 = await Product.SolrOperations.Value.AsQueryable().FirstOrDefaultAsync(p => p.Id == "qwe");
Product t2 = await Product.SolrOperations.Value.AsQueryable().SingleOrDefaultAsync(p => p.Id == "qwe");
Product t2 = await Product.SolrOperations.Value.AsQueryable().Where(p => p.Id == "qwe").FirstOrDefaultAsync();
Product t3 = await Product.SolrOperations.Value.AsQueryable().SingleOrDefaultAsync(p => p.Id == "qwe");
Product t4 = await Product.SolrOperations.Value.AsQueryable().Where(p => p.Id == "qwe").SingleOrDefaultAsync();

Assert.Null(t1);
Assert.Null(t2);
Assert.Null(t3);
Assert.Null(t4);
}
}
}
8 changes: 4 additions & 4 deletions SolrNet.Linq/SolrLinqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static async Task<TSource> FirstOrDefaultAsync<TSource>(this IQueryable<T
null,
GetMethod<TSource>(nameof(Queryable.FirstOrDefault), 1), query.Expression));

return result.First();
return result.FirstOrDefault();
}

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

return result.First();
return result.FirstOrDefault();
}

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

return result.Single();
return result.SingleOrDefault();
}

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

return result.Single();
return result.SingleOrDefault();
}

return query.SingleOrDefault(predicate);
Expand Down
4 changes: 2 additions & 2 deletions SolrNet.Linq/SolrNet.Linq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<AssemblyOriginatorKeyFile>sgn.snk</AssemblyOriginatorKeyFile>
<Authors>IharYakimush</Authors>
<Company />
<Description>SolrNet IQueryable provider. Extend SolrNet functionality by adding limited linq to solr support.</Description>
<Description>SolrNet IQueryable provider. Extend SolrNet functionality by adding linq to solr support for basic methods.</Description>
<PackageLicenseUrl>https://github.com/IharYakimush/solr-net-linq/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/IharYakimush/solr-net-linq</PackageProjectUrl>
<PackageTags>solr solrnet linq</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Version>1.2.0</Version>
<PackageReleaseNotes>support First(), FirstOrDefault(). Single(), SingleOrDefault() methods</PackageReleaseNotes>
<PackageReleaseNotes>support First(), FirstOrDefault(). Single(), SingleOrDefault() methods and its async versions</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit e075757

Please sign in to comment.