Skip to content

Commit 1d12871

Browse files
committed
add tests
1 parent ad0d391 commit 1d12871

2 files changed

Lines changed: 165 additions & 6 deletions

File tree

core-contracts/StakedLP/src/main/java/network/balanced/score/core/stakedlp/AddressBranchDictDB.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
public class AddressBranchDictDB {
1212

13-
private BranchDB<Address, DictDB<BigInteger, BigInteger>> legacyAddressDictDB;
14-
private BranchDB<String, DictDB<BigInteger, BigInteger>> addressDictDB;
15-
private BranchDB<Address, DictDB<BigInteger, Boolean>> migrationDB;
13+
protected BranchDB<Address, DictDB<BigInteger, BigInteger>> legacyAddressDictDB;
14+
protected BranchDB<String, DictDB<BigInteger, BigInteger>> addressDictDB;
15+
protected BranchDB<Address, DictDB<BigInteger, Boolean>> migrationDB;
1616

1717
public AddressBranchDictDB(String id) {
1818
this.legacyAddressDictDB = Context.newBranchDB(id, BigInteger.class);
@@ -24,11 +24,11 @@ private BigInteger getLegacy(Address address, BigInteger key) {
2424
return legacyAddressDictDB.at(address).getOrDefault(key, BigInteger.ZERO);
2525
}
2626

27-
private Boolean isMigrated(Address address, BigInteger key) {
27+
public Boolean isMigrated(Address address, BigInteger key) {
2828
return migrationDB.at(address).getOrDefault(key, false);
2929
}
3030

31-
public BigInteger get(NetworkAddress address, BigInteger id, Boolean readonly) {
31+
public BigInteger get(NetworkAddress address, BigInteger id, boolean readonly) {
3232
BigInteger total = addressDictDB.at(address.toString()).getOrDefault(id, BigInteger.ZERO);
3333
if (address.account().startsWith("hx") || address.account().startsWith("cx")) {
3434
Address iconAddr = Address.fromString(address.account());
@@ -44,7 +44,7 @@ public BigInteger get(NetworkAddress address, BigInteger id, Boolean readonly) {
4444
}
4545

4646
public void set(NetworkAddress address, BigInteger id, BigInteger value) {
47-
addressDictDB.at(address.toString()).set(id, value);
47+
addressDictDB.at(address.toString()).set(id, value);
4848
}
4949

5050
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* Copyright (c) 2022-2022 Balanced.network.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package network.balanced.score.core.stakedlp;
18+
19+
import com.iconloop.score.test.Account;
20+
import com.iconloop.score.test.Score;
21+
import com.iconloop.score.test.ServiceManager;
22+
import com.iconloop.score.test.TestBase;
23+
24+
import foundation.icon.xcall.NetworkAddress;
25+
26+
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.Test;
28+
29+
import score.Address;
30+
31+
import java.math.BigInteger;
32+
33+
import static org.junit.jupiter.api.Assertions.*;
34+
35+
public class DBTest extends TestBase {
36+
37+
private static final ServiceManager sm = getServiceManager();
38+
private static final Account owner = sm.createAccount();
39+
private static Score dummyScore;
40+
41+
public static class DummyScore extends AddressBranchDictDB {
42+
43+
public DummyScore() {
44+
super("test");
45+
}
46+
47+
public void setLegacy(Address address, BigInteger key, BigInteger value) {
48+
legacyAddressDictDB.at(address).set(key, value);
49+
}
50+
}
51+
52+
@BeforeAll
53+
public static void setup() throws Exception {
54+
dummyScore = sm.deploy(owner, DummyScore.class);
55+
}
56+
57+
@Test
58+
public void onlyLegacyValue() {
59+
// Arrange
60+
Address user = sm.createAccount().getAddress();
61+
BigInteger id = BigInteger.ONE;
62+
BigInteger value = BigInteger.TEN;
63+
dummyScore.invoke(owner, "setLegacy", user, id, value);
64+
65+
// Act
66+
BigInteger balance = (BigInteger) dummyScore.call("get", new NetworkAddress("test", user), id, true);
67+
68+
// Assert
69+
assertEquals(balance, value);
70+
}
71+
72+
@Test
73+
public void bothValues() {
74+
// Arrange
75+
Address user = sm.createAccount().getAddress();
76+
BigInteger id = BigInteger.ONE;
77+
BigInteger legacyValue = BigInteger.TEN;
78+
BigInteger value = BigInteger.TWO;
79+
dummyScore.invoke(owner, "setLegacy", user, id, legacyValue);
80+
dummyScore.invoke(owner, "set", new NetworkAddress("test", user), id, value);
81+
82+
// Act
83+
BigInteger balance = (BigInteger) dummyScore.call("get", new NetworkAddress("test", user), id, true);
84+
85+
// Assert
86+
assertEquals(balance, legacyValue.add(value));
87+
}
88+
89+
@Test
90+
public void migrateValue() {
91+
// Arrange
92+
Address user = sm.createAccount().getAddress();
93+
BigInteger id = BigInteger.ONE;
94+
BigInteger legacyValue = BigInteger.TEN;
95+
BigInteger value = BigInteger.TWO;
96+
dummyScore.invoke(owner, "setLegacy", user, id, legacyValue);
97+
dummyScore.invoke(owner, "set", new NetworkAddress("test", user), id, value);
98+
99+
// Act
100+
dummyScore.invoke(owner, "get", new NetworkAddress("test", user), id, false);
101+
102+
// Assert
103+
// In a real scenario set would be called, but for this the value is simply
104+
// marked as migrated and wont be used anymore
105+
BigInteger balance = (BigInteger) dummyScore.call("get", new NetworkAddress("test", user), id, true);
106+
assertEquals(balance, value);
107+
}
108+
109+
@Test
110+
public void migrateContractValue() {
111+
// Arrange
112+
Address user = dummyScore.getAddress();
113+
BigInteger id = BigInteger.ONE;
114+
BigInteger legacyValue = BigInteger.TEN;
115+
BigInteger value = BigInteger.TWO;
116+
dummyScore.invoke(owner, "setLegacy", user, id, legacyValue);
117+
dummyScore.invoke(owner, "set", new NetworkAddress("test", user), id, value);
118+
119+
// Act
120+
dummyScore.invoke(owner, "get", new NetworkAddress("test", user), id, false);
121+
122+
// Assert
123+
// In a real scenario set would be called, but for this the value is simply
124+
// marked as migrated and wont be used anymore
125+
BigInteger balance = (BigInteger) dummyScore.call("get", new NetworkAddress("test", user), id, true);
126+
assertEquals(balance, value);
127+
}
128+
129+
@Test
130+
public void onlyNew() {
131+
// Arrange
132+
Address user = sm.createAccount().getAddress();
133+
BigInteger id = BigInteger.ONE;
134+
BigInteger value = BigInteger.TWO;
135+
dummyScore.invoke(owner, "set", new NetworkAddress("test", user), id, value);
136+
137+
// Act
138+
BigInteger balance = (BigInteger) dummyScore.call("get", new NetworkAddress("test", user), id, true);
139+
140+
// Assert
141+
assertEquals(balance, value);
142+
}
143+
144+
@Test
145+
public void networkAddress() {
146+
// Arrange
147+
NetworkAddress user = new NetworkAddress("test", "test");
148+
BigInteger id = BigInteger.ONE;
149+
BigInteger value = BigInteger.TWO;
150+
dummyScore.invoke(owner, "set", user, id, value);
151+
152+
// Act
153+
BigInteger balance = (BigInteger) dummyScore.call("get", user, id, true);
154+
155+
// Assert
156+
assertEquals(balance, value);
157+
}
158+
159+
}

0 commit comments

Comments
 (0)