Skip to content

Commit

Permalink
Add Model unit tests + light cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Temikus committed Jun 6, 2018
1 parent a75ba64 commit ac2ad90
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@ 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
@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 respond to .all"
assert obj.respond_to?(:get), "#{klass} should respond to .get"
assert obj.respond_to?(:each), "#{klass} should be enumerable"
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,
Fog::Compute::Google::Region,
Fog::Compute::Google::DiskType,
Fog::Compute::Google::Operation,
Fog::Compute::Google::Zone,
Fog::Compute::Google::Snapshot,
Fog::Compute::Google::Project ]
# 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

0 comments on commit ac2ad90

Please sign in to comment.