Skip to content

Commit 02536e1

Browse files
committed
Written test for situations when db info is not in the query but is in the client.
1 parent 79a0368 commit 02536e1

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

src/test/java/org/influxdb/InfluxDBTest.java

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.influxdb;
22

3+
import java.util.Collections;
34
import org.influxdb.InfluxDB.LogLevel;
45
import org.influxdb.InfluxDB.ResponseFormat;
56
import org.influxdb.dto.BatchPoints;
@@ -39,6 +40,7 @@
3940
import java.util.concurrent.TimeUnit;
4041
import java.util.concurrent.atomic.LongAdder;
4142
import java.util.function.Consumer;
43+
import org.junit.runner.notification.RunListener;
4244

4345
/**
4446
* Test the InfluxDB API.
@@ -285,6 +287,162 @@ public void testWriteNoDatabase() {
285287
this.influxDB.query(new Query("DROP DATABASE " + dbName));
286288
}
287289

290+
/**
291+
* Tests that database information is used from {@link InfluxDB} when database information
292+
* is not present in query.
293+
*/
294+
@Test
295+
public void testQueryWithNoDatabase() {
296+
String dbName = "write_unittest_" + System.currentTimeMillis();
297+
this.influxDB.query(new Query("CREATE DATABASE " + dbName));
298+
this.influxDB.setDatabase(dbName); // Set db here, then after write query should pass.
299+
this.influxDB.write(Point
300+
.measurement("cpu")
301+
.tag("atag", "test")
302+
.addField("idle", 90L)
303+
.addField("usertime", 9L)
304+
.addField("system", 1L)
305+
.build());
306+
307+
Query query = new Query("SELECT * FROM cpu GROUP BY *");
308+
QueryResult result = this.influxDB.query(query);
309+
Assertions.assertEquals(
310+
result.getResults().get(0).getSeries().get(0).getTags(),
311+
Collections.singletonMap("atag", "test")
312+
);
313+
this.influxDB.query(new Query("DROP DATABASE " + dbName));
314+
}
315+
316+
/**
317+
* Tests that database information is used from {@link InfluxDB} when database information
318+
* is not present in query.
319+
*/
320+
@Test
321+
public void testQueryWithNoDatabaseWithConsumer() {
322+
String dbName = "write_unittest_" + System.currentTimeMillis();
323+
this.influxDB.query(new Query("CREATE DATABASE " + dbName));
324+
this.influxDB.setDatabase(dbName); // Set db here, then after write query should pass.
325+
this.influxDB.write(Point
326+
.measurement("cpu")
327+
.tag("atag", "test")
328+
.addField("idle", 90L)
329+
.addField("usertime", 9L)
330+
.addField("system", 1L)
331+
.build());
332+
333+
Query query = new Query("SELECT * FROM cpu GROUP BY *");
334+
this.influxDB.query(query,
335+
queryResult ->
336+
Assertions.assertEquals(
337+
queryResult.getResults().get(0).getSeries().get(0).getTags(),
338+
Collections.singletonMap("atag", "test")
339+
)
340+
,
341+
throwable -> Assertions.fail()
342+
);
343+
this.influxDB.query(new Query("DROP DATABASE " + dbName));
344+
}
345+
346+
347+
/**
348+
* Tests that database information is used from {@link InfluxDB} when database information
349+
* is not present in query.
350+
*/
351+
// Note: this test is copied from InfluxDBTest#testChunking but changed so that database
352+
// information is present in client not query. Combined both tests test situations with
353+
// and without database information present in query, hence no need for additional test
354+
// for situation where database info is not set in the client.
355+
public void testQueryNoDatabaseWithChunking() throws Exception {
356+
if (this.influxDB.version().startsWith("0.") || this.influxDB.version().startsWith("1.0")) {
357+
// do not test version 0.13 and 1.0
358+
return;
359+
}
360+
String dbName = "write_unittest_" + System.currentTimeMillis();
361+
this.influxDB.query(new Query("CREATE DATABASE " + dbName));
362+
this.influxDB.setDatabase(dbName); // Set database -> no need to use it as query parameter.
363+
String rp = TestUtils.defaultRetentionPolicy(this.influxDB.version());
364+
BatchPoints batchPoints = BatchPoints.database(dbName).retentionPolicy(rp).build();
365+
Point point1 = Point.measurement("disk").tag("atag", "a").addField("used", 60L).addField("free", 1L).build();
366+
Point point2 = Point.measurement("disk").tag("atag", "b").addField("used", 70L).addField("free", 2L).build();
367+
Point point3 = Point.measurement("disk").tag("atag", "c").addField("used", 80L).addField("free", 3L).build();
368+
batchPoints.point(point1);
369+
batchPoints.point(point2);
370+
batchPoints.point(point3);
371+
this.influxDB.write(batchPoints);
372+
373+
Thread.sleep(2000);
374+
final BlockingQueue<QueryResult> queue = new LinkedBlockingQueue<>();
375+
Query query = new Query("SELECT * FROM disk");
376+
this.influxDB.query(query, 2, queue::add);
377+
378+
Thread.sleep(2000);
379+
this.influxDB.query(new Query("DROP DATABASE " + dbName));
380+
381+
QueryResult result = queue.poll(20, TimeUnit.SECONDS);
382+
Assertions.assertNotNull(result);
383+
System.out.println(result);
384+
Assertions.assertEquals(2, result.getResults().get(0).getSeries().get(0).getValues().size());
385+
386+
result = queue.poll(20, TimeUnit.SECONDS);
387+
Assertions.assertNotNull(result);
388+
System.out.println(result);
389+
Assertions.assertEquals(1, result.getResults().get(0).getSeries().get(0).getValues().size());
390+
391+
result = queue.poll(20, TimeUnit.SECONDS);
392+
Assertions.assertNotNull(result);
393+
System.out.println(result);
394+
Assertions.assertEquals("DONE", result.getError());
395+
}
396+
397+
/**
398+
* Tests that database information is used from {@link InfluxDB} when database information
399+
* is not present in query and when different time format is requested from db.
400+
*/
401+
@Test
402+
public void testQueryNoDatabaseWithTimeFormat() {
403+
String dbName = "write_unittest_" + System.currentTimeMillis();
404+
long time = 1559027876L;
405+
this.influxDB.query(new Query("CREATE DATABASE " + dbName));
406+
this.influxDB.setDatabase(dbName); // Set db here, then after write query should pass.
407+
this.influxDB.write(Point
408+
.measurement("cpu")
409+
.tag("atag", "test")
410+
.addField("idle", 90L)
411+
.addField("usertime", 9L)
412+
.addField("system", 1L)
413+
.time(time, TimeUnit.MILLISECONDS) // Set time.
414+
.build());
415+
416+
Query query = new Query("SELECT * FROM cpu GROUP BY *");
417+
418+
// Test milliseconds
419+
QueryResult result = this.influxDB.query(query, TimeUnit.MILLISECONDS);
420+
Series series = result.getResults().get(0).getSeries().get(0);
421+
Assertions.assertEquals(
422+
((Number)series.getValues().get(0).get(series.getColumns().indexOf("time"))).longValue() ,
423+
time
424+
);
425+
426+
// Test nanoseconds
427+
result = this.influxDB.query(query, TimeUnit.NANOSECONDS);
428+
series = result.getResults().get(0).getSeries().get(0);
429+
Assertions.assertEquals(
430+
((Number)series.getValues().get(0).get(series.getColumns().indexOf("time"))).longValue(),
431+
TimeUnit.NANOSECONDS.convert(time, TimeUnit.MILLISECONDS)
432+
);
433+
434+
// Test seconds
435+
result = this.influxDB.query(query, TimeUnit.SECONDS);
436+
series = result.getResults().get(0).getSeries().get(0);
437+
Assertions.assertEquals(
438+
((Number)series.getValues().get(0).get(series.getColumns().indexOf("time"))).longValue(),
439+
TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS)
440+
441+
);
442+
443+
this.influxDB.query(new Query("DROP DATABASE " + dbName));
444+
}
445+
288446
/**
289447
* Test the implementation of {@link InfluxDB#write(int, Point)}'s async support.
290448
*/

0 commit comments

Comments
 (0)