Esempio n. 1
0
        public void EnsureDbCreated_RequestSent_ProperRequestGenerated()
        {
            // Arange
            var expectedQuery = new StringBuilder()
                                .Append("?q=")
                                .Append(WebUtility.UrlEncode($"CREATE DATABASE {this._dbSettings.DatabaseName}"))
                                .ToString();

            Uri expectedUri = new UriBuilder(this._dbSettings.DatabaseAddress)
            {
                Path  = "query",
                Query = expectedQuery
            }.Uri;
            Uri actualUri = null;

            var mockResponseHandler = new MockResponseHandler(request =>
            {
                actualUri = request.RequestUri;
                return(new HttpResponseMessage(HttpStatusCode.OK));
            });

            var factory = new InfluxDbRepositoryFactory(this._dbOptions, new HttpClient(mockResponseHandler), GetMockLogger <InfluxDbRepositoryFactory>());

            // Act
            var repository = factory.CreateRepository(true);

            // Assert
            Assert.AreEqual(expectedUri, actualUri);
        }
Esempio n. 2
0
        public async Task Write_RequestSent_ProperRequestGenerated()
        {
            // Arange
            var p1 = new Point(312.42m);
            var p2 = new Point(-24412.355m, DateTime.Now);

            var expectedContent = new StringBuilder()
                                  .Append($"Measurement value={p1.Value.ToString(new CultureInfo("en-US"))}\n")
                                  .Append($"Measurement value={p2.Value.ToString(new CultureInfo("en-US"))} {p2.Timestamp.Value.ToUnixNanoseconds()}")
                                  .ToString();

            string expectedQuery = $"?db={this._dbSettings.DatabaseName}";
            Uri    expectedUri   = new UriBuilder(this._dbSettings.DatabaseAddress)
            {
                Path  = "write",
                Query = expectedQuery
            }.Uri;
            Uri    actualUri     = null;
            string actualContent = null;

            var mockResponseHandler = new MockResponseHandler(request =>
            {
                actualUri     = request.RequestUri;
                actualContent = request.Content.ReadAsStringAsync().Result;
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(InfluxDbResources.ReadResponseJson)
                });
            });

            var repository = new InfluxDbRepository(this._dbOptions.Value, new HttpClient(mockResponseHandler), GetMockLogger <InfluxDbRepository>());

            // Act
            await repository.Write(new WriteRequest("Measurement", new[] { p1, p2 }));

            // Assert
            Assert.AreEqual(expectedUri, actualUri);
            Assert.AreEqual(expectedContent, actualContent);
        }
Esempio n. 3
0
        public async Task Query_RequestSent_ProperRequestGenerated()
        {
            // Arange
            var filter1 = new QueryDescriptor("filter1", order: OrderType.Ascending, limit: 1);
            var filter2 = new QueryDescriptor("filter2", DateTime.Now);

            var expectedQuery = new StringBuilder()
                                .Append($"?db={this._dbSettings.DatabaseName}")
                                .Append("&q=")
                                .Append(WebUtility.UrlEncode($"SELECT * FROM \"{filter1.Name}\" ORDER BY time ASC LIMIT 1;"))
                                .Append(WebUtility.UrlEncode($"SELECT * FROM \"{filter2.Name}\" WHERE time > {filter2.DateFrom.Value.ToUnixNanoseconds()}"))
                                .ToString();

            Uri expectedUri = new UriBuilder(this._dbSettings.DatabaseAddress)
            {
                Path  = "query",
                Query = expectedQuery
            }.Uri;
            Uri actualUri = null;

            var mockResponseHandler = new MockResponseHandler(request =>
            {
                actualUri = request.RequestUri;
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(InfluxDbResources.ReadResponseJson)
                });
            });

            var repository = new InfluxDbRepository(this._dbOptions.Value, new HttpClient(mockResponseHandler), GetMockLogger <InfluxDbRepository>());

            // Act
            ICollection <QueryResult> set = await repository.Query(new[] { filter1, filter2 });

            // Assert
            Assert.AreEqual(expectedUri, actualUri);
        }