// Copyright (C) 2003-2010 Xtensive LLC.
// All rights reserved.
// For conditions of distribution and use, see license.
// Created by: Ivan Galkin
// Created:    2009.05.20

using System;
using System.Linq;
using System.Reflection;
using Xtensive.Core;
using Xtensive.Orm.Tests.Issues.Issue_0716_UpgradeFailsInValidateMode.Model.Version1;
using Xtensive.Orm.Providers;
using M1 = Xtensive.Orm.Tests.Issues.Issue_0716_UpgradeFailsInValidateMode.Model.Version1;
using M2 = Xtensive.Orm.Tests.Issues.Issue_0716_UpgradeFailsInValidateMode.Model.Version2;
using NUnit.Framework;

namespace Xtensive.Orm.Tests.Issues.Issue_0716_UpgradeFailsInValidateMode
{
  [TestFixture]
  [Ignore("Outdated")]
  // Alowing behavior when types moved to different namespace 
  // can cause problems and corrupt data in database which is unacceptable.
  // As long as Validate mode is expected to be strict we have to forbid
  // cases when cross-namespace movements appear.
  public class UpgradeTest
  {
    [OneTimeSetUp]
    public void TestSetUp() => Require.AllFeaturesSupported(ProviderFeatures.FullText);

    [SetUp]
    public void SetUp()
    {
      using (var domain = BuildDomain("1", DomainUpgradeMode.Recreate))
      using (var session = domain.OpenSession())
      using (var tx = session.OpenTransaction()) {
        var acticle = new Article();
        tx.Complete();
      }
    }
    
    [Test]
    public void UpgradeToVersion2Test()
    {
      using (var domain = BuildDomain("2", DomainUpgradeMode.Validate))
      using (var session = domain.OpenSession())
      using (session.OpenTransaction()) {
        Assert.AreEqual(1, session.Query.All<Model.Version2.Article>().Count());
      }
    }

    private Domain BuildDomain(string version, DomainUpgradeMode upgradeMode)
    {
      var ns = typeof(Article).Namespace;
      var nsPrefix = ns.Substring(0, ns.Length - 1);

      var configuration = DomainConfigurationFactory.Create();
      configuration.UpgradeMode = upgradeMode;
      configuration.Types.RegisterCaching(Assembly.GetExecutingAssembly(), nsPrefix + version);
      configuration.Types.Register(typeof(Upgrader));

      using (Upgrader.Enable(version)) {
        return Domain.Build(configuration);
      }
    }
  }
}