diff --git a/apps/astarte_realm_management/test/astarte_realm_management/engine_test.exs b/apps/astarte_realm_management/test/astarte_realm_management/engine_test.exs index 7b10138a0..3445b68a7 100644 --- a/apps/astarte_realm_management/test/astarte_realm_management/engine_test.exs +++ b/apps/astarte_realm_management/test/astarte_realm_management/engine_test.exs @@ -618,20 +618,14 @@ defmodule Astarte.RealmManagement.EngineTest do @test_realm_name "autotestrealm" setup do - with {:ok, client} <- DatabaseTestHelper.connect_to_test_database() do - DatabaseTestHelper.seed_test_data(client) - end + DatabaseTestHelper.seed_test_data() end setup_all do - with {:ok, client} <- DatabaseTestHelper.connect_to_test_database() do - DatabaseTestHelper.create_test_keyspace(client) - end + DatabaseTestHelper.create_test_keyspace() on_exit(fn -> - with {:ok, client} <- DatabaseTestHelper.connect_to_test_database() do - DatabaseTestHelper.drop_test_keyspace(client) - end + DatabaseTestHelper.drop_test_keyspace() end) end @@ -677,7 +671,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.list_interface_versions( "autotestrealm", "com.ispirata.Hemera.DeviceLog.Configuration" - ) == {:ok, [[major_version: 1, minor_version: 0]]} + ) == {:ok, [%{major_version: 1, minor_version: 0}]} assert Engine.list_interface_versions( "autotestrealm", @@ -753,9 +747,7 @@ defmodule Astarte.RealmManagement.EngineTest do unpack_source({:ok, @test_draft_interface_a_0}) assert Engine.list_interface_versions("autotestrealm", "com.ispirata.Draft") == - {:ok, [[major_version: 0, minor_version: 2]]} - - {:ok, client} = Database.connect(realm: "autotestrealm") + {:ok, [%{major_version: 0, minor_version: 2}]} d = :crypto.strong_rand_bytes(16) @@ -767,10 +759,9 @@ defmodule Astarte.RealmManagement.EngineTest do ) p1 = "/filterRules/0/TEST/value" - DatabaseTestHelper.seed_properties_test_value(client, d, "com.ispirata.Draft", 0, e1, p1) + DatabaseTestHelper.seed_properties_test_value(d, "com.ispirata.Draft", 0, e1, p1) assert DatabaseTestHelper.count_interface_properties_for_device( - client, d, "com.ispirata.Draft", 0 @@ -779,7 +770,6 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.delete_interface("autotestrealm", "com.ispirata.Draft", 0) == :ok assert DatabaseTestHelper.count_interface_properties_for_device( - client, d, "com.ispirata.Draft", 0 @@ -801,7 +791,7 @@ defmodule Astarte.RealmManagement.EngineTest do unpack_source({:ok, @test_draft_interface_a_0}) assert Engine.list_interface_versions("autotestrealm", "com.ispirata.Draft") == - {:ok, [[major_version: 0, minor_version: 2]]} + {:ok, [%{major_version: 0, minor_version: 2}]} end test "install object aggregated interface" do @@ -810,7 +800,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["com.ObjectAggregation"]} assert Engine.list_interface_versions("autotestrealm", "com.ObjectAggregation") == - {:ok, [[major_version: 0, minor_version: 3]]} + {:ok, [%{major_version: 0, minor_version: 3}]} assert Engine.delete_interface("autotestrealm", "com.ObjectAggregation", 0) == :ok @@ -826,7 +816,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["com.ObjectAggregation"]} assert Engine.list_interface_versions("autotestrealm", "com.ObjectAggregation") == - {:ok, [[major_version: 0, minor_version: 3]]} + {:ok, [%{major_version: 0, minor_version: 3}]} assert Engine.delete_interface("autotestrealm", "com.ObjectAggregation", 0) == :ok end @@ -837,16 +827,13 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["com.ispirata.TestDatastream"]} assert Engine.list_interface_versions("autotestrealm", "com.ispirata.TestDatastream") == - {:ok, [[major_version: 0, minor_version: 10]]} - - {:ok, client} = Database.connect(realm: "autotestrealm") + {:ok, [%{major_version: 0, minor_version: 10}]} d = :crypto.strong_rand_bytes(16) e1 = CQLUtils.endpoint_id("com.ispirata.TestDatastream", 0, "/%{sensorId}/realValues") p1 = "/0/realValues" DatabaseTestHelper.seed_datastream_test_data( - client, d, "com.ispirata.TestDatastream", 0, @@ -858,7 +845,6 @@ defmodule Astarte.RealmManagement.EngineTest do p2 = "/0/integerValues" DatabaseTestHelper.seed_datastream_test_data( - client, d, "com.ispirata.TestDatastream", 0, @@ -869,7 +855,6 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.delete_interface("autotestrealm", "com.ispirata.TestDatastream", 0) == :ok assert DatabaseTestHelper.count_rows_for_datastream( - client, d, "com.ispirata.TestDatastream", 0, @@ -878,7 +863,6 @@ defmodule Astarte.RealmManagement.EngineTest do ) == 0 assert DatabaseTestHelper.count_rows_for_datastream( - client, d, "com.ispirata.TestDatastream", 0, @@ -897,7 +881,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["com.ispirata.TestDatastream"]} assert Engine.list_interface_versions("autotestrealm", "com.ispirata.TestDatastream") == - {:ok, [[major_version: 0, minor_version: 10]]} + {:ok, [%{major_version: 0, minor_version: 10}]} assert Engine.update_interface("autotestrealm", @test_draft_interface_c_1) == :ok @@ -908,7 +892,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["com.ispirata.TestDatastream"]} assert Engine.list_interface_versions("autotestrealm", "com.ispirata.TestDatastream") == - {:ok, [[major_version: 0, minor_version: 15]]} + {:ok, [%{major_version: 0, minor_version: 15}]} end test "update explicit timestamp, doc, description, expiry and retention for individual datastream interface" do @@ -917,7 +901,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["org.astarte-platform.Values"]} assert Engine.list_interface_versions("autotestrealm", "org.astarte-platform.Values") == - {:ok, [[major_version: 1, minor_version: 0]]} + {:ok, [%{major_version: 1, minor_version: 0}]} assert Engine.update_interface("autotestrealm", @test_interface_d_1) == :ok @@ -927,7 +911,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert {:ok, ^updated_interface} = unpack_source({:ok, @test_interface_d_1}) assert Engine.list_interface_versions("autotestrealm", "org.astarte-platform.Values") == - {:ok, [[major_version: 1, minor_version: 1]]} + {:ok, [%{major_version: 1, minor_version: 1}]} assert Engine.update_interface("autotestrealm", @test_interface_d_incompatible_change) == {:error, :incompatible_endpoint_change} @@ -942,7 +926,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["com.ObjectAggregation"]} assert Engine.list_interface_versions("autotestrealm", "com.ObjectAggregation") == - {:ok, [[major_version: 0, minor_version: 3]]} + {:ok, [%{major_version: 0, minor_version: 3}]} assert Engine.update_interface("autotestrealm", @test_draft_interface_b_1) == :ok @@ -952,7 +936,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["com.ObjectAggregation"]} assert Engine.list_interface_versions("autotestrealm", "com.ObjectAggregation") == - {:ok, [[major_version: 0, minor_version: 4]]} + {:ok, [%{major_version: 0, minor_version: 4}]} assert Engine.delete_interface("autotestrealm", "com.ObjectAggregation", 0) == :ok @@ -966,7 +950,7 @@ defmodule Astarte.RealmManagement.EngineTest do {:ok, ["com.autotest.AggregateValuesUpdate"]} assert Engine.list_interface_versions("autotestrealm", "com.autotest.AggregateValuesUpdate") == - {:ok, [[major_version: 1, minor_version: 0]]} + {:ok, [%{major_version: 1, minor_version: 0}]} assert Engine.update_interface("autotestrealm", @test_interface_e_1) == :ok @@ -978,7 +962,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert {:ok, ^updated_interface} = unpack_source({:ok, @test_interface_e_1}) assert Engine.list_interface_versions("autotestrealm", "com.autotest.AggregateValuesUpdate") == - {:ok, [[major_version: 1, minor_version: 1]]} + {:ok, [%{major_version: 1, minor_version: 1}]} assert Engine.update_interface("autotestrealm", @test_interface_e_incompatible_change) == {:error, :invalid_interface_document} @@ -994,7 +978,7 @@ defmodule Astarte.RealmManagement.EngineTest do "autotestrealm", "com.autotest.AggregateValuesUpdateAndAdd" ) == - {:ok, [[major_version: 1, minor_version: 0]]} + {:ok, [%{major_version: 1, minor_version: 0}]} assert Engine.update_interface("autotestrealm", @test_interface_f_1) == :ok @@ -1009,7 +993,7 @@ defmodule Astarte.RealmManagement.EngineTest do "autotestrealm", "com.autotest.AggregateValuesUpdateAndAdd" ) == - {:ok, [[major_version: 1, minor_version: 1]]} + {:ok, [%{major_version: 1, minor_version: 1}]} end test "fail update missing interface" do @@ -1026,7 +1010,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["com.ispirata.TestDatastream"]} assert Engine.list_interface_versions("autotestrealm", "com.ispirata.TestDatastream") == - {:ok, [[major_version: 0, minor_version: 10]]} + {:ok, [%{major_version: 0, minor_version: 10}]} assert Engine.update_interface("autotestrealm", @test_draft_interface_c_wrong_update) == {:error, :missing_endpoints} @@ -1034,7 +1018,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["com.ispirata.TestDatastream"]} assert Engine.list_interface_versions("autotestrealm", "com.ispirata.TestDatastream") == - {:ok, [[major_version: 0, minor_version: 10]]} + {:ok, [%{major_version: 0, minor_version: 10}]} assert Engine.update_interface("autotestrealm", @test_draft_interface_c_1) == :ok @@ -1048,7 +1032,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["com.ispirata.TestDatastream"]} assert Engine.list_interface_versions("autotestrealm", "com.ispirata.TestDatastream") == - {:ok, [[major_version: 0, minor_version: 15]]} + {:ok, [%{major_version: 0, minor_version: 15}]} end test "fail on interface type change" do @@ -1057,7 +1041,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["com.ispirata.TestDatastream"]} assert Engine.list_interface_versions("autotestrealm", "com.ispirata.TestDatastream") == - {:ok, [[major_version: 0, minor_version: 10]]} + {:ok, [%{major_version: 0, minor_version: 10}]} assert Engine.update_interface("autotestrealm", @test_draft_interface_c_invalid_change) == {:error, :invalid_update} @@ -1069,7 +1053,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["com.ispirata.TestDatastream"]} assert Engine.list_interface_versions("autotestrealm", "com.ispirata.TestDatastream") == - {:ok, [[major_version: 0, minor_version: 10]]} + {:ok, [%{major_version: 0, minor_version: 10}]} end test "fail on mapping incompatible change" do @@ -1078,7 +1062,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["com.ispirata.TestDatastream"]} assert Engine.list_interface_versions("autotestrealm", "com.ispirata.TestDatastream") == - {:ok, [[major_version: 0, minor_version: 10]]} + {:ok, [%{major_version: 0, minor_version: 10}]} assert Engine.update_interface("autotestrealm", @test_draft_interface_c_incompatible_change) == {:error, :incompatible_endpoint_change} @@ -1090,7 +1074,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.get_interfaces_list("autotestrealm") == {:ok, ["com.ispirata.TestDatastream"]} assert Engine.list_interface_versions("autotestrealm", "com.ispirata.TestDatastream") == - {:ok, [[major_version: 0, minor_version: 10]]} + {:ok, [%{major_version: 0, minor_version: 10}]} end test "fail on interface downgrade" do @@ -1101,7 +1085,7 @@ defmodule Astarte.RealmManagement.EngineTest do ) == unpack_source({:ok, @test_draft_interface_c_0}) assert Engine.list_interface_versions("autotestrealm", "com.ispirata.TestDatastream") == - {:ok, [[major_version: 0, minor_version: 10]]} + {:ok, [%{major_version: 0, minor_version: 10}]} assert Engine.update_interface("autotestrealm", @test_draft_interface_c_1) == :ok @@ -1110,7 +1094,7 @@ defmodule Astarte.RealmManagement.EngineTest do ) == unpack_source({:ok, @test_draft_interface_c_1}) assert Engine.list_interface_versions("autotestrealm", "com.ispirata.TestDatastream") == - {:ok, [[major_version: 0, minor_version: 15]]} + {:ok, [%{major_version: 0, minor_version: 15}]} assert Engine.update_interface("autotestrealm", @test_draft_interface_c_downgrade) == {:error, :downgrade_not_allowed} @@ -1120,7 +1104,7 @@ defmodule Astarte.RealmManagement.EngineTest do ) == unpack_source({:ok, @test_draft_interface_c_1}) assert Engine.list_interface_versions("autotestrealm", "com.ispirata.TestDatastream") == - {:ok, [[major_version: 0, minor_version: 15]]} + {:ok, [%{major_version: 0, minor_version: 15}]} end test "get JWT public key PEM with existing realm" do @@ -1417,7 +1401,7 @@ defmodule Astarte.RealmManagement.EngineTest do assert Engine.delete_trigger_policy("autotestrealm", "aname") == :ok end - test "trigger with non existant policy fails" do + test "trigger with non existent policy fails" do trigger = %{ realm_name: "autotestrealm", name: "test_trigger", diff --git a/apps/astarte_realm_management/test/astarte_realm_management/queries_test.exs b/apps/astarte_realm_management/test/astarte_realm_management/queries_test.exs index 9d52c1082..8a78af3e4 100644 --- a/apps/astarte_realm_management/test/astarte_realm_management/queries_test.exs +++ b/apps/astarte_realm_management/test/astarte_realm_management/queries_test.exs @@ -19,12 +19,12 @@ defmodule Astarte.RealmManagement.QueriesTest do use ExUnit.Case require Logger - alias CQEx.Query, as: DatabaseQuery alias Astarte.Core.Interface, as: InterfaceDocument alias Astarte.Core.InterfaceDescriptor alias Astarte.RealmManagement.DatabaseTestHelper alias Astarte.RealmManagement.Queries - alias Astarte.RealmManagement.Config + + @test_realm_name "autotestrealm" @object_datastream_interface_json """ { @@ -75,41 +75,41 @@ defmodule Astarte.RealmManagement.QueriesTest do } """ @insert_log_line0_device_a """ - INSERT INTO com_ispirata_hemera_devicelog_v1 + INSERT INTO #{@test_realm_name}.com_ispirata_hemera_devicelog_v1 (device_id, path, reception_timestamp, reception_timestamp_submillis, v_message, v_timestamp, v_monotonictimestamp, v_applicationid, v_pid, v_cmdline) VALUES (536be249-aaaa-4e02-9583-5a4833cbfe49, '/', '2010-02-04 04:05+0000', 0, 'test', '2010-02-03 04:05+0000', 9123456789012345678, 'com.test', 5, '/bin/test') """ @insert_log_line1_device_a """ - INSERT INTO com_ispirata_hemera_devicelog_v1 + INSERT INTO #{@test_realm_name}.com_ispirata_hemera_devicelog_v1 (device_id, path, reception_timestamp, reception_timestamp_submillis, v_message, v_timestamp, v_monotonictimestamp, v_applicationid, v_pid, v_cmdline) VALUES (536be249-aaaa-4e02-9583-5a4833cbfe49, '/', '2012-02-04 04:06+0000', 0, 'testです', '2012-02-03 04:06+0000', -1, 'this.is.a.bit.longer.string', -2, '/usr/bin/things/test') """ @insert_log_line0_device_b """ - INSERT INTO com_ispirata_hemera_devicelog_v1 + INSERT INTO #{@test_realm_name}.com_ispirata_hemera_devicelog_v1 (device_id, path, reception_timestamp, reception_timestamp_submillis, v_message, v_timestamp, v_monotonictimestamp, v_applicationid, v_pid, v_cmdline) VALUES (536be249-bbbb-4e02-9583-5a4833cbfe49, '/', '2012-02-03 04:06+0000', 0, 'testです', '2010-02-03 04:06+0000', -1, 'this.is.a.bit.longer.string', -2, '/usr/bin/things/test') """ @count_log_entries_for_device_a """ - SELECT COUNT(*) FROM autotestrealm.com_ispirata_hemera_devicelog_v1 WHERE device_id=536be249-aaaa-4e02-9583-5a4833cbfe49 AND path='/'; + SELECT COUNT(*) FROM #{@test_realm_name}.com_ispirata_hemera_devicelog_v1 WHERE device_id=536be249-aaaa-4e02-9583-5a4833cbfe49 AND path='/'; """ @count_log_entries_for_device_b """ - SELECT COUNT(*) FROM autotestrealm.com_ispirata_hemera_devicelog_v1 WHERE device_id=536be249-bbbb-4e02-9583-5a4833cbfe49 AND path='/'; + SELECT COUNT(*) FROM #{@test_realm_name}.com_ispirata_hemera_devicelog_v1 WHERE device_id=536be249-bbbb-4e02-9583-5a4833cbfe49 AND path='/'; """ @count_log_entries_for_device_c """ - SELECT COUNT(*) FROM autotestrealm.com_ispirata_hemera_devicelog_v1 WHERE device_id=536be249-cccc-4e02-9583-5a4833cbfe49 AND path='/'; + SELECT COUNT(*) FROM #{@test_realm_name}.com_ispirata_hemera_devicelog_v1 WHERE device_id=536be249-cccc-4e02-9583-5a4833cbfe49 AND path='/'; """ @a_log_entry_for_device_a """ - SELECT * FROM autotestrealm.com_ispirata_hemera_devicelog_v1 WHERE device_id=536be249-aaaa-4e02-9583-5a4833cbfe49 AND path='/' AND reception_timestamp > '2011-02-03 04:05+0000'; + SELECT * FROM #{@test_realm_name}.com_ispirata_hemera_devicelog_v1 WHERE device_id=536be249-aaaa-4e02-9583-5a4833cbfe49 AND path='/' AND reception_timestamp > '2011-02-03 04:05+0000'; """ @an_older_log_entry_for_device_a """ - SELECT * FROM autotestrealm.com_ispirata_hemera_devicelog_v1 WHERE device_id=536be249-aaaa-4e02-9583-5a4833cbfe49 AND path='/' AND reception_timestamp <= '2011-02-03 04:05+0000'; + SELECT * FROM #{@test_realm_name}.com_ispirata_hemera_devicelog_v1 WHERE device_id=536be249-aaaa-4e02-9583-5a4833cbfe49 AND path='/' AND reception_timestamp <= '2011-02-03 04:05+0000'; """ @individual_property_device_owned_interface """ @@ -130,25 +130,25 @@ defmodule Astarte.RealmManagement.QueriesTest do """ @insert_devicelog_status_0 """ - INSERT INTO individual_properties + INSERT INTO #{@test_realm_name}.individual_properties (device_id, interface_id, endpoint_id, path, reception_timestamp, reception_timestamp_submillis, string_value) VALUES (536be249-aaaa-4e02-9583-5a4833cbfe49, :interface_id, :endpoint_id, '/filterRules/0/testKey/value', '2012-02-03 04:06+0000', 0, 'T€ST_VÆLÙE') """ @insert_devicelog_status_1 """ - INSERT INTO individual_properties + INSERT INTO #{@test_realm_name}.individual_properties (device_id, interface_id, endpoint_id, path, reception_timestamp, reception_timestamp_submillis, string_value) VALUES (536be249-aaaa-4e02-9583-5a4833cbfe49, :interface_id, :endpoint_id, '/filterRules/1/testKey2/value', '2012-02-03 04:06+0000', 0, 'test') """ @find_devicelog_status_entry """ SELECT device_id, path, reception_timestamp, string_value - FROM individual_properties + FROM #{@test_realm_name}.individual_properties WHERE device_id=536be249-aaaa-4e02-9583-5a4833cbfe49 AND interface_id=:interface_id AND endpoint_id=:endpoint_id AND path='/filterRules/0/testKey/value' """ @find_devicelog_status_entries """ - SELECT path FROM individual_properties WHERE device_id=536be249-aaaa-4e02-9583-5a4833cbfe49 AND interface_id=:interface_id AND endpoint_id=:endpoint_id; + SELECT path FROM #{@test_realm_name}.individual_properties WHERE device_id=536be249-aaaa-4e02-9583-5a4833cbfe49 AND interface_id=:interface_id AND endpoint_id=:endpoint_id; """ @individual_datastream_with_explicit_timestamp_interface_json """ @@ -172,89 +172,35 @@ defmodule Astarte.RealmManagement.QueriesTest do """ @insert_timestamp_test_value """ - INSERT INTO individual_datastreams (device_id, interface_id, endpoint_id, path, value_timestamp, reception_timestamp, reception_timestamp_submillis, longinteger_value) + INSERT INTO #{@test_realm_name}.individual_datastreams (device_id, interface_id, endpoint_id, path, value_timestamp, reception_timestamp, reception_timestamp_submillis, longinteger_value) VALUES (536be249-aaaa-4e02-9583-5a4833cbfe49, :interface_id, :endpoint_id, '/test/:ind/v', :value_timestamp, :reception_timestamp, 0, :num) ; """ @list_endpoints_by_interface """ - SELECT * FROM endpoints WHERE interface_id = :interface_id; + SELECT * FROM #{@test_realm_name}.endpoints WHERE interface_id = :interface_id; """ @find_interface_by_interface_name """ - SELECT * FROM interfaces WHERE name = :name AND major_version = :major_version; + SELECT * FROM #{@test_realm_name}.interfaces WHERE name = :name AND major_version = :major_version; """ @list_timestamp_test_values """ - SELECT value_timestamp FROM individual_datastreams WHERE device_id=536be249-aaaa-4e02-9583-5a4833cbfe49 AND interface_id=:interface_id AND endpoint_id=:endpoint_id AND path='/test/:ind/v'; + SELECT value_timestamp FROM #{@test_realm_name}.individual_datastreams WHERE device_id=536be249-aaaa-4e02-9583-5a4833cbfe49 AND interface_id=:interface_id AND endpoint_id=:endpoint_id AND path='/test/:ind/v'; """ setup do - with {:ok, client} <- DatabaseTestHelper.connect_to_test_database() do - DatabaseTestHelper.seed_test_data(client) - end + DatabaseTestHelper.seed_test_data() end setup_all do - with {:ok, client} <- DatabaseTestHelper.connect_to_test_database() do - DatabaseTestHelper.create_test_keyspace(client) - end + DatabaseTestHelper.create_test_keyspace() on_exit(fn -> - with {:ok, client} <- DatabaseTestHelper.connect_to_test_database() do - DatabaseTestHelper.drop_test_keyspace(client) - end - end) - end - - def connect_to_test_realm(realm) do - cqex_options = - Config.cqex_options!() - |> Keyword.put(:keyspace, realm) - - CQEx.Client.new!(Config.cassandra_node!(), cqex_options) - end - - def retrieve_endpoint_id(client, interface_name, interface_major, path) do - query = - DatabaseQuery.new() - |> DatabaseQuery.statement(@find_interface_by_interface_name) - |> DatabaseQuery.put(:name, interface_name) - |> DatabaseQuery.put(:major_version, interface_major) - - interface_row = - DatabaseQuery.call!(client, query) - |> Enum.take(1) - |> hd - - automaton = - {:erlang.binary_to_term(interface_row[:automaton_transitions]), - :erlang.binary_to_term(interface_row[:automaton_accepting_states])} - - {:ok, endpoint_id} = Astarte.Core.Mapping.EndpointsAutomaton.resolve_path(path, automaton) - - endpoint_id - end - - def find_endpoint(client, interface_name, interface_major, endpoint) do - query = - DatabaseQuery.new() - |> DatabaseQuery.statement(@list_endpoints_by_interface) - |> DatabaseQuery.put( - :interface_id, - Astarte.Core.CQLUtils.interface_id(interface_name, interface_major) - ) - - DatabaseQuery.call!(client, query) - |> Enum.to_list() - |> Enum.find(fn row -> - row[:endpoint] == endpoint + DatabaseTestHelper.drop_test_keyspace() end) end test "object interface install" do - {:ok, _} = DatabaseTestHelper.connect_to_test_database() - client = connect_to_test_realm("autotestrealm") - json_obj = Jason.decode!(@object_datastream_interface_json) interface_changeset = InterfaceDocument.changeset(%InterfaceDocument{}, json_obj) {:ok, intdoc} = Ecto.Changeset.apply_action(interface_changeset, :insert) @@ -267,64 +213,73 @@ defmodule Astarte.RealmManagement.QueriesTest do {:ok, automaton} = Astarte.Core.Mapping.EndpointsAutomaton.build(intdoc.mappings) - assert Queries.is_interface_major_available?(client, interface_name, major_version) == + assert Queries.is_interface_major_available?(@test_realm_name, interface_name, major_version) == {:ok, false} - assert Queries.is_interface_major_available?(client, interface_name, major_version - 1) == + assert Queries.is_interface_major_available?( + @test_realm_name, + interface_name, + major_version - 1 + ) == {:ok, false} - assert Queries.interface_available_versions(client, interface_name) == + assert Queries.interface_available_versions(@test_realm_name, interface_name) == {:error, :interface_not_found} - assert Queries.get_interfaces_list(client) == {:ok, []} + assert Queries.get_interfaces_list(@test_realm_name) == {:ok, []} - Queries.install_new_interface(client, intdoc, automaton) + Queries.install_new_interface(@test_realm_name, intdoc, automaton) - assert Queries.is_interface_major_available?(client, interface_name, major_version) == + assert Queries.is_interface_major_available?(@test_realm_name, interface_name, major_version) == {:ok, true} - assert Queries.is_interface_major_available?(client, interface_name, major_version - 1) == + assert Queries.is_interface_major_available?( + @test_realm_name, + interface_name, + major_version - 1 + ) == {:ok, false} - assert Queries.interface_available_versions(client, interface_name) == + assert Queries.interface_available_versions(@test_realm_name, interface_name) == {:ok, [ - [ + %{ major_version: major_version, minor_version: minor_version - ] + } ]} - assert Queries.get_interfaces_list(client) == {:ok, ["com.ispirata.Hemera.DeviceLog"]} + assert Queries.get_interfaces_list(@test_realm_name) == + {:ok, ["com.ispirata.Hemera.DeviceLog"]} - DatabaseQuery.call!(client, @insert_log_line0_device_a) - DatabaseQuery.call!(client, @insert_log_line1_device_a) - DatabaseQuery.call!(client, @insert_log_line0_device_b) + execute_query!(@insert_log_line0_device_a) + execute_query!(@insert_log_line1_device_a) + execute_query!(@insert_log_line0_device_b) count = - DatabaseQuery.call!(client, @count_log_entries_for_device_a) + execute_query!(@count_log_entries_for_device_a) |> Enum.to_list() - assert count == [[count: 2]] + assert count == [%{count: 2}] count = - DatabaseQuery.call!(client, @count_log_entries_for_device_b) + execute_query!(@count_log_entries_for_device_b) |> Enum.to_list() - assert count == [[count: 1]] + assert count == [%{count: 1}] count = - DatabaseQuery.call!(client, @count_log_entries_for_device_c) + execute_query!(@count_log_entries_for_device_c) |> Enum.to_list() - assert count == [[count: 0]] + assert count == [%{count: 0}] a_log_entry = - DatabaseQuery.call!(client, @a_log_entry_for_device_a) + execute_query!(@a_log_entry_for_device_a) |> Enum.to_list() assert a_log_entry == [ - [ + %{ device_id: <<83, 107, 226, 73, 170, 170, 78, 2, 149, 131, 90, 72, 51, 203, 254, 73>>, path: "/", @@ -336,15 +291,15 @@ defmodule Astarte.RealmManagement.QueriesTest do v_monotonictimestamp: -1, v_pid: -2, v_timestamp: 1_328_241_960_000 - ] + } ] an_older_log_entry = - DatabaseQuery.call!(client, @an_older_log_entry_for_device_a) + execute_query!(@an_older_log_entry_for_device_a) |> Enum.to_list() assert an_older_log_entry == [ - [ + %{ device_id: <<83, 107, 226, 73, 170, 170, 78, 2, 149, 131, 90, 72, 51, 203, 254, 73>>, path: "/", @@ -356,16 +311,13 @@ defmodule Astarte.RealmManagement.QueriesTest do v_monotonictimestamp: 9_123_456_789_012_345_678, v_pid: 5, v_timestamp: 1_265_169_900_000 - ] + } ] - DatabaseQuery.call!(client, "DROP TABLE com_ispirata_hemera_devicelog_v1") + execute_query!("DROP TABLE #{@test_realm_name}.com_ispirata_hemera_devicelog_v1") end test "individual interface install" do - {:ok, _} = DatabaseTestHelper.connect_to_test_database() - client = connect_to_test_realm("autotestrealm") - json_obj = Jason.decode!(@individual_property_device_owned_interface) interface_changeset = InterfaceDocument.changeset(%InterfaceDocument{}, json_obj) {:ok, intdoc} = Ecto.Changeset.apply_action(interface_changeset, :insert) @@ -378,39 +330,47 @@ defmodule Astarte.RealmManagement.QueriesTest do {:ok, automaton} = Astarte.Core.Mapping.EndpointsAutomaton.build(intdoc.mappings) - assert Queries.is_interface_major_available?(client, interface_name, major_version) == + assert Queries.is_interface_major_available?(@test_realm_name, interface_name, major_version) == {:ok, false} - assert Queries.is_interface_major_available?(client, interface_name, major_version - 1) == + assert Queries.is_interface_major_available?( + @test_realm_name, + interface_name, + major_version - 1 + ) == {:ok, false} - assert Queries.interface_available_versions(client, interface_name) == + assert Queries.interface_available_versions(@test_realm_name, interface_name) == {:error, :interface_not_found} - assert Queries.get_interfaces_list(client) == {:ok, []} + assert Queries.get_interfaces_list(@test_realm_name) == {:ok, []} - Queries.install_new_interface(client, intdoc, automaton) + Queries.install_new_interface(@test_realm_name, intdoc, automaton) - assert Queries.is_interface_major_available?(client, interface_name, major_version) == + assert Queries.is_interface_major_available?(@test_realm_name, interface_name, major_version) == {:ok, true} - assert Queries.is_interface_major_available?(client, interface_name, major_version - 1) == + assert Queries.is_interface_major_available?( + @test_realm_name, + interface_name, + major_version - 1 + ) == {:ok, false} - assert Queries.interface_available_versions(client, interface_name) == + assert Queries.interface_available_versions(@test_realm_name, interface_name) == {:ok, [ - [ + %{ major_version: major_version, minor_version: minor_version - ] + } ]} - assert Queries.get_interfaces_list(client) == {:ok, ["com.ispirata.Hemera.DeviceLog.Status"]} + assert Queries.get_interfaces_list(@test_realm_name) == + {:ok, ["com.ispirata.Hemera.DeviceLog.Status"]} endpoint = find_endpoint( - client, "com.ispirata.Hemera.DeviceLog.Status", 2, "/filterRules/%{ruleId}/%{filterKey}/value" @@ -427,124 +387,87 @@ defmodule Astarte.RealmManagement.QueriesTest do assert endpoint[:allow_unset] == true assert endpoint[:value_type] == 7 - query = - DatabaseQuery.new() - |> DatabaseQuery.statement(@insert_devicelog_status_0) - |> DatabaseQuery.put(:interface_id, interface_id) - |> DatabaseQuery.put(:endpoint_id, endpoint_id) - - DatabaseQuery.call!(client, query) + params = %{interface_id: interface_id, endpoint_id: endpoint_id} + execute_query!(@insert_devicelog_status_0, params) - query = - DatabaseQuery.new() - |> DatabaseQuery.statement(@insert_devicelog_status_1) - |> DatabaseQuery.put(:interface_id, interface_id) - |> DatabaseQuery.put(:endpoint_id, endpoint_id) - - DatabaseQuery.call!(client, query) - - query = - DatabaseQuery.new() - |> DatabaseQuery.statement(@find_devicelog_status_entry) - |> DatabaseQuery.put(:interface_id, interface_id) - |> DatabaseQuery.put(:endpoint_id, endpoint_id) + execute_query!(@insert_devicelog_status_1, params) entry = - DatabaseQuery.call!(client, query) + execute_query!(@find_devicelog_status_entry, params) |> Enum.to_list() assert entry == [ - [ + %{ device_id: <<83, 107, 226, 73, 170, 170, 78, 2, 149, 131, 90, 72, 51, 203, 254, 73>>, path: "/filterRules/0/testKey/value", reception_timestamp: 1_328_241_960_000, string_value: "T€ST_VÆLÙE" - ] + } ] - query = - DatabaseQuery.new() - |> DatabaseQuery.statement(@find_devicelog_status_entries) - |> DatabaseQuery.put(:interface_id, interface_id) - |> DatabaseQuery.put(:endpoint_id, endpoint_id) - entries = - DatabaseQuery.call!(client, query) + execute_query!(@find_devicelog_status_entries, params) |> Enum.to_list() assert entries == [ - [path: "/filterRules/0/testKey/value"], - [path: "/filterRules/1/testKey2/value"] + %{path: "/filterRules/0/testKey/value"}, + %{path: "/filterRules/1/testKey2/value"} ] end test "timestamp handling" do - {:ok, _} = DatabaseTestHelper.connect_to_test_database() - client = connect_to_test_realm("autotestrealm") - json_obj = Jason.decode!(@individual_datastream_with_explicit_timestamp_interface_json) interface_changeset = InterfaceDocument.changeset(%InterfaceDocument{}, json_obj) {:ok, doc} = Ecto.Changeset.apply_action(interface_changeset, :insert) {:ok, automaton} = Astarte.Core.Mapping.EndpointsAutomaton.build(doc.mappings) - Queries.install_new_interface(client, doc, automaton) + Queries.install_new_interface(@test_realm_name, doc, automaton) - endpoint_id = retrieve_endpoint_id(client, "com.timestamp.Test", 1, "/test/0/v") + endpoint_id = retrieve_endpoint_id("com.timestamp.Test", 1, "/test/0/v") - timestamp_handling_insert_values(client, endpoint_id, 0, 100) - timestamp_handling_insert_values(client, endpoint_id, 1, 20) - timestamp_handling_insert_values(client, endpoint_id, 2, 10) + timestamp_handling_insert_values(endpoint_id, 0, 100) + timestamp_handling_insert_values(endpoint_id, 1, 20) + timestamp_handling_insert_values(endpoint_id, 2, 10) - assert timestamp_handling_check_order(client, endpoint_id, 0) == {100, true} - assert timestamp_handling_check_order(client, endpoint_id, 1) == {20, true} - assert timestamp_handling_check_order(client, endpoint_id, 2) == {10, true} + assert timestamp_handling_check_order(endpoint_id, 0) == {100, true} + assert timestamp_handling_check_order(endpoint_id, 1) == {20, true} + assert timestamp_handling_check_order(endpoint_id, 2) == {10, true} end - defp timestamp_handling_insert_values(_client, _endpoint_id, _ind, 0) do + defp timestamp_handling_insert_values(_endpoint_id, _ind, 0) do end - defp timestamp_handling_insert_values(client, endpoint_id, ind, n) do + defp timestamp_handling_insert_values(endpoint_id, ind, n) do statement = @insert_timestamp_test_value |> String.replace(":ind", Integer.to_string(ind)) - query = - DatabaseQuery.new() - |> DatabaseQuery.statement(statement) - |> DatabaseQuery.put(:endpoint_id, endpoint_id) - |> DatabaseQuery.put( - :interface_id, - Astarte.Core.CQLUtils.interface_id("com.timestamp.Test", 1) - ) - |> DatabaseQuery.put(:value_timestamp, 1_504_800_339_954 + Enum.random(0..157_700_000_000)) - |> DatabaseQuery.put( - :reception_timestamp, - 1_504_800_339_954 + Enum.random(0..157_700_000_000) - ) - |> DatabaseQuery.put(:num, n) + params = %{ + endpoint_id: endpoint_id, + interface_id: Astarte.Core.CQLUtils.interface_id("com.timestamp.Test", 1), + value_timestamp: 1_504_800_339_954 + Enum.random(0..157_700_000_000), + reception_timestamp: 1_504_800_339_954 + Enum.random(0..157_700_000_000), + num: n + } - DatabaseQuery.call!(client, query) + execute_query!(statement, params) - timestamp_handling_insert_values(client, endpoint_id, ind, n - 1) + timestamp_handling_insert_values(endpoint_id, ind, n - 1) end - defp timestamp_handling_check_order(client, endpoint_id, ind) do + defp timestamp_handling_check_order(endpoint_id, ind) do statement = @list_timestamp_test_values |> String.replace(":ind", Integer.to_string(ind)) - query = - DatabaseQuery.new() - |> DatabaseQuery.statement(statement) - |> DatabaseQuery.put( - :interface_id, - Astarte.Core.CQLUtils.interface_id("com.timestamp.Test", 1) - ) - |> DatabaseQuery.put(:endpoint_id, endpoint_id) + params = %{ + interface_id: Astarte.Core.CQLUtils.interface_id("com.timestamp.Test", 1), + endpoint_id: endpoint_id + } timestamps = - DatabaseQuery.call!(client, query) + execute_query!(statement, params) |> Enum.to_list() sorted_timestamps = Enum.sort(timestamps, &(&1[:value_timestamp] <= &2[:value_timestamp])) @@ -553,28 +476,62 @@ defmodule Astarte.RealmManagement.QueriesTest do end test "get JWT public key PEM" do - DatabaseTestHelper.connect_to_test_database() - client = connect_to_test_realm("autotestrealm") - - assert Queries.get_jwt_public_key_pem(client) == + assert Queries.get_jwt_public_key_pem(@test_realm_name) == {:ok, DatabaseTestHelper.jwt_public_key_pem_fixture()} end test "update JWT public key PEM" do - DatabaseTestHelper.connect_to_test_database() - client = connect_to_test_realm("autotestrealm") - new_pem = "not_exactly_a_PEM_but_will_do" - assert Queries.update_jwt_public_key_pem(client, new_pem) == :ok - assert Queries.get_jwt_public_key_pem(client) == {:ok, new_pem} + assert Queries.update_jwt_public_key_pem(@test_realm_name, new_pem) == :ok + assert Queries.get_jwt_public_key_pem(@test_realm_name) == {:ok, new_pem} # Put the PEM fixture back assert Queries.update_jwt_public_key_pem( - client, + @test_realm_name, DatabaseTestHelper.jwt_public_key_pem_fixture() ) == :ok - assert Queries.get_jwt_public_key_pem(client) == + assert Queries.get_jwt_public_key_pem(@test_realm_name) == {:ok, DatabaseTestHelper.jwt_public_key_pem_fixture()} end + + defp retrieve_endpoint_id(interface_name, interface_major, path) do + params = %{name: interface_name, major_version: interface_major} + + interface_row = + execute_query!(@find_interface_by_interface_name, params) + |> Enum.to_list() + |> hd + + automaton = + {:erlang.binary_to_term(interface_row[:automaton_transitions]), + :erlang.binary_to_term(interface_row[:automaton_accepting_states])} + + {:ok, endpoint_id} = Astarte.Core.Mapping.EndpointsAutomaton.resolve_path(path, automaton) + + endpoint_id + end + + defp find_endpoint(interface_name, interface_major, endpoint) do + params = %{ + interface_id: Astarte.Core.CQLUtils.interface_id(interface_name, interface_major) + } + + interface_row = + execute_query!(@list_endpoints_by_interface, params) + |> Enum.to_list() + |> Enum.find(fn row -> + row[:endpoint] == endpoint + end) + end + + defp execute_query!(statement, params \\ %{}) do + Xandra.Cluster.run( + :xandra, + fn conn -> + {:ok, prepared} = Xandra.prepare(conn, statement) + Xandra.execute!(conn, prepared, params, uuid_format: :binary, timestamp_format: :integer) + end + ) + end end diff --git a/apps/astarte_realm_management/test/support/database_test_helper.exs b/apps/astarte_realm_management/test/support/database_test_helper.exs index a9d11bd1a..092aa71b6 100644 --- a/apps/astarte_realm_management/test/support/database_test_helper.exs +++ b/apps/astarte_realm_management/test/support/database_test_helper.exs @@ -19,10 +19,6 @@ defmodule Astarte.RealmManagement.DatabaseTestHelper do alias Astarte.Core.CQLUtils alias Astarte.Core.Device - alias CQEx.Query, as: DatabaseQuery - alias CQEx.Client, as: DatabaseClient - alias CQEx.Result, as: DatabaseResult - alias Astarte.RealmManagement.Config require Logger @jwt_public_key_pem """ @@ -136,160 +132,156 @@ defmodule Astarte.RealmManagement.DatabaseTestHelper do @insert_public_key """ INSERT INTO autotestrealm.kv_store (group, key, value) - VALUES ('auth', 'jwt_public_key_pem', varcharAsBlob(:pem)); + VALUES ('auth', 'jwt_public_key_pem', :pem); """ - def seed_datastream_test_data(client, device_id, interface_name, major, endpoint_id, path) do + def seed_datastream_test_data(device_id, interface_name, major, endpoint_id, path) do interface_id = CQLUtils.interface_id(interface_name, major) Enum.each( [ """ - INSERT INTO individual_properties + INSERT INTO autotestrealm.individual_properties (device_id, interface_id, endpoint_id, path) VALUES (:device_id, :interface_id, :endpoint_id, :path); """, """ - INSERT INTO individual_datastreams + INSERT INTO autotestrealm.individual_datastreams (device_id, interface_id, endpoint_id, path, value_timestamp, reception_timestamp, reception_timestamp_submillis, integer_value) VALUES (:device_id, :interface_id, :endpoint_id, '/0/integerValues', '2017-09-28 04:06+0000', '2017-09-28 05:06+0000', 0, 42); """ ], fn statement -> - query = - DatabaseQuery.new() - |> DatabaseQuery.statement(statement) - |> DatabaseQuery.put(:device_id, device_id) - |> DatabaseQuery.put(:interface_id, interface_id) - |> DatabaseQuery.put(:endpoint_id, endpoint_id) - |> DatabaseQuery.put(:path, path) - - DatabaseQuery.call!(client, query) + params = %{ + device_id: device_id, + interface_id: interface_id, + endpoint_id: endpoint_id, + path: path + } + + execute_query!(statement, params) end ) - kv_store_statement = "INSERT INTO kv_store (group, key) VALUES (:group, :key)" + kv_store_statement = "INSERT INTO autotestrealm.kv_store (group, key) VALUES (:group, :key)" - kv_query = - DatabaseQuery.new() - |> DatabaseQuery.statement(kv_store_statement) - |> DatabaseQuery.put(:group, "devices-with-data-on-interface-#{interface_name}-v0") - |> DatabaseQuery.put(:key, Device.encode_device_id(device_id)) - |> DatabaseQuery.consistency(:all) + kv_params = %{ + group: "devices-with-data-on-interface-#{interface_name}-v0", + key: Device.encode_device_id(device_id) + } - DatabaseQuery.call!(client, kv_query) + execute_query!(kv_store_statement, kv_params) :ok end - def count_rows_for_datastream(client, device_id, interface_name, major, endpoint_id, path) do + def count_rows_for_datastream(device_id, interface_name, major, endpoint_id, path) do count_statement = """ SELECT COUNT(*) - FROM individual_datastreams + FROM autotestrealm.individual_datastreams WHERE device_id=:device_id AND interface_id=:interface_id AND endpoint_id=:endpoint_id AND path=:path """ interface_id = CQLUtils.interface_id(interface_name, major) - count_query = - DatabaseQuery.new() - |> DatabaseQuery.statement(count_statement) - |> DatabaseQuery.put(:device_id, device_id) - |> DatabaseQuery.put(:interface_id, interface_id) - |> DatabaseQuery.put(:endpoint_id, endpoint_id) - |> DatabaseQuery.put(:path, path) - - DatabaseQuery.call!(client, count_query) - |> DatabaseResult.head() - |> Keyword.fetch!(:count) + count_params = %{ + device_id: device_id, + interface_id: interface_id, + endpoint_id: endpoint_id, + path: path + } + + execute_query!(count_statement, count_params, consistency: :all) + |> Enum.to_list() + |> (fn [%{count: count}] -> count end).() end - def seed_properties_test_value(client, device_id, interface_name, major, endpoint_id, path) do + def seed_properties_test_value(device_id, interface_name, major, endpoint_id, path) do interface_id = CQLUtils.interface_id(interface_name, major) property_statement = """ - INSERT INTO individual_properties + INSERT INTO autotestrealm.individual_properties (device_id, interface_id, endpoint_id, path) VALUES (:device_id, :interface_id, :endpoint_id, :path) """ - query = - DatabaseQuery.new() - |> DatabaseQuery.statement(property_statement) - |> DatabaseQuery.put(:device_id, device_id) - |> DatabaseQuery.put(:interface_id, interface_id) - |> DatabaseQuery.put(:endpoint_id, endpoint_id) - |> DatabaseQuery.put(:path, path) + property_params = %{ + device_id: device_id, + interface_id: interface_id, + endpoint_id: endpoint_id, + path: path + } - DatabaseQuery.call!(client, query) + execute_query!(property_statement, property_params) - kv_store_statement = "INSERT INTO kv_store (group, key) VALUES (:group, :key)" + kv_store_statement = "INSERT INTO autotestrealm.kv_store (group, key) VALUES (:group, :key)" - kv_query = - DatabaseQuery.new() - |> DatabaseQuery.statement(kv_store_statement) - |> DatabaseQuery.put(:group, "devices-with-data-on-interface-#{interface_name}-v0") - |> DatabaseQuery.put(:key, Device.encode_device_id(device_id)) - |> DatabaseQuery.consistency(:all) + kv_params = %{ + group: "devices-with-data-on-interface-#{interface_name}-v0", + key: Device.encode_device_id(device_id) + } - DatabaseQuery.call!(client, kv_query) + execute_query!(kv_store_statement, kv_params) :ok end - def count_interface_properties_for_device(client, device_id, interface_name, major) do + def count_interface_properties_for_device(device_id, interface_name, major) do count_statement = """ SELECT COUNT(*) - FROM individual_properties + FROM autotestrealm.individual_properties WHERE device_id=:device_id AND interface_id=:interface_id """ interface_id = CQLUtils.interface_id(interface_name, major) - count_query = - DatabaseQuery.new() - |> DatabaseQuery.statement(count_statement) - |> DatabaseQuery.put(:device_id, device_id) - |> DatabaseQuery.put(:interface_id, interface_id) + count_params = %{ + device_id: device_id, + interface_id: interface_id + } - DatabaseQuery.call!(client, count_query) - |> DatabaseResult.head() - |> Keyword.fetch!(:count) + execute_query!(count_statement, count_params, consistency: :all) + |> Enum.to_list() + |> (fn [%{count: count}] -> count end).() end - def create_test_keyspace(client) do - DatabaseQuery.call!(client, @create_autotestrealm) - DatabaseQuery.call!(client, @create_interfaces_table) - DatabaseQuery.call!(client, @create_endpoints_table) - DatabaseQuery.call!(client, @create_individual_properties_table) - DatabaseQuery.call!(client, @create_kv_store_table) - DatabaseQuery.call!(client, @create_simple_triggers_table) + def create_test_keyspace() do + Xandra.Cluster.run( + :xandra, + fn conn -> + Xandra.execute!(conn, @create_autotestrealm) + Xandra.execute!(conn, @create_interfaces_table) + Xandra.execute!(conn, @create_endpoints_table) + Xandra.execute!(conn, @create_individual_properties_table) + Xandra.execute!(conn, @create_kv_store_table) + Xandra.execute!(conn, @create_simple_triggers_table) + end + ) :ok end - def seed_test_data(client) do + def seed_test_data() do Enum.each(["interfaces", "endpoints", "individual_properties", "kv_store"], fn table -> - DatabaseQuery.call!(client, "TRUNCATE autotestrealm.#{table}") + Xandra.Cluster.run(:xandra, &Xandra.execute!(&1, "TRUNCATE autotestrealm.#{table}")) end) - query = - DatabaseQuery.new() - |> DatabaseQuery.statement(@insert_public_key) - |> DatabaseQuery.put(:pem, @jwt_public_key_pem) - - with {:ok, _result} <- DatabaseQuery.call(client, query) do - :ok - else - error -> - Logger.warn("Database error: #{inspect(error)}") - {:error, :database_error} - end + Xandra.Cluster.run(:xandra, fn conn -> + with {:ok, prepared} <- Xandra.prepare(conn, @insert_public_key), + {:ok, _result} <- Xandra.execute(conn, prepared, %{pem: @jwt_public_key_pem}) do + :ok + else + error -> + Logger.warn("Database error: #{inspect(error)}") + {:error, :database_error} + end + end) end - def drop_test_keyspace(client) do - with {:ok, _result} <- DatabaseQuery.call(client, "DROP KEYSPACE autotestrealm") do + def drop_test_keyspace() do + with {:ok, _result} <- + Xandra.Cluster.run(:xandra, &Xandra.execute(&1, "DROP KEYSPACE autotestrealm")) do :ok else error -> @@ -298,11 +290,23 @@ defmodule Astarte.RealmManagement.DatabaseTestHelper do end end - def connect_to_test_database do - DatabaseClient.new(List.first(Config.cqex_nodes!())) - end - def jwt_public_key_pem_fixture do @jwt_public_key_pem end + + defp execute_query!(statement, params \\ %{}, opts \\ []) do + Xandra.Cluster.run( + :xandra, + fn conn -> + {:ok, prepared} = Xandra.prepare(conn, statement) + + Xandra.execute!( + conn, + prepared, + params, + opts ++ [uuid_format: :binary, timestamp_format: :integer] + ) + end + ) + end end