github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/names/handle_clean_test.go (about)

     1  package namesPkg
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
     8  )
     9  
    10  func Test_cleanCommon(t *testing.T) {
    11  	type args struct {
    12  		name *types.Name
    13  	}
    14  	tests := []struct {
    15  		name         string
    16  		args         args
    17  		wantModified bool
    18  	}{
    19  		{
    20  			name: "Tags lower than 8",
    21  			args: args{
    22  				name: &types.Name{
    23  					Tags: "80",
    24  				},
    25  			},
    26  			wantModified: false,
    27  		},
    28  	}
    29  	for _, tt := range tests {
    30  		t.Run(tt.name, func(t *testing.T) {
    31  			if gotModified := cleanCommon(tt.args.name); gotModified != tt.wantModified {
    32  				t.Errorf("cleanCommon() = %v, want %v", gotModified, tt.wantModified)
    33  			}
    34  		})
    35  	}
    36  }
    37  
    38  func Test_cleanNonContract(t *testing.T) {
    39  	type args struct {
    40  		name        *types.Name
    41  		wasContract bool
    42  	}
    43  	tests := []struct {
    44  		name         string
    45  		args         args
    46  		wantModified bool
    47  		expectedName *types.Name
    48  	}{
    49  		{
    50  			name: "Humanity DAO",
    51  			args: args{
    52  				name: &types.Name{
    53  					Tags: "30-Contracts:Humanity DAO",
    54  				},
    55  			},
    56  			wantModified: true,
    57  			expectedName: &types.Name{
    58  				Tags: "90-Individuals:Humanity DAO",
    59  			},
    60  		},
    61  		{
    62  			name: "was contract = true",
    63  			args: args{
    64  				name:        &types.Name{},
    65  				wasContract: true,
    66  			},
    67  			wantModified: true,
    68  			expectedName: &types.Name{
    69  				Tags:       "37-SelfDestructed",
    70  				IsContract: true,
    71  			},
    72  		},
    73  		{
    74  			name: "empty tags",
    75  			args: args{
    76  				name: &types.Name{},
    77  			},
    78  			wantModified: true,
    79  			expectedName: &types.Name{
    80  				Tags: "90-Individuals:Other",
    81  			},
    82  		},
    83  	}
    84  	for _, tt := range tests {
    85  		t.Run(tt.name, func(t *testing.T) {
    86  			if gotModified := cleanNonContract(tt.args.name, tt.args.wasContract); gotModified != tt.wantModified {
    87  				t.Errorf("cleanNonContract() = %v, want %v", gotModified, tt.wantModified)
    88  			}
    89  			if !reflect.DeepEqual(tt.args.name, tt.expectedName) {
    90  				t.Errorf("cleanNonContract() = %v, want %v", tt.args.name, tt.expectedName)
    91  			}
    92  		})
    93  	}
    94  }
    95  
    96  func Test_cleanToken(t *testing.T) {
    97  	type args struct {
    98  		token *types.Token
    99  		name  *types.Name
   100  	}
   101  	tests := []struct {
   102  		name         string
   103  		args         args
   104  		wantModified bool
   105  		expectedName *types.Name
   106  	}{
   107  		{
   108  			name: "ERC-20 token",
   109  			args: args{
   110  				name: &types.Name{},
   111  				token: &types.Token{
   112  					TokenType: types.TokenErc20,
   113  				},
   114  			},
   115  			wantModified: true,
   116  			expectedName: &types.Name{
   117  				IsErc20: true,
   118  				Source:  "On chain",
   119  				Tags:    "50-Tokens:ERC20",
   120  			},
   121  		},
   122  		{
   123  			name: "NFT (ERC-721)",
   124  			args: args{
   125  				name: &types.Name{},
   126  				token: &types.Token{
   127  					TokenType: types.TokenErc721,
   128  				},
   129  			},
   130  			wantModified: true,
   131  			expectedName: &types.Name{
   132  				IsErc721: true,
   133  				Source:   "On chain",
   134  				Tags:     "50-Tokens:ERC721",
   135  			},
   136  		},
   137  		{
   138  			name: "Fix airdrops tags",
   139  			args: args{
   140  				name: &types.Name{
   141  					Tags: "60-Airdrops",
   142  				},
   143  				token: &types.Token{
   144  					TokenType: types.TokenErc20,
   145  				},
   146  			},
   147  			wantModified: true,
   148  			expectedName: &types.Name{
   149  				IsErc20: true,
   150  				Source:  "On chain",
   151  				Tags:    "50-Tokens:ERC20",
   152  			},
   153  		},
   154  		{
   155  			name: "Fix token tags",
   156  			args: args{
   157  				name: &types.Name{
   158  					Tags: "token",
   159  				},
   160  				token: &types.Token{
   161  					TokenType: types.TokenErc20,
   162  				},
   163  			},
   164  			wantModified: true,
   165  			expectedName: &types.Name{
   166  				IsErc20: true,
   167  				Source:  "On chain",
   168  				Tags:    "50-Tokens:ERC20",
   169  			},
   170  		},
   171  		{
   172  			name: "Fix contracts tags",
   173  			args: args{
   174  				name: &types.Name{
   175  					Tags: "30-contracts",
   176  				},
   177  				token: &types.Token{
   178  					TokenType: types.TokenErc20,
   179  				},
   180  			},
   181  			wantModified: true,
   182  			expectedName: &types.Name{
   183  				IsErc20: true,
   184  				Source:  "On chain",
   185  				Tags:    "50-Tokens:ERC20",
   186  			},
   187  		},
   188  		{
   189  			name: "Fix defi tags",
   190  			args: args{
   191  				name: &types.Name{
   192  					Tags: "55-defi",
   193  				},
   194  				token: &types.Token{
   195  					TokenType: types.TokenErc20,
   196  				},
   197  			},
   198  			wantModified: true,
   199  			expectedName: &types.Name{
   200  				IsErc20: true,
   201  				Source:  "On chain",
   202  				Tags:    "50-Tokens:ERC20",
   203  			},
   204  		},
   205  		{
   206  			name: "Fill token name",
   207  			args: args{
   208  				name: &types.Name{},
   209  				token: &types.Token{
   210  					Name:      "TrueBlocksCoin",
   211  					TokenType: types.TokenErc20,
   212  				},
   213  			},
   214  			wantModified: true,
   215  			expectedName: &types.Name{
   216  				Name:    "TrueBlocksCoin",
   217  				IsErc20: true,
   218  				Source:  "On chain",
   219  				Tags:    "50-Tokens:ERC20",
   220  			},
   221  		},
   222  		{
   223  			name: "Fill token symbol",
   224  			args: args{
   225  				name: &types.Name{},
   226  				token: &types.Token{
   227  					Symbol:    "TBC",
   228  					TokenType: types.TokenErc20,
   229  				},
   230  			},
   231  			wantModified: true,
   232  			expectedName: &types.Name{
   233  				Symbol:  "TBC",
   234  				IsErc20: true,
   235  				Source:  "On chain",
   236  				Tags:    "50-Tokens:ERC20",
   237  			},
   238  		},
   239  		{
   240  			name: "Fill token decimals",
   241  			args: args{
   242  				name: &types.Name{},
   243  				token: &types.Token{
   244  					Decimals:  18,
   245  					TokenType: types.TokenErc20,
   246  				},
   247  			},
   248  			wantModified: true,
   249  			expectedName: &types.Name{
   250  				Decimals: 18,
   251  				IsErc20:  true,
   252  				Source:   "On chain",
   253  				Tags:     "50-Tokens:ERC20",
   254  			},
   255  		},
   256  	}
   257  	for _, tt := range tests {
   258  		t.Run(tt.name, func(t *testing.T) {
   259  			if gotModified := cleanToken(tt.args.name, tt.args.token); gotModified != tt.wantModified {
   260  				t.Errorf("cleanToken() = %v, want %v", gotModified, tt.wantModified)
   261  			}
   262  			if !reflect.DeepEqual(tt.args.name, tt.expectedName) {
   263  				t.Errorf("cleanToken() = %+v, want %+v", tt.args.name, tt.expectedName)
   264  			}
   265  		})
   266  	}
   267  }
   268  
   269  func Test_cleanContract(t *testing.T) {
   270  	type args struct {
   271  		token *types.Token
   272  		name  *types.Name
   273  	}
   274  	tests := []struct {
   275  		name         string
   276  		args         args
   277  		wantModified bool
   278  		wantErr      bool
   279  		expectedName *types.Name
   280  	}{
   281  		{
   282  			name: "Fix IsContract",
   283  			args: args{
   284  				name: &types.Name{},
   285  			},
   286  			wantModified: true,
   287  			wantErr:      false,
   288  			expectedName: &types.Name{
   289  				IsContract: true,
   290  				Tags:       "30-Contracts",
   291  			},
   292  		},
   293  		{
   294  			name: "Trim name and symbol",
   295  			args: args{
   296  				name: &types.Name{
   297  					Name:   "  Name with spaces  ",
   298  					Symbol: "WEIRD   ",
   299  				},
   300  			},
   301  			wantModified: true,
   302  			wantErr:      false,
   303  			expectedName: &types.Name{
   304  				Name:       "Name with spaces",
   305  				Symbol:     "WEIRD",
   306  				IsContract: true,
   307  				Tags:       "30-Contracts",
   308  			},
   309  		},
   310  	}
   311  	for _, tt := range tests {
   312  		t.Run(tt.name, func(t *testing.T) {
   313  			gotModified, err := cleanContract(tt.args.token, tt.args.name)
   314  			if (err != nil) != tt.wantErr {
   315  				t.Errorf("cleanContract() error = %v, wantErr %v", err, tt.wantErr)
   316  				return
   317  			}
   318  			if gotModified != tt.wantModified {
   319  				t.Errorf("cleanContract() = %v, want %v", gotModified, tt.wantModified)
   320  			}
   321  			if !reflect.DeepEqual(tt.args.name, tt.expectedName) {
   322  				t.Errorf("cleanContract() = %+v, want %+v", tt.args.name, tt.expectedName)
   323  			}
   324  		})
   325  	}
   326  }