<commit_before>using System;
using System.IO;
using System.Linq;
using Moq;
using NUnit.Framework;
using Toggl.Phoebe.Data;
using Toggl.Phoebe.Data.Models;
using Toggl.Phoebe.Data.Views;
using Toggl.Phoebe.Net;
using XPlatUtils;

namespace Toggl.Phoebe.Tests.Views
{
    [TestFixture]
    public class RecentTimeEntriesViewTest : Test
    {
        private string tmpDb;
        private WorkspaceModel workspace;
        private UserModel user;

        private IModelStore ModelStore {
            get { return ServiceContainer.Resolve<IModelStore> (); }
        }

        private AuthManager AuthManager {
            get { return ServiceContainer.Resolve<AuthManager> (); }
        }

        public override void SetUp ()
        {
            base.SetUp ();

            tmpDb = Path.GetTempFileName ();
            ServiceContainer.Register<IModelStore> (new TestSqliteStore (tmpDb));

            CreateTestData ();

            ServiceContainer.Register<ISettingsStore> (Mock.Of<ISettingsStore> (
                (store) => store.ApiToken == "<PASSWORD>" &&
                store.UserId == user.Id));
            ServiceContainer.Register<AuthManager> (new AuthManager ());
        }

        public override void TearDown ()
        {
            ModelStore.Commit ();

            base.TearDown ();

            File.Delete (tmpDb);
            tmpDb = null;
        }

        [Test]
        public void TestInitialGrouping ()
        {
            var view = new RecentTimeEntriesView ();
            Assert.AreEqual (4, view.Count);
            Assert.AreEqual (
                new long[] { 5, 4, 3, 2 },
                view.Models.Select ((entry) => entry.RemoteId.Value).ToArray ()
            );
        }

        private void CreateTestData ()
        {
            workspace = Model.Update (new WorkspaceModel () {
                RemoteId = 1,
                Name = "<NAME>",
                IsPersisted = true,
            });

            user = Model.Update (new UserModel () {
                RemoteId = 1,
                Name = "Tester",
                DefaultWorkspace = workspace,
                IsPersisted = true,
            });

            var project = Model.Update (new ProjectModel () {
                RemoteId = 1,
                Name = "Ad design",<commit_msg>Add tests for expected RecentTimeEntriesView behaviour.<commit_after>using System;
using System.IO;
using System.Linq;
using Moq;
using NUnit.Framework;
using Toggl.Phoebe.Data;
using Toggl.Phoebe.Data.Models;
using Toggl.Phoebe.Data.Views;
using Toggl.Phoebe.Net;
using XPlatUtils;

namespace Toggl.Phoebe.Tests.Views
{
    [TestFixture]
    public class RecentTimeEntriesViewTest : Test
    {
        private string tmpDb;
        private WorkspaceModel workspace;
        private UserModel user;

        private IModelStore ModelStore {
            get { return ServiceContainer.Resolve<IModelStore> (); }
        }

        private AuthManager AuthManager {
            get { return ServiceContainer.Resolve<AuthManager> (); }
        }

        public override void SetUp ()
        {
            base.SetUp ();

            tmpDb = Path.GetTempFileName ();
            ServiceContainer.Register<IModelStore> (new TestSqliteStore (tmpDb));

            CreateTestData ();

            ServiceContainer.Register<ISettingsStore> (Mock.Of<ISettingsStore> (
                (store) => store.ApiToken == "<PASSWORD>" &&
                store.UserId == user.Id));
            ServiceContainer.Register<AuthManager> (new AuthManager ());
        }

        public override void TearDown ()
        {
            ModelStore.Commit ();

            base.TearDown ();

            File.Delete (tmpDb);
            tmpDb = null;
        }

        [Test]
        public void TestInitialGrouping ()
        {
            var view = new RecentTimeEntriesView ();
            Assert.AreEqual (4, view.Count);
            Assert.AreEqual (
                new long[] { 5, 4, 3, 2 },
                view.Models.Select ((entry) => entry.RemoteId.Value).ToArray ()
            );
        }

        [Test]
        public void TestInitialReload ()
        {
            var view = new RecentTimeEntriesView ();
            view.Reload ();
            Assert.AreEqual (4, view.Count);
            Assert.AreEqual (
                new long[] { 5, 4, 3, 2 },
                view.Models.Select ((entry) => entry.RemoteId.Value).ToArray ()
            );
        }

        [Test]
        public void TestChangeGroupBottom ()
        {
            var view = new RecentTimeEntriesView ();

            var model = Model.ByRemoteId<TimeEntryModel> (1);
            model.Description += " and some";

            Assert.AreEqual (5, view.Count);
            Assert.AreEqual (
                new long[] { 5, 4, 3, 2, 1 },
                view.Models.Select ((entry) => entry.RemoteId.Value).ToArray ()
            );
        }

        [Test]
        public void TestChangeGroupTop ()
        {
            var view = new RecentTimeEntriesView ();

            var model = Model.ByRemoteId<TimeEntryModel> (3);
            model.Description += " and some";

            Assert.AreEqual (5, view.Count);
            Assert.AreEqual (
                new long[] { 5, 4, 3, 2, 1 },
                view.Models.Select ((entry) => entry.RemoteId.Value).ToArray ()
            );
        }

        [Test]
        public void TestChangeGroupBottomToTop ()
        {
            var view = new RecentTimeEntriesView ();

            var model = Model.ByRemoteId<TimeEntryModel> (1);
            model.StopTime += TimeSpan.FromDays (1);
            model.StartTime += TimeSpan.FromDays (1);

            Assert.AreEqual (4, view.Count);
            Assert.AreEqual (
                new long[] { 1, 5, 4, 2 },
                view.Models.Select ((entry) => entry.RemoteId.Value).ToArray ()
            );
        }

        [Test]
        public void TestChangeGroupTopToBottom ()
        {
            var view = new RecentTimeEntriesView ();

            var model = Model.ByRemoteId<TimeEntryModel> (3);
            model.StopTime -= TimeSpan.FromDays (1);
            model.StartTime -= TimeSpan.FromDays (1);

            Assert.AreEqual (4, view.Count);
            Assert.AreEqual (
                new long[] { 5, 4, 2, 1 },
                view.Models.Select ((entry) => entry.RemoteId.Value).ToArray ()
            );
        }

        private void CreateTestData ()
        {
            workspace = Model.Update (new WorkspaceModel () {
                RemoteId = 1,
                Name = "<NAME>",
                IsPersisted = true,
            });

            user = Model.Update (new UserModel () {
                RemoteId = 1,
                Name = "Tester",
                DefaultWorkspace = workspace,
                IsPersisted = true,
            });

            var project = Model.Update (new ProjectModel () {
                RemoteId = 1,
                Name = "<NAME>",
{'fraction_non_alphanumeric': 0.10594130279169649, 'fraction_numerical': 0.007301360057265569, 'mean_word_length': 1.5533625730994152, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1997396', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1811, 'n_words': 417}