
using NUnit.Framework;
using Sooda.Caching;
using Sooda.UnitTests.BaseObjects;
using System;

namespace Sooda.UnitTests.TestCases.ObjectMapper
{
    [TestFixture]
    public class PrecommitTest
    {
        [Test]
        public void Test1()
        {
            using (TestSqlDataSource testDataSource = new TestSqlDataSource("default"))
            {
                testDataSource.Open();
                SoodaCache.DefaultCache.Clear();;

                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);

                    // create new uncommitted object
                    Contact nc = new Contact();

                    Console.WriteLine("Precommitting...");

                    // force precommit
                    // the 'type' field precommits as 'Customer'
                    ContactList cl = Contact.GetList(SoodaWhereClause.Unrestricted);

                    // update name

                    Console.WriteLine("Precommitting again...");
                    nc.Name = "name1";

                    // force precommit again
                    cl = Contact.GetList(SoodaWhereClause.Unrestricted);

                    Console.WriteLine("Precommitting again (2)...");

                    nc.Type = ContactType.Customer;
                    // force precommit again
                    cl = Contact.GetList(SoodaWhereClause.Unrestricted);

                    // this should do nothing

                    Console.WriteLine("Comitting...");
                    tran.Commit();
                }
            }
        }

        [Test]
        public void Test2()
        {
            using (TestSqlDataSource testDataSource = new TestSqlDataSource("default"))
            {
                testDataSource.Open();
                SoodaCache.DefaultCache.Clear();;

                using (SoodaTransaction tran = new SoodaTransaction())
                {
                    tran.RegisterDataSource(testDataSource);

                    // create new uncommitted object
                    Contact nc = new Contact();

                    Console.WriteLine("Precommitting...");

                    // force precommit
                    // the 'type' field precommits as 'Customer'
                    ContactList cl = Contact.GetList(new SoodaWhereClause("Type={0} and ContactId={1}", "Customer", nc.ContactId));

                    // we get one record
                    Assert.AreEqual(1, cl.Count, "#1");

                    // force precommit
                    // the 'type' field precommits as 'Customer'
                    ContactList cl2 = Contact.GetList(new SoodaWhereClause("Type={0} and ContactId={1}", "Customer", nc.ContactId), SoodaSnapshotOptions.VerifyAfterLoad);

                    // we use SoodaSnapshotOptions.Verify to check in-memory
                    // values after the load
                    Assert.AreEqual(0, cl2.Count, "#2");

                    nc.Type = ContactType.Customer;

                    // force precommit
                    ContactList cl3 = Contact.GetList(new SoodaWhereClause("Type={0} and ContactId={1}", "Customer", nc.ContactId), SoodaSnapshotOptions.VerifyAfterLoad);

                    // we use SoodaSnapshotOptions.Verify to check in-memory
                    // values after the load
                    Assert.AreEqual(1, cl3.Count, "#3");

                    nc.Type = ContactType.Employee;

                    ContactList cl4 = Contact.GetList(new SoodaWhereClause("Type={0} and ContactId={1}", "Customer", nc.ContactId), SoodaSnapshotOptions.VerifyAfterLoad);

                    // we use SoodaSnapshotOptions.Verify to check in-memory
                    // values after the load
                    Assert.AreEqual(0, cl4.Count, "#4");

                    // tran.Commit();
                }
            }
        }

        [Test]
        public void EventsTest()
        {
            using (TestSqlDataSource ds = new TestSqlDataSource("default"))
            {
                ds.Open();
                int roleID;

                using (SoodaTransaction t = new SoodaTransaction())
                {
                    t.RegisterDataSource(ds);
                    Role r = new Role();
                    r.Name = "aaa";
                    Console.WriteLine("Precommitting...");
                    t.SaveObjectChanges();
                    Console.WriteLine("Comitting.");
                    t.Commit();
                    roleID = r.Id;
                    Assert.AreEqual(0, r.BeforeObjectInsertEventCounter);
                    Assert.AreEqual(2, r.AfterObjectInsertEventCounter);
                    Assert.AreEqual(-1, r.BeforeObjectUpdateEventCounter);
                    Assert.AreEqual(-1, r.AfterObjectUpdateEventCounter);

                    Assert.AreEqual(1, r.Second.BeforeObjectInsertEventCounter);
                    Assert.AreEqual(3, r.Second.AfterObjectInsertEventCounter);
                    Assert.AreEqual(-1, r.Second.BeforeObjectUpdateEventCounter);
                    Assert.AreEqual(-1, r.Second.AfterObjectUpdateEventCounter);
                }
            }
        }
    }
}
