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

Adding unit tests #353

Merged
merged 4 commits into from
Jun 6, 2018
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
14 changes: 12 additions & 2 deletions tasks/test.rake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "rake/testtask"

Rake::TestTask.new do |t|
t.description = "Run all integration tests"
t.description = "Run integration and unit tests"
t.libs << "test"
t.pattern = File.join("test", "**", "test_*.rb")
t.warning = false
Expand All @@ -10,7 +10,7 @@ end
namespace :test do
mock = ENV["FOG_MOCK"] || "true"
task :travis do
sh("export FOG_MOCK=#{mock} && bundle exec shindont")
sh("bundle exec rake test:unit")
end

desc "Run all integration tests in parallel"
Expand All @@ -19,6 +19,16 @@ namespace :test do
"test:pubsub",
"test:sql",
"test:storage"]

Rake::TestTask.new do |t|
t.name = "unit"
t.description = "Run Unit tests"
t.libs << "test"
t.pattern = FileList['test/unit/**/test_*.rb']

Choose a reason for hiding this comment

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

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Choose a reason for hiding this comment

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

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

t.warning = false
t.verbose = true
end

Rake::TestTask.new do |t|
t.name = "compute"
t.description = "Run Compute API tests"
Expand Down
31 changes: 31 additions & 0 deletions test/unit/compute/test_common_collections.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "helpers/test_helper"

class UnitTestCollections < MiniTest::Test
def setup
Fog.mock!
@client = Fog::Compute.new(:provider => "Google", :google_project => "foo")

# Top-level ancestors we do not dest
common_ancestors = [Fog::Collection, Fog::Association, Fog::PagedCollection]
# Projects do not have a "list" method in compute API
exceptions = [Fog::Compute::Google::Projects]
# Enumerate all descendants of Fog::Collection
descendants = ObjectSpace.each_object(Fog::Collection.singleton_class).to_a

@collections = descendants - common_ancestors - exceptions
end

def teardown
Fog.unmock!
end

def test_common_methods
# This tests whether Fog::Compute::Google collections have common lifecycle methods
@collections.each do |klass|
obj = klass.new
assert obj.respond_to?(:all), "#{klass} should have an .all method"
assert obj.respond_to?(:get), "#{klass} should have a .get method"
assert obj.respond_to?(:each), "#{klass} should behave like Enumerable"
end
end
end
36 changes: 36 additions & 0 deletions test/unit/compute/test_common_models.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "helpers/test_helper"

class UnitTestModels < MiniTest::Test
def setup
Fog.mock!
@client = Fog::Compute.new(:provider => "Google", :google_project => "foo")

# Top-level ancestors we do not test
common_ancestors = [Fog::Model, Fog::Compute::Server]
# Do not test models that do not have a create method in API
exceptions = [ Fog::Compute::Google::MachineType,

Choose a reason for hiding this comment

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

Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.

Fog::Compute::Google::Region,
Fog::Compute::Google::DiskType,
Fog::Compute::Google::Operation,
Fog::Compute::Google::Zone,
Fog::Compute::Google::Snapshot,
Fog::Compute::Google::Project ]

Choose a reason for hiding this comment

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

Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.

# Enumerate all descendants of Fog::Model
descendants = ObjectSpace.each_object(Fog::Model.singleton_class).to_a

@models = descendants - common_ancestors - exceptions
end

def teardown
Fog.unmock!
end

def test_common_methods
# This tests whether Fog::Compute::Google models have common lifecycle methods
@models.each do |klass|
obj = klass.new
assert obj.respond_to?(:save), "#{klass} should have a .save method"
assert obj.respond_to?(:destroy), "#{klass} should have a .destroy method"
end
end
end