Repository: trustwallet/blockatlas
Pull request #1255 (2020-10-24 19:43:52)

Enable numeric values in the memo

codecov[bot]: # [Codecov](https://codecov.io/gh/trustwallet/blockatlas/pull/1255?src=pr&el=h1) Report
> Merging [#1255](https://codecov.io/gh/trustwallet/blockatlas/pull/1255?src=pr&el=desc) into [master](https://codecov.io/gh/trustwallet/blockatlas/commit/f8a7bb5c9b001e0e7c99aa017ae14d472f4052ec?el=desc) will **increase** coverage by `0%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/trustwallet/blockatlas/pull/1255/graphs/tree.svg?width=650&height=150&src=pr&token=jnrjoEQAqv)](https://codecov.io/gh/trustwallet/blockatlas/pull/1255?src=pr&el=tree)

```diff
@@          Coverage Diff           @@
##           master   #1255   +/-   ##
======================================
  Coverage      41%     41%           
======================================
  Files         162     162           
  Lines        5076    5079    +3     
======================================
+ Hits         2123    2130    +7     
+ Misses       2765    2761    -4     
  Partials      188     188           
```


| [Impacted Files](https://codecov.io/gh/trustwallet/blockatlas/pull/1255?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/blockatlas/tx.go](https://codecov.io/gh/trustwallet/blockatlas/pull/1255/diff?src=pr&el=tree#diff-cGtnL2Jsb2NrYXRsYXMvdHguZ28=) | `48% <100%> (+2%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/trustwallet/blockatlas/pull/1255?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/trustwallet/blockatlas/pull/1255?src=pr&el=footer). Last update [f8a7bb5...d6e3ea0](https://codecov.io/gh/trustwallet/blockatlas/pull/1255?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/pkg/blockatlas/tx.go b/pkg/blockatlas/tx.go
index ea3fb26..ecfb566 100644
--- a/pkg/blockatlas/tx.go
+++ b/pkg/blockatlas/tx.go
@@ -231,13 +231,20 @@ func (t Txs) FilterUniqueID() Txs {
 func (txs TxPage) FilterTransactionsByMemo() TxPage {
 	result := make(TxPage, 0)
 	for _, tx := range txs {
-		//TODO. Temporary disable memo
-		tx.Memo = ""
+		if !AllowMemo(tx.Memo) {
+			tx.Memo = ""
+		}
 		result = append(result, tx)
 	}
 	return result
 }
 
+func AllowMemo(memo string) bool {
+	// only allows numeric values
+	_, err := strconv.ParseFloat(memo, 64)
+	return err == nil
+}
+
 func (txs TxPage) FilterTransactionsByToken(token string) TxPage {
 	result := make(TxPage, 0)
 	for _, tx := range txs {


Test patch:
diff --git a/pkg/blockatlas/tx_test.go b/pkg/blockatlas/tx_test.go
index 0e5ba3f..842542a 100644
--- a/pkg/blockatlas/tx_test.go
+++ b/pkg/blockatlas/tx_test.go
@@ -5,6 +5,7 @@ import (
 	mapset "github.com/deckarep/golang-set"
 	"github.com/stretchr/testify/assert"
 	"github.com/trustwallet/golibs/coin"
+	"reflect"
 	"sort"
 	"testing"
 )
@@ -684,12 +685,78 @@ func Test_filterTransactionsByToken(t *testing.T) {
 	assert.Equal(t, wantedTransactionsToken, string(rawResult))
 }
 
-//func Test_filterTransactionsByMemo(t *testing.T) {
-//	var p TxPage
-//	spamfilter.SpamList = []string{"word", "free"}
-//	assert.Nil(t, json.Unmarshal([]byte(beforeTransactionsMemo), &p))
-//	result := p.FilterTransactionsByMemo()
-//	rawResult, err := json.Marshal(result)
-//	assert.Nil(t, err)
-//	assert.Equal(t, wantedTransactionsMemo, string(rawResult))
-//}
+func Test_AllowMemo(t *testing.T) {
+	type args struct {
+		memo string
+	}
+	tests := []struct {
+		name string
+		args args
+		want bool
+	}{
+		{
+			"Numeric memo",
+			args{memo: "123"},
+			true,
+		},
+		{
+			"Numeric memo",
+			args{memo: "12356172321321"},
+			true,
+		},
+		{
+			"Numeric memo",
+			args{memo: "test"},
+			false,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := AllowMemo(tt.args.memo); got != tt.want {
+				t.Errorf("isMemoAllowed() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}
+
+func TestTxPage_FilterTransactionsByMemo(t *testing.T) {
+	tests := []struct {
+		name string
+		txs  TxPage
+		want TxPage
+	}{
+		{
+			name: "Allow memo",
+			txs: TxPage{
+				{
+					Memo: "123",
+				},
+			},
+			want: TxPage{
+				{
+					Memo: "123",
+				},
+			},
+		},
+		{
+			name: "Disallow memo",
+			txs: TxPage{
+				{
+					Memo: "test",
+				},
+			},
+			want: TxPage{
+				{
+					Memo: "",
+				},
+			},
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := tt.txs.FilterTransactionsByMemo(); !reflect.DeepEqual(got, tt.want) {
+				t.Errorf("FilterTransactionsByMemo() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}
diff --git a/tests/integration/observer_test/full_flow_test.go b/tests/integration/observer_test/full_flow_test.go
index 65bb41e..b9c9c0e 100644
--- a/tests/integration/observer_test/full_flow_test.go
+++ b/tests/integration/observer_test/full_flow_test.go
@@ -83,7 +83,7 @@ func (p *PlatformFullFlow) GetBlockByNumber(num int64) (*blockatlas.Block, error
 					Date:   1555117625,
 					Block:  7928667,
 					Status: blockatlas.StatusCompleted,
-					Memo:   "test",
+					Memo:   "123",
 					Meta: blockatlas.NativeTokenTransfer{
 						TokenID:  "YLC-D8B",
 						Symbol:   "YLC",
@@ -131,7 +131,7 @@ func ConsumerToTestTransactionsFull(delivery amqp.Delivery, t *testing.T, cancel
 			Date:      1555117625,
 			Block:     7928667,
 			Status:    blockatlas.StatusCompleted,
-			Memo:      "",
+			Memo:      "123",
 			Meta:      &memo,
 		},
 	}, notifications[0])