1 /**
2  This module makes sure that using vibe corresponds in using the HTTP API
3  correctly.
4  */
5 module integration.vibe;
6 
7 import unit_threaded;
8 import integration.common: influxURL;
9 import influxdb.vibe: manage, query, write;
10 import std.json: JSONValue, parseJSON;
11 
12 
13 ///
14 @Serial
15 @("manage")
16 unittest {
17     manage(influxURL, "DROP DATABASE test_vibe_db");
18     wait;
19     manage(influxURL, "CREATE DATABASE test_vibe_db");
20     wait;
21     manage(influxURL, "DROP DATABASE test_vibe_db");
22     wait;
23 }
24 
25 
26 ///
27 @Serial
28 @("query empty database")
29 unittest {
30     manage(influxURL, "DROP DATABASE test_vibe_db");
31     wait;
32     manage(influxURL, "CREATE DATABASE test_vibe_db");
33     wait;
34     scope(exit) {
35         manage(influxURL, "DROP DATABASE test_vibe_db");
36         wait;
37     }
38 
39     const json = query(influxURL, "test_vibe_db", "SELECT * from foo").parseJSON;
40     JSONValue expected;
41     JSONValue result;
42     result["statement_id"] = JSONValue(0);
43     expected["results"] = [result];
44     json.shouldEqual(expected);
45 }
46 
47 /**
48   Example of a response (prettified):
49   {
50     "results": [{
51             "series": [{
52                     "columns": ["time", "othervalue", "tag1", "tag2", "value"],
53                     "name": "foo",
54                     "values": [
55                             ["2015-06-11T20:46:02Z", 4, "toto", "titi", 2],
56                             ["2017-03-14T23:15:01.06282785Z", 3, "letag", "othertag", 1]
57                     ]
58             }],
59             "statement_id": 0
60     }]
61  }
62 */
63 
64 
65 ///
66 @Serial
67 @("query database with data")
68 unittest {
69     import std.algorithm: map;
70 
71     manage(influxURL, "DROP DATABASE test_vibe_db");
72     wait;
73     manage(influxURL, "CREATE DATABASE test_vibe_db");
74     wait;
75     scope(exit) {
76         manage(influxURL, "DROP DATABASE test_vibe_db");
77         wait;
78     }
79 
80     write(influxURL, "test_vibe_db", "foo,tag1=letag,tag2=othertag value=1,othervalue=3");
81     write(influxURL, "test_vibe_db", "foo,tag1=toto,tag2=titi value=2,othervalue=4 1434055562000000000");
82     wait;
83 
84     {
85         const json = query(influxURL, "test_vibe_db", "SELECT * from foo").parseJSON;
86         const result = json.object["results"].array[0].object;
87         const table = result["series"].array[0].object;
88         table["columns"].array.map!(a => a.str).shouldBeSameSetAs(
89             ["time", "othervalue", "tag1", "tag2", "value"]);
90         table["name"].str.shouldEqual("foo");
91         table["values"].array.length.shouldEqual(2);
92     }
93 }
94 
95 
96 private void wait() {
97     import core.thread;
98     Thread.sleep(10.msecs);
99 }