public Task Write(StoreItems changes) { lock (_syncroot) { foreach (var change in changes) { IStoreItem newValue = change.Value as IStoreItem; IStoreItem oldValue = null; if (_memory.TryGetValue(change.Key, out object x)) { oldValue = x as IStoreItem; } if (oldValue == null || newValue.eTag == "*" || oldValue.eTag == newValue.eTag) { // clone and set etag newValue = FlexObject.Clone <IStoreItem>(newValue); newValue.eTag = (_eTag++).ToString(); _memory[change.Key] = newValue; } else { throw new Exception("etag conflict"); } } } return(Task.CompletedTask); }
public void FlexObject_JsonNestedDynamicSeralizeFormat() { var parent = new FlexObject(); parent["test"] = "testProperty"; var child = new FlexObject(); child["prop1"] = "property1"; parent["nested"] = child; var parentJson = JsonConvert.SerializeObject(parent); string targetJson = @" { 'test' : 'testProperty', 'nested' : { 'prop1' : 'property1' } }"; JObject target = JObject.Parse(targetJson); JObject fromFlexObject = JObject.Parse(parentJson); bool areSame = JToken.DeepEquals(target, fromFlexObject); Assert.IsTrue(areSame, "Json documents did not match"); }
public void FlexObject_DynamicProperties() { var testValue = "testValue"; var context = new FlexObject(); context["test"] = testValue; Assert.AreEqual(context["test"], "testValue"); }
public void CalculateFizzBuzz_ReturnsCorrectResult() { var six = new FlexObject {Value = 6}; Service.CalculateFizzBuzz(six, 2, 4).Should().Be(DivisibilityService.Fizz); Service.CalculateFizzBuzz(six, 4, 6).Should().Be(DivisibilityService.Buzz); Service.CalculateFizzBuzz(six, 1, 3).Should().Be(DivisibilityService.FizzBuzz); Service.CalculateFizzBuzz(six, 4, 5).Should().Be(DivisibilityService.NotApplicable); }
public void FlexObject_JsonSimpleSeralizeFormat() { var flex = new FlexObject(); flex["test"] = "testProperty"; var json = JsonConvert.SerializeObject(flex); string targetJson = "{'test':'testProperty'}"; JObject target = JObject.Parse(targetJson); JObject fromFlexObject = JObject.Parse(json); bool areSame = JToken.DeepEquals(target, fromFlexObject); Assert.IsTrue(areSame, "Json documents did not match"); }
public Task <StoreItems> Read(string[] keys) { var storeItems = new StoreItems(); lock (_syncroot) { foreach (var key in keys) { if (_memory.TryGetValue(key, out object value)) { if (value != null) { storeItems[key] = FlexObject.Clone(value); } else { storeItems[key] = null; } } } } return(Task.FromResult(storeItems)); }
public void FlexObject_JsonConcreteSeralizeFormat() { var parent = new FlexObject(); parent["test"] = "testProperty"; parent["nested"] = new Nested(); var parentJson = JsonConvert.SerializeObject(parent); string correctJson = @" { 'test' : 'testProperty', 'nested' : { 'Property1' : 'one' } }"; JObject target = JObject.Parse(correctJson); JObject fromFlexObject = JObject.Parse(parentJson); bool areSame = JToken.DeepEquals(target, fromFlexObject); Assert.IsTrue(areSame, "Json documents did not match"); }
protected async Task _updateObjectTest(IStorage storage) { dynamic storeItems = new StoreItems(); storeItems.updatePocoItem = new PocoItem() { Id = "1", Count = 1 }; storeItems.updatePocoStoreItem = new PocoStoreItem() { Id = "1", Count = 1 }; storeItems.updateStoreItem = new StoreItem(); storeItems.updateStoreItem.Id = "3"; storeItems.updateStoreItem.Count = 1; //first write should work await storage.Write(storeItems); dynamic result = await storage.Read(((StoreItems)storeItems).GetDynamicMemberNames().ToArray()); Assert.IsNotNull(result.updatePocoStoreItem.eTag, "updatePocoItem.eTag should not be null"); Assert.IsNotNull(result.updateStoreItem.eTag, "updateStoreItem.eTag should not be null"); // 2nd write should work, because we have new etag, or no etag result.updatePocoItem.Count++; result.updatePocoStoreItem.Count++; result.updateStoreItem.Count++; await storage.Write(result); dynamic result2 = await storage.Read(((StoreItems)storeItems).GetDynamicMemberNames().ToArray()); Assert.IsNotNull(result2.updatePocoStoreItem.eTag, "updatePocoItem.eTag should not be null"); Assert.IsNotNull(result2.updateStoreItem.eTag, "updateStoreItem.eTag should not be null"); Assert.AreNotEqual(result.updatePocoStoreItem.eTag, result2.updatePocoStoreItem.eTag, "updatePocoItem.eTag should not be different"); Assert.AreNotEqual(result.updateStoreItem.eTag, result2.updateStoreItem.eTag, "updateStoreItem.eTag should not be different"); Assert.AreEqual(result2.updatePocoItem.Count, 2, "updatePocoItem.Count should be 2"); Assert.AreEqual(result2.updatePocoStoreItem.Count, 2, "updatePocoStoreItem.Count should be 2"); Assert.AreEqual(result2.updateStoreItem.Count, 2, "updateStoreItem.Count should be 2"); // write with old etag should succeed for updatePocoItem, but fail for the other 2 try { dynamic storeItemsUpdate = new StoreItems(); storeItemsUpdate.updatePocoItem = result.updatePocoItem; storeItemsUpdate.updatePocoItem.Count++; await storage.Write(storeItemsUpdate); } catch { Assert.Fail("Should not throw exception on write with pocoItem"); } try { dynamic storeItemsUpdate = new StoreItems(); storeItemsUpdate.updatePocoStoreItem = result.updatePocoStoreItem; storeItemsUpdate.updatePocoStoreItem.Count++; await storage.Write(storeItemsUpdate); Assert.Fail("Should not throw exception on write with pocoStoreItem because of old etag"); } catch { } try { dynamic storeItemsUpdate = new StoreItems(); storeItemsUpdate.updateStoreItem = result.updateStoreItem; storeItemsUpdate.updateStoreItem.Count++; await storage.Write(storeItemsUpdate); Assert.Fail("Should not throw exception on write with StoreItem because of old etag"); } catch { } dynamic result3 = await storage.Read(((StoreItems)storeItems).GetDynamicMemberNames().ToArray()); Assert.AreEqual(result3.updatePocoItem.Count, 3, "updatePocoItem.Count should be 3"); Assert.AreEqual(result3.updatePocoStoreItem.Count, 2, "updatePocoStoreItem.Count should be 2"); Assert.AreEqual(result3.updateStoreItem.Count, 2, "updateStoreItem.Count should be 2"); // write with wildcard etag should work result3.updatePocoItem.Count = 100; result3.updatePocoStoreItem.Count = 100; result3.updatePocoStoreItem.eTag = "*"; result3.updateStoreItem.Count = 100; result3.updateStoreItem.eTag = "*"; await storage.Write(result3); dynamic result4 = await storage.Read(((StoreItems)storeItems).GetDynamicMemberNames().ToArray()); Assert.AreEqual(result3.updatePocoItem.Count, 100, "updatePocoItem.Count should be 100"); Assert.AreEqual(result3.updatePocoStoreItem.Count, 100, "updatePocoStoreItem.Count should be 100"); Assert.AreEqual(result3.updateStoreItem.Count, 100, "updateStoreItem.Count should be 100"); // write with empty etag should not work try { dynamic storeItemsUpdate = new StoreItems(); storeItemsUpdate.updatePocoStoreItem = FlexObject.Clone(result4.updatePocoStoreItem); storeItemsUpdate.updatePocoStoreItem.eTag = ""; await storage.Write(result); Assert.Fail("Should not throw exception on write with pocoStoreItem because of empty etag"); } catch { } try { dynamic storeItemsUpdate = new StoreItems(); storeItemsUpdate.updateStoreItem = FlexObject.Clone(result4.updateStoreItem); storeItemsUpdate.updateStoreItem.eTag = ""; await storage.Write(result); Assert.Fail("Should not throw exception on write with storeItem because of empty etag"); } catch { } dynamic result5 = await storage.Read(((StoreItems)storeItems).GetDynamicMemberNames().ToArray()); Assert.AreEqual(result3.updatePocoItem.Count, 100, "updatePocoItem.Count should be 100"); Assert.AreEqual(result3.updatePocoStoreItem.Count, 100, "updatePocoStoreItem.Count should be 100"); Assert.AreEqual(result3.updateStoreItem.Count, 100, "updateStoreItem.Count should be 100"); }
public override Task OnReceiveActivity(IBotContext context) { if (HasActiveTopic) { ActiveTopic.OnReceiveActivity(context); return(Task.CompletedTask); } if (context.TopIntent != null) { foreach (LuisEntity item in context.TopIntent.Entities) { // CUSTOMER if (item.Type == "entity.customer") { this.State.Workitem.Customer = item.Value; } //DATE if (item.Type == "builtin.datetimeV2.date") { FlexObject resolution = item.Resolution; var instance = JsonConvert.SerializeObject(resolution["values"][0]); TimeEx time = JsonConvert.DeserializeObject <TimeEx>(instance); this.State.Workitem.Date = DateTime.Parse(time.value); } } } if (this.State.Workitem.Object == null) { this.SetActiveTopic(Constants.OBJECT_PROMPT); this.ActiveTopic.OnReceiveActivity(context); return(Task.CompletedTask); } if (this.State.Workitem.Customer == null) { this.SetActiveTopic(Constants.CUSTOMER_PROMPT); this.ActiveTopic.OnReceiveActivity(context); return(Task.CompletedTask); } if (this.State.Workitem.Date == null) { this.SetActiveTopic(Constants.DATE_PROMPT); this.ActiveTopic.OnReceiveActivity(context); return(Task.CompletedTask); } if (this.State.Workitem.Hours == null) { this.SetActiveTopic(Constants.HOURS_PROMPT); this.ActiveTopic.OnReceiveActivity(context); return(Task.CompletedTask); } if (this.State.Workitem.Description == null) { this.SetActiveTopic(Constants.DESCRIPTION_PROMPT); this.ActiveTopic.OnReceiveActivity(context); return(Task.CompletedTask); } if (this.State.Workitem.Attachment == null) { this.SetActiveTopic(Constants.ATTACHMENT_PROMPT); this.ActiveTopic.OnReceiveActivity(context); return(Task.CompletedTask); } this.OnSuccess(context, this.State.Workitem); return(Task.CompletedTask); }
public void IsDivisble_DividingByZero_ReturnsFalse() { var ten = new FlexObject { Value = 10 }; Service.IsDivisble(ten, 0).Should().BeFalse(); }
public void IsDivisble_DividingByZero_DoesNotThrowException() { var ten = new FlexObject { Value = 10 }; Service.Invoking(s => s.IsDivisble(ten, 0)).ShouldNotThrow<DivideByZeroException>(); }