From f107c1e4db5aa30906fb5328264dfcf8ed85ab17 Mon Sep 17 00:00:00 2001 From: Oleg Agafonov Date: Sat, 18 Sep 2021 00:24:10 +0400 Subject: [PATCH] Tests: improved performance in game tests; --- .../base/impl/CardTestPlayerAPIImpl.java | 3 ++- .../mage/cards/repository/CardRepository.java | 24 +++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Mage.Tests/src/test/java/org/mage/test/serverside/base/impl/CardTestPlayerAPIImpl.java b/Mage.Tests/src/test/java/org/mage/test/serverside/base/impl/CardTestPlayerAPIImpl.java index b15a032084..ae6a38570e 100644 --- a/Mage.Tests/src/test/java/org/mage/test/serverside/base/impl/CardTestPlayerAPIImpl.java +++ b/Mage.Tests/src/test/java/org/mage/test/serverside/base/impl/CardTestPlayerAPIImpl.java @@ -672,7 +672,8 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement Assert.fail("Can't add card " + cardName + " - alias " + aliasName + " already exists for " + player.getName()); } - CardInfo cardInfo = CardRepository.instance.findCard(cardName); + // game tests don't need cards from a specific set, so it can be from any set + CardInfo cardInfo = CardRepository.instance.findCard(cardName, true); if (cardInfo == null) { throw new IllegalArgumentException("[TEST] Couldn't find a card: " + cardName); } diff --git a/Mage/src/main/java/mage/cards/repository/CardRepository.java b/Mage/src/main/java/mage/cards/repository/CardRepository.java index fcfc1ce01a..15de5a2274 100644 --- a/Mage/src/main/java/mage/cards/repository/CardRepository.java +++ b/Mage/src/main/java/mage/cards/repository/CardRepository.java @@ -369,12 +369,18 @@ public enum CardRepository { return Collections.emptyList(); } + public CardInfo findCard(String name) { + return findCard(name, false); + } + /** * @param name + * @param returnAnySet return card from first available set (WARNING, it's a performance optimization for tests, + * don't use it in real games - users must get random set) * @return random card with the provided name or null if none is found */ - public CardInfo findCard(String name) { - List cards = findCards(name); + public CardInfo findCard(String name, boolean returnAnySet) { + List cards = returnAnySet ? findCards(name, 1) : findCards(name); if (!cards.isEmpty()) { return cards.get(RandomUtil.nextInt(cards.size())); } @@ -447,9 +453,23 @@ public enum CardRepository { } public List findCards(String name) { + return findCards(name, 0); + } + + /** + * Find card's reprints from all sets + * + * @param name + * @param limitByMaxAmount return max amount of different cards (if 0 then return card from all sets) + * @return + */ + public List findCards(String name, long limitByMaxAmount) { try { QueryBuilder queryBuilder = cardDao.queryBuilder(); queryBuilder.where().eq("name", new SelectArg(name)); + if (limitByMaxAmount > 0) { + queryBuilder.limit(limitByMaxAmount); + } return cardDao.query(queryBuilder.prepare()); } catch (SQLException ex) { }