Using Steem-API with Ruby Part 17 — Bonus: Unit Testing

avatar
(Edited)

Steemit_Ruby_Engine.png

Repositories

SteemRubyTutorial

All examples from this tutorial can be found as fully functional scripts on GitHub:

steem-ruby

steem-mechanize

radiator

Steem Engine

steem-engine_logo-horizontal-dark.png

What Will I Learn?

This tutorial shows how to write unit tests for Steem blockchain and Steem database using Ruby. When using Ruby you have three APIs available to chose: steem-api, steem-mechanize and radiator which differentiates in how return values and errors are handled:

  • steem-api uses closures and exceptions and provides low level computer readable data.
  • steem-mechanize drop in replacement for steem-api with more performat network I/O
  • radiator uses classic function return values and provides high level human readable data.

Since both APIs have advantages and disadvantages sample code for both APIs will be provided so the reader ca decide which is more suitable.

Requirements

Basic knowledge of Ruby programming is needed. It is necessary to install at least Ruby 2.5 as well as the following ruby gems:

gem install test-unit
gem install bundler
gem install colorize
gem install contracts
gem install steem-ruby
gem install steem-mechanize
gem install radiator

Note: All APIs steem-ruby, steem-mechanize and radiator provide a file called steem.rb. This means that:

  1. When more then one APIs is installed ruby must be told which one to use.
  2. The tree APIs can't be used in the same script.

If there is anything not clear you can ask in the comments.

Difficulty

For reader with programming experience this tutorial is basic level.

Tutorial Contents

The various classes in described in this tutorial need trough testing. For this a variety of Test classes where created:

Implementation

Tests

Test are very easy to write. Just create a class descending from Test::Unit::TestCase and a methods to perform the tests. There are a variety of asserts to check for the desired results.

class Radiator_Amount_Test < Test::Unit::TestCase

   def test_to_sbd_01
      _test = Steem::Type::Amount.to_amount(1.0, Steem::Type::Amount::STEEM)

      assert_not_nil(_test, "An amount should be created")
      assert_instance_of(Steem::Type::Amount, _test, "The amount should be of type «Steem::Type::Amount»")
      assert_equal(Steem::Type::Amount::STEEM, _test.asset, "The amount is if a «STEEEM» asset")

      _sbd = _test.to_sbd

      assert_not_nil(_sbd, "A sbd amount should be created")
      assert_instance_of(Steem::Type::Amount, _sbd, "The amount should be of type «Steem::Type::Amount»")
      assert_equal(Steem::Type::Amount::SBD, _sbd.asset, "The amount is if a «SBD» asset")
   end # test_to_sbd_01
end # Radiator_Amount_Test

There are currently 29 test on GitHub to draw imspiration from. However unit tests tend to by quite repetitive so there is not point in showing multiple examples.

One speciality can be found in the Steem-Engine tests. Since Steem-Engine has no query option it is necessary to iterate over the whole database table. This is quite resource and time consuming so those tests should only be executed when the all option is added to the command line.

#!/opt/local/bin/ruby

require_relative '../Scripts/SCC/Balance'
require "test/unit"

unless defined?(Test_All) then
   Test_All = ARGV[0] == "all"
end

class Balance_Test < Test::Unit::TestCase
   # Thee “all” tests but considerable strain on the
   # Steem Engine server so we only do them when
   # explicitly requested
   #
   if Test_All then
      def test_all_01
         _test = SCC::Balance.all

         assert_not_nil(_test, "There should be balances")
         assert_instance_of(Array, _test, "balances should be an array")

         _balance = _test[0]

         assert_not_nil(_balance, "First balance should exist")
         assert_instance_of(SCC::Balance, _balance, "First balance should be of type «SCC::Balance»")
         assert_instance_of(SCC::Metric, _balance.metric, "First balance metric should be of type «SCC::Metric»")
         assert_instance_of(SCC::Token, _balance.token, "First balance token should be of type «SCC::Token»")
         assert_equal(:symbol, _balance.key, "First balance key should be «:symbol»")
         assert_equal("ENG", _balance.value, "First balance value should be “ENG”")
      end # test_all_01
   end #if
end # Balance_Test  

Suites

A collection of tests can be grouped to a test suite. All that is needed is to require the individual tests.

Steem-API

The Steem-API tutorial currently only use one class and so there is only one test in the suite.

#!/opt/local/bin/ruby

require_relative '../Test/Steem_Amount_Test.rb'

Radiator

The Radiator tutorial used a total of three classes each with its own test class.

#!/opt/local/bin/ruby

require_relative '../Test/Radiator_Amount_Test.rb'
require_relative '../Test/Radiator_Reward_Fund_Test.rb'
require_relative '../Test/Radiator_Price_Test.rb'

Steem-Engine

#!/opt/local/bin/ruby

require_relative '../Test/SCC_Test.rb'
require_relative '../Test/SCC_Contract_Test.rb'
require_relative '../Test/SCC_Token_Test.rb'
require_relative '../Test/SCC_Balance_Test.rb'
require_relative '../Test/SCC_Metric_Test.rb'

All tests

A test suite can call further suites to create a hierarchy of tests. However, steem-ruby, steem-mechanize and radiator are incompatible with each other so at the top level a shell script is used.

#!/opt/local/bin/zsh

Test/SCC_Suite.rb       "${1}"
Test/Radiator_Suite.rb  "${1}"
Test/Steem_Suite.rb     "${1}"

The output of the Test/Suite.command command looks like this:

Screenshot at Sep 16 112224.png

Curriculum

First tutorial

Previous tutorial

Next tutorial

Proof of Work

Image Source




0
0
0.000
5 comments
avatar

Congratulations @krischik! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 4000 upvotes. Your next target is to reach 5000 upvotes.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

You can upvote this notification to help all Steem users. Learn how here!

0
0
0.000
avatar

Note: All APIs steem-ruby, steem-mechanize and radiator provide a file called steem.rb.

Note, as of radiator-0.4.7 (or just before, probably):

So there was a time when you could not require both radiator and steem-ruby in the same runtime. But now, they should co-exist fine.

0
0
0.000
avatar

Well, radiator still provides a steem.rb so I'm sceptical.

image.png

But I'll give it a try — maybe with the right order it works.

0
0
0.000
avatar

Hi, @krischik!

You just got a 9.89% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.

0
0
0.000