github.com/status-im/status-go@v1.1.0/services/communitytokens/communitytokensdatabase/database_test.go (about)

     1  package communitytokensdatabase
     2  
     3  import (
     4  	"database/sql"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/status-im/status-go/appdatabase"
    11  	"github.com/status-im/status-go/protocol/communities/token"
    12  	"github.com/status-im/status-go/protocol/protobuf"
    13  	"github.com/status-im/status-go/protocol/sqlite"
    14  	"github.com/status-im/status-go/services/wallet/bigint"
    15  	"github.com/status-im/status-go/t/helpers"
    16  )
    17  
    18  func TestDatabaseSuite(t *testing.T) {
    19  	suite.Run(t, new(DatabaseSuite))
    20  }
    21  
    22  type DatabaseSuite struct {
    23  	suite.Suite
    24  
    25  	db *Database
    26  }
    27  
    28  func (s *DatabaseSuite) addCommunityToken(db *sql.DB, token *token.CommunityToken) error {
    29  	_, err := db.Exec(`INSERT INTO community_tokens (community_id, address, type, name, symbol, description, supply_str,
    30  		infinite_supply, transferable, remote_self_destruct, chain_id, deploy_state, image_base64, decimals, deployer, privileges_level) 
    31  		VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, token.CommunityID, token.Address, token.TokenType, token.Name,
    32  		token.Symbol, token.Description, token.Supply.String(), token.InfiniteSupply, token.Transferable, token.RemoteSelfDestruct,
    33  		token.ChainID, token.DeployState, token.Base64Image, token.Decimals, token.Deployer, token.PrivilegesLevel)
    34  	return err
    35  }
    36  
    37  func (s *DatabaseSuite) setupDatabase(db *sql.DB) error {
    38  	token721 := &token.CommunityToken{
    39  		CommunityID:        "123",
    40  		TokenType:          protobuf.CommunityTokenType_ERC721,
    41  		Address:            "0x123",
    42  		Name:               "StatusToken",
    43  		Symbol:             "STT",
    44  		Description:        "desc",
    45  		Supply:             &bigint.BigInt{Int: big.NewInt(123)},
    46  		InfiniteSupply:     false,
    47  		Transferable:       true,
    48  		RemoteSelfDestruct: true,
    49  		ChainID:            1,
    50  		DeployState:        token.InProgress,
    51  		Base64Image:        "ABCD",
    52  		Deployer:           "0xDEP1",
    53  		PrivilegesLevel:    token.OwnerLevel,
    54  	}
    55  
    56  	token20 := &token.CommunityToken{
    57  		CommunityID:        "345",
    58  		TokenType:          protobuf.CommunityTokenType_ERC20,
    59  		Address:            "0x345",
    60  		Name:               "StatusToken",
    61  		Symbol:             "STT",
    62  		Description:        "desc",
    63  		Supply:             &bigint.BigInt{Int: big.NewInt(345)},
    64  		InfiniteSupply:     false,
    65  		Transferable:       true,
    66  		RemoteSelfDestruct: true,
    67  		ChainID:            2,
    68  		DeployState:        token.Failed,
    69  		Base64Image:        "QWERTY",
    70  		Decimals:           21,
    71  		Deployer:           "0xDEP2",
    72  		PrivilegesLevel:    token.CommunityLevel,
    73  	}
    74  
    75  	err := s.addCommunityToken(db, token721)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	return s.addCommunityToken(db, token20)
    80  }
    81  
    82  func (s *DatabaseSuite) SetupTest() {
    83  	s.db = nil
    84  
    85  	db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
    86  	s.NoError(err, "creating sqlite db instance")
    87  
    88  	err = sqlite.Migrate(db)
    89  	s.NoError(err, "protocol migrate")
    90  
    91  	s.db = &Database{db: db}
    92  
    93  	err = s.setupDatabase(db)
    94  	s.NoError(err, "setting up database")
    95  }
    96  
    97  func (s *DatabaseSuite) TestGetTokenType() {
    98  	contractType, err := s.db.GetTokenType(1, "0x123")
    99  	s.Require().NoError(err)
   100  	s.Equal(contractType, protobuf.CommunityTokenType_ERC721)
   101  
   102  	contractType, err = s.db.GetTokenType(2, "0x345")
   103  	s.Require().NoError(err)
   104  	s.Equal(contractType, protobuf.CommunityTokenType_ERC20)
   105  
   106  	_, err = s.db.GetTokenType(10, "0x777")
   107  	s.Require().Error(err)
   108  }
   109  
   110  func (s *DatabaseSuite) TestGetPrivilegesLevel() {
   111  	privLevel, err := s.db.GetTokenPrivilegesLevel(1, "0x123")
   112  	s.Require().NoError(err)
   113  	s.Equal(privLevel, token.OwnerLevel)
   114  
   115  	privLevel, err = s.db.GetTokenPrivilegesLevel(2, "0x345")
   116  	s.Require().NoError(err)
   117  	s.Equal(privLevel, token.CommunityLevel)
   118  
   119  	_, err = s.db.GetTokenType(10, "0x777")
   120  	s.Require().Error(err)
   121  }