From 61fe9ad7bc60a05018af4c4cf30219d4daa242d1 Mon Sep 17 00:00:00 2001 From: Kristopher Bredemeier Date: Wed, 9 Nov 2022 14:37:06 +0100 Subject: [PATCH] Appends org to url params when using flux query with v1 connection --- lib/instream/query/url.ex | 1 + .../connection/query_language_org_test.exs | 75 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 test/influxdb_v2/connection/query_language_org_test.exs diff --git a/lib/instream/query/url.ex b/lib/instream/query/url.ex index 25adb44..0661ccb 100644 --- a/lib/instream/query/url.ex +++ b/lib/instream/query/url.ex @@ -41,6 +41,7 @@ defmodule Instream.Query.URL do |> url("api/v2/query") |> append_param("db", opts[:database] || config[:database]) |> append_param("epoch", encode_precision(opts[:precision])) + |> append_param("org", opts[:org] || config[:org]) {:v1, _} -> config diff --git a/test/influxdb_v2/connection/query_language_org_test.exs b/test/influxdb_v2/connection/query_language_org_test.exs new file mode 100644 index 0000000..8b7c6ee --- /dev/null +++ b/test/influxdb_v2/connection/query_language_org_test.exs @@ -0,0 +1,75 @@ +defmodule Instream.InfluxDBv2.Connection.QueryLanguageOrgTest do + use ExUnit.Case, async: true + + @moduletag :"influxdb_include_2.x" + + import Mox + + alias Instream.TestHelpers.HTTPClientMock + + setup :verify_on_exit! + + describe "v1 client" do + defmodule MockConnectionV1 do + use Instream.Connection, + config: [ + bucket: "default_bucket", + http_client: HTTPClientMock, + loggers: [], + org: "default_org", + version: :v1, + database: "mapped_database" + ] + end + + test "adds org to query params when using flux" do + expected_url = "http://localhost:8086/api/v2/query?db=mapped_database&org=default_org" + + HTTPClientMock + |> expect(:request, fn :post, ^expected_url, _, _, _ -> {:ok, 200, [], ""} end) + + MockConnectionV1.query("--ignored--", query_language: :flux) + end + + test "doesn't add org to query params when using influxql" do + expected_url = "http://localhost:8086/query?db=mapped_database&q=--ignored--" + + HTTPClientMock + |> expect(:request, fn :get, ^expected_url, _, _, _ -> {:ok, 200, [], ""} end) + + MockConnectionV1.query("--ignored--", query_language: :influxql) + end + end + + describe "v2 client" do + defmodule MockConnectionV2 do + use Instream.Connection, + config: [ + bucket: "default_bucket", + http_client: HTTPClientMock, + loggers: [], + org: "default_org", + version: :v2, + database: "mapped_database" + ] + end + + test "adds org to query params when using flux" do + expected_url = "http://localhost:8086/api/v2/query?org=default_org" + + HTTPClientMock + |> expect(:request, fn :post, ^expected_url, _, _, _ -> {:ok, 200, [], ""} end) + + MockConnectionV2.query("--ignored--", query_language: :flux) + end + + test "doesn't add org to query params when using influxql" do + expected_url = "http://localhost:8086/query?db=mapped_database&q=--ignored--" + + HTTPClientMock + |> expect(:request, fn :post, ^expected_url, _, _, _ -> {:ok, 200, [], ""} end) + + MockConnectionV2.query("--ignored--", query_language: :influxql) + end + end +end