Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds tests and read ttl functionality #11

Merged
merged 3 commits into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/cassava/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def select(table, columns = nil)
StatementBuilder.new(executor).select(table, columns)
end

# @param table [Symbol] the table name
# @param target_attr [String] The attribute to select the TTL for
# @param where_arguments [Hash] Pairs of keys and values for the where clause
def select_ttl(table, target_attr, where_arguments)
statement = select_ttl_statement(table, target_attr, where_arguments)
executor.execute(statement).rows.first["ttl(#{target_attr})"]
end

# @param table [Symbol] the table name
# @param columns [Array<String] A list of columns that will be deleted. If nil, all columns will be deleted.
# @return [StatementBuilder] A statement builder representing the partially completed statement.
Expand Down Expand Up @@ -60,6 +68,22 @@ def insert_statement(table, data, ttl = nil)
statement_cql += " USING TTL #{ttl}" if ttl
executor.prepare(statement_cql)
end

# @param table [Symbol] the table name
# @param target_attr [Symbol] The attribute to select the TTL for
# @param where_arguments [Hash] Pairs of keys and values for the where clause
def select_ttl_statement(table, target_attr, where_arguments)
statement = "SELECT ttl(#{target_attr}) FROM #{table} WHERE "
where_clause = where_arguments.map do |(key, val)|
if val.is_a? Integer
"#{key} = #{val}"
else
"#{key} = '#{val}'"
end
end

statement + where_clause.join(" AND ")
end
end

class StatementBuilder
Expand Down
2 changes: 1 addition & 1 deletion lib/cassava/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Cassava
VERSION = "0.1.2"
VERSION = "0.1.3"
end
27 changes: 27 additions & 0 deletions test/cassava/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,33 @@ def string_keys(hash)
end
end

context 'select_ttl' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to see one more test with two where_arguments (one integer and one string) just to fully exercise that select_ttl_statement method ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah good idea i can do that

should 'allow an existing ttl to be read' do
ttl = 12345
item = { :id => 'i', :a => 1, :b => 'b', :c => "'\"item(", :d => 1, :ttl => ttl }
@client.insert(:test, item)

resulting_ttl = @client.select_ttl(:test, :d, {:id => 'i'})
assert (1..ttl).include? resulting_ttl

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the include? in this test instead of just an assert_equal ttl, resulting_ttl?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

depending on how long the query takes to execute the TTL could be less than it originally was

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, now I get it

end

should 'return nil if there is no ttl set on a cell' do
item = { :id => 'i', :a => 1, :b => 'b', :c => "'\"item(", :d => 1 }
@client.insert(:test, item)

resulting_ttl = @client.select_ttl(:test, :d, {:id => 'i'})
assert resulting_ttl.nil?
end

should 'handle string vs integer arguments properly' do
where_args = { :id => 'i', :a => 1, :b => 'b', :c => "'\"item(" }
statement = @client.send(:select_ttl_statement, :test, :c, where_args)

assert_match /a\s=\s1/, statement
assert_match /b\s=\s'b'/, statement
end
end

context 'delete' do
setup do
@client.insert(:test, :id => 'i', :a => 2, :b => 'a', :c => '1', :d => 1)
Expand Down