Algorithms, Blockchain and Cloud

Adding PHPUnit Tests for Discord Cryptocurrency Bot Regarding the Coin Exchange Pairs

crypto-discord-bot

Unit tests are still one of the most effective ways to ensure the existing features not broken due to new code changes/commits. The Discord cryptocurrency bots have been observed broken when it came to NGN currency because the NGN is relatively small compared to USD which causes some rounding issues and also it is not supported in some exchanges (via API support).

I have added the tests (via PHPUnit) to make sure that these crypto conversions are always working – when new code/changes are commited. Also, I run these tests manually to make sure that I am notified as soon as it stops working e.g. due to API changes from exchanges that I do not control.

<?php
use PHPUnit\Framework\TestCase;
require('discord-crypto-bot.php');

class Test_coins extends TestCase { 

  private $MIN_BTC_USD = 5000;
  private $MIN_BTC_CNY = 20000;
  private $MIN_SBD_USD = 0.1;

  public function test_isCoin() {
    $this->assertTrue(isCoin("BTC"));
    $this->assertTrue(isCoin("btc"));
    $this->assertTrue(isCoin("steem"));
    $this->assertTrue(isCoin("sTEem"));
    $this->assertTrue(isCoin("cny"));
    $this->assertTrue(isCoin("cNY"));
    $this->assertTrue(!isCoin("1234123dadf4"));
    $this->assertTrue(!isCoin(" steem "));
    $this->assertTrue(!isCoin(" steem"));
    $this->assertTrue(isCoin("steem-dollars")); 
  }   
  
  public function test_GetPrice() {
    $x = GetPrice("btc", "usd");
    $this->assertTrue($x >= $this->MIN_BTC_USD);
    $y = GetPrice("BTC", "USD");
    $this->assertTrue(abs($x - $y) < 2);
    $z = GetPrice("BTC", "CNY");
    $this->assertTrue($z >= $this->MIN_BTC_CNY);    
  }
  
  public function test_GetPriceCC() {
    $x = GetPriceCC("btc", "usd");
    $this->assertTrue($x >= $this->MIN_BTC_USD);
    $y = GetPriceCC("BTC", "USD");
    $this->assertTrue(abs($x - $y) < 2);
    $z = GetPriceCC("BTC", "CNY");
    $this->assertTrue($z >= $this->MIN_BTC_CNY);        
  }  
  
  public function test_getPriceOf() {
    $x = getPriceOf("btc", "usd");
    $this->assertTrue($x >= $this->MIN_BTC_USD);
    $y = getPriceOf("BTC", "USD");
    $this->assertTrue(abs($x - $y) < 2);
    $z = getPriceOf("BTC", "CNY");
    $this->assertTrue($z >= $this->MIN_BTC_CNY);
    $u = getPriceOf("bitcoin", "usd");
    $this->assertTrue($u >= $this->MIN_BTC_USD);
    $v = getPriceOf("steem-dollars", "usd");
    $this->assertTrue($v >= $this->MIN_SBD_USD);                  
  }
  
  public function test_getPriceOf1BTC() {
    $x = getPriceOf1BTC("cny");
    $this->assertTrue($x >= $this->MIN_BTC_CNY);
    $y = getPriceOf1BTC("usd");
    $this->assertTrue($y >= $this->MIN_BTC_USD);
  }
  
  public function test_getPriceOfUSD() {
    $x = getPriceOfUSD("btc");
    $this->assertTrue($x >= $this->MIN_BTC_USD);
    $x = getPriceOfUSD("bitcoin");
    $this->assertTrue($x >= $this->MIN_BTC_USD);
    $a = getPriceOfUSD("SBD");
    $this->assertTrue($a >= $this->MIN_SBD_USD);    
    $b = getPriceOfUSD("steem-dollars");
    $this->assertTrue($b >= $this->MIN_SBD_USD);
    $this->assertTrue(abs($a - $b) <= 0.01);            
  }  
  
  public function test_ConvertCoinToCoinRate() {
    $x = ConvertCoinToCoinRate("btc", "sbd");
    $this->assertTrue($x >= $this->MIN_BTC_USD);
    $y = ConvertCoinToCoinRate("sbd", "btc");
    $this->assertTrue(abs($x * $y - 1) <= 1e-3);
    $x = ConvertCoinToCoinRate("btc", "usd");
    $this->assertTrue($x >= $this->MIN_BTC_USD);
    $v = ConvertCoinToCoinRate("steem-dollars", "usd");
    $this->assertTrue($v >= $this->MIN_SBD_USD);                  
  }

   public function test_NGN() {
       $this->coinTest("NGN", "BTC", 0, 0);
       $this->coinTest("BTC", "NGN", 1000, 10e8);
       $this->coinTest("STEEM", "NGN", 100, 10e8);
       $this->coinTest("NGN", "STEEM", 0, 1);
       $this->coinTest("NGN", "USD", 0, 1);
       $this->coinTest("USD", "NGN", 1, 1000);
       $this->coinTest("NGN", "SBD", 0, 1);
       $this->coinTest("SBD", "NGN", 100, 1000);
   }

   private function assertRange($val, $left, $right) {
       $this->assertTrue(($val >= $left) && ($val <= $right), $val);
   }

   private function coinTest($from, $to, $left, $right) {
       $data = ConvertCoinToCoin($from, $to);
       $arr = explode(" ", $data);
       $this->assertEquals("1", $arr[0]);
       $this->assertEquals($from, $arr[1]);
       $this->assertEquals("=", $arr[2]);
       $this->assertEquals($to, $arr[4]);
       $val = (float)$arr[3];
       $this->assertTrue(($val >= $left) && ($val <= $right), $val);
   }
}

You can add to your discord channel via This Discord Link.

Currently added to 127 servers – Click “Authorize” to add to your discord channel.

crypto-discord-bot

–EOF (The Ultimate Computing & Technology Blog) —

778 words
Last Post: Depth First Search (Backtracking) Algorithm to Solve a Sudoku Game
Next Post: Find the Real Root of 4^x + 6^x = 9^x

The Permanent URL is: Adding PHPUnit Tests for Discord Cryptocurrency Bot Regarding the Coin Exchange Pairs (AMP Version)

Exit mobile version