Skip to content

Commit

Permalink
Pop conn-related options out of global options
Browse files Browse the repository at this point in the history
And also fix all tests.

Co-authored-by: Aman Kumar Singh <[email protected]>
Co-authored-by: Harisudhan V R <[email protected]>
  • Loading branch information
3 people committed Dec 19, 2024
1 parent 6b8b45b commit 1660528
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 31 deletions.
9 changes: 5 additions & 4 deletions lib/azurex/blob.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ defmodule Azurex.Blob do
@spec put_blob(
String.t(),
binary() | {:stream, Enumerable.t()},
String.t(),
keyword()
) ::
:ok
Expand Down Expand Up @@ -109,7 +110,7 @@ defmodule Azurex.Blob do
{params, options} = Keyword.pop(options, :params, [])
{headers, options} = Keyword.pop(options, :headers, [])
headers = Enum.map(headers, fn {k, v} -> {to_string(k), v} end)
connection_params = Config.get_connection_params(options)
{connection_params, options} = Config.get_connection_params(options)

%HTTPoison.Request{
method: :put,
Expand Down Expand Up @@ -205,7 +206,7 @@ defmodule Azurex.Blob do
{:ok, HTTPoison.Response.t()} | {:error, term()}
def copy_blob(source_name, destination_name, overrides \\ []) do
content_type = "application/octet-stream"
connection_params = Config.get_connection_params(overrides)
{connection_params, _overrides} = Config.get_connection_params(overrides)
source_url = get_url(source_name, connection_params)
headers = [{"x-ms-copy-source", source_url}, {"content-type", content_type}]

Expand Down Expand Up @@ -243,7 +244,7 @@ defmodule Azurex.Blob do
def blob_request(name, method, options) do
{params, options} = Keyword.pop(options, :params, [])
{headers, options} = Keyword.pop(options, :headers, [])
connection_params = Config.get_connection_params(options)
{connection_params, options} = Config.get_connection_params(options)

%HTTPoison.Request{
method: method,
Expand Down Expand Up @@ -277,7 +278,7 @@ defmodule Azurex.Blob do
| {:error, HTTPoison.AsyncResponse.t() | HTTPoison.Error.t() | HTTPoison.Response.t()}
def list_blobs(options \\ []) do
{params, options} = Keyword.pop(options, :params, [])
connection_params = Config.get_connection_params(options)
{connection_params, _options} = Config.get_connection_params(options)

%HTTPoison.Request{
url: get_url(connection_params),
Expand Down
4 changes: 2 additions & 2 deletions lib/azurex/blob/block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Azurex.Blob.Block do
{headers, options} = Keyword.pop(options, :headers, [])
headers = Enum.map(headers, fn {k, v} -> {to_string(k), v} end)
params = [{:comp, "block"}, {:blockid, block_id} | params]
connection_params = Config.get_connection_params(options)
{connection_params, options} = Config.get_connection_params(options)

%HTTPoison.Request{
method: :put,
Expand Down Expand Up @@ -66,7 +66,7 @@ defmodule Azurex.Blob.Block do
params = [{:comp, "blocklist"} | params]
content_type = "text/plain; charset=UTF-8"
blob_content_type = blob_content_type || "application/octet-stream"
connection_params = Config.get_connection_params(options)
{connection_params, options} = Config.get_connection_params(options)

blocks =
block_ids
Expand Down
19 changes: 15 additions & 4 deletions lib/azurex/blob/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,21 @@ defmodule Azurex.Blob.Config do
Returns the given configuration keyword list.
If the parameter is a string, it is interpreted as the container for backwards compatibility.
"""
@spec get_connection_params(config_overrides | nil) :: keyword()
def get_connection_params(nil), do: []
def get_connection_params(container) when is_binary(container), do: [container: container]
def get_connection_params(config), do: config
@spec get_connection_params(keyword()) :: {keyword(), keyword()}
def get_connection_params(nil), do: {[], []}
def get_connection_params(container) when is_binary(container), do: {[container: container], []}

def get_connection_params(config) when is_list(config) do
conn_keys = [
:api_url,
:container,
:storage_account_connection_string,
:storage_account_key,
:storage_account_name
]

{Keyword.take(config, conn_keys), Keyword.drop(config, conn_keys)}
end

@spec get_connection_string_from_params(String.t(), config_overrides) :: String.t() | nil
defp get_connection_string_from_params(key, connection_params) do
Expand Down
4 changes: 2 additions & 2 deletions lib/azurex/blob/container.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Azurex.Blob.Container do
alias Azurex.Authorization.SharedKey

def head_container(container, overrides \\ []) do
connection_params = Config.get_connection_params(overrides)
{connection_params, _overrides} = Config.get_connection_params(overrides)

%HTTPoison.Request{
url: Config.api_url(connection_params) <> "/" <> container,
Expand All @@ -27,7 +27,7 @@ defmodule Azurex.Blob.Container do
end

def create(container, overrides \\ []) do
connection_params = Config.get_connection_params(overrides)
{connection_params, _overrides} = Config.get_connection_params(overrides)

%HTTPoison.Request{
url: Config.api_url(connection_params) <> "/" <> container,
Expand Down
2 changes: 1 addition & 1 deletion lib/azurex/blob/shared_access_signature.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Azurex.Blob.SharedAccessSignature do
"""
@spec sas_url(Config.config_overrides(), String.t(), [{atom(), any()}]) :: String.t()
def sas_url(overrides \\ [], resource, opts \\ []) do
connection_params = Config.get_connection_params(overrides)
{connection_params, _overrides} = Config.get_connection_params(overrides)
base_url = Config.api_url(connection_params)
resource_type = Keyword.get(opts, :resource_type, :container)
permissions = Keyword.get(opts, :permissions, [:read])
Expand Down
71 changes: 64 additions & 7 deletions test/azurex/blob/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,70 @@ defmodule Azurex.Blob.ConfigTest do

describe "get_connection_params/1" do
test "assumes it's a container if it's given a string" do
assert get_connection_params("container_name") == [container: "container_name"]
end

test "returns a keyword list as is" do
keyword = [storage_account_name: "name", container: "container"]

assert get_connection_params(keyword) == keyword
{conn, remaining} = get_connection_params("container_name")
assert conn == [container: "container_name"]
assert remaining == []
end

test "returns 2 keyword lists, separating out connection-related configuration" do
{conn, remaining} =
get_connection_params(
foo: "bar",
baz: "qux",
api_url: "api-url",
container: "container",
storage_account_connection_string: "sa-conn-string",
storage_account_key: "sa-key",
storage_account_name: "sa-name"
)

assert conn == [
api_url: "api-url",
container: "container",
storage_account_connection_string: "sa-conn-string",
storage_account_key: "sa-key",
storage_account_name: "sa-name"
]

assert remaining == [
foo: "bar",
baz: "qux"
]
end

test "returns 2 keyword lists, separating out connection-related configuration wven when not all elements specified" do
{conn, remaining} =
get_connection_params(
foo: "bar",
baz: "qux",
api_url: "api-url",
container: "container"
)

assert conn == [
api_url: "api-url",
container: "container"
]

assert remaining == [
foo: "bar",
baz: "qux"
]
end

test "returns 2 keyword lists, separating out connection-related configuration wven when no elements specified" do
{conn, remaining} =
get_connection_params(
foo: "bar",
baz: "qux"
)

assert conn == []

assert remaining == [
foo: "bar",
baz: "qux"
]
end
end
end
20 changes: 10 additions & 10 deletions test/integration/blob_integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ defmodule Azurex.BlobIntegrationTests do
blob_name,
@sample_file_contents,
"text/plain",
@integration_testing_container
container: @integration_testing_container
) == :ok

assert Blob.get_blob(
blob_name,
@integration_testing_container
container: @integration_testing_container
) == {:ok, @sample_file_contents}
end

Expand All @@ -73,7 +73,7 @@ defmodule Azurex.BlobIntegrationTests do
blob_name,
@sample_file_contents,
"text/plain",
@integration_testing_container,
container: @integration_testing_container,
options: [
params: [timeout: 10, ignored_param: "ignored_param_value"],
headers: [ignored_header: "ignored_header_value"]
Expand All @@ -82,7 +82,7 @@ defmodule Azurex.BlobIntegrationTests do

assert Blob.get_blob(
blob_name,
@integration_testing_container,
container: @integration_testing_container,
timeout: 10
) == {:ok, @sample_file_contents}
end
Expand All @@ -98,7 +98,7 @@ defmodule Azurex.BlobIntegrationTests do

AzuriteSetup.set_env()

assert Blob.get_blob(blob_name, @integration_testing_container) ==
assert Blob.get_blob(blob_name, container: @integration_testing_container) ==
{:ok, @sample_file_contents}
end
end
Expand Down Expand Up @@ -129,11 +129,11 @@ defmodule Azurex.BlobIntegrationTests do
blob_name,
@sample_file_contents,
"text/plain",
@integration_testing_container
container: @integration_testing_container
) == :ok

assert {:error, :not_found} = Blob.head_blob(blob_name)
assert {:ok, headers} = Blob.head_blob(blob_name, @integration_testing_container)
assert {:ok, headers} = Blob.head_blob(blob_name, container: @integration_testing_container)
headers = Map.new(headers)

assert headers["content-md5"] ==
Expand All @@ -147,7 +147,6 @@ defmodule Azurex.BlobIntegrationTests do
blob_name,
@sample_file_contents,
"text/plain",
nil,
headers: ["x-ms-meta-foo": "bar"]
) == :ok

Expand Down Expand Up @@ -216,7 +215,8 @@ defmodule Azurex.BlobIntegrationTests do
end

test "passing container, not checking result" do
assert {:ok, _result_not_checked} = Blob.list_blobs(@integration_testing_container)
assert {:ok, _result_not_checked} =
Blob.list_blobs(container: @integration_testing_container)
end

test "passing container as connection parameter, not checking result" do
Expand All @@ -226,7 +226,7 @@ defmodule Azurex.BlobIntegrationTests do

test "passing container and params, not checking result" do
assert {:ok, _result_not_checked} =
Blob.list_blobs(@integration_testing_container, timeout: 10)
Blob.list_blobs(container: @integration_testing_container, timeout: 10)
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/support/azurite_setup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule AzuriteSetup do
@test_blob_name,
"test_blob_content",
"text/plain",
@default_container
container: @default_container
)
end
end

0 comments on commit 1660528

Please sign in to comment.