create account

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

View this thread on: hive.blogpeakd.comecency.com
· @krischik · (edited)
$0.21
Using Steem-API with Ruby Part 17 — Bonus: Unit Testing
![Steemit_Ruby_Engine.png](https://cdn.steemitimages.com/DQmR1jWexK1B1gGwUgcVdGtwRkAZPZ5rUwBXBt6x55TMPjY/Steemit_Ruby_Engine.png)

## Repositories

### SteemRubyTutorial

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

- [SteemRubyTutorial](https://github.com/krischik/SteemRubyTutorial)
- steem-api sample code: [Steem_Suite.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/Steem_Suite.rb)
- radiator sample code: [Radiator_Suite.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/Radiator_Suite.rb).
- steem engine sample code: [SCC_Suite.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/SCC_Suite.rb).

### steem-ruby

- Project Name: Steem Ruby
- Repository: [https://github.com/steemit/steem-ruby](https://github.com/steemit/steem-ruby)
- Official Documentation: [https://www.rubydoc.info/gems/steem-ruby](https://www.rubydoc.info/gems/steem-ruby)
- Official Tutorial: N/A

### steem-mechanize

- Project Name: Steem Mechanize
- Repository: [https://github.com/steemit/steem-mechanize](https://github.com/steemit/steem-mechanize)
- Official Documentation: [https://www.rubydoc.info/gems/steem-mechanize](https://www.rubydoc.info/gems/steem-mechanize)
- Official Tutorial: N/A

### radiator

- Project Name: Radiator
- Repository: [https://github.com/inertia186/radiator](https://github.com/inertia186/radiator)
- Official Documentation: [https://www.rubydoc.info/gems/radiator](https://www.rubydoc.info/gems/radiator)
- Official Tutorial: [https://developers.steem.io/tutorials-ruby/getting_started](https://developers.steem.io/tutorials-ruby/getting_started)

### Steem Engine

![steem-engine_logo-horizontal-dark.png](https://cdn.steemitimages.com/DQmcuU8q2NnZjUcj74ChEQDsUBdE4LNc8t9LpucE25TP7Sf/steem-engine_logo-horizontal-dark.png)

- Project Name: Steem Engine
- Home Page: [https://steem-engine.com](https://steem-engine.com)
- Repository: [https://github.com/harpagon210/steem-engine](https://github.com/harpagon210/steem-engine)
- Official Documentation: [https://github.com/harpagon210/sscjs](https://github.com/harpagon210/sscjs) (JavaScript only)
- Official Tutorial: N/A

## 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:

- [Test/Steem_Suite.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/Steem_Suite.rb)
    -  [Test/Steem_Amount_Test.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/Steem_Amount_Test.rb)
 - [Test/Radiator_Suite.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/Radiator_Suite.rb)
    - [Test/Radiator_Amount_Test.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/Radiator_Amount_Test.rb)
    - [Test/Radiator_Reward_Fund_Test.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/Radiator_Reward_Fund_Test.rb)
    - [Test/Radiator_Price_Test.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/Price_Test.rb)
- [Test/SCC_Suite.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/SCC_Suite.rb)
   - [Test/SCC_Test.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/SCC_Test.rb) 
   - [Test/SCC_Contract_Test.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/Contract_Test.rb) 
   - [Test/SCC_Token_Test.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/SCC_Token_Test.rb)
   - [Test/SCC_Balance_Test.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/SCC_Balance_Test.rb)
   - [Test/SCC_Metric_Test.rb](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/SCC_Metric_Test.rb)
- [Test/Suite.command](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/Suite.command)

## 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](https://github.com/krischik/SteemRubyTutorial/blob/master/Test) 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](https://github.com/krischik/SteemRubyTutorial/blob/master/Test/Suite.command) command looks like this:

![Screenshot at Sep 16 112224.png](https://steemitimages.com/0x0/https://files.steempeak.com/file/steempeak/krischik/7TH9CfnZ-Screenshot20at20Sep20162011-22-24.png)

# Curriculum

## First tutorial

- [Using Steem-API with Ruby Part 1](https://steemit.com/@krischik/using-steem-api-with-ruby-part-1)

## Previous tutorial

- [Using Steem-API with Ruby Part 16](https://steemit.com/@krischik/using-steem-api-with-ruby-part-16)

## Next tutorial

- [Using Steem-API with Ruby Part 18](https://steemit.com/@krischik/using-steem-api-with-ruby-part-18)

## Proof of Work

- GitHub: [SteemRubyTutorial Issue #19](https://github.com/krischik/SteemRubyTutorial/issues/19)

## Image Source

- Ruby symbol: [Wikimedia](https://commons.wikimedia.org/wiki/File:Ruby_logo.svg), CC BY-SA 2.5.
- Steemit logo [Wikimedia](https://commons.wikimedia.org/wiki/File:Steemit_New_Logo.png), CC BY-SA 4.0.
- Steem Engine logo [Steem Engine](https://steem-engine.com)
- Screenshots: [@krischik](https://steemit.com/@krischik), CC BY-NC-SA 4.0

-----

<center>![](https://steemitimages.com/50x60/http://steemitboard.com/@krischik/Comments.png?6) ![](https://steemitimages.com/0x0/http://steemitimages.com/60x70/http://steemitboard.com/@krischik/Votes.png)![](https://steemitimages.com/0x0/http://steemitimages.com/70x80/http://steemitboard.com/@krischik/Posts.png)![](https://steemitimages.com/0x0/http://steemitimages.com/100x80/http://steemitboard.com/@krischik/Level.png)![](https://steemitimages.com/0x0/http://steemitimages.com/70x80/http://steemitboard.com/@krischik/Payout.png)![](https://steemitimages.com/0x0/http://steemitimages.com/60x70/http://steemitboard.com/@krischik/Commented.png) ![](https://steemitimages.com/50x60/http://steemitboard.com/@krischik/voted.png?6)</center>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorkrischik
permlinkusing-steem-api-with-ruby-part-17
categoryutopian-io
json_metadata{"app":"steempeak/1.17.1","format":"markdown","tags":["utopian-io","tutorials","ruby","steem-api","programming","palnet","neoxian","marlians","stem","steemleo"],"users":["krischik"],"links":["https://github.com/krischik/SteemRubyTutorial","https://github.com/krischik/SteemRubyTutorial/blob/master/Test/Steem_Suite.rb","https://github.com/krischik/SteemRubyTutorial/blob/master/Test/Radiator_Suite.rb","https://github.com/krischik/SteemRubyTutorial/blob/master/Test/SCC_Suite.rb","https://github.com/steemit/steem-ruby","https://www.rubydoc.info/gems/steem-ruby","https://github.com/steemit/steem-mechanize","https://www.rubydoc.info/gems/steem-mechanize","https://github.com/inertia186/radiator","https://www.rubydoc.info/gems/radiator"],"image":["https://cdn.steemitimages.com/DQmR1jWexK1B1gGwUgcVdGtwRkAZPZ5rUwBXBt6x55TMPjY/Steemit_Ruby_Engine.png","https://cdn.steemitimages.com/DQmcuU8q2NnZjUcj74ChEQDsUBdE4LNc8t9LpucE25TP7Sf/steem-engine_logo-horizontal-dark.png","https://files.steempeak.com/file/steempeak/krischik/7TH9CfnZ-Screenshot20at20Sep20162011-22-24.png","https://steemitimages.com/50x60/http://steemitboard.com/@krischik/Comments.png?6","http://steemitimages.com/60x70/http://steemitboard.com/@krischik/Votes.png","http://steemitimages.com/70x80/http://steemitboard.com/@krischik/Posts.png","http://steemitimages.com/100x80/http://steemitboard.com/@krischik/Level.png","http://steemitimages.com/70x80/http://steemitboard.com/@krischik/Payout.png","http://steemitimages.com/60x70/http://steemitboard.com/@krischik/Commented.png","https://steemitimages.com/50x60/http://steemitboard.com/@krischik/voted.png?6"]}
created2019-09-16 11:17:18
last_update2019-09-22 07:09:42
depth0
children5
last_payout2019-09-23 11:17:18
cashout_time1969-12-31 23:59:59
total_payout_value0.100 HBD
curator_payout_value0.108 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length11,030
author_reputation15,247,708,436,415
root_title"Using Steem-API with Ruby Part 17 — Bonus: Unit Testing"
beneficiaries
0.
accountsteempeak
weight500
1.
accountsteemplus-pay
weight500
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id90,627,692
net_rshares932,334,737,692
author_curate_reward""
vote details (38)
@inertia ·
> 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):

[![](https://cdn.steemitimages.com/DQmS8AqpSN92ZRZvw6mXSoZ3JwPcaTKGPqSdy15RShumfvk/image.png)](https://github.com/inertia186/radiator/commit/6754e0739076c1b5d49bc921c176f7e428c4ba25)

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.
👎  
properties (23)
authorinertia
permlinkpxxkst
categoryutopian-io
json_metadata{"tags":["utopian-io","stem"],"image":["https://cdn.steemitimages.com/DQmS8AqpSN92ZRZvw6mXSoZ3JwPcaTKGPqSdy15RShumfvk/image.png"],"links":["https://github.com/inertia186/radiator/commit/6754e0739076c1b5d49bc921c176f7e428c4ba25"],"app":"stemgeeks/0.1"}
created2019-09-16 15:38:57
last_update2019-09-16 15:38:57
depth1
children1
last_payout2019-09-23 15:38:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length468
author_reputation346,568,901,399,561
root_title"Using Steem-API with Ruby Part 17 — Bonus: Unit Testing"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id90,635,041
net_rshares-1,910,020,955
author_curate_reward""
vote details (1)
@krischik ·
Well, radiator still provides a `steem.rb` so I'm sceptical.

![image.png](https://files.steempeak.com/file/steempeak/krischik/KCiznPxj-image.png)

But I'll give it a try — maybe with the right order it works.
properties (22)
authorkrischik
permlinkre-inertia-pxxobu
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steempeak/1.16.1"}
created2019-09-16 16:55:12
last_update2019-09-16 16:55:12
depth2
children0
last_payout2019-09-23 16:55:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length209
author_reputation15,247,708,436,415
root_title"Using Steem-API with Ruby Part 17 — Bonus: Unit Testing"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id90,637,323
net_rshares0
@steem-plus ·
SteemPlus upvote
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](https://steemit.com/@steem-plus) to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.
properties (22)
authorsteem-plus
permlinkusing-steem-api-with-ruby-part-17---vote-steemplus
categoryutopian-io
json_metadata{}
created2019-09-16 18:07:27
last_update2019-09-16 18:07:27
depth1
children0
last_payout2019-09-23 18:07:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length435
author_reputation247,952,188,232,400
root_title"Using Steem-API with Ruby Part 17 — Bonus: Unit Testing"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id90,639,405
net_rshares0
@steemeow ·
I like you
![cuddle_hug3.png](https://steemitimages.com/DQmejF1kwRBW9k7qVRvyPXCo6vCbvHgSp9JPKodLVhtXB4V/cuddle_hug3.png)
properties (22)
authorsteemeow
permlinkre-using-steem-api-with-ruby-part-17-20190924t062725z
categoryutopian-io
json_metadata"{"app": "beem/0.20.22"}"
created2019-09-24 06:27:30
last_update2019-09-24 06:27:30
depth1
children0
last_payout2019-10-01 06:27:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length109
author_reputation1,482,344,262,397
root_title"Using Steem-API with Ruby Part 17 — Bonus: Unit Testing"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id90,878,419
net_rshares0
@steemitboard ·
Congratulations @krischik! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://steemitimages.com/60x70/http://steemitboard.com/@krischik/voted.png?201909161237"></td><td>You received more than 4000 upvotes. Your next target is to reach 5000 upvotes.</td></tr>
</table>

<sub>_You can view [your badges on your Steem Board](https://steemitboard.com/@krischik) and compare to others on the [Steem Ranking](https://steemitboard.com/ranking/index.php?name=krischik)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



> You can upvote this notification to help all Steem users. Learn how [here](https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png)!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-krischik-20190916t150347000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2019-09-16 15:03:45
last_update2019-09-16 15:03:45
depth1
children0
last_payout2019-09-23 15:03:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length828
author_reputation38,975,615,169,260
root_title"Using Steem-API with Ruby Part 17 — Bonus: Unit Testing"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id90,633,798
net_rshares0