// Copyright (C) 2012 Xtensive LLC.
// All rights reserved.
// For conditions of distribution and use, see license.
// Created by: Denis Krjuchkov
// Created:    2012.01.25

using System.Linq;
using NUnit.Framework;
using Xtensive.Orm.Configuration;
using Xtensive.Orm.Tests.Issues.IssueJira0071_DistinctDoesNotWorkProperlyModel;

namespace Xtensive.Orm.Tests.Issues.IssueJira0071_DistinctDoesNotWorkProperlyModel
{
  [HierarchyRoot]
  public class Jira0071Entity : Entity
  {
    [Field, Key]
    public long Id { get; private set; }

    [Field]
    public string Text { get; set; }

    [Field]
    public int? Index { get; set; }
  }
}

namespace Xtensive.Orm.Tests.Issues
{

  public class IssueJira0071_DistinctDoesNotWorkProperly : AutoBuildTest
  {
    protected override DomainConfiguration BuildConfiguration()
    {
      var configuration = base.BuildConfiguration();
      configuration.Types.RegisterCaching(typeof (Jira0071Entity).Assembly, typeof (Jira0071Entity).Namespace);
      return configuration;
    }

    protected override void PopulateData()
    {
      using (Domain.OpenSession())
      using (var t = Session.Current.OpenTransaction()) {
        new Jira0071Entity {Text = "Some text", Index = 1};
        new Jira0071Entity {Text = "Some text", Index = 1};
        new Jira0071Entity {Text = "Some text", Index = 2};
        new Jira0071Entity {Text = "Some text", Index = null};
        new Jira0071Entity {Text = "Some text", Index = 1};
        new Jira0071Entity {Text = "Other text", Index = 1};
        new Jira0071Entity {Text = "Other text", Index = null};
        t.Complete();
      }
    }

    [Test]
    public void MainTest()
    {
      using (Domain.OpenSession())
      using (var t = Session.Current.OpenTransaction()) {

        var q =
          from x in Query.All<Jira0071Entity>()
          select new {x.Text, OneOrNull = x.Index==null ? true : x.Index==1 ? true : false};

        var r = q.Distinct().OrderBy(x => x.Text).ThenBy(x => x.OneOrNull).ToList();

        Assert.That(r.Count, Is.EqualTo(3));
        Assert.That(r[0].Text, Is.EqualTo("Other text"));
        Assert.That(r[0].OneOrNull, Is.EqualTo(true));
        Assert.That(r[1].Text, Is.EqualTo("Some text"));
        Assert.That(r[1].OneOrNull, Is.EqualTo(false));
        Assert.That(r[2].Text, Is.EqualTo("Some text"));
        Assert.That(r[2].OneOrNull, Is.EqualTo(true));
      }
    }
  }
}