Esempio n. 1
0
        public void AddEvents_PartialFailure_Throws()
        {
            var client = new KeenClient(SettingsEnv);

            if (UseMocks)
            {
                client.Event = new EventMock(SettingsEnv,
                                             addEvents: new Func <JObject, IProjectSettings, IEnumerable <CachedEvent> >((e, p) =>
                {
                    var err = e.SelectToken("$.AddEventTest[2]") as JObject;
                    if (null == err)
                    {
                        throw new Exception("Unexpected error, test data not found");
                    }

                    return(new List <CachedEvent>()
                    {
                        new CachedEvent("AddEventTest", e)
                    });
                }));
            }

            object invalidEvent = new ExpandoObject();

            ((IDictionary <string, object>)invalidEvent).Add("$" + new string('.', 260), "AValue");

            var events = (from i in Enumerable.Range(1, 2)
                          select new { AProperty = "AValue" + i }).ToList <object>();

            events.Add(invalidEvent);

            Assert.Throws <KeenBulkException>(() => client.AddEvents("AddEventTest", events));
        }
Esempio n. 2
0
        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");
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        public async Task DefaultHeaders_Success()
        {
            object responseData = new[] { new { result = 2 } };

            var handler = new FuncHandler()
            {
                PreProcess = (req, ct) =>
                {
                    // Make sure the default headers are in place
                    Assert.IsTrue(req.Headers.Contains("Keen-Sdk"));
                    Assert.AreEqual(KeenUtil.GetSdkVersion(), req.Headers.GetValues("Keen-Sdk").Single());

                    Assert.IsTrue(req.Headers.Contains("Authorization"));

                    var key = req.Headers.GetValues("Authorization").Single();
                    Assert.IsTrue(SettingsEnv.ReadKey == key ||
                                  SettingsEnv.WriteKey == key ||
                                  SettingsEnv.MasterKey == key);
                },
                ProduceResultAsync = (req, ct) =>
                {
                    return(CreateJsonStringResponseAsync(responseData));
                },
                DeferToDefault = false
            };

            var client = new KeenClient(SettingsEnv, new TestKeenHttpClientProvider()
            {
                ProvideKeenHttpClient =
                    (url) => KeenHttpClientFactory.Create(url,
                                                          new HttpClientCache(),
                                                          null,
                                                          new DelegatingHandlerMock(handler))
            });

            // Try out all the endpoints
            Assert.DoesNotThrow(() => client.GetSchemas());

            // Remaining operations expect an object, not an array of objects
            responseData = new { result = 2 };

            var @event = new { AProperty = "AValue" };

            Assert.DoesNotThrow(() => client.AddEvent("AddEventTest", @event));
            Assert.DoesNotThrow(() => client.AddEvents("AddEventTest", new[] { @event }));

            Assert.DoesNotThrow(() => client.DeleteCollection("DeleteColTest"));
            Assert.IsNotNull(client.GetSchema("AddEventTest"));

            // Currently all the queries/extraction go through the same KeenWebApiRequest() call.
            var count = await client.QueryAsync(
                QueryType.Count(),
                "testCollection",
                "",
                QueryRelativeTimeframe.ThisMonth());

            Assert.IsNotNull(count);
            Assert.AreEqual("2", count);
        }
Esempio n. 5
0
 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" }}));
 }
Esempio n. 6
0
        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" } }));
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        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: new Func <JObject, IProjectSettings, IEnumerable <CachedEvent> >((e, p) =>
                {
                    if ((p == settings) &&
                        (p.ProjectId == "X"))
                    {
                        throw new KeenException();
                    }
                    else
                    {
                        throw new Exception("Unexpected value");
                    }
                }));
            }
            Assert.Throws <KeenException>(() => client.AddEvents("AddEventTest", new [] { new { AProperty = "AValue" } }));
        }
Esempio n. 9
0
        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");
        }