public void Should_Register_Failed_Items_Correctly()
        {
            var errorMessage1 = Guid.NewGuid().ToString();

            _updateResult.Failed(_assessment1, errorMessage1);
            var errorMessage2 = Guid.NewGuid().ToString();

            _updateResult.Failed(_assessment2, errorMessage2);

            Assert.That(_updateResult.GetErrorMessage(_assessment1), Is.EqualTo(errorMessage1));
            Assert.That(_updateResult.GetErrorMessage(_assessment2), Is.EqualTo(errorMessage2));
            Assert.That(_updateResult.FailedItems, Is.EquivalentTo(new[] { _assessment1, _assessment2 }));
        }
        public async Task <UpdateResult <T> > Push <T>(string userToken, Predicate <T> criteria = null) where T : BaseModel
        {
            var headers = new Dictionary <string, string>(Connection.Headers)
            {
                { "user-token", userToken }
            };

            criteria = criteria ?? (a => true);

            var context = PersistenceContextFactory.CreateFor <T>();
            var items   = await Asyncer.Async(() => context.LoadDeletedObjects());

            var result = new UpdateResult <T>();

            foreach (var item in items.Where(i => criteria(i)))
            {
                var url  = Connection.GenerateTableUrl <T>(item.ObjectId);
                var json = await Caller.Delete(url, headers);

                var asDictionary = Serializer.AsDictionary(json);
                if (asDictionary.ContainsKey(DeletionTime))
                {
                    context.Purge(item.ObjectId);
                    result.Success(item, item.ObjectId);
                }
                else
                {
                    var parsed = ResponseParser.Parse <T>(json);
                    result.Failed(item, parsed.ToString());
                }
            }
            return(result);
        }
Esempio n. 3
0
        private static UpdateResult <T> Failure <T>(BackendlessResponse <T> parsed) where T : BaseModel
        {
            var result = new UpdateResult <T>();

            result.Failed(null, parsed.ToString());
            return(result);
        }
Esempio n. 4
0
        private Tuple <UpdateResult <Assessment>, Assessment> PrepareFailedSyncResult()
        {
            var localId    = Guid.NewGuid().ToString();
            var assessment = new Assessment {
                ObjectId = Guid.NewGuid().ToString()
            };
            var syncResult = new UpdateResult <Assessment>();

            syncResult.Failed(assessment, localId);
            return(new Tuple <UpdateResult <Assessment>, Assessment>(syncResult, assessment));
        }
Esempio n. 5
0
        public async Task Should_NOT_Pull_Attributes_When_Assessments_Are_Not_Pulled_Successfully()
        {
            SetupEmptyPush();
            var assessmentUpdateResult = new UpdateResult <Assessment>();

            assessmentUpdateResult.Failed(CreateItem <Assessment>(), "error!!");
            _pullService.Pull <Assessment>(_query).Returns(Task.FromResult(assessmentUpdateResult));

            var syncResult = await _syncService.Sync(_user);

            Assert.That(syncResult.HasErrors);
            await _pullService.DidNotReceiveWithAnyArgs().Pull <AssessmentAttribute>();
        }
        public async Task Should_Return_Failure_When_Pull_Is_Not_Successful()
        {
            var result = new UpdateResult <Disaster>();

            result.Success(new Disaster {
                ObjectId = "fff"
            }, "ccc");
            result.Failed(null, "test error");
            _assessmentContext.LoadAll().Returns(new[] { CreateItem <Assessment>() });
            _shelterContext.LoadAll().Returns(new[] { CreateItem <Shelter>() });
            _pullService.Pull <Disaster>().Returns(Task.FromResult(result));

            var syncResult = await _service.Sync(_user);

            Assert.That(syncResult.HasErrors);
            _assessmentContext.DidNotReceive().Purge(Arg.Any <string>());
            _shelterContext.DidNotReceive().Purge(Arg.Any <string>());
        }
Esempio n. 7
0
        public async Task Should_Not_Purge_Anything_When_Attribute_Was_Not_Pulled_Successfully()
        {
            SetupEmptyPush();
            var attributePullResult = new UpdateResult <Assessment>();

            attributePullResult.Failed(CreateItem <Assessment>(), "error!!");
            _pullService.Pull <Assessment>(_query).Returns(Task.FromResult(attributePullResult));

            var assessmentContext          = _persistenceContextFactory.CreateFor <Assessment>();
            var attr1                      = CreateItem <AssessmentAttribute>();
            var attr2                      = CreateItem <AssessmentAttribute>();
            var assessmentAttributeContext = _persistenceContextFactory.CreateFor <AssessmentAttribute>();

            await SetupPurgeTest(assessmentContext, attr1, attr2, assessmentAttributeContext);

            var syncResult = await _syncService.Sync(_user);

            Assert.That(syncResult.HasErrors);
            assessmentAttributeContext.DidNotReceiveWithAnyArgs().Purge("");
            assessmentContext.DidNotReceiveWithAnyArgs().Purge("");
        }
        public async Task <UpdateResult <T> > Push <T>(string userToken, Predicate <T> criteria = null) where T : BaseModel
        {
            var headers = new Dictionary <string, string>(Connection.Headers)
            {
                { "user-token", userToken }
            };

            criteria = criteria ?? (a => true);

            var context = PersistenceContextFactory.CreateFor <T>();
            var items   = await Asyncer.Async(() => context.LoadAll());

            var result = new UpdateResult <T>();

            foreach (var item in items.Where(i => criteria(i)))
            {
                var json = await PushItem(item, headers);

                var parsed = ResponseParser.Parse <T>(json);
                if (parsed.IsSuccessful)
                {
                    var content = parsed.Content.Single();
                    context.Save(content);
                    if (item.IsNew())
                    {
                        context.Purge(item.ObjectId);
                    }
                    result.Success(content, item.ObjectId);
                }
                else
                {
                    result.Failed(item, parsed.ToString());
                }
            }
            return(result);
        }