Skip to content

Commit

Permalink
Clean up things here and there in tests (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide authored Oct 21, 2023
1 parent f206676 commit a027f12
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 77 deletions.
12 changes: 6 additions & 6 deletions test/integration/authentication_test.exs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
defmodule AuthenticationTest do
auth_options = [username: "cassandra", password: "cassandra"]
port = System.get_env("CASSANDRA_WITH_AUTH_PORT", "9053")

use XandraTest.IntegrationCase,
start_options: [
authentication: {Xandra.Authenticator.Password, auth_options},
nodes: ["127.0.0.1:#{port}"]
]
authentication:
{Xandra.Authenticator.Password, [username: "cassandra", password: "cassandra"]},
nodes: ["127.0.0.1:#{XandraTest.IntegrationCase.port_with_auth()}"]
],
async: true

alias Xandra.TestHelper

@moduletag :authentication

@tag start_conn: false
test "challenge is passed to cluster connections", %{start_options: start_options} do
cluster = start_link_supervised!({Xandra.Cluster, start_options})
TestHelper.await_cluster_connected(cluster)
Expand Down
5 changes: 3 additions & 2 deletions test/integration/batch_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule BatchTest do
use XandraTest.IntegrationCase
use XandraTest.IntegrationCase, async: true

alias Xandra.{Batch, Error, Void, Page}

Expand All @@ -9,7 +9,7 @@ defmodule BatchTest do
:ok
end

setup %{conn: conn} do
setup %{setup_conn: conn} do
Xandra.execute!(conn, "TRUNCATE users")
:ok
end
Expand Down Expand Up @@ -93,6 +93,7 @@ defmodule BatchTest do
]
end

@tag start_conn: false
test "an error is raised if named parameters are used with simple queries" do
message = ~r/non-prepared statements inside batch queries only support positional/

Expand Down
2 changes: 2 additions & 0 deletions test/integration/compression_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ defmodule CompressionTest do
end

describe "compression with the LZ4 algorithm" do
@describetag start_conn: false

setup %{start_options: start_options} do
%{start_options: start_options ++ [compressor: LZ4Compressor]}
end
Expand Down
22 changes: 5 additions & 17 deletions test/integration/datatypes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule DataTypesTest do
:tuple
])

@tag start_conn: false
test "all the datatypes are documented in the guides" do
types_in_guides =
Mix.Project.project_file()
Expand Down Expand Up @@ -515,9 +516,7 @@ defmodule DataTypesTest do
Xandra.execute!(conn, prepared, [1, foo_profile])
Xandra.execute!(conn, prepared, [2, bar_profile])

statement = "SELECT id, profile FROM users"
page = Xandra.execute!(conn, statement)
assert [foo, bar] = Enum.to_list(page)
assert [foo, bar] = conn |> Xandra.execute!("SELECT id, profile FROM users") |> Enum.to_list()
assert Map.fetch!(foo, "id") == 1
assert Map.fetch!(foo, "profile") == foo_profile
assert Map.fetch!(bar, "id") == 2
Expand All @@ -527,17 +526,8 @@ defmodule DataTypesTest do
"full_name" => %{"first_name" => nil, "last_name" => "Bar"}
}

statement = """
ALTER TYPE profile ADD email text
"""

Xandra.execute!(conn, statement)

statement = """
ALTER TYPE profile ADD age int
"""

Xandra.execute!(conn, statement)
Xandra.execute!(conn, "ALTER TYPE profile ADD email text")
Xandra.execute!(conn, "ALTER TYPE profile ADD age int")

statement = "INSERT INTO #{keyspace}.users (id, profile) VALUES (?, ?)"

Expand All @@ -550,9 +540,7 @@ defmodule DataTypesTest do
prepared = Xandra.prepare!(conn, statement)
Xandra.execute!(conn, prepared, [3, baz_profile])

statement = "SELECT id, profile FROM users"
page = Xandra.execute!(conn, statement)

page = Xandra.execute!(conn, "SELECT id, profile FROM users")
assert [foo, bar, baz] = Enum.to_list(page)
assert Map.fetch!(foo, "id") == 1

Expand Down
6 changes: 4 additions & 2 deletions test/integration/encryption_test.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
defmodule EncryptionTest do
start_options = [
nodes: ["127.0.0.1:9152"],
nodes: ["127.0.0.1:#{XandraTest.IntegrationCase.cassandra_port_with_ssl()}"],
encryption: true,
transport_options: [verify: :verify_none]
]

use XandraTest.IntegrationCase, start_options: start_options
use XandraTest.IntegrationCase, start_options: start_options, async: true

@moduletag :encryption
@moduletag :cassandra_specific
@moduletag start_conn: false

test "encrypted connections", %{keyspace: keyspace, start_options: start_options} do
assert {:ok, conn} = start_supervised({Xandra, start_options})
Expand Down
3 changes: 2 additions & 1 deletion test/integration/errors_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ defmodule ErrorsTest do

@tag :cassandra_specific
@tag :skip_for_native_protocol_v3
@tag start_conn: false
test "function_failure error", %{keyspace: keyspace, start_options: start_options} do
# This is only supported in native protocol v4.
start_options = Keyword.put(start_options, :protocol_version, :v4)

{:ok, conn} = Xandra.start_link(start_options)
conn = start_supervised!({Xandra, start_options})
Xandra.execute!(conn, "USE #{keyspace}")
Xandra.execute!(conn, "CREATE TABLE funs (id int PRIMARY KEY, name text)")

Expand Down
5 changes: 2 additions & 3 deletions test/integration/protocol_negotiation_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule ProtocolNegotiationTest do
use ExUnit.Case, async: true
use XandraTest.IntegrationCase, async: true

# This test tests a few things.
#
Expand All @@ -9,8 +9,7 @@ defmodule ProtocolNegotiationTest do
# BETA flag (which we DO NOT support), so this test tests that Xandra can correctly
# downgrade to a lower version of the protocol
#
test "beta protocol v5" do
conn = start_supervised!({Xandra, XandraTest.IntegrationCase.default_start_options()})
test "beta protocol v5", %{conn: conn} do
assert %Xandra.Page{} = Xandra.execute!(conn, "SELECT * FROM system.local")
end
end
3 changes: 2 additions & 1 deletion test/integration/results_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ defmodule ResultsTest do
describe "SCHEMA_CHANGE updates since native protocol v4" do
@describetag :cassandra_specific
@describetag :skip_for_native_protocol_v3
@describetag start_conn: false

setup %{start_options: start_options} do
start_options = Keyword.put(start_options, :protocol_version, :v4)
{:ok, conn} = Xandra.start_link(start_options)
conn = start_supervised!({Xandra, start_options})
%{conn: conn}
end

Expand Down
41 changes: 21 additions & 20 deletions test/integration/telemetry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@ defmodule TelemetryTest do

alias Xandra.Prepared

@port String.to_integer(System.get_env("CASSANDRA_PORT", "9052"))

setup_all %{setup_conn: conn, keyspace: keyspace} do
Xandra.execute!(conn, "CREATE TABLE #{keyspace}.names (name text PRIMARY KEY)")
:ok
end

describe "connection" do
@tag start_conn: false
test "sends event on connection/disconnection", %{start_options: start_options} do
ref = :telemetry_test.attach_event_handlers(self(), [[:xandra, :connected]])

start_supervised!({Xandra, [name: :telemetry_test_connection] ++ start_options})
conn = start_supervised!({Xandra, [name: :telemetry_test_connection] ++ start_options})

assert_receive {[:xandra, :connected], ^ref, measurements, metadata}
assert_receive {[:xandra, :connected], ^ref, measurements, %{connection: ^conn} = metadata}

assert measurements == %{}
assert metadata.connection_name == :telemetry_test_connection
assert metadata.address == "127.0.0.1"
assert metadata.port == @port
assert metadata.port == XandraTest.IntegrationCase.port()
end
end

Expand All @@ -35,31 +34,31 @@ defmodule TelemetryTest do
statement = "SELECT * FROM names"
assert {:ok, prepared} = Xandra.prepare(conn, statement)

assert_receive {[:xandra, :prepared_cache, :miss], ^ref, %{}, metadata}
assert_receive {[:xandra, :prepared_cache, :miss], ^ref, %{}, %{connection: ^conn} = metadata}

assert metadata.query.statement == statement
assert metadata.connection_name == nil
assert metadata.address == "127.0.0.1"
assert metadata.port == @port
assert metadata.port == XandraTest.IntegrationCase.port()

# Successive call to prepare uses cache.
assert {:ok, ^prepared} = Xandra.prepare(conn, statement)

assert_receive {[:xandra, :prepared_cache, :hit], ^ref, %{}, metadata}
assert_receive {[:xandra, :prepared_cache, :hit], ^ref, %{}, %{connection: ^conn} = metadata}

assert metadata.query.statement == statement
assert metadata.connection_name == nil
assert metadata.address == "127.0.0.1"
assert metadata.port == @port
assert metadata.port == XandraTest.IntegrationCase.port()

assert {:ok, ^prepared} = Xandra.prepare(conn, statement, force: true)

assert_receive {[:xandra, :prepared_cache, :hit], ^ref, %{}, metadata}
assert_receive {[:xandra, :prepared_cache, :hit], ^ref, %{}, %{connection: ^conn} = metadata}

assert metadata.query.statement == statement
assert metadata.connection_name == nil
assert metadata.address == "127.0.0.1"
assert metadata.port == @port
assert metadata.port == XandraTest.IntegrationCase.port()
end

test "prepare query", %{conn: conn} do
Expand All @@ -70,37 +69,39 @@ defmodule TelemetryTest do
])

statement = "SELECT name FROM names"
assert {:ok, %Prepared{}} = Xandra.prepare(conn, statement, telemetry_metadata: %{foo: :bar})
assert %Prepared{} = Xandra.prepare!(conn, statement, telemetry_metadata: %{foo: :bar})

assert_receive {[:xandra, :prepare_query, :start], ^ref, %{system_time: system_time},
metadata}
%{connection: ^conn} = metadata}

assert metadata.query.statement == statement
assert metadata.connection_name == nil
assert metadata.address == "127.0.0.1"
assert metadata.port == @port
assert metadata.port == XandraTest.IntegrationCase.port()
assert metadata.extra_metadata == %{foo: :bar}
assert is_integer(system_time)

assert_receive {[:xandra, :prepare_query, :stop], ^ref, %{duration: duration}, metadata}
assert_receive {[:xandra, :prepare_query, :stop], ^ref, %{duration: duration},
%{connection: ^conn} = metadata}

assert metadata.query.statement == statement
assert metadata.connection_name == nil
assert metadata.address == "127.0.0.1"
assert metadata.port == @port
assert metadata.port == XandraTest.IntegrationCase.port()
assert metadata.extra_metadata == %{foo: :bar}
assert metadata.reprepared == false
assert is_integer(duration)

assert {:ok, %Prepared{}} =
Xandra.prepare(conn, statement, telemetry_metadata: %{foo: :bar}, force: true)

assert_receive {[:xandra, :prepare_query, :stop], ^ref, %{duration: duration}, metadata}
assert_receive {[:xandra, :prepare_query, :stop], ^ref, %{duration: duration},
%{connection: ^conn} = metadata}

assert metadata.query.statement == statement
assert metadata.connection_name == nil
assert metadata.address == "127.0.0.1"
assert metadata.port == @port
assert metadata.port == XandraTest.IntegrationCase.port()
assert metadata.extra_metadata == %{foo: :bar}
assert metadata.reprepared == true
assert is_integer(duration)
Expand All @@ -124,7 +125,7 @@ defmodule TelemetryTest do
assert metadata.query.statement == statement
assert metadata.connection_name == nil
assert metadata.address == "127.0.0.1"
assert metadata.port == @port
assert metadata.port == XandraTest.IntegrationCase.port()
assert metadata.extra_metadata == %{foo: :bar}
assert is_integer(system_time)

Expand All @@ -133,7 +134,7 @@ defmodule TelemetryTest do
assert metadata.query.statement == statement
assert metadata.connection_name == nil
assert metadata.address == "127.0.0.1"
assert metadata.port == @port
assert metadata.port == XandraTest.IntegrationCase.port()
assert metadata.extra_metadata == %{foo: :bar}
assert is_integer(duration)
end
Expand Down
Loading

0 comments on commit a027f12

Please sign in to comment.