Adding PHPUnit Tests for Discord Cryptocurrency Bot Regarding th

  • 时间:2020-09-07 12:26:38
  • 分类:网络文摘
  • 阅读:139 次

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?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);
   }
}
<?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 Adding PHPUnit Tests for Discord Cryptocurrency Bot Regarding the Coin Exchange Pairs blockchain Cryptocurrency php

crypto-discord-bot

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
爱你在心口难开(24至25章) 吴志俨  珍惜生活的机会  美丽的青海作文  庆六一游园活动有感作文  我的一次旅行  一次有意义的教师节  莲花池作文  关于比的应用题练习  和自然数有关的数学题  数学题:下图中圆的面积和长方形的面积相等 
评论列表
添加评论