mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-23 23:06:39 +01:00
Fix TradesChartsViewModelTest
These tests now run. One test fails due a TradesChartsViewModel bug/inconsistency, to be fixed.
This commit is contained in:
parent
ae0aef325d
commit
7d70f61e68
2 changed files with 146 additions and 59 deletions
|
@ -20,6 +20,7 @@ package io.bisq.gui.main.market.trades;
|
|||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.inject.Inject;
|
||||
import io.bisq.common.GlobalSettings;
|
||||
import io.bisq.common.app.Log;
|
||||
import io.bisq.common.locale.CryptoCurrency;
|
||||
import io.bisq.common.locale.CurrencyUtil;
|
||||
import io.bisq.common.locale.TradeCurrency;
|
||||
|
@ -136,14 +137,6 @@ class TradesChartsViewModel extends ActivatableViewModel {
|
|||
GUIUtil.fillCurrencyListItems(tradeCurrencyList, currencyListItems, showAllCurrencyListItem, preferences);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
TradesChartsViewModel() {
|
||||
setChangeListener = null;
|
||||
preferences = null;
|
||||
tradeStatisticsManager = null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void activate() {
|
||||
tradeStatisticsManager.getObservableTradeStatisticsSet().addListener(setChangeListener);
|
||||
|
|
|
@ -1,44 +1,53 @@
|
|||
package io.bisq.gui.main.market.trades;
|
||||
|
||||
import io.bisq.common.crypto.KeyRing;
|
||||
import io.bisq.common.crypto.KeyStorage;
|
||||
import io.bisq.common.locale.FiatCurrency;
|
||||
import io.bisq.common.monetary.Price;
|
||||
import io.bisq.core.offer.OfferPayload;
|
||||
import io.bisq.core.provider.price.PriceFeedService;
|
||||
import io.bisq.core.trade.statistics.TradeStatistics;
|
||||
import io.bisq.core.trade.statistics.TradeStatisticsManager;
|
||||
import io.bisq.core.user.Preferences;
|
||||
import io.bisq.gui.Navigation;
|
||||
import io.bisq.gui.main.market.trades.charts.CandleData;
|
||||
import io.bisq.gui.util.BSFormatter;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableSet;
|
||||
import mockit.*;
|
||||
import mockit.integration.junit4.JMockit;
|
||||
import org.bitcoinj.core.Coin;
|
||||
import org.bitcoinj.utils.Fiat;
|
||||
import org.junit.Ignore;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.security.Security;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
//TODO causes java.lang.NullPointerException
|
||||
// at io.bisq.gui.main.market.trades.TradesChartsViewModel.getCurrencyCode(TradesChartsViewModel.java:209)
|
||||
@Ignore
|
||||
@RunWith(JMockit.class)
|
||||
public class TradesChartsViewModelTest {
|
||||
@Tested TradesChartsViewModel model;
|
||||
@Injectable Preferences preferences;
|
||||
@Injectable PriceFeedService priceFeedService;
|
||||
@Injectable Navigation navigation;
|
||||
@Injectable BSFormatter formatter;
|
||||
@Injectable TradeStatisticsManager tsm;
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TradesChartsViewModelTest.class);
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void testGetCandleData() {
|
||||
TradesChartsViewModel model = new TradesChartsViewModel();
|
||||
|
||||
long low = Fiat.parseFiat("EUR", "500").value;
|
||||
long open = Fiat.parseFiat("EUR", "520").value;
|
||||
long close = Fiat.parseFiat("EUR", "580").value;
|
||||
long high = Fiat.parseFiat("EUR", "600").value;
|
||||
long average = Fiat.parseFiat("EUR", "550").value;
|
||||
long amount = Coin.parseCoin("4").value;
|
||||
long volume = Fiat.parseFiat("EUR", "2200").value;
|
||||
boolean isBullish = true;
|
||||
|
||||
Set<TradeStatistics> set = new HashSet<>();
|
||||
final Date now = new Date();
|
||||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z");
|
||||
private KeyRing keyRing;
|
||||
private File dir;
|
||||
OfferPayload offer = new OfferPayload(null,
|
||||
0,
|
||||
null,
|
||||
|
@ -50,7 +59,7 @@ public class TradesChartsViewModelTest {
|
|||
0,
|
||||
0,
|
||||
"BTC",
|
||||
"USD",
|
||||
"EUR",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
|
@ -78,11 +87,38 @@ public class TradesChartsViewModelTest {
|
|||
null,
|
||||
1
|
||||
);
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
dir = File.createTempFile("temp_tests1", "");
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
dir.delete();
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
dir.mkdir();
|
||||
keyRing = new KeyRing(new KeyStorage(dir));
|
||||
}
|
||||
|
||||
set.add(new TradeStatistics(offer, Price.parse("520", "EUR"), Coin.parseCoin("1"), new Date(now.getTime()), null, null));
|
||||
set.add(new TradeStatistics(offer, Price.parse("500", "EUR"), Coin.parseCoin("1"), new Date(now.getTime() + 100), null, null));
|
||||
set.add(new TradeStatistics(offer, Price.parse("600", "EUR"), Coin.parseCoin("1"), new Date(now.getTime() + 200), null, null));
|
||||
set.add(new TradeStatistics(offer, Price.parse("580", "EUR"), Coin.parseCoin("1"), new Date(now.getTime() + 300), null, null));
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void testGetCandleData() {
|
||||
model.selectedTradeCurrencyProperty.setValue(new FiatCurrency("EUR"));
|
||||
|
||||
long low = Fiat.parseFiat("EUR", "500").value;
|
||||
long open = Fiat.parseFiat("EUR", "520").value;
|
||||
long close = Fiat.parseFiat("EUR", "580").value;
|
||||
long high = Fiat.parseFiat("EUR", "600").value;
|
||||
long average = Fiat.parseFiat("EUR", "550").value;
|
||||
long amount = Coin.parseCoin("4").value;
|
||||
long volume = Fiat.parseFiat("EUR", "2200").value;
|
||||
boolean isBullish = true;
|
||||
|
||||
Set<TradeStatistics> set = new HashSet<>();
|
||||
final Date now = new Date();
|
||||
|
||||
set.add(new TradeStatistics(offer, Price.parse("EUR","520"), Coin.parseCoin("1"), new Date(now.getTime()), null, keyRing.getPubKeyRing().getSignaturePubKeyBytes()));
|
||||
set.add(new TradeStatistics(offer, Price.parse("EUR","500"), Coin.parseCoin("1"), new Date(now.getTime() + 100), null, keyRing.getPubKeyRing().getSignaturePubKeyBytes()));
|
||||
set.add(new TradeStatistics(offer, Price.parse("EUR","600"), Coin.parseCoin("1"), new Date(now.getTime() + 200), null, keyRing.getPubKeyRing().getSignaturePubKeyBytes()));
|
||||
set.add(new TradeStatistics(offer, Price.parse("EUR","580"), Coin.parseCoin("1"), new Date(now.getTime() + 300), null, keyRing.getPubKeyRing().getSignaturePubKeyBytes()));
|
||||
|
||||
CandleData candleData = model.getCandleData(model.getTickFromTime(now.getTime(), TradesChartsViewModel.TickUnit.DAY), set);
|
||||
assertEquals(open, candleData.open);
|
||||
|
@ -94,4 +130,62 @@ public class TradesChartsViewModelTest {
|
|||
assertEquals(volume, candleData.accumulatedVolume);
|
||||
assertEquals(isBullish, candleData.isBullish);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testItemLists() throws ParseException {
|
||||
// Helper class to add historic trades
|
||||
class Trade {
|
||||
Trade (String date, String size, String price, String cc) {
|
||||
try {
|
||||
this.date = dateFormat.parse(date);
|
||||
} catch (ParseException p) {
|
||||
this.date = new Date();
|
||||
}
|
||||
this.size = size;
|
||||
this.price = price;
|
||||
this.cc = cc;
|
||||
}
|
||||
Date date;
|
||||
String size;
|
||||
String price;
|
||||
String cc;
|
||||
};
|
||||
|
||||
// Trade EUR
|
||||
model.selectedTradeCurrencyProperty.setValue(new FiatCurrency("EUR"));
|
||||
|
||||
ArrayList<Trade> trades = new ArrayList<Trade>();
|
||||
|
||||
// Set predetermined time to use as "now" during test
|
||||
Date test_time = dateFormat.parse("2018-01-01T00:00:05 -0000"); // Monday
|
||||
new MockUp<System>() {
|
||||
@Mock long currentTimeMillis() { return test_time.getTime(); }
|
||||
};
|
||||
// 10 seconds ago, different YEAR, MONTH, WEEK, DAY, HOUR, MINUTE_10
|
||||
trades.add(new Trade("2017-12-31T23:59:50 +0000", "1", "100", "EUR"));
|
||||
trades.add(new Trade("2018-01-01T00:00:02 +0000", "1", "110", "EUR"));
|
||||
|
||||
final Date now = new Date();
|
||||
Set<TradeStatistics> set = new HashSet<>();
|
||||
trades.forEach (t ->
|
||||
{
|
||||
set.add(new TradeStatistics(offer, Price.parse(t.cc, t.price), Coin.parseCoin(t.size), t.date, null, keyRing.getPubKeyRing().getSignaturePubKeyBytes()));
|
||||
}
|
||||
);
|
||||
ObservableSet<TradeStatistics> tradeStats = FXCollections.observableSet(set);
|
||||
|
||||
// Run test for each tick type
|
||||
for (TradesChartsViewModel.TickUnit tick : TradesChartsViewModel.TickUnit.values()) {
|
||||
new Expectations() {{
|
||||
tsm.getObservableTradeStatisticsSet();
|
||||
result = tradeStats;
|
||||
}};
|
||||
|
||||
// Trigger chart update
|
||||
model.setTickUnit(tick);
|
||||
assertEquals(model.selectedTradeCurrencyProperty.get().getCode(), tradeStats.iterator().next().getCurrencyCode());
|
||||
assertEquals(2, model.priceItems.size());
|
||||
assertEquals(2, model.volumeItems.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue