Skip to content

Commit

Permalink
* Fix issue #4
Browse files Browse the repository at this point in the history
* Get test coverage over 80%
  • Loading branch information
vintikzzz committed Jan 6, 2017
1 parent b7ad6eb commit 569ca53
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
3 changes: 1 addition & 2 deletions lib/cassandra_ecto/adapter/cql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ defmodule Cassandra.Ecto.Adapter.CQL do

defp boolean(_name, [], _query), do: []
defp boolean(name, [%{expr: expr} | query_exprs], query) do
name <> " " <>
Enum.reduce(query_exprs, paren_expr(expr, query), fn
name <> " " <> Enum.reduce(query_exprs, expr(expr, query), fn
%BooleanExpr{expr: expr, op: op}, {op, acc} ->
{op, acc <> operator_to_boolean(op) <> paren_expr(expr, query)}
%BooleanExpr{expr: expr, op: op}, {_, acc} ->
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule CassandraEcto.Mixfile do
use Mix.Project

@version "0.4.0"
@version "0.4.1"

def project do
[app: :cassandra_ecto,
Expand Down
20 changes: 10 additions & 10 deletions spec/cassandra_ecto/adapter/cql_spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ defmodule CassandraEctoAdapterCQLSpec do
it "generates cql with binary clauses" do
query = (from p in "posts", where: p.id >= 1 and p.title == "abra") |> normalize
expect(to_cql(:all, query))
|> to(eq "SELECT * FROM \"posts\" WHERE ((\"id\" >= 1) AND (\"title\" = 'abra'))")
|> to(eq "SELECT * FROM \"posts\" WHERE (\"id\" >= 1) AND (\"title\" = 'abra')")
end
it "generates cql with in clauses" do
query = (from p in "posts", where: p.id in [1, 2]) |> normalize
expect(to_cql(:all, query))
|> to(eq "SELECT * FROM \"posts\" WHERE (\"id\" IN (1, 2))")
|> to(eq "SELECT * FROM \"posts\" WHERE \"id\" IN (1, 2)")
end
it "alters :in to :contains in array field search" do
query = (from p in "posts", where: "abra" in p.tags) |> normalize
expect(to_cql(:all, query))
|> to(eq "SELECT * FROM \"posts\" WHERE (\"tags\" CONTAINS 'abra')")
|> to(eq "SELECT * FROM \"posts\" WHERE \"tags\" CONTAINS 'abra'")
end
it "supports fragments" do
query = (from p in "posts", where: p.id > fragment("token(?)", 1)) |> normalize
expect(to_cql(:all, query))
|> to(eq "SELECT * FROM \"posts\" WHERE (\"id\" > token(1))")
|> to(eq "SELECT * FROM \"posts\" WHERE \"id\" > token(1)")
end
end
context "with :offset" do
Expand Down Expand Up @@ -121,7 +121,7 @@ defmodule CassandraEctoAdapterCQLSpec do
it "generates cql" do
query = (from p in "posts", where: p.id == 1) |> normalize
expect(to_cql(:all, query, allow_filtering: true))
|> to(eq "SELECT * FROM \"posts\" WHERE (\"id\" = 1) ALLOW FILTERING")
|> to(eq "SELECT * FROM \"posts\" WHERE \"id\" = 1 ALLOW FILTERING")
end
end
context "with :per_partition_limit" do
Expand Down Expand Up @@ -220,37 +220,37 @@ defmodule CassandraEctoAdapterCQLSpec do
it "generates cql" do
query = (from p in "posts", where: p.id == 1, update: [inc: [visits: 5]]) |> normalize(:update_all)
expect(to_cql(:update_all, query))
|> to(eq "UPDATE \"posts\" SET \"visits\" = \"visits\" + 5 WHERE (\"id\" = 1)")
|> to(eq "UPDATE \"posts\" SET \"visits\" = \"visits\" + 5 WHERE \"id\" = 1")
end
end
context "with :set" do
it "generates cql" do
query = (from p in "posts", where: p.id == 1, update: [set: [title: "a", text: "b"]]) |> normalize(:update_all)
expect(to_cql(:update_all, query))
|> to(eq "UPDATE \"posts\" SET \"title\" = 'a', \"text\" = 'b' WHERE (\"id\" = 1)")
|> to(eq "UPDATE \"posts\" SET \"title\" = 'a', \"text\" = 'b' WHERE \"id\" = 1")
end
end
# context "with :push" do
# it "generates cql", focus: true do
# id = Ecto.UUID.bingenerate()
# query = (from p in Post, where: p.id == ^id, update: [push: [tags: "a"]]) |> normalize(:update_all)
# expect(to_cql(:update_all, query))
# |> to(eq "UPDATE \"posts\" SET \"tags\" + {'a'} WHERE (\"id\" = ?)")
# |> to(eq "UPDATE \"posts\" SET \"tags\" + {'a'} WHERE \"id\" = ?")
# end
# end
context "with if: :exists" do
it "generates cql" do
query = (from p in "posts", where: p.id == 1, update: [set: [title: "a", text: "b"]]) |> normalize(:update_all)
expect(to_cql(:update_all, query, if: :exists))
|> to(eq "UPDATE \"posts\" SET \"title\" = 'a', \"text\" = 'b' WHERE (\"id\" = 1) IF EXISTS")
|> to(eq "UPDATE \"posts\" SET \"title\" = 'a', \"text\" = 'b' WHERE \"id\" = 1 IF EXISTS")
end
end
end
context "with :delete_all" do
it "generates cql" do
query = (from p in "posts", where: p.id == 1) |> normalize(:delete_all)
expect(to_cql(:delete_all, query))
|> to(eq "DELETE FROM \"posts\" WHERE (\"id\" = 1)")
|> to(eq "DELETE FROM \"posts\" WHERE \"id\" = 1")
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/cassandra_ecto/adapter_spec.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ defmodule CassandraEctoAdapterSpec do
expect(TestRepo.all((from p in Post, where: "abra" in p.tags), allow_filtering: true) |> List.first |> Map.get(:id))
|> to(eq id)
end
it "writes log to io in :cyan when logging enabled" do
message = capture_log(fn ->
TestRepo.all((from p in Post), log: true)
end)
expect(message) |> to(start_with "\e[36")
end
end
end
context "with :update_all" do
Expand Down
19 changes: 19 additions & 0 deletions spec/cassandra_ecto/log_spec.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule CassandraEctoLogSpec do
alias Cassandra.Ecto.Log

use ESpec, async: true

describe "Cassandra.Log" do
describe "log/4" do
it "writes log to io in cyan color for select queries" do
entry = %{connection_time: 0, decode_time: nil,
pool_time: nil, result: {:ok, []}, query: "SELECT something"}
message = capture_log(fn ->
Log.log(TestRepo, [], entry, [])
end)
expect(message) |> to(start_with "\e[36m\n")
expect(message) |> to(end_with "[debug] QUERY OK db=0.0ms\nSELECT something []\n\e[0m")
end
end
end
end
4 changes: 2 additions & 2 deletions spec/cassandra_ecto/migration/cql_spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ defmodule CassandraEctoMigrationCQLSpec do
|> to(eq "CREATE TYPE \"test\" (\"a\" int, \"b\" int)")
end
it "generates cql to create materialized view" do
to_cql({:create, %Table{name: :test_view, options: [type: :materialized_view, as: (from p in "test", select: {p.a, p.b}), primary_key: {:a, :b}, comment: "test"]}, []})
|> to(eq "CREATE MATERIALIZED VIEW \"test_view\" AS SELECT \"a\", \"b\" FROM \"test\" PRIMARY KEY (\"a\", \"b\") WITH COMMENT = 'test'")
to_cql({:create, %Table{name: :test_view, options: [type: :materialized_view, as: (from p in "test", select: {p.a, p.b}, where: not(is_nil(p.a)) and not(is_nil(p.b))), primary_key: {:a, :b}, comment: "test"]}, []})
|> to(eq "CREATE MATERIALIZED VIEW \"test_view\" AS SELECT \"a\", \"b\" FROM \"test\" WHERE \"a\" IS NOT NULL AND \"b\" IS NOT NULL PRIMARY KEY (\"a\", \"b\") WITH COMMENT = 'test'")
end
end
context "with :alter" do
Expand Down

0 comments on commit 569ca53

Please sign in to comment.