Home Manual Reference Source Test

unit/influx.test.js

  1. import { expect } from "chai";
  2. import * as sinon from "sinon";
  3. import { FieldType, InfluxDB, toNanoDate } from "../../src";
  4. import { dbFixture } from "./helpers";
  5. describe("influxdb", () => {
  6. describe("constructor", () => {
  7. it("uses default options", () => {
  8. expect(new InfluxDB()._options).to.deep.equal({
  9. username: "root",
  10. password: "root",
  11. database: null,
  12. pool: undefined,
  13. schema: [],
  14. hosts: [
  15. {
  16. host: "127.0.0.1",
  17. port: 8086,
  18. path: "",
  19. protocol: "http",
  20. options: undefined,
  21. },
  22. ],
  23. });
  24. });
  25. it("parses dsns", () => {
  26. expect(new InfluxDB("https://connor:password@192.168.0.1:1337/foo")
  27. ._options).to.deep.equal({
  28. username: "connor",
  29. password: "password",
  30. database: "foo",
  31. pool: undefined,
  32. schema: [],
  33. hosts: [
  34. {
  35. host: "192.168.0.1",
  36. port: 1337,
  37. path: "",
  38. protocol: "https",
  39. options: undefined,
  40. },
  41. ],
  42. });
  43. });
  44. it("parses single configs", () => {
  45. expect(new InfluxDB({ database: "foo", host: "192.168.0.1" })._options).to.deep.equal({
  46. username: "root",
  47. password: "root",
  48. database: "foo",
  49. pool: undefined,
  50. schema: [],
  51. hosts: [
  52. {
  53. host: "192.168.0.1",
  54. port: 8086,
  55. path: "",
  56. protocol: "http",
  57. options: undefined,
  58. },
  59. ],
  60. });
  61. });
  62. it("parses cluster configs", () => {
  63. expect(new InfluxDB({
  64. database: "foo",
  65. hosts: [{ host: "192.168.0.1", options: { ca: null } }],
  66. })._options).to.deep.equal({
  67. username: "root",
  68. password: "root",
  69. database: "foo",
  70. schema: [],
  71. hosts: [
  72. {
  73. host: "192.168.0.1",
  74. port: 8086,
  75. path: "",
  76. protocol: "http",
  77. options: { ca: null },
  78. },
  79. ],
  80. });
  81. });
  82. it("parses parses schema", () => {
  83. let client = new InfluxDB({
  84. schema: [
  85. {
  86. database: "my_db",
  87. measurement: "my_measurement",
  88. fields: {},
  89. tags: ["my_tag"],
  90. },
  91. ],
  92. hosts: [{ host: "192.168.0.1", options: undefined }],
  93. });
  94. expect(client._schema.my_db.my_measurement).to.not.be.undefined;
  95. client = new InfluxDB({
  96. schema: [
  97. {
  98. measurement: "my_measurement",
  99. fields: {},
  100. tags: ["my_tag"],
  101. },
  102. ],
  103. database: "my_db",
  104. hosts: [{ host: "192.168.0.1" }],
  105. });
  106. expect(client._schema.my_db.my_measurement).to.not.be.undefined;
  107. expect(() => {
  108. new InfluxDB({
  109. // eslint-disable-line no-new
  110. schema: [
  111. {
  112. measurement: "my_measurement",
  113. fields: {},
  114. tags: ["my_tag"],
  115. },
  116. ],
  117. hosts: [{ host: "192.168.0.1" }],
  118. });
  119. }).to.throw(/no default database is provided/);
  120. });
  121. });
  122. describe("methods", () => {
  123. let influx;
  124. let pool;
  125. const expectations = [];
  126. beforeEach(() => {
  127. influx = new InfluxDB({
  128. hosts: [],
  129. schema: [
  130. {
  131. database: "my_db",
  132. measurement: "my_schemed_measure",
  133. tags: ["my_tag"],
  134. fields: {
  135. int: FieldType.INTEGER,
  136. float: FieldType.FLOAT,
  137. string: FieldType.STRING,
  138. bool: FieldType.BOOLEAN,
  139. },
  140. },
  141. ],
  142. });
  143. pool = influx._pool;
  144. sinon.stub(pool, "discard");
  145. sinon.stub(pool, "json");
  146. sinon.stub(pool, "text");
  147. });
  148. afterEach(() => {
  149. while (expectations.length) {
  150. expectations.pop()();
  151. }
  152. });
  153. const setDefaultDB = (db) => {
  154. influx._options.database = db;
  155. };
  156. const expectQuery = (method, options, httpMethod = "POST", yields = { results: [{}] }) => {
  157. if (typeof options === "string") {
  158. options = { q: options };
  159. }
  160. pool[method].returns(Promise.resolve(yields));
  161. expectations.push(() => {
  162. expect(pool[method]).to.have.been.calledWith({
  163. method: httpMethod,
  164. path: "/query",
  165. query: Object.assign({ u: "root", p: "root" }, options),
  166. });
  167. });
  168. };
  169. const expectWrite = (body, options) => {
  170. if (typeof options === "string") {
  171. options = { q: options };
  172. }
  173. pool.discard.returns(Promise.resolve());
  174. expectations.push(() => {
  175. expect(pool.discard).to.have.been.calledWith({
  176. method: "POST",
  177. path: "/write",
  178. body,
  179. query: Object.assign({ u: "root", p: "root" }, options),
  180. });
  181. });
  182. };
  183. it(".createDatabase()", () => {
  184. expectQuery("json", 'create database "foo"');
  185. influx.createDatabase("foo");
  186. expectQuery("json", 'create database "f\\"oo"');
  187. influx.createDatabase('f"oo');
  188. });
  189. it(".dropDatabase()", () => {
  190. expectQuery("json", 'drop database "foo"');
  191. influx.dropDatabase("foo");
  192. expectQuery("json", 'drop database "f\\"oo"');
  193. influx.dropDatabase('f"oo');
  194. });
  195. it(".dropShard()", () => {
  196. expectQuery("json", "drop shard 1");
  197. influx.dropShard(1);
  198. });
  199. it(".getDatabaseNames()", () => {
  200. expectQuery("json", "show databases", "GET", dbFixture("showDatabases"));
  201. return influx.getDatabaseNames().then((names) => {
  202. expect(names).to.deep.equal(["_internal", "influx_test_gen"]);
  203. });
  204. });
  205. it(".getMeasurements()", () => {
  206. setDefaultDB("mydb");
  207. expectQuery("json", {
  208. db: "mydb",
  209. q: "show measurements",
  210. }, "GET", dbFixture("showMeasurements"));
  211. return influx.getMeasurements().then((names) => {
  212. expect(names).to.deep.equal(["series_0", "series_1", "series_2"]);
  213. });
  214. });
  215. it(".getSeries() from all", () => {
  216. setDefaultDB("mydb");
  217. expectQuery("json", {
  218. db: "mydb",
  219. q: "show series",
  220. }, "GET", dbFixture("showSeries"));
  221. return influx.getSeries().then((names) => {
  222. expect(names).to.deep.equal([
  223. "series_0,my_tag=0",
  224. "series_0,my_tag=1",
  225. "series_0,my_tag=5",
  226. "series_0,my_tag=6",
  227. "series_0,my_tag=7",
  228. "series_0,my_tag=8",
  229. "series_0,my_tag=9",
  230. "series_1,my_tag=0",
  231. "series_1,my_tag=2",
  232. "series_1,my_tag=4",
  233. "series_1,my_tag=5",
  234. "series_1,my_tag=6",
  235. "series_1,my_tag=7",
  236. "series_1,my_tag=8",
  237. "series_1,my_tag=9",
  238. "series_2,my_tag=1",
  239. "series_2,my_tag=2",
  240. "series_2,my_tag=3",
  241. "series_2,my_tag=4",
  242. "series_2,my_tag=5",
  243. "series_2,my_tag=6",
  244. "series_2,my_tag=7",
  245. "series_2,my_tag=8",
  246. "series_2,my_tag=9",
  247. ]);
  248. });
  249. });
  250. it(".getSeries() from single", () => {
  251. expectQuery("json", {
  252. db: "mydb",
  253. q: 'show series from "measure_1"',
  254. }, "GET", dbFixture("showSeriesFromOne"));
  255. return influx
  256. .getSeries({
  257. database: "mydb",
  258. measurement: "measure_1",
  259. })
  260. .then((names) => {
  261. expect(names).to.deep.equal([
  262. "series_1,my_tag=0",
  263. "series_1,my_tag=2",
  264. "series_1,my_tag=4",
  265. "series_1,my_tag=5",
  266. "series_1,my_tag=6",
  267. "series_1,my_tag=7",
  268. "series_1,my_tag=8",
  269. "series_1,my_tag=9",
  270. ]);
  271. });
  272. });
  273. it(".dropMeasurement()", () => {
  274. expectQuery("json", {
  275. db: "my_db",
  276. q: 'drop measurement "series_1"',
  277. });
  278. return influx.dropMeasurement("series_1", "my_db");
  279. });
  280. describe(".dropSeries()", () => {
  281. beforeEach(() => setDefaultDB("my_db"));
  282. it("drops with only from clause by string", () => {
  283. expectQuery("json", { db: "my_db", q: 'drop series from "series_0"' });
  284. influx.dropSeries({ measurement: '"series_0"' });
  285. });
  286. it("drops with only from clause by builder", () => {
  287. expectQuery("json", { db: "my_db", q: 'drop series from "series_0"' });
  288. influx.dropSeries({ measurement: (m) => m.name("series_0") });
  289. });
  290. it("drops with only where clause by string", () => {
  291. expectQuery("json", {
  292. db: "my_db",
  293. q: 'drop series where "my_tag" = 1',
  294. });
  295. influx.dropSeries({ where: '"my_tag" = 1' });
  296. });
  297. it("drops with only where clause by builder", () => {
  298. expectQuery("json", {
  299. db: "my_db",
  300. q: 'drop series where "my_tag" = 1',
  301. });
  302. influx.dropSeries({ where: (e) => e.tag("my_tag").equals.value(1) });
  303. });
  304. it("drops with both", () => {
  305. expectQuery("json", {
  306. db: "my_db",
  307. q: 'drop series from "series_0" where "my_tag" = 1',
  308. });
  309. influx.dropSeries({
  310. measurement: (m) => m.name("series_0"),
  311. where: (e) => e.tag("my_tag").equals.value(1),
  312. });
  313. });
  314. });
  315. it(".getUsers()", () => {
  316. expectQuery("json", "show users", "GET", dbFixture("showUsers"));
  317. return influx.getUsers().then((names) => {
  318. expect(names.slice()).to.deep.equal([
  319. { user: "john", admin: true },
  320. { user: "steve", admin: false },
  321. ]);
  322. });
  323. });
  324. describe(".createUser()", () => {
  325. it("works with admin specified == true", () => {
  326. expectQuery("json", "create user \"con\\\"nor\" with password 'pa55\\'word' with all privileges");
  327. return influx.createUser('con"nor', "pa55'word", true);
  328. });
  329. it("works with admin specified == false", () => {
  330. expectQuery("json", "create user \"con\\\"nor\" with password 'pa55\\'word'");
  331. return influx.createUser('con"nor', "pa55'word", false);
  332. });
  333. it("works with admin unspecified", () => {
  334. expectQuery("json", "create user \"con\\\"nor\" with password 'pa55\\'word'");
  335. return influx.createUser('con"nor', "pa55'word");
  336. });
  337. });
  338. describe(".grantPrivilege()", () => {
  339. it("queries correctly", () => {
  340. expectQuery("json", 'grant READ on "my_\\"_db" to "con\\"nor"');
  341. return influx.grantPrivilege('con"nor', "READ", 'my_"_db');
  342. });
  343. it("throws if DB unspecified", () => {
  344. expect(() => influx.grantPrivilege('con"nor', "READ")).to.throw(/default database/);
  345. });
  346. it("fills in default DB", () => {
  347. setDefaultDB('my_\\"_db');
  348. expectQuery("json", 'grant READ on "my_\\"_db" to "con\\"nor"');
  349. return influx.grantPrivilege('con"nor', "READ", 'my_"_db');
  350. });
  351. });
  352. describe(".revokePrivilege()", () => {
  353. it("queries correctly", () => {
  354. expectQuery("json", 'revoke READ on "my_\\"_db" from "con\\"nor"');
  355. return influx.revokePrivilege('con"nor', "READ", 'my_"_db');
  356. });
  357. it("throws if DB unspecified", () => {
  358. expect(() => influx.revokePrivilege('con"nor', "READ")).to.throw(/default database/);
  359. });
  360. it("fills in default DB", () => {
  361. setDefaultDB('my_\\"_db');
  362. expectQuery("json", 'revoke READ on "my_\\"_db" from "con\\"nor"');
  363. return influx.revokePrivilege('con"nor', "READ", 'my_"_db');
  364. });
  365. });
  366. it(".grantAdminPrivilege()", () => {
  367. expectQuery("json", 'grant all to "con\\"nor"');
  368. return influx.grantAdminPrivilege('con"nor');
  369. });
  370. it(".revokeAdminPrivilege()", () => {
  371. expectQuery("json", 'revoke all from "con\\"nor"');
  372. return influx.revokeAdminPrivilege('con"nor');
  373. });
  374. it(".dropUser()", () => {
  375. expectQuery("json", 'drop user "con\\"nor"');
  376. return influx.dropUser('con"nor');
  377. });
  378. describe(".createContinuousQuery()", () => {
  379. it("queries correctly no resample", () => {
  380. expectQuery("json", 'create continuous query "my_\\"q" on "my_\\"_db" begin foo end');
  381. return influx.createContinuousQuery('my_"q', "foo", 'my_"_db');
  382. });
  383. it("queries correctly with resample", () => {
  384. expectQuery("json", 'create continuous query "my_\\"q" on "my_\\"_db" resample for 4m begin foo end');
  385. return influx.createContinuousQuery('my_"q', "foo", 'my_"_db', "resample for 4m");
  386. });
  387. it("throws if DB unspecified", () => {
  388. expect(() => influx.createContinuousQuery('my_"q', "foo")).to.throw(/default database/);
  389. });
  390. it("fills in default DB", () => {
  391. setDefaultDB('my_"_db');
  392. expectQuery("json", 'create continuous query "my_\\"q" on "my_\\"_db" begin foo end');
  393. return influx.createContinuousQuery('my_"q', "foo");
  394. });
  395. });
  396. describe(".dropContinuousQuery()", () => {
  397. it("queries correctly", () => {
  398. expectQuery("json", 'drop continuous query "my_\\"q" on "my_\\"_db"');
  399. return influx.dropContinuousQuery('my_"q', 'my_"_db');
  400. });
  401. it("throws if DB unspecified", () => {
  402. expect(() => influx.dropContinuousQuery('my_"q')).to.throw(/default database/);
  403. });
  404. it("fills in default DB", () => {
  405. setDefaultDB('my_"_db');
  406. expectQuery("json", 'drop continuous query "my_\\"q" on "my_\\"_db"');
  407. return influx.dropContinuousQuery('my_"q');
  408. });
  409. });
  410. describe(".showContinousQueries()", () => {
  411. it("queries correctly", () => {
  412. expectQuery("json", { q: "show continuous queries", db: "my_db" }, "GET");
  413. return influx.showContinousQueries("my_db");
  414. });
  415. it("throws if DB unspecified", () => {
  416. expect(() => influx.showContinousQueries()).to.throw(/default database/);
  417. });
  418. it("fills in default DB", () => {
  419. setDefaultDB("my_db");
  420. expectQuery("json", { q: "show continuous queries", db: "my_db" }, "GET");
  421. return influx.showContinousQueries();
  422. });
  423. });
  424. describe(".writePoints()", () => {
  425. it("writes with all options specified without a schema", () => {
  426. expectWrite("mymeas,my_tag=1 myfield=90 1463683075", {
  427. precision: "s",
  428. rp: "1day",
  429. db: "my_db",
  430. });
  431. return influx.writePoints([
  432. {
  433. measurement: "mymeas",
  434. tags: { my_tag: "1" },
  435. fields: { myfield: 90 },
  436. timestamp: new Date(1463683075000),
  437. },
  438. ], {
  439. database: "my_db",
  440. precision: "s",
  441. retentionPolicy: "1day",
  442. });
  443. });
  444. it("writes using default options without a schema", () => {
  445. setDefaultDB("my_db");
  446. expectWrite("mymeas,my_tag=1 myfield=90 1463683075000000000", {
  447. precision: "n",
  448. rp: undefined,
  449. db: "my_db",
  450. });
  451. return influx.writePoints([
  452. {
  453. measurement: "mymeas",
  454. tags: { my_tag: "1" },
  455. fields: { myfield: 90 },
  456. timestamp: new Date(1463683075000),
  457. },
  458. ]);
  459. });
  460. it("uses a schema to coerce", () => {
  461. setDefaultDB("my_db");
  462. expectWrite("my_schemed_measure,my_tag=1 bool=T,float=43,int=42i", {
  463. precision: "n",
  464. rp: undefined,
  465. db: "my_db",
  466. });
  467. return influx.writePoints([
  468. {
  469. measurement: "my_schemed_measure",
  470. tags: { my_tag: "1" },
  471. fields: {
  472. int: 42,
  473. float: 43,
  474. bool: true,
  475. },
  476. },
  477. ]);
  478. });
  479. it("can accept a schema at runtime", () => {
  480. setDefaultDB("my_db");
  481. expectWrite("my_runtime_schema_measure,my_tag=1 bool=T,float=43,int=42i", {
  482. precision: "n",
  483. rp: undefined,
  484. db: "my_db",
  485. });
  486. influx.addSchema({
  487. database: "my_db",
  488. measurement: "my_runtime_schema_measure",
  489. fields: {
  490. bool: FieldType.BOOLEAN,
  491. float: FieldType.FLOAT,
  492. int: FieldType.INTEGER,
  493. },
  494. tags: ["my_tag"],
  495. });
  496. return influx.writePoints([
  497. {
  498. measurement: "my_runtime_schema_measure",
  499. tags: { my_tag: "1" },
  500. fields: {
  501. int: 42,
  502. float: 43,
  503. bool: true,
  504. },
  505. },
  506. ]);
  507. });
  508. it("handles lack of tags", () => {
  509. expectWrite("mymeas myfield=90", {
  510. precision: "n",
  511. rp: undefined,
  512. db: "my_db",
  513. });
  514. return influx.writePoints([
  515. {
  516. measurement: "mymeas",
  517. fields: { myfield: 90 },
  518. },
  519. ], { database: "my_db" });
  520. });
  521. it("handles lack of fields", () => {
  522. expectWrite("mymeas,my_tag=90", {
  523. precision: "n",
  524. rp: undefined,
  525. db: "my_db",
  526. });
  527. return influx.writePoints([
  528. {
  529. measurement: "mymeas",
  530. tags: { my_tag: "90" },
  531. },
  532. ], { database: "my_db" });
  533. });
  534. it("handles multiple tags", () => {
  535. expectWrite("mymeas,my_tag1=90,my_tag2=45", {
  536. precision: "n",
  537. rp: undefined,
  538. db: "my_db",
  539. });
  540. return influx.writePoints([
  541. {
  542. measurement: "mymeas",
  543. tags: { my_tag1: "90", my_tag2: "45" },
  544. },
  545. ], { database: "my_db" });
  546. });
  547. it("writes with the .writeMeasurement method", () => {
  548. setDefaultDB("my_db");
  549. expectWrite("mymeas,my_tag=1 myfield=90 1463683075000000000", {
  550. precision: "n",
  551. rp: undefined,
  552. db: "my_db",
  553. });
  554. return influx.writeMeasurement("mymeas", [
  555. {
  556. tags: { my_tag: "1" },
  557. fields: { myfield: 90 },
  558. timestamp: new Date(1463683075000),
  559. },
  560. ]);
  561. });
  562. it("accepts nanoseconds (as ms)", () => {
  563. setDefaultDB("my_db");
  564. expectWrite("mymeas,my_tag=1 myfield=90 1463683075000000000", {
  565. precision: "n",
  566. rp: undefined,
  567. db: "my_db",
  568. });
  569. return influx.writeMeasurement("mymeas", [
  570. {
  571. tags: { my_tag: "1" },
  572. fields: { myfield: 90 },
  573. timestamp: toNanoDate("1463683075000000000"),
  574. },
  575. ]);
  576. });
  577. it("accepts timestamp overriding", () => {
  578. setDefaultDB("my_db");
  579. expectWrite("mymeas,my_tag=1 myfield=90 1463683075000", {
  580. precision: "ms",
  581. rp: undefined,
  582. db: "my_db",
  583. });
  584. return influx.writeMeasurement("mymeas", [
  585. {
  586. tags: { my_tag: "1" },
  587. fields: { myfield: 90 },
  588. timestamp: toNanoDate("1463683075000000000"),
  589. },
  590. ], { precision: "ms" });
  591. });
  592. });
  593. describe(".parsePoint()", () => {
  594. it("parses a minimal valid point with default options", () => {
  595. setDefaultDB("my_db");
  596. let point = {
  597. measurement: "mymeas",
  598. fields: { myfield: 90 },
  599. };
  600. let parsedPoint = influx.parsePoint(point);
  601. expect(parsedPoint.measurement).to.equal(point.measurement);
  602. expect(parsedPoint.fields).to.equal(point.fields);
  603. expect(parsedPoint.tags).to.deep.equals({});
  604. expect(parsedPoint.fieldsPairs).to.deep.equals([["myfield", "90"]]);
  605. expect(parsedPoint.tagsNames).to.deep.equals([]);
  606. expect(parsedPoint.castedTimestamp).to.be.undefined;
  607. });
  608. it("parses a point with fields, tags, and timestamp", () => {
  609. setDefaultDB("my_db");
  610. let date = new Date();
  611. let point = {
  612. measurement: "mymeas",
  613. fields: { myfield: 90 },
  614. tags: { my_tag: "2" },
  615. timestamp: date,
  616. };
  617. let parsedPoint = influx.parsePoint(point);
  618. expect(parsedPoint.measurement).to.equal(point.measurement);
  619. expect(parsedPoint.fields).to.equal(point.fields);
  620. expect(parsedPoint.tags).to.equals(point.tags);
  621. expect(parsedPoint.fieldsPairs).to.deep.equals([["myfield", "90"]]);
  622. expect(parsedPoint.tagsNames).to.deep.equals(["my_tag"]);
  623. expect(parsedPoint.castedTimestamp).not.to.be.undefined;
  624. });
  625. it("accepts custom precision option", () => {
  626. setDefaultDB("my_db");
  627. let date = new Date();
  628. let point = {
  629. measurement: "mymeas",
  630. fields: { myfield: 90 },
  631. timestamp: date,
  632. };
  633. let parsedPoint = influx.parsePoint(point, { precision: "ms" });
  634. expect(parsedPoint.castedTimestamp).to.equal(date.getTime().toString());
  635. });
  636. it("accepts custom database option", () => {
  637. let date = new Date();
  638. let point = {
  639. measurement: "mymeas",
  640. fields: { myfield: 90 },
  641. timestamp: date,
  642. };
  643. let parsedPoint = influx.parsePoint(point, { database: "my_db" });
  644. expect(parsedPoint).to.exist;
  645. });
  646. it("uses a schema to coerce", () => {
  647. setDefaultDB("my_db");
  648. let date = new Date();
  649. let point = {
  650. measurement: "my_schemed_measure",
  651. fields: { bool: true, float: 43, int: 42 },
  652. tags: { my_tag: "2" },
  653. timestamp: date,
  654. };
  655. let parsedPoint = influx.parsePoint(point);
  656. expect(parsedPoint.measurement).to.equal(point.measurement);
  657. expect(parsedPoint.fields).to.equal(point.fields);
  658. expect(parsedPoint.tags).to.equals(point.tags);
  659. expect(parsedPoint.fieldsPairs).to.deep.equals([
  660. ["bool", "T"],
  661. ["float", "43"],
  662. ["int", "42i"],
  663. ]);
  664. expect(parsedPoint.tagsNames).to.deep.equals(["my_tag"]);
  665. expect(parsedPoint.castedTimestamp).not.to.be.undefined;
  666. });
  667. it("should throw an error if extraneous tags are given", () => {
  668. setDefaultDB("my_db");
  669. expect(() => {
  670. let point = {
  671. measurement: "my_schemed_measure",
  672. tags: { not_a_tag: "1" },
  673. };
  674. influx.parsePoint(point);
  675. }).to.throw(/extraneous tags/i);
  676. });
  677. it("should throw an error if extraneous fields are given", () => {
  678. setDefaultDB("my_db");
  679. expect(() => {
  680. let point = {
  681. measurement: "my_schemed_measure",
  682. fields: { not_a_field: "1" },
  683. };
  684. influx.parsePoint(point);
  685. }).to.throw(/extraneous fields/i);
  686. });
  687. it("should throw an error if invalid value for field type given", () => {
  688. setDefaultDB("my_db");
  689. expect(() => {
  690. let point = {
  691. measurement: "my_schemed_measure",
  692. fields: { bool: "lol, not a bool" },
  693. };
  694. influx.parsePoint(point);
  695. }).to.throw(/expected bool/i);
  696. });
  697. });
  698. describe(".query", () => {
  699. beforeEach(() => setDefaultDB("my_db"));
  700. it("runs raw queries", () => {
  701. expectQuery("json", {
  702. q: "select * from series_0",
  703. epoch: undefined,
  704. rp: undefined,
  705. db: "my_db",
  706. params: "{}",
  707. }, "GET", dbFixture("selectFromOne"));
  708. return influx.queryRaw("select * from series_0").then((res) => {
  709. expect(res).to.deep.equal(dbFixture("selectFromOne"));
  710. });
  711. });
  712. it("parses query output", () => {
  713. expectQuery("json", {
  714. q: "select * from series_0",
  715. epoch: undefined,
  716. rp: undefined,
  717. db: "my_db",
  718. params: "{}",
  719. }, "GET", dbFixture("selectFromOne"));
  720. return influx.query("select * from series_0").then((res) => {
  721. expect(res.slice()).to.deep.equal([
  722. {
  723. time: new Date("2016-09-29T02:19:09.38Z"),
  724. my_tag: "1",
  725. my_value: 67,
  726. },
  727. {
  728. time: new Date("2016-09-29T02:19:09.379Z"),
  729. my_tag: "1",
  730. my_value: 32,
  731. },
  732. ]);
  733. });
  734. });
  735. it("selects from multiple", () => {
  736. expectQuery("json", {
  737. q: "select * from series_0;select * from series_1",
  738. epoch: undefined,
  739. rp: undefined,
  740. db: "my_db",
  741. params: "{}",
  742. }, "GET", dbFixture("selectFromOne"));
  743. return influx.query([
  744. "select * from series_0",
  745. "select * from series_1",
  746. ]);
  747. });
  748. it("passes in options", () => {
  749. expectQuery("json", {
  750. q: "select * from series_0",
  751. epoch: "ms",
  752. rp: "asdf",
  753. db: "my_db",
  754. params: "{}",
  755. }, "GET", dbFixture("selectFromOne"));
  756. return influx.query(["select * from series_0"], {
  757. precision: "ms",
  758. retentionPolicy: "asdf",
  759. });
  760. });
  761. it("rewrites nanosecond precisions", () => {
  762. expectQuery("json", {
  763. q: "select * from series_0",
  764. epoch: undefined,
  765. rp: "asdf",
  766. db: "my_db",
  767. params: "{}",
  768. }, "GET", dbFixture("selectFromOne"));
  769. return influx.query(["select * from series_0"], {
  770. precision: "n",
  771. retentionPolicy: "asdf",
  772. });
  773. });
  774. it("uses placeholders", () => {
  775. expectQuery("json", {
  776. q: "select * from series_0 WHERE time > now() - $<since> AND value >= $<minimumValue>",
  777. epoch: undefined,
  778. rp: "asdf",
  779. db: "my_db",
  780. params: '{"since":"10s","minimumValue":12}',
  781. }, "GET", dbFixture("selectFromOne"));
  782. return influx.query([
  783. "select * from series_0 WHERE time > now() - $<since> AND value >= $<minimumValue>",
  784. ], {
  785. precision: "n",
  786. retentionPolicy: "asdf",
  787. placeholders: {
  788. since: "10s",
  789. minimumValue: 12,
  790. },
  791. });
  792. });
  793. });
  794. describe(".createRetentionPolicy", () => {
  795. beforeEach(() => setDefaultDB("my_db"));
  796. it("creates non-default policies", () => {
  797. expectQuery("json", 'create retention policy "7d\\"" on "test" duration 7d replication 1');
  798. return influx.createRetentionPolicy('7d"', {
  799. database: "test",
  800. duration: "7d",
  801. replication: 1,
  802. });
  803. });
  804. it("creates default policies", () => {
  805. expectQuery("json", 'create retention policy "7d\\"" on "my_db" duration 7d replication 1 default');
  806. return influx.createRetentionPolicy('7d"', {
  807. duration: "7d",
  808. replication: 1,
  809. isDefault: true,
  810. });
  811. });
  812. });
  813. describe(".alterRetentionPolicy", () => {
  814. beforeEach(() => setDefaultDB("my_db"));
  815. it("creates non-default policies", () => {
  816. expectQuery("json", 'alter retention policy "7d\\"" on "test" duration 7d replication 1');
  817. return influx.alterRetentionPolicy('7d"', {
  818. database: "test",
  819. duration: "7d",
  820. replication: 1,
  821. });
  822. });
  823. it("creates default policies", () => {
  824. expectQuery("json", 'alter retention policy "7d\\"" on "my_db" duration 7d replication 1 default');
  825. return influx.alterRetentionPolicy('7d"', {
  826. duration: "7d",
  827. replication: 1,
  828. isDefault: true,
  829. });
  830. });
  831. });
  832. it("drops retention policies", () => {
  833. setDefaultDB("my_db");
  834. expectQuery("json", 'drop retention policy "7d\\"" on "my_db"');
  835. return influx.dropRetentionPolicy('7d"');
  836. });
  837. it("shows retention policies", () => {
  838. const data = dbFixture("showRetentionPolicies");
  839. expectQuery("json", 'show retention policies on "my\\"db"', "GET", data);
  840. influx.showRetentionPolicies('my"db');
  841. setDefaultDB("my_db");
  842. expectQuery("json", 'show retention policies on "my_db"', "GET", data);
  843. return influx.showRetentionPolicies().then((res) => {
  844. expect(res.slice()).to.deep.equal([
  845. {
  846. name: "autogen",
  847. duration: "0s",
  848. shardGroupDuration: "168h0m0s",
  849. replicaN: 1,
  850. default: true,
  851. },
  852. {
  853. name: "7d",
  854. duration: "168h0m0s",
  855. shardGroupDuration: "24h0m0s",
  856. replicaN: 1,
  857. default: false,
  858. },
  859. ]);
  860. });
  861. });
  862. it("shows shards", () => {
  863. setDefaultDB("_internal");
  864. expectQuery("json", "show shards ", "GET", dbFixture("showShards"));
  865. return influx.showShards().then((res) => {
  866. expect(res.slice()).to.deep.equal([
  867. {
  868. id: 1,
  869. database: "_internal",
  870. retention_policy: "monitor",
  871. shard_group: 1,
  872. start_time: "2019-06-13T00:00:00Z",
  873. end_time: "2019-06-14T00:00:00Z",
  874. expiry_time: "2019-06-21T00:00:00Z",
  875. owners: "",
  876. },
  877. ]);
  878. });
  879. });
  880. });
  881. });