public void AddEvents_Cache_Success() { var client = new KeenClient(SettingsEnv, new EventCacheMemory()); if (UseMocks) client.Event = new EventMock(SettingsEnv, addEvents: (e, p) => { // Should not be called with caching enabled Assert.Fail(); return new List<CachedEvent>(); }); var events = from i in Enumerable.Range(1, 3) select new { AProperty = "AValue" + i }; Assert.DoesNotThrow(() => client.AddEvents("AddEventTest", events)); // reset the AddEvents mock if (UseMocks) client.Event = new EventMock(SettingsEnv, addEvents: (e, p) => { Assert.True(p == SettingsEnv, "Incorrect settings"); Assert.NotNull(e.Property("AddEventTest"), "Expected collection not found"); Assert.AreEqual(e.Property("AddEventTest").Value.AsEnumerable().Count(), 3, "Expected exactly 3 collection members"); foreach (var q in e["AddEventTest"]) Assert.NotNull(q["keen"]["timestamp"], "keen.timestamp properties should exist"); return new List<CachedEvent>(); }); Assert.DoesNotThrow(client.SendCachedEvents); }
public void AddEvents_InvalidProject_Throws() { var settings = new ProjectSettingsProvider(projectId: "X", writeKey: SettingsEnv.WriteKey); var client = new KeenClient(settings); if (UseMocks) client.Event = new EventMock(settings, addEvents: (e, p) => { if ((p == settings) &&(p.ProjectId=="X")) throw new KeenResourceNotFoundException(); throw new Exception("Unexpected value"); }); Assert.Throws<KeenResourceNotFoundException>(() => client.AddEvents("AddEventTest", new []{ new {AProperty = "AValue" }})); }
public void IpToGeo_Send_Success() { var client = new KeenClient(SettingsEnv); if (UseMocks) client.EventCollection = new EventCollectionMock(SettingsEnv, addEvent: (c, e, p) => { if (!e["keen"].ToString().Contains("keen:ip_to_geo")) throw new Exception("Unexpected values"); }); var a = AddOn.IpToGeo("an_ip", "geocode"); Assert.DoesNotThrow(() => client.AddEvent("AddOnTest", new {an_ip = "70.187.8.97"}, new List<AddOn> {a})); }
public void IpToGeo_MissingInput_Throws() { var client = new KeenClient(SettingsEnv); if (UseMocks) client.EventCollection = new EventCollectionMock(SettingsEnv, addEvent: (c, e, p) => { if (!e["keen"].ToString().Contains("\"ip\": \"an_ip\"")) throw new KeenException("Unexpected values"); }); var a = AddOn.IpToGeo("wrong_field", "geocode"); Assert.Throws<KeenException>( () => client.AddEvent("AddOnTest", new {an_ip = "70.187.8.97"}, new List<AddOn> {a})); }
public void UrlParser_Send_Success() { var client = new KeenClient(SettingsEnv); if (UseMocks) client.EventCollection = new EventCollectionMock(SettingsEnv, addEvent: (c, e, p) => { if (!e["keen"].ToString().Contains("keen:url_parser")) throw new Exception("Unexpected values"); }); var a = AddOn.UrlParser("url", "url_parsed"); Assert.DoesNotThrow( () => client.AddEvent("AddOnTest", new {url = "https://keen.io/docs/data-collection/data-enrichment/#anchor"}, new List<AddOn> {a})); }
public void UserAgentParser_Send_Success() { var client = new KeenClient(SettingsEnv); if (UseMocks) client.EventCollection = new EventCollectionMock(SettingsEnv, addEvent: (c, e, p) => { if (!e["keen"].ToString().Contains("keen:ua_parser")) throw new Exception("Unexpected values"); }); var a = AddOn.UserAgentParser("user_agent_string", "user_agent_parsed"); Assert.DoesNotThrow( () => client.AddEvent("AddOnTest", new { user_agent_string = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36" }, new List<AddOn> {a})); }
public void AddGlobalProperty_DelegateNullDynamicValue_Throws() { var client = new KeenClient(SettingsEnv); Assert.Throws<KeenException>(() => client.AddGlobalProperty("AGlobal", new DynamicPropertyValue(() => null))); }
public void AddGlobalProperty_DelegateValueProviderNullReturn_Throws() { var client = new KeenClient(SettingsEnv); if (UseMocks) client.EventCollection = new EventCollectionMock(SettingsEnv, addEvent: (c, e, p) => { if ((p == SettingsEnv) && (c == "AddEventTest") && (e["AProperty"].Value<string>() == "AValue") && (e["AGlobal"].Value<string>() == "value")) return; throw new Exception("Unexpected value"); }); var i = 0; // return valid for the first two tests, then null client.AddGlobalProperty("AGlobal", new DynamicPropertyValue(() => i++ > 1 ? null : "value")); // This is the second test (AddGlobalProperty runs the first) Assert.DoesNotThrow(() => client.AddEvent("AddEventTest", new { AProperty = "AValue" })); // Third test should fail. Assert.Throws<KeenException>(() => client.AddEvent("AddEventTest", new { AProperty = "AValue" })); }
public void CachingPCL_SendEmptyEvents_Success(IEventCache cache) { var client = new KeenClient(SettingsEnv, cache); Assert.DoesNotThrow(client.SendCachedEvents); }
public void AddGlobalProperty_DelegateArrayValue_Success() { var client = new KeenClient(SettingsEnv); if (UseMocks) client.EventCollection = new EventCollectionMock(SettingsEnv, addEvent: (c, e, p) => { if ((p == SettingsEnv) && (c == "AddEventTest") && (e["AProperty"].Value<string>() == "AValue") && (e["AGlobal"].Values<int>().All(x => (x == 1) || (x == 2) || (x == 3)))) return; throw new Exception("Unexpected value"); }); Assert.DoesNotThrow(() => { client.AddGlobalProperty("AGlobal", new DynamicPropertyValue(() => new[] { 1, 2, 3 })); client.AddEvent("AddEventTest", new { AProperty = "AValue" }); }); }
public void AddGlobalProperty_InvalidValueNameLength_Throws() { var client = new KeenClient(SettingsEnv); Assert.Throws <KeenException>(() => client.AddGlobalProperty(new String('A', 256), "AValue")); }
public void AddGlobalProperty_InvalidValueNameLength_Throws() { var client = new KeenClient(SettingsEnv); Assert.Throws<KeenException>(() => client.AddGlobalProperty(new String('A', 256), "AValue")); }
public void Caching_ClearEvents_Success() { var client = new KeenClient(SettingsEnv, new EventCacheMemory()); Assert.DoesNotThrow(() => client.EventCache.Clear()); }
public void Caching_SendManyEvents_Success() { var client = new KeenClient(SettingsEnv, new EventCacheMemory()); var total = 0; if (UseMocks) client.Event = new EventMock(SettingsEnv, addEvents: (e, p) => { if ((p == SettingsEnv) && (null != e.Property("CachedEventTest")) && ((e.Property("CachedEventTest").Value.Children().Count() == KeenConstants.BulkBatchSize))) { total += e.Property("CachedEventTest").Value.Children().Count(); return new List<CachedEvent>(); } throw new Exception("Unexpected value"); }); for (int i = 0; i < KeenConstants.BulkBatchSize; i++) client.AddEvent("CachedEventTest", new { AProperty = "AValue" }); Assert.DoesNotThrow(client.SendCachedEvents); Assert.Null(client.EventCache.TryTake(), "Cache is empty"); Assert.True( !UseMocks || ( total == KeenConstants.BulkBatchSize)); }
public void AddGlobalProperty_DelegateValueProviderThrows_Throws() { var client = new KeenClient(SettingsEnv); Assert.Throws <KeenException>(() => client.AddGlobalProperty("AGlobal", new DynamicPropertyValue(() => { throw new Exception("test exception"); }))); }
public void Caching_SendEmptyEvents_Success() { var client = new KeenClient(SettingsEnv, new EventCacheMemory()); Assert.DoesNotThrow(client.SendCachedEvents); }
public void AddGlobalProperty_DelegateNullValueProvider_Throws() { var client = new KeenClient(SettingsEnv); Assert.Throws <KeenException>(() => client.AddGlobalProperty("AGlobal", null)); }
public void AddGlobalProperty_DelegateNullDynamicValue_Throws() { var client = new KeenClient(SettingsEnv); Assert.Throws <KeenException>(() => client.AddGlobalProperty("AGlobal", new DynamicPropertyValue(() => null))); }
public void AddGlobalProperty_InvalidValueNameBlank_Throws() { var client = new KeenClient(SettingsEnv); Assert.Throws <KeenException>(() => client.AddGlobalProperty("", "AValue")); }
public void Caching_SendEmptyEvents_Success() { var client = new KeenClient(SettingsEnv, new EventCacheMemory()); Assert.DoesNotThrow(client.SendCachedEvents); }
public async Task Async_GetCollectionSchema_NullProjectId_Throws() { var client = new KeenClient(settingsEnv); await client.GetSchemaAsync(null); }
public void Caching_AddEvents_Success() { var client = new KeenClient(SettingsEnv, new EventCacheMemory()); Assert.DoesNotThrow(() => client.AddEvent("CachedEventTest", new { AProperty = "AValue" })); Assert.DoesNotThrow(() => client.AddEvent("CachedEventTest", new { AProperty = "AValue" })); Assert.DoesNotThrow(() => client.AddEvent("CachedEventTest", new { AProperty = "AValue" })); }
public async Task Async_AddEvents_NullCollection_Throws() { var client = new KeenClient(settingsEnv); await client.AddEventsAsync("AddEventTest", null); }
public void AddEvents_NoCache_Success() { var client = new KeenClient(SettingsEnv); var done = false; if (UseMocks) client.Event = new EventMock(SettingsEnv, addEvents: (e, p) => { done = true; Assert.True(p == SettingsEnv, "Incorrect settings"); Assert.NotNull(e.Property("AddEventTest"), "Expected collection not found"); Assert.AreEqual(e.Property("AddEventTest").Value.AsEnumerable().Count(), 3, "Expected exactly 3 collection members"); foreach (var q in e["AddEventTest"]) Assert.NotNull(q["keen"]["timestamp"], "keen.timestamp properties should exist"); return new List<CachedEvent>(); }); var events = from i in Enumerable.Range(1,3) select new { AProperty = "AValue" + i}; Assert.DoesNotThrow(() => client.AddEvents("AddEventTest", events)); Assert.True((UseMocks && done) || !UseMocks, "AddEvent mock was not called"); }
public void GetCollectionSchema_NullProjectId_Throws() { var client = new KeenClient(settingsEnv); Assert.Throws <KeenException>(() => client.GetSchema(null)); }
public void DeleteCollection_Success() { var client = new KeenClient(SettingsEnv); if (UseMocks) client.EventCollection = new EventCollectionMock(SettingsEnv, deleteCollection: (c, p) => { if ((p == SettingsEnv) && (c == "DeleteColTest")) return; throw new Exception("Unexpected value"); }); // Idempotent, does not matter if collection does not exist. Assert.DoesNotThrow(() => client.DeleteCollection("DeleteColTest")); }
public void AddEvent_InvalidCollectionNameDollarSign_Throws() { var client = new KeenClient(settingsEnv); Assert.Throws <KeenException>(() => client.AddEvent("$Invalid", new { AProperty = "AValue" })); }
public void AddGlobalProperty_InvalidValueNameBlank_Throws() { var client = new KeenClient(SettingsEnv); Assert.Throws<KeenException>(() => client.AddGlobalProperty("", "AValue")); }
public void AddEvent_InvalidCollectionNameTooLong_Throws() { var client = new KeenClient(settingsEnv); Assert.Throws <KeenException>(() => client.AddEvent(new String('X', 257), new { AProperty = "AValue" })); }
public void AddGlobalProperty_DelegateSimpleValue_Success() { var client = new KeenClient(SettingsEnv); if (UseMocks) client.EventCollection = new EventCollectionMock(SettingsEnv, addEvent: (c, e, p) => { if ((p == SettingsEnv) && (c == "AddEventTest") && (e["AProperty"].Value<string>() == "AValue") && (e["AGlobal"]!=null)) return; throw new Exception("Unexpected value"); }); Assert.DoesNotThrow(() => { client.AddGlobalProperty("AGlobal", new DynamicPropertyValue(() => DateTime.Now.Millisecond)); client.AddEvent("AddEventTest", new { AProperty = "AValue" }); client.AddEvent("AddEventTest", new { AProperty = "AValue" }); }); }
public void Async_AddEvents_NullCollection_Throws() { var client = new KeenClient(SettingsEnv); Assert.ThrowsAsync <Keen.Core.KeenException>(() => client.AddEventsAsync("AddEventTest", null)); }
public void AddGlobalProperty_DelegateObjectValue_Success() { var client = new KeenClient(SettingsEnv); if (UseMocks) client.EventCollection = new EventCollectionMock(SettingsEnv, addEvent: (c, e, p) => { if ((p == SettingsEnv) && (c == "AddEventTest") && (e["AProperty"].Value<string>() == "AValue") && (e["AGlobal"].Value<JObject>()["SubProp1"].Value<string>() == "Value")) return; throw new Exception("Unexpected value"); }); Assert.DoesNotThrow(() => { client.AddGlobalProperty("AGlobal", new DynamicPropertyValue(() => new { SubProp1 = "Value", SubProp2 = "Value" })); client.AddEvent("AddEventTest", new { AProperty = "AValue" }); }); }
public void AddEvent_ValidProjectIdInvalidWriteKey_Throws() { var settings = new ProjectSettingsProvider(projectId: SettingsEnv.ProjectId, writeKey: "X"); var client = new KeenClient(settings); if (UseMocks) client.EventCollection = new EventCollectionMock(settings, addEvent: (c, e, p) => { if ((p == settings) && (c == "X") && (e["X"].Value<string>() == "X")) throw new KeenInvalidApiKeyException(c); }); Assert.Throws<KeenInvalidApiKeyException>(() => client.AddEvent("X", new { X = "X" })); }
public void AddGlobalProperty_DelegateNullValueProvider_Throws() { var client = new KeenClient(SettingsEnv); Assert.Throws<KeenException>(() => client.AddGlobalProperty("AGlobal", null)); }
public void AddEvent_InvalidCollectionNameDollarSign_Throws() { var client = new KeenClient(SettingsEnv); Assert.Throws<KeenException>(() => client.AddEvent("$Invalid", new { AProperty = "AValue" })); }
public void AddGlobalProperty_DelegateValueProviderThrows_Throws() { var client = new KeenClient(SettingsEnv); Assert.Throws<KeenException>(() => client.AddGlobalProperty("AGlobal", new DynamicPropertyValue(() => { throw new Exception("test exception"); }))); }
public void Async_GetCollectionSchema_NullProjectId_Throws() { var client = new KeenClient(SettingsEnv); Assert.ThrowsAsync <Keen.Core.KeenException>(() => client.GetSchemaAsync(null)); }
public void Caching_ClearEvents_Success() { var client = new KeenClient(SettingsEnv, new EventCacheMemory()); Assert.DoesNotThrow(() => client.EventCache.Clear()); }
public void CachingPCL_ClearEvents_Success(IEventCache cache) { var client = new KeenClient(SettingsEnv, cache); Assert.DoesNotThrow(() => client.EventCache.Clear()); }
public void Caching_SendEvents_Success() { var client = new KeenClient(SettingsEnv, new EventCacheMemory()); if (UseMocks) client.Event = new EventMock(SettingsEnv, addEvents: (e, p) => { if ((p == SettingsEnv) && (null!=e.Property("CachedEventTest")) && (e.Property("CachedEventTest").Value.Children().Count()==3)) return new List<CachedEvent>(); throw new Exception("Unexpected value"); }); client.AddEvent("CachedEventTest", new { AProperty = "AValue" }); client.AddEvent("CachedEventTest", new { AProperty = "AValue" }); client.AddEvent("CachedEventTest", new { AProperty = "AValue" }); Assert.DoesNotThrow(client.SendCachedEvents); Assert.Null(client.EventCache.TryTake(), "Cache is empty"); }
public async Task Funnel_Simple_Success() { var client = new KeenClient(SettingsEnv); var timeframe = QueryRelativeTimeframe.ThisHour(); IEnumerable <FunnelStep> funnelsteps = new List <FunnelStep> { new FunnelStep { EventCollection = FunnelColA, ActorProperty = "id", }, new FunnelStep { EventCollection = FunnelColB, ActorProperty = "id" }, }; var expected = new FunnelResult { Steps = new [] { new FunnelResultStep { EventCollection = FunnelColA, }, new FunnelResultStep { EventCollection = FunnelColB, }, }, Result = new[] { 3, 2 } }; Mock <IQueries> queryMock = null; if (UseMocks) { queryMock = new Mock <IQueries>(); queryMock.Setup(m => m.Funnel( It.Is <IEnumerable <FunnelStep> >(f => f.Equals(funnelsteps)), It.Is <IQueryTimeframe>(t => t == timeframe), It.Is <string>(t => t == "") )) .Returns(Task.FromResult(expected)); client.Queries = queryMock.Object; } var reply = (await client.QueryFunnelAsync(funnelsteps, timeframe)); Assert.NotNull(reply); Assert.NotNull(reply.Result); Assert.NotNull(reply.Steps); Assert.AreEqual(reply.Steps.Count(), 2); if (null != queryMock) { queryMock.VerifyAll(); } }
public void Caching_SendInvalidEvents_Throws() { var client = new KeenClient(SettingsEnv, new EventCacheMemory()); if (UseMocks) client.Event = new EventMock(SettingsEnv, addEvents: (e, p) => { if (p == SettingsEnv) throw new KeenBulkException("Mock exception", new List<CachedEvent> {new CachedEvent("CachedEventTest", e, new Exception())}); throw new Exception("Unexpected value"); }); var anEvent = new JObject {{"AProperty", "AValue"}}; client.AddEvent("CachedEventTest", anEvent); anEvent.Add("keen", JObject.FromObject(new {invalidPropName = "value"} )); client.AddEvent("CachedEventTest", anEvent); Assert.DoesNotThrow(() => { try { client.SendCachedEvents(); } catch (KeenBulkException ex) { if (ex.FailedEvents.Count() != 1) throw new Exception("Expected 1 failed event."); } }); }
public void AddEvent_ScopedKeyWrite_Success() { const string scope = "{\"timestamp\": \"2014-02-25T22:09:27.320082\", \"allowed_operations\": [\"write\"]}"; var scopedKey = ScopedKey.EncryptString(SettingsEnv.MasterKey, scope); var settings = new ProjectSettingsProvider(masterKey: SettingsEnv.MasterKey, projectId: SettingsEnv.ProjectId, writeKey: scopedKey); var client = new KeenClient(settings); if (UseMocks) client.EventCollection = new EventCollectionMock(settings, addEvent: (c, e, p) => { var key = JObject.Parse(ScopedKey.Decrypt(p.MasterKey, p.WriteKey)); if ((key["allowed_operations"].Values<string>().First()=="write") && (p == settings) && (c == "AddEventTest") && (e["AProperty"].Value<string>()=="CustomKey")) return; throw new Exception("Unexpected value"); }); Assert.DoesNotThrow(() => client.AddEvent("AddEventTest", new { AProperty = "CustomKey" })); }
public void AddEvent_InvalidCollectionNameTooLong_Throws() { var client = new KeenClient(SettingsEnv); Assert.Throws<KeenException>(() => client.AddEvent(new String('X', 257), new { AProperty = "AValue" })); }
public void AddEvent_InvalidCollectionNameNull_Throws() { var client = new KeenClient(SettingsEnv); Assert.Throws <KeenException>(() => client.AddEvent(null, new { AProperty = "AValue" })); }
public void AddEvent_InvalidKeenNamespaceProperty_Throws() { var client = new KeenClient(SettingsEnv); if (UseMocks) client.EventCollection = new EventCollectionMock(SettingsEnv, addEvent: (c,e,p) => { if ((p == SettingsEnv) && (c == "X") && (null != e.Property("keen")) && (e.Property("keen").Value.GetType()==typeof(JObject)) && (null!=((JObject)e.Property("keen").Value).Property("AProperty"))) throw new KeenInvalidKeenNamespacePropertyException(); throw new Exception("Unexpected value"); }); Assert.Throws<KeenInvalidKeenNamespacePropertyException>(() => client.AddEvent("X", new { keen = new { AProperty = "AValue" } })); }
public void AddEvent_Success() { var client = new KeenClient(SettingsEnv); if (UseMocks) client.EventCollection = new EventCollectionMock(SettingsEnv, addEvent: (c, e, p) => { if ((p == SettingsEnv) && (c == "AddEventTest") && (e["AProperty"].Value<string>()=="AValue")) return; throw new Exception("Unexpected values"); }); Assert.DoesNotThrow(() => client.AddEvent("AddEventTest", new { AProperty = "AValue" })); }
public void ReferrerParser_Send_Success() { var client = new KeenClient(SettingsEnv); if (UseMocks) client.EventCollection = new EventCollectionMock(SettingsEnv, addEvent: (c, e, p) => { if (!e["keen"].ToString().Contains("keen:referrer_parser")) throw new Exception("Unexpected values"); }); var a = AddOn.ReferrerParser("referrer", "page", "referrer_parsed"); Assert.DoesNotThrow(() => client.AddEvent("AddOnTest", new {page = "", referrer = ""}, new List<AddOn> {a})); }
public async Task Funnel_ValidTimeframeInSteps_Success() { var client = new KeenClient(SettingsEnv); IEnumerable <FunnelStep> funnelsteps = new [] { new FunnelStep { EventCollection = FunnelColA, ActorProperty = "id", Timeframe = QueryRelativeTimeframe.ThisMonth(), }, new FunnelStep { EventCollection = FunnelColB, ActorProperty = "id", Timeframe = new QueryAbsoluteTimeframe(DateTime.Now.AddDays(-30), DateTime.Now), }, }; var expected = new FunnelResult { Steps = new[] { new FunnelResultStep { EventCollection = FunnelColA, }, new FunnelResultStep { EventCollection = FunnelColB, }, }, Result = new[] { 3, 2 } }; Mock <IQueries> queryMock = null; if (UseMocks) { queryMock = new Mock <IQueries>(); queryMock.Setup(m => m.Funnel( It.Is <IEnumerable <FunnelStep> >(f => f.Equals(funnelsteps)), It.Is <IQueryTimeframe>(t => null == t), It.Is <string>(t => t == "") )) .Returns(Task.FromResult(expected)); client.Queries = queryMock.Object; } var reply = (await client.QueryFunnelAsync(funnelsteps, null)); Assert.NotNull(reply); Assert.NotNull(reply.Result); Assert.True(reply.Result.SequenceEqual(expected.Result)); Assert.NotNull(reply.Steps); Assert.AreEqual(reply.Steps.Count(), 2); if (null != queryMock) { queryMock.VerifyAll(); } }