Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Fix JasperFxGH-1374

- Fix JasperFxGH-1374
- Add unit tests

* Fix related failing unit test
  • Loading branch information
mysticmind authored Nov 10, 2019
1 parent f81e4c1 commit 6235a66
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void use_searchable_fields_in_generated_sql()
var cmd2 = session.Query<Target>().Where(x => !x.Flag).ToCommand();

cmd1.CommandText.ShouldBe("select d.data, d.id, d.mt_version from public.mt_doc_target as d where d.flag = :arg0");
cmd2.CommandText.ShouldBe("select d.data, d.id, d.mt_version from public.mt_doc_target as d where d.flag != :arg0");
cmd2.CommandText.ShouldBe("select d.data, d.id, d.mt_version from public.mt_doc_target as d where (d.flag IS NULL or d.flag != :arg0)");
}
}

Expand Down
64 changes: 64 additions & 0 deletions src/Marten.Testing/Linq/BoolNotVisitorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Linq;
using Marten.Services;
using Xunit;

namespace Marten.Testing.Linq
{
public class BoolNotVisitorTests : DocumentSessionFixture<NulloIdentityMap>
{
private class TestClass
{
public TestClass()
{
Id = Guid.NewGuid();
}

public Guid Id { get; set; }
public bool Flag { get; set; }
}

[Fact]
public void when_doc_with_bool_false_should_return_records()
{
var docWithFlagFalse = new TestClass();

theSession.Store(docWithFlagFalse);
theSession.SaveChanges();

using (var s = theStore.OpenSession())
{
var items = s.Query<TestClass>().Where(x => !x.Flag).ToList();

Assert.Single(items);
Assert.Equal(docWithFlagFalse.Id, items[0].Id);
}
}

[Fact]
public void when_doc_with_bool_false_with_serializer_default_value_handling_null_should_return_records()
{
var serializer = new JsonNetSerializer();
serializer.Customize(s =>
{
s.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;
});

StoreOptions(x => x.Serializer(serializer));

// Note: with serializer settings DefaultValueHandling.Ignore, serialized JSON won't have Flag property
var docWithFlagFalse = new TestClass();

theSession.Store(docWithFlagFalse);
theSession.SaveChanges();

using (var s = theStore.OpenSession())
{
var items = s.Query<TestClass>().Where(x => !x.Flag).ToList();

Assert.Single(items);
Assert.Equal(docWithFlagFalse.Id, items[0].Id);
}
}
}
}
8 changes: 7 additions & 1 deletion src/Marten/Linq/NotVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Baseline;
Expand Down Expand Up @@ -58,7 +59,12 @@ protected override Expression VisitMember(MemberExpression expression)
if (expression.Type == typeof(bool))
{
var locator = _mapping.JsonLocator(expression);
var @where = new WhereFragment($"{locator} != ?", true);
var whereFragments = new List<IWhereFragment>
{
new WhereFragment($"{locator} IS NULL"),
new WhereFragment($"{locator} != ?", true)
};
var @where = new CompoundWhereFragment("or", whereFragments.ToArray());
_callback(@where);
}

Expand Down

0 comments on commit 6235a66

Please sign in to comment.