diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/wmi_spec.rb | 84 |
1 files changed, 76 insertions, 8 deletions
diff --git a/spec/unit/wmi_spec.rb b/spec/unit/wmi_spec.rb index be79e30..cc26262 100644 --- a/spec/unit/wmi_spec.rb +++ b/spec/unit/wmi_spec.rb @@ -16,21 +16,89 @@ # limitations under the License. # -require 'wmi_lite/wmi' +require 'spec_helper' describe WmiLite::Wmi do - let(:wbem_connection) { double 'WIN32OLE', :ExecQuery => [] } let(:wbem_locator) { double 'WIN32OLE', :ConnectServer => wbem_connection } - - before do - WIN32OLE.stub(:new).and_return(wbem_locator) + let(:wmi_query_instance1) { double 'Wmi::Instance', :wmi_ole_object => native_query_instance1, :[] => native_properties1 } + let(:wmi_query_instance2) { double 'Wmi::Instance', :wmi_ole_object => native_query_instance2, :[] => native_properties2 } + let(:wmi_query_result1) { [ wmi_query_instance1 ].to_enum } + let(:wmi_query_result2) { [ wmi_query_instance1, wmi_query_instance2 ].to_enum } + let(:native_query_result1) { [ native_query_instance1 ].to_enum } + let(:native_query_result2) { [ native_query_instance1, native_query_instance2 ].to_enum } + let(:wmi_query_result_empty) { [].to_enum } + let(:native_properties1) { wmi_properties1.map { | property, value | double 'WIN32OLE', :name => property } } + let(:native_properties2) { wmi_properties2.map { | property, value | double 'WIN32OLE', :name => property } } + let(:native_query_instance1) { double 'WIN32OLE', :properties_ => native_properties1, :invoke => 'value1' } + let(:native_query_instance2) { double 'WIN32OLE', :properties_ => native_properties2, :invoke => 'value2' } + let(:wbem_connection) { double 'WIN32OLE', :ExecQuery => native_query_result } + + def validate_query_result(actual, expected) + expected_result = actual.count == expected.count + + index = 0 + if expected_result + expected.each do | expected_value | + actual_value = actual[index] + expected_value.invoke == actual_value.wmi_ole_object.invoke + expected_value.properties_.each do | expected_property | + if actual_value[expected_property.name].nil? + expected_result = false + end + if !! actual_value.wmi_ole_object.properties_.find { | actual_property | actual_property == expected_property.name } + expected_result = false + end + if ! expected_result + break + end + end + index += 1 + end + end + + expected_result + end + + before(:each) do + WIN32OLE.stub(:new).with("WbemScripting.SWbemLocator").and_return(wbem_locator) end let(:wmi) { WmiLite::Wmi.new } - + let(:wmi_query_result) { wmi_query_result_empty } + let(:native_query_result) { [].to_enum } + it "should not fail with empty query results" do - result = wmi.query('') - expect( result ).to eq([]) + results = wmi.query('') + result_count = 0 + results.each { | result | result_count += 1 } + + expect( result_count ).to eq(0) end + + shared_examples_for "the first method" do + + let(:wmi_properties1) { { 'cores' => 4, 'name' => 'mycomputer1', 'diskspace' => 400, 'os' => 'windows' } } + let(:wmi_properties2) { { 'cores' => 2, 'name' => 'mycomputer2', 'bios' => 'ami', 'os' => 'windows' } } + let(:native_query_result) { [].to_enum } + + it "should not fail with empty query results" do + results = wmi.first_of('vm') + expect( results ).to eq(nil) + end + + context "when returning one instance in the query" do + let(:wmi_query_result) { wmi_query_result1 } + let(:native_query_result) { native_query_result1 } + + it "should get one instance" do + results = wmi.first_of('vm') + expected_result = WmiLite::Wmi::Instance.new(native_query_result.first) + is_expected = validate_query_result([results], [expected_result.wmi_ole_object]) + expect(is_expected).to eq(true) + end + end + end + + it_should_behave_like "the first method" end |