public async Task MessageCanBeRetrieved()
        {
            // Arrange
            using (var con = OpenConnection())
                using (var cache = new SqliteCache(con))
                {
                    var message = DummyGenerator.CreateDummyMessage();
                    message.ID          = 111;
                    message.SenderID    = 222;
                    message.RecipientID = 333;
                    message.Text        = "Hello World";
                    using (var cmd = con.CreateCommand())
                    {
                        cmd.CommandText = "INSERT INTO Messages (Id, Sender, Recipient, Data) VALUES (111,222,333, @json);";
                        cmd.AddParameter("json", JsonConvert.SerializeObject(message));
                        cmd.ExecuteNonQuery();
                    }

                    // Act
                    var list = await cache.GetMessages();

                    var fromDb = list.FirstOrDefault();

                    // Assert
                    Assert.IsNotNull(fromDb);

                    Assert.AreEqual(message.SenderID, fromDb.Sender);
                    Assert.AreEqual(message.RecipientID, fromDb.Recipient);
                    Assert.AreEqual(message.ID, fromDb.Id);

                    var msg = JsonConvert.DeserializeObject <DirectMessage>(fromDb.Data);
                    Assert.AreEqual(message.Text, msg.Text);
                }
        }
Exemple #2
0
        public void MessageIsAlwaysRejected()
        {
            // Arrange
            var column = new FavoritesTestColumn();

            var msg = DummyGenerator.CreateDummyMessage();

            // Act
            bool suitable = column.Suitable(msg);

            // Assert
            Assert.IsFalse(suitable);
        }
        public void NotifyPropertyChangedIsImplementedCorrectly()
        {
            // Arrange
            var resolver = new Mock <ITypeResolver>();
            var context  = new Mock <IContextEntry>();

            resolver.Setup(r => r.Resolve(typeof(MessageViewModel)))
            .Returns(new MessageViewModel(DummyGenerator.CreateDummyMessage(), context.Object, null, null));

            var vm     = new ComposeMessageViewModel();
            var tester = new PropertyChangedTester(vm, false, resolver.Object);

            // Act
            tester.Test();

            // Assert
            tester.Verify();
        }
        public void ReplyCommandOpensDialog()
        {
            // Arrange
            var viewServices = new Mock <IViewServiceRepository>();

            viewServices.Setup(v => v.ReplyToMessage(It.Is <MessageViewModel>(m => m.Id == 123))).Returns(Task.CompletedTask).Verifiable();

            var context = new Mock <IContextEntry>();

            var msg = DummyGenerator.CreateDummyMessage();

            msg.ID = 123;
            var vm = new MessageViewModel(msg, context.Object, null, viewServices.Object);

            // Act
            vm.ReplyCommand.Execute(null);

            // Assert
            viewServices.Verify(v => v.ReplyToMessage(It.Is <MessageViewModel>(m => m.Id == 123)), Times.Once());
        }
        public async Task ConversationIsCorrectlyLoaded()
        {
            // Arrange
            var user1 = DummyGenerator.CreateDummyUser(1);
            var user2 = DummyGenerator.CreateDummyUser(2);
            var user3 = DummyGenerator.CreateDummyUser(3);

            var msg1 = DummyGenerator.CreateDummyMessage(user1, user2, 1);
            var msg2 = DummyGenerator.CreateDummyMessage(user2, user1, 2);
            var msg3 = DummyGenerator.CreateDummyMessage(user1, user3, 3);

            var messageList = new List <MessageCacheEntry>
            {
                new MessageCacheEntry(1, 1, 2, JsonConvert.SerializeObject(msg1)),
                new MessageCacheEntry(2, 2, 1, JsonConvert.SerializeObject(msg2)),
                new MessageCacheEntry(3, 1, 3, JsonConvert.SerializeObject(msg3))
            };

            var context = new Mock <IContextEntry>();

            context.SetupGet(c => c.UserId).Returns(1);

            var cache = new Mock <ICache>();

            cache.Setup(c => c.GetMessages()).Returns(Task.FromResult(messageList));

            var vm = new MessageDetailsViewModel
            {
                Cache      = cache.Object,
                Dispatcher = new SyncDispatcher(),
                Message    = new MessageViewModel(msg2, context.Object, null, null)
            };

            // Act
            await vm.OnLoad(null);

            // Assert
            Assert.AreEqual(1, vm.PreviousMessages.Count);
        }
        public async Task ClearRemovesAllDataFromCache()
        {
            // Arrange
            using (var con = OpenConnection())
                using (var cache = new SqliteCache(con))
                {
                    await cache.AddHashtags(new[] { "one", "two", "three" });

                    await cache.AddMessages(new[] { new MessageCacheEntry(DummyGenerator.CreateDummyMessage()) });

                    await cache.AddStatuses(new[] { DummyGenerator.CreateDummyStatus() });

                    var userToCache = DummyGenerator.CreateDummyUserEx(1);
                    userToCache.Name = userToCache.ScreenName = "test";
                    await cache.AddUsers(new[] { new UserCacheEntry(userToCache) });

                    // Act
                    await cache.Clear();

                    // Assert
                    var tags = await cache.GetKnownHashtags();

                    Assert.AreEqual(0, tags.Count());

                    var msg = await cache.GetMessages();

                    Assert.AreEqual(0, msg.Count);

                    var user = await cache.GetUser(1);

                    Assert.IsNull(user);

                    var status = await cache.GetStatus(0);

                    Assert.IsNull(status);
                }
        }
        public async Task MessageCanBeAdded()
        {
            // Arrange
            using (var con = OpenConnection())
                using (var cache = new SqliteCache(con))
                {
                    var message = DummyGenerator.CreateDummyMessage();
                    message.SenderID    = 111;
                    message.RecipientID = 222;
                    message.ID          = 333;
                    message.Text        = "Hello World";

                    var entry = new MessageCacheEntry(message);

                    // Act
                    await cache.AddMessages(new[] { entry });

                    // Assert
                    using (var cmd = con.CreateCommand())
                    {
                        cmd.CommandText = "SELECT Id, Sender, Recipient, Data FROM Messages;";
                        using (var reader = cmd.ExecuteReader())
                        {
                            Assert.IsTrue(reader.Read());

                            Assert.AreEqual(333, reader.GetInt32(0));
                            Assert.AreEqual(111, reader.GetInt32(1));
                            Assert.AreEqual(222, reader.GetInt32(2));

                            var json = reader.GetString(3);
                            var from = JsonConvert.DeserializeObject <DirectMessage>(json);
                            Assert.AreEqual(message.Text, from.Text);
                        }
                    }
                }
        }
        public void ReceivingDmMessageAddsDm()
        {
            // Arrange
            var messenger = new Messenger();

            var dm = DummyGenerator.CreateDummyMessage();

            var context    = new Mock <IContextEntry>();
            var definition = new ColumnDefinition(ColumnType.User);
            var config     = new Mock <IConfig>();

            config.SetupGet(c => c.General).Returns(new GeneralConfig());
            var parser = new Mock <IStreamParser>();
            var vm     = new MessageColumn(context.Object, definition, config.Object, parser.Object, messenger)
            {
                Dispatcher = new SyncDispatcher()
            };

            // Act
            messenger.Send(new DmMessage(dm, EntityAction.Create));

            // Assert
            Assert.AreEqual(1, vm.Items.Count);
        }
        public async Task MessagesAreLoadedFromCacheAndTwitter()
        {
            // Arrange
            var dm1 = DummyGenerator.CreateDummyMessage();

            dm1.ID          = 1;
            dm1.Sender      = DummyGenerator.CreateDummyUser(1);
            dm1.Recipient   = DummyGenerator.CreateDummyUser(2);
            dm1.SenderID    = 1;
            dm1.RecipientID = 2;

            var dm2 = DummyGenerator.CreateDummyMessage();

            dm2.ID          = 2;
            dm2.Sender      = DummyGenerator.CreateDummyUser(2);
            dm2.Recipient   = DummyGenerator.CreateDummyUser(1);
            dm2.SenderID    = 2;
            dm2.RecipientID = 1;

            var dm3 = DummyGenerator.CreateDummyMessage();

            dm3.ID          = 3;
            dm3.Sender      = DummyGenerator.CreateDummyUser(1);
            dm3.Recipient   = DummyGenerator.CreateDummyUser(3);
            dm3.SenderID    = 1;
            dm3.RecipientID = 3;

            var cachedMessages = new List <MessageCacheEntry> {
                new MessageCacheEntry(dm1), new MessageCacheEntry(dm2)
            };
            var incomingMessages = new List <DirectMessage> {
                dm1
            };
            var outgoingMessages = new List <DirectMessage> {
                dm2, dm3
            };

            var cache = new Mock <ICache>();

            cache.Setup(c => c.AddMessages(It.IsAny <IList <MessageCacheEntry> >())).Returns(Task.CompletedTask).Verifiable();
            cache.Setup(c => c.GetMessages()).Returns(Task.FromResult(cachedMessages));

            var context = new Mock <IContextEntry>();

            context.SetupGet(c => c.UserId).Returns(1);
            context.Setup(c => c.Twitter.Messages.IncomingMessages(It.IsAny <int>(), It.IsAny <ulong?>())).Returns(
                Task.FromResult(incomingMessages));
            context.Setup(c => c.Twitter.Messages.OutgoingMessages(It.IsAny <int>(), It.IsAny <ulong?>())).Returns(
                Task.FromResult(outgoingMessages));

            var definition = new ColumnDefinition(ColumnType.User);
            var config     = new Mock <IConfig>();

            config.SetupGet(c => c.General).Returns(new GeneralConfig());
            var parser = new Mock <IStreamParser>();
            var vm     = new MessageColumn(context.Object, definition, config.Object, parser.Object)
            {
                Dispatcher = new SyncDispatcher(),
                Cache      = cache.Object
            };

            // Act
            await vm.Load(AsyncLoadContext.Default);

            // Assert
            cache.Verify(c => c.AddMessages(It.IsAny <IList <MessageCacheEntry> >()), Times.Once());
            Assert.AreEqual(2, vm.Items.Count);

            var partners = vm.Items.OfType <MessageViewModel>().Select(i => i.Partner.UserId).Distinct().ToArray();

            CollectionAssert.AreEquivalent(new ulong[] { 2, 3 }, partners);
        }