-
Notifications
You must be signed in to change notification settings - Fork 4
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
Added 'id' param to all Ariadne 'get' methods #6
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added 'id' param to all Ariadne 'get' methods
- 'id' param will help improving the overall performance of application as the user will be able to fetch the required data directly by passing the 'id' i.e. previously used to store the data. - new TCs written for modified methods
- Loading branch information
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
|
||
module DataUtil | ||
def self.init_redis_cli(obj = nil) | ||
@redis_cli = obj | ||
def self.init_redis_cli(redis_obj: nil) | ||
raise "Redis object not found!" if redis_obj.nil? | ||
@redis_cli = redis_obj | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
rescue StandardError => e | ||
puts e | ||
end | ||
|
||
def self.get_data_from_redis(app_name) | ||
keys = @redis_cli.keys "#{app_name}*" | ||
def self.get_data_from_redis(id:, app_name:) | ||
key = id.nil? ? "#{app_name}*" : "#{app_name}:#{id}" | ||
keys = @redis_cli.keys key | ||
raise "Data not available for #{app_name}!" if keys.empty? | ||
keys.compact! | ||
redis_data = (@redis_cli.mget keys) | ||
|
@@ -14,13 +18,13 @@ def self.get_data_from_redis(app_name) | |
puts e | ||
end | ||
|
||
def self.insert_data_in_redis(options = {}) | ||
key = "#{options[:app_name]}:#{options[:id]}" | ||
def self.insert_data_in_redis(options:, app_name:) | ||
key = "#{app_name}:#{options['id']}" | ||
options[:time] = Time.now | ||
redis_data = nil | ||
@redis_cli.pipelined do | ||
redis_data = @redis_cli.set key, options.to_json | ||
@redis_cli.expireat(key, Time.now + (24 * 60 * 60)) # expire a key after 1 day | ||
@redis_cli.expire(key, (24 * 60 * 60)) # expire a key after 1 day | ||
end | ||
redis_data | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
require_relative 'data_util' | ||
require 'fakeredis' | ||
|
||
DataUtil.init_redis_cli(Redis.new) | ||
DataUtil.init_redis_cli(redis_obj: Redis.new) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,90 @@ | ||
require_relative 'test_helper' | ||
|
||
class AriadneTest < MiniTest::Test | ||
def test_insert_data | ||
app_name = 'test' | ||
data = { | ||
id: 123, | ||
state: 'active', | ||
app_name: app_name | ||
'id' => 123, | ||
'state' => 'active' | ||
} | ||
|
||
out = Ariadne.insert_data(data) | ||
out = Ariadne.insert_data(options: data, app_name: app_name) | ||
assert_equal Redis::Future, out.class | ||
end | ||
|
||
def test_get_data | ||
app_name = 'test' | ||
data = { | ||
id: 123, | ||
state: 'active', | ||
app_name: app_name | ||
'id' => 123, | ||
'state' => 'active' | ||
} | ||
|
||
Ariadne.insert_data(data) | ||
out = Ariadne.get_data(data[:app_name]) | ||
assert_equal JSON.parse(out[0])['id'], data[:id] | ||
Ariadne.insert_data(options: data, app_name: app_name) | ||
out = Ariadne.get_data(app_name: app_name) | ||
assert_equal JSON.parse(out[0])['id'], data['id'] | ||
end | ||
|
||
def test_get_data_with_time_diff_without_delay_should_be_empty | ||
app_name = 'test' | ||
data = { | ||
id: 123, | ||
state: 'active', | ||
app_name: app_name | ||
'id' => 123, | ||
'state' => 'active' | ||
} | ||
|
||
Ariadne.insert_data(data) | ||
out = Ariadne.get_data_with_time_difference(app_name) | ||
Ariadne.insert_data(options: data, app_name: app_name) | ||
out = Ariadne.get_data_with_time_difference(app_name: app_name) | ||
assert_empty JSON.parse(out) | ||
end | ||
|
||
def test_get_data_with_time_diff_with_delay_should_not_be_empty | ||
app_name = 'test' | ||
app_name = 'test' | ||
delay_interval = 2 | ||
data = { | ||
'id' => 123, | ||
'state' => 'active', | ||
'delay_interval' => delay_interval | ||
} | ||
|
||
Ariadne.insert_data(options: data, app_name: app_name) | ||
sleep delay_interval | ||
out = Ariadne.get_data_with_time_difference(app_name: app_name) | ||
assert_equal JSON.parse(out)[0]['id'], data['id'] | ||
end | ||
|
||
def test_get_data_with_id | ||
app_name = 'test' | ||
data = { | ||
'id' => 123, | ||
'state' => 'active' | ||
} | ||
|
||
Ariadne.insert_data(options: data, app_name: app_name) | ||
out = Ariadne.get_data(id: data['id'], app_name: app_name) | ||
assert_equal JSON.parse(out[0])['id'], data['id'] | ||
end | ||
|
||
def test_get_data_with_time_diff_with_id_without_delay_should_be_empty | ||
app_name = 'test' | ||
data = { | ||
'id' => 123, | ||
'state' => 'active' | ||
} | ||
|
||
Ariadne.insert_data(options: data, app_name: app_name) | ||
out = Ariadne.get_data_with_time_difference(id: data['id'], app_name: app_name) | ||
assert_empty JSON.parse(out) | ||
end | ||
|
||
def test_get_data_with_time_diff_with_id_with_delay_should_not_be_empty | ||
app_name = 'test' | ||
delay_interval = 2 | ||
data = { | ||
id: 123, | ||
state: 'active', | ||
app_name: app_name, | ||
delay_interval: delay_interval | ||
'id' => 123, | ||
'state' => 'active', | ||
'delay_interval' => delay_interval | ||
} | ||
|
||
Ariadne.insert_data(data) | ||
Ariadne.insert_data(options: data, app_name: app_name) | ||
sleep delay_interval | ||
out = Ariadne.get_data_with_time_difference(app_name) | ||
assert_equal JSON.parse(out)[0]['id'], data[:id] | ||
out = Ariadne.get_data_with_time_difference(id: data['id'], app_name: app_name) | ||
assert_equal JSON.parse(out)[0]['id'], data['id'] | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this gem is automatically added after
bundle install