-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test(db): add test for db #5463
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package org.tron.core.db; | ||
|
||
import com.google.common.primitives.Longs; | ||
import com.google.protobuf.ByteString; | ||
|
||
import java.util.Map; | ||
import javax.annotation.Resource; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.tron.common.BaseTest; | ||
import org.tron.common.utils.ByteArray; | ||
import org.tron.core.Constant; | ||
import org.tron.core.Wallet; | ||
import org.tron.core.capsule.AccountCapsule; | ||
import org.tron.core.capsule.AssetIssueCapsule; | ||
import org.tron.core.config.args.Args; | ||
import org.tron.core.store.AccountAssetStore; | ||
import org.tron.core.store.AccountStore; | ||
import org.tron.protos.Protocol; | ||
import org.tron.protos.contract.AssetIssueContractOuterClass; | ||
|
||
public class AccountAssetStoreTest extends BaseTest { | ||
|
||
private static final byte[] ASSET_KEY = "20000".getBytes(); | ||
private static AccountCapsule ownerCapsule; | ||
|
||
private static String OWNER_ADDRESS = Wallet.getAddressPreFixString() | ||
+ "abd4b9367799eaa3197fecb144eb71de1e049abc"; | ||
private static final long TOTAL_SUPPLY = 1000_000_000L; | ||
private static final int TRX_NUM = 10; | ||
private static final int NUM = 1; | ||
private static final long START_TIME = 1; | ||
private static final long END_TIME = 2; | ||
private static final int VOTE_SCORE = 2; | ||
private static final String DESCRIPTION = "TRX"; | ||
private static final String URL = "https://tron.network"; | ||
|
||
@Resource | ||
private AccountAssetStore accountAssetStore; | ||
|
||
@Resource | ||
private AccountStore accountStore; | ||
|
||
static { | ||
dbPath = "db_AccountAssetStore_test"; | ||
Args.setParam( | ||
new String[]{ | ||
"--output-directory", dbPath, | ||
}, | ||
Constant.TEST_CONF | ||
); | ||
} | ||
|
||
@Before | ||
public void init() { | ||
accountAssetStore.put(ASSET_KEY, Longs.toByteArray(200L)); | ||
|
||
ownerCapsule = | ||
new AccountCapsule( | ||
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)), | ||
ByteString.copyFromUtf8("owner"), | ||
Protocol.AccountType.AssetIssue); | ||
} | ||
|
||
|
||
private long createAsset(String tokenName) { | ||
long id = chainBaseManager.getDynamicPropertiesStore().getTokenIdNum() + 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this be conflict in any two test cases? |
||
chainBaseManager.getDynamicPropertiesStore().saveTokenIdNum(id); | ||
AssetIssueContractOuterClass.AssetIssueContract assetIssueContract = | ||
AssetIssueContractOuterClass.AssetIssueContract.newBuilder() | ||
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS))) | ||
.setName(ByteString.copyFrom(ByteArray.fromString(tokenName))) | ||
.setId(Long.toString(id)) | ||
.setTotalSupply(TOTAL_SUPPLY) | ||
.setTrxNum(TRX_NUM) | ||
.setNum(NUM) | ||
.setStartTime(START_TIME) | ||
.setEndTime(END_TIME) | ||
.setVoteScore(VOTE_SCORE) | ||
.setDescription(ByteString.copyFrom(ByteArray.fromString(DESCRIPTION))) | ||
.setUrl(ByteString.copyFrom(ByteArray.fromString(URL))) | ||
.build(); | ||
AssetIssueCapsule assetIssueCapsule = new AssetIssueCapsule(assetIssueContract); | ||
chainBaseManager.getAssetIssueV2Store() | ||
.put(assetIssueCapsule.createDbV2Key(), assetIssueCapsule); | ||
try { | ||
ownerCapsule.addAssetV2(ByteArray.fromString(String.valueOf(id)), TOTAL_SUPPLY); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
accountStore.put(ownerCapsule.getAddress().toByteArray(), ownerCapsule); | ||
return id; | ||
} | ||
|
||
@Test | ||
public void testPut() { | ||
byte[] key = "10000".getBytes(); | ||
accountAssetStore.put(key, Longs.toByteArray(100L)); | ||
byte[] bytes = accountAssetStore.get(key); | ||
Assert.assertEquals(100L, Longs.fromByteArray(bytes)); | ||
} | ||
|
||
@Test | ||
public void testGet() { | ||
byte[] bytes = accountAssetStore.get(ASSET_KEY); | ||
Assert.assertEquals(200L, Longs.fromByteArray(bytes)); | ||
} | ||
|
||
@Test | ||
public void testGetAccountAssets() { | ||
long assetKey = createAsset("testToken1"); | ||
AccountCapsule accountCapsule = accountStore.get(ownerCapsule.getAddress().toByteArray()); | ||
long assetValue = accountCapsule.getAssetV2(String.valueOf(assetKey)); | ||
Assert.assertEquals(assetValue, TOTAL_SUPPLY); | ||
} | ||
|
||
@Test | ||
public void testGetAllAssets() { | ||
long assetKey1 = createAsset("testToken1"); | ||
long assetKey2 = createAsset("testToken2"); | ||
AccountCapsule accountCapsule = accountStore.get(ownerCapsule.getAddress().toByteArray()); | ||
|
||
Map<String, Long> allAssets = accountAssetStore.getAllAssets(accountCapsule.getInstance()); | ||
Long assetValue1 = allAssets.get(String.valueOf(assetKey1)); | ||
Assert.assertNotNull(assetValue1); | ||
|
||
Long assetV1 = accountCapsule.getAssetV2(String.valueOf(assetKey1)); | ||
Assert.assertEquals(assetValue1, assetV1); | ||
|
||
Long assetValue2 = allAssets.get(String.valueOf(assetKey2)); | ||
Assert.assertNotNull(assetValue2); | ||
|
||
Long assetV2 = accountCapsule.getAssetV2(String.valueOf(assetKey2)); | ||
Assert.assertEquals(assetValue1, assetV2); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.tron.core.db; | ||
|
||
import com.google.protobuf.ByteString; | ||
import javax.annotation.Resource; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.tron.common.BaseTest; | ||
import org.tron.common.utils.ByteArray; | ||
import org.tron.core.Constant; | ||
import org.tron.core.capsule.AssetIssueCapsule; | ||
import org.tron.core.config.args.Args; | ||
import org.tron.core.store.AssetIssueStore; | ||
import org.tron.protos.contract.AssetIssueContractOuterClass; | ||
|
||
public class AssetIssueStoreTest extends BaseTest { | ||
|
||
private static final String NAME = "test-asset"; | ||
private static final long TOTAL_SUPPLY = 10000L; | ||
private static final int TRX_NUM = 10000; | ||
private static final int NUM = 100000; | ||
private static final String DESCRIPTION = "myCoin"; | ||
private static final String URL = "tron.network"; | ||
|
||
@Resource | ||
private AssetIssueStore assetIssueStore; | ||
|
||
static { | ||
dbPath = "db_AssetIssueStoreTest_test"; | ||
Args.setParam( | ||
new String[]{ | ||
"--output-directory", dbPath, | ||
}, | ||
Constant.TEST_CONF | ||
); | ||
} | ||
|
||
@Before | ||
public void init() { | ||
long id = dbManager.getDynamicPropertiesStore().getTokenIdNum() + 1; | ||
AssetIssueCapsule assetIssueCapsule = createAssetIssue(id, NAME); | ||
assetIssueStore.put(assetIssueCapsule.createDbKey(), assetIssueCapsule); | ||
} | ||
|
||
private AssetIssueCapsule createAssetIssue(long id, String name) { | ||
dbManager.getDynamicPropertiesStore().saveTokenIdNum(id); | ||
AssetIssueContractOuterClass.AssetIssueContract assetIssueContract = | ||
AssetIssueContractOuterClass.AssetIssueContract.newBuilder() | ||
.setName(ByteString.copyFrom(ByteArray.fromString(name))).setId(Long.toString(id)) | ||
.setTotalSupply(TOTAL_SUPPLY) | ||
.setTrxNum(TRX_NUM).setNum(NUM).setStartTime(1).setEndTime(100).setVoteScore(2) | ||
.setDescription(ByteString.copyFrom(ByteArray.fromString(DESCRIPTION))) | ||
.setUrl(ByteString.copyFrom(ByteArray.fromString(URL))).build(); | ||
AssetIssueCapsule assetIssueCapsule = new AssetIssueCapsule(assetIssueContract); | ||
return assetIssueCapsule; | ||
} | ||
|
||
@Test | ||
public void testPut() { | ||
long id = dbManager.getDynamicPropertiesStore().getTokenIdNum() + 1; | ||
String issueName = "test-asset2"; | ||
AssetIssueCapsule assetIssueCapsule = createAssetIssue(id, issueName); | ||
assetIssueStore.put(assetIssueCapsule.createDbKey(), assetIssueCapsule); | ||
AssetIssueCapsule assetIssueCapsule1 = assetIssueStore.get(ByteArray.fromString(issueName)); | ||
|
||
Assert.assertNotNull(assetIssueCapsule1); | ||
Assert.assertEquals(issueName, new String(assetIssueCapsule1.getName().toByteArray())); | ||
} | ||
|
||
@Test | ||
public void testGet() { | ||
AssetIssueCapsule assetIssueCapsule = assetIssueStore.get(ByteArray.fromString(NAME)); | ||
Assert.assertNotNull(assetIssueCapsule); | ||
Assert.assertEquals(NAME, new String(assetIssueCapsule.getName().toByteArray())); | ||
Assert.assertEquals(TOTAL_SUPPLY, assetIssueCapsule.getInstance().getTotalSupply()); | ||
} | ||
|
||
@Test | ||
public void testDelete() { | ||
long id = dbManager.getDynamicPropertiesStore().getTokenIdNum() + 1; | ||
String issueName = "test-asset-delete"; | ||
AssetIssueCapsule assetIssueCapsule = createAssetIssue(id, issueName); | ||
assetIssueStore.put(assetIssueCapsule.createDbKey(), assetIssueCapsule); | ||
AssetIssueCapsule assetIssueCapsule1 = assetIssueStore.get(ByteArray.fromString(issueName)); | ||
Assert.assertNotNull(assetIssueCapsule1); | ||
assetIssueStore.delete(assetIssueCapsule1.createDbKey()); | ||
AssetIssueCapsule assetIssueCapsule2 = assetIssueStore.get(ByteArray.fromString(issueName)); | ||
Assert.assertNull(assetIssueCapsule2); | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.tron.core.db; | ||
|
||
import com.google.protobuf.ByteString; | ||
import javax.annotation.Resource; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.tron.common.BaseTest; | ||
import org.tron.core.Constant; | ||
import org.tron.core.capsule.AssetIssueCapsule; | ||
import org.tron.core.config.args.Args; | ||
import org.tron.core.store.AssetIssueV2Store; | ||
import org.tron.protos.contract.AssetIssueContractOuterClass; | ||
|
||
|
||
public class AssetIssueV2StoreTest extends BaseTest { | ||
|
||
static { | ||
dbPath = "db_AssetIssueV2StoreTest_test"; | ||
Args.setParam( | ||
new String[]{ | ||
"--output-directory", dbPath, | ||
}, | ||
Constant.TEST_CONF | ||
); | ||
} | ||
|
||
private AssetIssueCapsule assetIssueCapsule; | ||
|
||
@Resource | ||
private AssetIssueV2Store assetIssueV2Store; | ||
|
||
@Before | ||
public void init() { | ||
String firstTokenId = "abc"; | ||
assetIssueCapsule = | ||
new AssetIssueCapsule( | ||
AssetIssueContractOuterClass.AssetIssueContract.newBuilder() | ||
.setName(ByteString.copyFrom(firstTokenId.getBytes())) | ||
.build()); | ||
assetIssueCapsule.setId(String.valueOf(1L)); | ||
assetIssueV2Store | ||
.put(assetIssueCapsule.createDbV2Key(), assetIssueCapsule); | ||
} | ||
|
||
@Test | ||
public void testPut() { | ||
String firstTokenId = "efg"; | ||
assetIssueCapsule = | ||
new AssetIssueCapsule( | ||
AssetIssueContractOuterClass.AssetIssueContract.newBuilder() | ||
.setName(ByteString.copyFrom(firstTokenId.getBytes())) | ||
.build()); | ||
assetIssueCapsule.setId(String.valueOf(2L)); | ||
assetIssueV2Store | ||
.put(assetIssueCapsule.createDbV2Key(), assetIssueCapsule); | ||
AssetIssueCapsule assetIssueCapsule = | ||
assetIssueV2Store.get(this.assetIssueCapsule.createDbV2Key()); | ||
Assert.assertNotNull(assetIssueCapsule); | ||
String assetName = new String(assetIssueCapsule.getName().toByteArray()); | ||
Assert.assertEquals(firstTokenId, assetName); | ||
} | ||
|
||
@Test | ||
public void testGet() { | ||
AssetIssueCapsule assetIssueCapsule1 = assetIssueV2Store.get(assetIssueCapsule.createDbV2Key()); | ||
Assert.assertNotNull(assetIssueCapsule1); | ||
String assetName = new String(assetIssueCapsule1.getName().toByteArray()); | ||
Assert.assertEquals("abc", assetName); | ||
} | ||
|
||
@Test | ||
public void testDelete() { | ||
String firstTokenId = "hij"; | ||
assetIssueCapsule = | ||
new AssetIssueCapsule( | ||
AssetIssueContractOuterClass.AssetIssueContract.newBuilder() | ||
.setName(ByteString.copyFrom(firstTokenId.getBytes())) | ||
.build()); | ||
assetIssueCapsule.setId(String.valueOf(2L)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this not be the same as the token "efg"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. efg is the test data in testPut, and as this is a deleted test case, it makes sense not to use the same data |
||
assetIssueV2Store | ||
.put(assetIssueCapsule.createDbV2Key(), assetIssueCapsule); | ||
AssetIssueCapsule assetIssueCapsule = | ||
assetIssueV2Store.get(this.assetIssueCapsule.createDbV2Key()); | ||
Assert.assertNotNull(assetIssueCapsule); | ||
|
||
assetIssueV2Store.delete(assetIssueCapsule.createDbV2Key()); | ||
AssetIssueCapsule assetIssueCapsule1 = | ||
assetIssueV2Store.get(this.assetIssueCapsule.createDbV2Key()); | ||
Assert.assertNull(assetIssueCapsule1); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use static final?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not necessary, and not using final doesn't hurt.