-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from blackducksoftware/OTWO-5172
Add tests for settings & utils
- Loading branch information
Showing
4 changed files
with
150 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'test_helper' | ||
|
||
describe 'Time' do | ||
describe 'to_ms' do | ||
it 'must convert time to millisecond' do | ||
time = Time.new(2017, 05, 01, 01, 03, 05).utc | ||
time.to_ms.must_equal(time.to_i * 1000) | ||
end | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'test_helper' | ||
|
||
class TestRetriever | ||
include GHTorrent::Settings | ||
end | ||
|
||
describe 'Settings' do | ||
let(:retriever) { TestRetriever.new } | ||
|
||
describe 'config' do | ||
it 'must return the value from settings when present' do | ||
expected_token = Faker::Number.number(5) | ||
settings = { 'mirror' => { 'token' => expected_token } } | ||
retriever.stubs(:settings).returns(settings) | ||
retriever.config(:github_token).must_equal(expected_token) | ||
end | ||
|
||
it 'must return a DEFAULT value when the key does not exist in settings' do | ||
retriever.stubs(:settings).returns({}) | ||
retriever.config(:github_token).must_equal(GHTorrent::Settings::DEFAULTS[:github_token]) | ||
end | ||
|
||
it 'must catch exception for missing settings and return default value' do | ||
retriever.config(:github_token).must_equal(GHTorrent::Settings::DEFAULTS[:github_token]) | ||
end | ||
|
||
it 'must raise exception when use default is false and no settings exists' do | ||
-> { retriever.config(:github_token, false) }.must_raise(StandardError) | ||
end | ||
end | ||
|
||
describe 'merge' do | ||
it 'must override values in CONFIGKEYS' do | ||
token = Faker::Internet.password | ||
retriever.merge(github_token: token) | ||
TestRetriever::CONFIGKEYS[:github_token].must_equal token | ||
end | ||
end | ||
|
||
describe 'override_config' do | ||
it 'must override a value in the given hash' do | ||
settings = { 'amqp' => { 'password' => Faker::Internet.password } } | ||
expected = { 'amqp' => { 'password' => :foo } } | ||
retriever.override_config(settings, :amqp_password, :foo).must_equal(expected) | ||
end | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'test_helper' | ||
|
||
describe 'TransactedGHTorrent' do | ||
let(:gh_torrent) { TransactedGHTorrent.new({}) } | ||
before { gh_torrent.stubs(:db).returns(stub('in_transaction?' => true)) } | ||
|
||
describe 'all methods' do | ||
it 'must call the equivalent method in super class' do | ||
{ | ||
ensure_repo: 2, | ||
ensure_commit: 3, | ||
ensure_commit_comment: 4, | ||
ensure_fork: 3, | ||
ensure_fork_commits: 4, | ||
ensure_pull_request: 3, | ||
ensure_pullreq_comment: 4, | ||
ensure_issue: 3, | ||
ensure_issue_event: 4, | ||
ensure_issue_comment: 4, | ||
ensure_issue_label: 4, | ||
ensure_watcher: 3, | ||
ensure_repo_label: 3, | ||
ensure_user_followers: 1, | ||
ensure_orgs: 1, | ||
ensure_org: 1, | ||
ensure_topics: 2 | ||
}.each do |method, no_of_args| | ||
args = Array.new(no_of_args) { Faker::Name.first_name } | ||
GHTorrent::Mirror.any_instance.expects(method) | ||
gh_torrent.send(method, *args) | ||
end | ||
end | ||
end | ||
|
||
describe 'db not in transaction' do | ||
it 'must call the super class transaction method' do | ||
gh_torrent.stubs(:db).returns(stub('in_transaction?' => false)) | ||
GHTorrent::Mirror.any_instance.expects(:transaction) | ||
gh_torrent.ensure_repo(:foo, :bar) | ||
end | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'test_helper' | ||
|
||
class TestRetriever | ||
include GHTorrent::Utils | ||
end | ||
|
||
describe 'Utils' do | ||
let(:retriever) { TestRetriever.new } | ||
|
||
describe 'read_value' do | ||
it 'must read a nested value using 2 dot syntax' do | ||
hsh = { 'a' => { 'b' => { 'c' => :d } } } | ||
retriever.read_value(hsh, 'a.b.c').must_equal :d | ||
end | ||
|
||
it 'must read a nested value using N dot syntax' do | ||
hsh = { 'a' => { 'b' => { 'c' => { 'd' => { 'e' => :f } } } } } | ||
retriever.read_value(hsh, 'a.b.c.d').must_equal('e' => :f) | ||
retriever.read_value(hsh, 'a.b.c.d.e').must_equal :f | ||
end | ||
end | ||
|
||
describe 'write_value' do | ||
it 'must append or overwrite a nested key using 1 dot syntax' do | ||
hsh = { 'x' => { 'y' => { 'z' => :o } } } | ||
retriever.write_value(hsh, '', '').must_equal(hsh) | ||
|
||
expected_merge = { 'x' => { 'y' => { 'z' => :o, '@' => '#' } } } | ||
retriever.write_value(hsh, 'x.y', '@' => '#').must_equal(expected_merge) | ||
|
||
retriever.write_value(hsh, 'x.y', '@').must_equal('x' => { 'y' => '@' }) | ||
end | ||
|
||
it 'must append or overwrite a nested key using N dot syntax' do | ||
hsh = { 'x' => { 'y' => { 'z' => { 'o' => { 'm' => :n } } } } } | ||
|
||
expected_merge = { 'x' => { 'y' => { 'z' => { 'o' => { 'm' => :n }, '@' => '#' } } } } | ||
retriever.write_value(hsh, 'x.y.z', '@' => '#').must_equal(expected_merge) | ||
|
||
expected_merge = { 'x' => { 'y' => { 'z' => { 'o' => { 'm' => :p } } } } } | ||
retriever.write_value(hsh, 'x.y.z.o.m', :p).must_equal(expected_merge) | ||
end | ||
end | ||
|
||
describe 'user_type' do | ||
it 'must return value based on input' do | ||
retriever.user_type('User').must_equal('USR') | ||
retriever.user_type(Faker::Name.first_name).must_equal('ORG') | ||
end | ||
end | ||
end |