Using the testdata gem

The testdata gem is a basic test framework which reads testdata from a Polyrex file.

Installation

gem install testdata

Example: testing the Dynarex gem

Here's the main tests I used for testing Dynarex:

#!/usr/bin/env ruby

# file: test_dynarex.rb

require 'json'
require 'timecop'


class TestDynarex < Testdata

  def tests()

    # 2011-07-24 19:52:15
    Timecop.freeze(Time.local(2011, 7, 24, 19, 52, 15))

    test 'create records' do |schema, records|
      dynarex = Dynarex.new schema            
      JSON.parse(records).each {|record| dynarex.create record }
      dynarex.to_xml
    end

    test 'parse to xml' do |string|
      dynarex = Dynarex.new             
      dynarex.parse string
      dynarex.to_xml
    end    
    
    test 'open document' do |string|
      Dynarex.new(string).to_xml pretty: true
    end        

    test 'parse to pretty xml' do |string|
      dynarex = Dynarex.new             
      dynarex.parse string
      dynarex.to_xml pretty: true
    end    

    test 'parse to_s' do |string|
      dynarex = Dynarex.new             
      dynarex.parse string
      dynarex.to_s
    end  
  end
end

... and here's the testdata:

file: testdata_dynarex.xml

<tests>
  <summary>
<title>dynarex testdata</title>
<recordx_type>polyrex</recordx_type>
<schema>tests/test[path,description]/io[type,*]</schema>
<ruby_version>ruby-1.9.2-p180</ruby_version>
<script>//job:test http://rorbuilder.info/r/gemtest/dynarex.rsf</script>
<test_dir>/home/james/test-ruby/rexle</test_dir>    
</summary> <records>
<test>
  <summary>
    <path>1</path>
    <type>create records</type>
    <description>Creates a couple of records using a hash for each record input/</description>
  </summary>
  <records>
    <io>
      <summary>
        <type>input</type>
        <schema>companies/company(name, last_contacted, contact)</schema>
        <xml>[{"name":"Julie","last_contact":"12-May-2010","contact":"0353 5363"},{"name":"Amy","last_contact":"16-May-2010","contact":"0353 5377"}]
</xml>
      </summary>
    </io>
    <io>
      <summary>
        <type>output</type>
        <value>
          <![CDATA[
<?xml version='1.0' encoding='UTF-8'?><companies><summary><recordx_type>dynarex</recordx_type><format_mask>[!name] [!last_contacted] [!contact]</format_mask><schema>companies/company(name, last_contacted, contact)</schema><default_key>name</default_key></summary><records><company id='1' created='2011-07-24 19:52:15 +0100' last_modified=''><name>Julie</name><last_contacted/><contact>0353 5363</contact></company><company id='2' created='2011-07-24 19:52:15 +0100' last_modified=''><name>Amy</name><last_contacted/><contact>0353 5377</contact></company></records></companies>
          ]]>
        </value>
      </summary>
    </io>
  </records>
</test>    
<test>
  <summary>
    <path>2</path>
    <type>parse to xml</type>
    <description>The delimiter attribute contains spaces before and after the equals sign</description>
  </summary>
  <records>
    <io>
      <summary>
        <type>input</type>
        <string>
          <![CDATA[
<?dynarex schema="entries/entry(title,class,url)" delimiter = " # "?>CV # pdf # abc Finding out disk space # snippets # u444
          ]]>
        </string>
      </summary>
    </io>
    <io>
      <summary>
        <type>output</type>
        <value>
          <![CDATA[
<?xml version='1.0' encoding='UTF-8'?><entries><summary><recordx_type>dynarex</recordx_type><format_mask>[!title] # [!class] # [!url]</format_mask><schema>entries/entry(title,class,url)</schema><default_key>title</default_key><delimiter> # </delimiter></summary><records><entry id='1' created='2011-07-24 19:52:15 +0100' last_modified=''><title>CV</title><class>pdf</class><url>abc</url></entry><entry id='2' created='2011-07-24 19:52:15 +0100' last_modified=''><title>Finding out disk space</title><class>snippets</class><url>u444</url></entry></records></entries>
          ]]>
        </value>
      </summary>
    </io>
  </records>
</test>
</records> </tests>

Running testdata

test = TestDynarex.new 'testdata_dynarex.xml'
test.run

output:

{:passed=>true, :score=>"2/2", :failed=>[]}

Resources

Tags:
Source:
1850hrs.txt
Published:
14-03-2014 18:50