class TestNicknameUtil(): def setup_method(self, method): self.database = KfpDb(":memory:") def teardown_method(self, method): self.database.teardown() def test_getNickname_empty(self): nicknames = NicknameUtil.get_all_nicknames(1, 2) assert len(nicknames) == 0 def test_setNickname_success(self): NicknameUtil.set_nickname(guild_id=1, user_id=1, nickname="hello") nicknames = NicknameUtil.get_all_nicknames(guild_id=1, user_id=1) assert nicknames == ["hello"] def test_setNicknames_success(self): NicknameUtil.set_nickname(guild_id=1, user_id=1, nickname="hello") NicknameUtil.set_nickname(guild_id=1, user_id=1, nickname="world") nicknames = NicknameUtil.get_all_nicknames(guild_id=1, user_id=1) assert nicknames == ["hello", "world"] def test_setNicknames_clear(self): NicknameUtil.set_nickname(guild_id=1, user_id=1, nickname="hello") NicknameUtil.set_nickname(guild_id=1, user_id=1, nickname="world") NicknameUtil.clear_nickname(guild_id=1, user_id=1) nicknames = NicknameUtil.get_all_nicknames(guild_id=1, user_id=1) assert len(nicknames) == 0
class TestStatusUtil(): def setup_method(self, method): self.database = KfpDb(":memory:") def teardown_mehtod(self, method): self.database.teardown() def test_createRestStatus_success(self): expected_status = StatusUtil.createRestStatus(1, 1, 20, 30) status = StatusUtil.getStatus(1, 1, StatusType.REST) assert expected_status == status def test_applyExpiredStatus_success(self): c: RPGCharacter = RPGCharacterUtil.createNewRPGCharacter(1) RPGCharacterUtil.changeHp(c, -1000) StatusUtil.createRestStatus(1, 1, 20, -500) # expired status StatusUtil.applyExpiredStatus() assert None == StatusUtil.getStatus(1, 1, StatusType.REST) c = RPGCharacterUtil.getRPGCharacter(1) assert c.hp_current > 0 def test_createComaStatus_success(self): c: RPGCharacter = RPGCharacterUtil.createNewRPGCharacter(1) comaStatus = StatusUtil.createComaStatus(guild_id=1, user=c, hp_max=c.hp_max) status = StatusUtil.getStatus(guild_id=1, member_id=c.id, type=StatusType.COMA) assert comaStatus == status statuslist = StatusUtil.reviveComaStatus() assert len(statuslist) == 0 ch: RPGCharacter = RPGCharacterUtil.createNewRPGCharacter(2) comaStatus1 = StatusUtil.createComaStatus(guild_id=1, user=ch, hp_max=ch.hp_max) status1 = StatusUtil.getStatus(guild_id=1, member_id=ch.id, type=StatusType.COMA) assert comaStatus1 == status1 statuslist = StatusUtil.reviveComaStatus() assert len(statuslist) == 0 assert c.hp_max == c.hp_current def test_removeComaStatusToRest(self): c: RPGCharacter = RPGCharacterUtil.createNewRPGCharacter(1) StatusUtil.createComaStatus(guild_id=1, user=c, hp_max=c.hp_max) StatusUtil.startResting(guild_id=1, user=c) status = StatusUtil.getStatus(guild_id=1, member_id=c.id, type=StatusType.COMA) assert status is None status = StatusUtil.getStatus(guild_id=1, member_id=c.id, type=StatusType.REST) assert status is not None
def test_itemDropHiddenMigration(self): oldDbPath = getDBPath("item_has_hidden.db") testDbPath = getDBPath("test.db") shutil.copyfile(oldDbPath, testDbPath) database = KfpDb(testDbPath) assert KfpMigrator.KfpMigrate(database.sqliteDb) database.teardown() os.remove(testDbPath)
def test_memberTokenMigration(self): oldDbPath = getDBPath("old.db") testDbPath = getDBPath("test.db") shutil.copyfile(oldDbPath, testDbPath) database = KfpDb(testDbPath) assert KfpMigrator.KfpMigrate(database.sqliteDb) database.teardown() os.remove(testDbPath)
class TestKujiUtil(): def setup_method(self): self.database = KfpDb(":memory:") def teardown_method(self): self.database.teardown() def test_canDraw_true(self): assert KujiUtil.canDrawJp(1) assert KujiUtil.canDrawCn(1) assert KujiUtil.canDrawLs(1) def test_canDrawJp_false(self): KujiUtil.updateMemberJp(1, 1) assert not KujiUtil.canDrawJp(1) def test_canDrawCn_false(self): KujiUtil.updateMemberCn(1, 0, 0) assert not KujiUtil.canDrawCn(1) def test_canDrawLs_false(self): KujiUtil.updateMemberLs(1, 1) assert not KujiUtil.canDrawLs(1) def test_canDraw_false(self): KujiUtil.updateMemberJp(1, 1) KujiUtil.updateMemberCn(1, 0, 0) KujiUtil.updateMemberLs(1, 1) assert not KujiUtil.canDrawLs(1) def test_getHistoryJp(self): KujiUtil.updateMemberJp(1, 1) history = KujiUtil.getHistoryJp(1) assert history[0] == 1 def test_getHistoryLs(self): KujiUtil.updateMemberLs(1, 1) history = KujiUtil.getHistoryLs(1) assert history[0] == 1 def test_getHistoryCn(self): KujiUtil.updateMemberCn(1, 1, 2) history = KujiUtil.getHistoryCn(1) assert history[0] == 1 assert history[1] == 2 def test_clearData(self): KujiUtil.updateMemberJp(1, 1) KujiUtil.clearData() assert KujiUtil.canDrawJp(1) KujiUtil.updateMemberCn(1, 1, 2) assert not KujiUtil.canDrawCn(1)
class TestForwardUtil(): def setup_method(self, method): self.database = KfpDb(":memory:") def teardown_method(self, method): self.database.teardown() def test_getAllForward_empty(self): forwards = ForwardUtil.get_all_forward() assert len(forwards) == 0 def test_getAllForward_empty(self): ForwardUtil.create_forward(1, 1, 1, 2) ForwardUtil.create_forward(1, 1, 1, 5) forwards = ForwardUtil.get_all_forward() assert len(forwards) == 2 def test_getForward_empty(self): forwards = ForwardUtil.get_forward(1, 1) assert len(forwards) == 0 def test_createForward_success(self): ForwardUtil.create_forward(1, 1, 1, 2, True) forward: Forward = ForwardUtil.get_forward(1, 1)[0] assert forward.receive_channel_id == 2 assert forward.receive_guild_id == 1 assert forward.delete_original == True def test_createForward_noOverride(self): ForwardUtil.create_forward(1, 1, 1, 2) ForwardUtil.create_forward(1, 1, 1, 3) forwards = ForwardUtil.get_forward(1, 1) assert forwards[0].receive_channel_id == 2 assert forwards[1].receive_channel_id == 3 def test_deleteForward_noRecord(self): ForwardUtil.delete(123) forwards = ForwardUtil.get_all_forward() assert len(forwards) == 0 def test_deleteForward_success(self): ForwardUtil.create_forward(1, 1, 1, 2) ForwardUtil.delete(1) forwards = ForwardUtil.get_all_forward() assert len(forwards) == 0
class TestMemberUtil(): def setup_method(self, method): self.database = KfpDb(":memory:") def teardown_method(self, method): self.database.teardown() def test_getCountZero(self): assert MemberUtil.get_member_count() == 0 def test_getCountSuccess(self): self.database.add_member(1) assert MemberUtil.get_member_count() == 1 self.database.add_member(2) assert MemberUtil.get_member_count() == 2 def test_getTotalTokenZero(self): assert MemberUtil.get_total_token() == 0 def test_getTotalTokenSuccess(self): self.database.add_member(123) assert MemberUtil.get_total_token() == 100 self.database.add_member(321) assert MemberUtil.get_total_token() == 200 def test_getTotalCoinZero(self): assert MemberUtil.get_total_coin() == 0 def test_getTotalCoinSuccess(self): self.database.add_member(123) self.database.add_coin(123, 100) assert MemberUtil.get_total_coin() == 100 self.database.add_member(321) self.database.add_coin(321, 100) assert MemberUtil.get_total_coin() == 200
class TestPoliceUtil(): def setup_method(self, method): self.database = KfpDb(":memory:") def teardown_method(self, method): self.database.teardown() def test_getCurrentPolice_empty(self): currentPoliceType = PoliceUtil.getCurrentPoliceType(guild_id=1, user_id=1) assert len(currentPoliceType) == 0 def test_createNewPolice_success(self): assert PoliceUtil.createNewPolice(guild_id=1, user_id=1, type="SLEEP") currentPoliceType = PoliceUtil.getCurrentPoliceType(guild_id=1, user_id=1) assert currentPoliceType == "SLEEP" def test_createNewPolice_failed(self): assert PoliceUtil.createNewPolice(guild_id=1, user_id=1, type="SLEEP") assert not PoliceUtil.createNewPolice( guild_id=1, user_id=1, type="STUDY") def test_stopPolice_fail(self): assert not PoliceUtil.stopPolice(guild_id=1, user_id=1) def test_stopPolice_success(self): assert PoliceUtil.createNewPolice(guild_id=1, user_id=1, type="SLEEP") assert PoliceUtil.stopPolice(guild_id=1, user_id=1) assert len(PoliceUtil.getCurrentPoliceType(guild_id=1, user_id=1)) == 0 def test_timeout_success(self): assert PoliceUtil.createNewPoliceWithDuration(guild_id=1, user_id=1, duration_min=-61, type="SLEEP") assert len(PoliceUtil.getCurrentPoliceType(guild_id=1, user_id=1)) == 0
class TestChannelUtil(): def setup_method(self, method): self.database = KfpDb(dbFile=":memory:") def teardown_method(self, method): self.database.teardown() def test_setChannel(self): ChannelUtil.setChannel(123, 321, Util.ChannelType.RANK_UP) result: Channel = ChannelUtil.GetChannelWithGuild( 123, Util.ChannelType.RANK_UP)[0] assert result.channel_id == 321 assert result.channel_guild_id == 123 assert result.channel_type == Util.ChannelType.RANK_UP.value def test_setRankupChannel(self): ChannelUtil.setRankupChannel(0, 123) assert ChannelUtil.getMessageChannelId(0) == 123 ChannelUtil.setRankupChannel(0, 456) assert ChannelUtil.getMessageChannelId(0) == 456 def test_setRankupChannel_notExist(self): assert not ChannelUtil.getMessageChannelId(123)
class TestRPGCharacterUtil(): def setup_method(self, method): self.database = KfpDb(":memory:") def teardown_method(self, method): self.database.teardown() def test_hasAdventureStarted_noMember(self): assert not RPGCharacterUtil.hasAdventureStared(100) def test_hasAdventureStarted_hasMember_notStarted(self): self.database.add_member(20) assert not RPGCharacterUtil.hasAdventureStared(20) def test_hasAdventureStarted_started(self): member = self.database.add_member(33) RPGCharacterUtil.createNewRPGCharacter(member) assert RPGCharacterUtil.hasAdventureStared(33) def test_createNewCharacter_success(self): member = self.database.add_member(33) character = RPGCharacterUtil.createNewRPGCharacter(member) assert character.character == member def test_createExistedCharacter_false(self): member = self.database.add_member(33) RPGCharacterUtil.createNewRPGCharacter(member) character = RPGCharacterUtil.createNewRPGCharacter(member) assert character == None def test_retireCharacter_notExist(self): assert not RPGCharacterUtil.retireRPGCharacter(123) def test_retireCharacter_notAdventurer(self): member = self.database.add_member(33) assert not RPGCharacterUtil.retireRPGCharacter(member.member_id) def test_retireCharacter_success(self): member = self.database.add_member(33) RPGCharacterUtil.createNewRPGCharacter(member) RPGCharacterUtil.retireRPGCharacter(member.member_id) assert not RPGCharacterUtil.hasAdventureStared(member.member_id) def test_getRPGChracter_success(self): member = self.database.add_member(100) rpg = RPGCharacterUtil.createNewRPGCharacter(member) rpg_get = RPGCharacterUtil.getRPGCharacter(member) assert rpg_get == rpg def test_getRPGCharacter_successWithId(self): member = self.database.add_member(100) rpg = RPGCharacterUtil.createNewRPGCharacter(member) rpg_get = RPGCharacterUtil.getRPGCharacter(member.member_id) assert rpg_get == rpg def test_levelUpCharacter_success(self): member = self.database.add_member(100) rpg: RPGCharacterUtil = RPGCharacterUtil.createNewRPGCharacter(member) old_hp = rpg.hp_max old_mp = rpg.mp_max old_atk = rpg.attack_basic old_def = rpg.defense_basic RPGCharacterUtil.levelUpCharacter(member.member_id, 1, 10) rpg: RPGCharacterUtil = RPGCharacterUtil.getRPGCharacter(member.member_id) assert rpg.hp_max > old_hp assert rpg.mp_max > old_mp assert rpg.attack_basic > old_atk assert rpg.defense_basic > old_def def test_changeHp_success(self): member = self.database.add_member(100) rpg: RPGCharacterUtil = RPGCharacterUtil.createNewRPGCharacter(member) expected_hp = rpg.hp_current dead = RPGCharacterUtil.changeHp(rpg, -10000) assert (rpg.hp_current == 0) and (dead is True) dead = RPGCharacterUtil.changeHp(rpg, +100) assert (rpg.hp_current == expected_hp) and (dead is not True)
class TestItemUtil(): def setup_method(self, method): self.database = KfpDb(":memory:") def teardown_method(self, method): self.database.teardown() def test_searchItem_success(self): item1 = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=1, buff_value=-1, buff_round=2) result = ItemUtil.searchItem(guild_id=1, item_name="hello") assert result == item1 def test_searchItem_fail(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=1, buff_value=-1, buff_round=2) result = ItemUtil.searchItem(guild_id=1, item_name="he") assert result == None def test_createItem_success(self): item1 = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=1, buff_value=-1, buff_round=2) result = ItemUtil.ListAllItem(guild_id=1) assert len(result) == 1 and result[0] == item1 def test_createItem_failed_itemExist(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=1, buff_value=-1, buff_round=2) result = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=1, buff_value=-1, buff_round=2) assert result == -1 def test_deleteItem_success(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=1, buff_value=-1, buff_round=2, level_required=0, price=1) ItemUtil.deleteItem(guild_id=1, item_name="hello") result = ItemUtil.ListAllItem(guild_id=1) assert result == [] def test_deleteItem_failed_noItem(self): ItemUtil.deleteItem(guild_id=1, item_name="hello") result = ItemUtil.ListAllItem(guild_id=1) assert result == [] def test_deleteItems_success(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=1, buff_value=-1, buff_round=2, level_required=0, price=1) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=2, buff_value=10, buff_round=3, level_required=0, price=1) ItemUtil.createItem(guild_id=1, item_name="heap", item_type=ItemType.STATUS, buff_type=2, buff_value=10, buff_round=3, level_required=0, price=1) ItemUtil.createItem(guild_id=1, item_name="hot", item_type=ItemType.STATUS, buff_type=2, buff_value=10, buff_round=3, level_required=0, price=1) ItemUtil.deleteItems(guild_id=1) result = ItemUtil.ListAllItem(guild_id=1) assert result == [] def test_deleteItems_failed_noItem(self): ItemUtil.deleteItems(guild_id=1) result = ItemUtil.ListAllItem(guild_id=1) assert result == [] def test_IntToItemType(self): assert ItemType.list()[0] == ItemType.NONE
class TestInventoryUtil(): def setup_method(self, method): self.database = KfpDb(":memory:") def teardown_method(self, method): self.database.teardown() def test_getInventory_empty(self): inventory = InventoryUtil.getAllItemsBelongToUser(1, 1) assert len(inventory) == 0 def test_buffTypeStorage_success(self): item = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=30, buff_round=3) item_buff: Buff = item.buff assert item_buff.buff_type == BuffType.ATTACK assert item_buff.buff_value == 30 assert item_buff.buff_round == 3 def test_buffTypeStorageRead_success(self): item = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=30, buff_round=3) item_from_db = ItemUtil.ListAllItem(guild_id=1)[0] item_buff: Buff = item_from_db.buff assert item_buff.buff_type == BuffType.ATTACK assert item_buff.buff_value == 30 assert item_buff.buff_round == 3 def test_addItemToItemdb_success(self): item1 = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) item2 = ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3) items = ItemUtil.ListAllItem(guild_id=1) assert items == [item1, item2] def test_addItemToItemdb_failed_samename(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) item2 = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) items = ItemUtil.ListAllItem(guild_id=1) assert len(items) == 1 and item2 == -1 def test_addItemTOShop_success(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3) shopItem1 = InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=10) result = InventoryUtil.ShopMenu(guild_id=1) assert result == [shopItem1] def test_addItemTOShop_failed_noitem(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3) InventoryUtil.addItemToShop(guild_id=1, item_name="he", amount=10) result = InventoryUtil.ShopMenu(guild_id=1) assert result == [] def test_addItemTOShop_success_addAmount(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) result = InventoryUtil.ShopMenu(guild_id=1) assert result[0].amount == 20 and len(result) == 1 def test_addItemTOShop_failed_addAmount(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) InventoryUtil.addItemToShop(guild_id=1, item_name="he", amount=10) InventoryUtil.addItemToShop(guild_id=1, item_name="he", amount=10) result = InventoryUtil.ShopMenu(guild_id=1) assert result == [] def test_addItemTOShop_failed_unlimitedSupply(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=-1) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) result = InventoryUtil.ShopMenu(guild_id=1) assert result[0].amount == -1 def test_addItemTOShop_failed_limitedToUnlimited(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) shopitem = InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) result = InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=-1) assert result == -3 and shopitem.amount == 10 def test_findShopItem_success(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3) shopItem1 = InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=10) result = InventoryUtil.findShopItem(guild_id=1, item=shopItem1.item) assert result == shopItem1 def test_findShopItem_failed(self): item1 = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3) InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=10) result = InventoryUtil.findShopItem(guild_id=1, item=item1) assert result == None def test_deleteShopItem(self): item1 = ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=100) list = InventoryUtil.ShopMenu(guild_id=1) assert len(list) == 2 InventoryUtil.deleteShopItem(guild_id=1, item=item1) list = InventoryUtil.ShopMenu(guild_id=1) assert len(list) == 1 def test_deleteShopItems(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=100) list = InventoryUtil.ShopMenu(guild_id=1) assert len(list) == 2 InventoryUtil.deleteShopItems(guild_id=1) list = InventoryUtil.ShopMenu(guild_id=1) assert len(list) == 0 def test_getAllItemsBelongToUser_empty(self): MemberUtil.add_member(member_id=123) MemberUtil.add_token(member_id=123, amount=100) itemList = InventoryUtil.getAllItemsBelongToUser(guild_id=1, user_id=123) assert len(itemList) == 0 def test_buyShopitem_success(self): MemberUtil.add_member(member_id=123) MemberUtil.add_token(member_id=123, amount=100) ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) shopItem1 = InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=10) InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="hello", count=2) itemList = InventoryUtil.getAllItemsBelongToUser(guild_id=1, user_id=123) assert itemList[0].user_id == 123 and itemList[0].item == shopItem1.item def test_buyShopitem_success_shopItemchangeHidden(self): MemberUtil.add_member(member_id=123) MemberUtil.add_token(member_id=123, amount=500) ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=1) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="hello", count=10) InventoryUtil.checkZeroAmount(guild_id=1) result = InventoryUtil.ShopMenu(guild_id=1) assert len(result) == 0 and result == [] def test_buyShopitems_success(self): MemberUtil.add_member(member_id=123) MemberUtil.add_token(member_id=123, amount=500) ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3, level_required=0, price=10) shopItem1 = InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) shopItem2 = InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=10) InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="hello", count=2) InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="hey", count=4) itemList = InventoryUtil.getAllItemsBelongToUser(guild_id=1, user_id=123) assert len(itemList) == 2 and itemList[0].user_id == 123 and itemList[ 1].user_id == 123 def test_buyShopitem_failed_noProduct(self): MemberUtil.add_member(member_id=123) MemberUtil.add_token(member_id=123, amount=100) ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3, level_required=0, price=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=10, hidden=True) result = InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="he", count=2) assert result == ErrorCode.CannotFindProduct result == InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="hey", count=2) assert result == ErrorCode.CannotFindProduct def test_buyShopitem_failed_levelDoesNotRequired(self): MemberUtil.add_member(member_id=123) MemberUtil.add_token(member_id=123, amount=10) ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=100, price=100) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) result = InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="hello", count=2) assert result == ErrorCode.LevelDoesNotReach def test_buyShopitem_failed_noMoney(self): MemberUtil.add_member(member_id=123) MemberUtil.add_token(member_id=123, amount=10) ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=100) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) result = InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="hello", count=2) assert result == ErrorCode.TokenDoesNotEnough def test_buyShopitem_failed_notEnoughSupply(self): MemberUtil.add_member(member_id=123) MemberUtil.add_token(member_id=123, amount=100) ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=1) result = InventoryUtil.buyShopitem(guild_id=1, user_id=123, item_name="hello", count=2) assert result == ErrorCode.SupplyDoesNotEnough def test_changeSupplyAmount_success(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) shopItem1 = InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=-1) assert shopItem1.amount == -1 shopItem2 = InventoryUtil.changeSupplyAmount(guild_id=1, item_name="hello", newAmount=10) assert shopItem2.amount == 10 def test_changeSupplyAmount_success_limitedToUnlimited(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) shopItem1 = InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) assert shopItem1.amount == 10 shopItem2 = InventoryUtil.changeSupplyAmount(guild_id=1, item_name="hello", newAmount=-1) assert shopItem2.amount == -1 def test_changeSupplyAmount_failed(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3, level_required=0, price=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) # item cannot find result1 = InventoryUtil.changeSupplyAmount(guild_id=1, item_name="he", newAmount=-1) assert result1 == -1 # shopitem cannot find result2 = InventoryUtil.changeSupplyAmount(guild_id=1, item_name="hey", newAmount=-1) assert result2 == -2 def test_changeShopitemHiddenStatus_success(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) result = InventoryUtil.changeShopitemHiddenStatus(guild_id=1, item_name="hello", hidden=True) assert result.hidden == True result2 = InventoryUtil.ShopMenu(guild_id=1) assert len(result2) == 0 result3 = InventoryUtil.changeShopitemHiddenStatus(guild_id=1, item_name="hello", hidden=False) assert result3.hidden == False def test_changeShopitemHiddenStatus_failed_noitem(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) result = InventoryUtil.changeShopitemHiddenStatus(guild_id=1, item_name="he", hidden=True) assert result == -1 def test_changeShopitemHiddenStatus_failed_noShopitem(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3, level_required=0, price=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) result = InventoryUtil.changeShopitemHiddenStatus(guild_id=1, item_name="hey", hidden=True) assert result == -2 def test_checkShopitemStatus_success(self): ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) result = InventoryUtil.checkShopitemStatus(guild_id=1, item_name="hello") assert result.hidden == False def test_checkShopitemStatus_failed(self): # no item result1 = InventoryUtil.checkShopitemStatus(guild_id=1, item_name="hello") assert result1 == -1 # no shopitem ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=10) result2 = InventoryUtil.checkShopitemStatus(guild_id=1, item_name="hello") assert result2 == -2 def test_listHiddenitem_success(self): result = InventoryUtil.listHiddenShopItem(guild_id=1) assert len(result) == 0 ItemUtil.createItem(guild_id=1, item_name="hello", item_type=ItemType.ATTACK, buff_type=BuffType.ATTACK, buff_value=-1, buff_round=2, level_required=0, price=1) ItemUtil.createItem(guild_id=1, item_name="hey", item_type=ItemType.STATUS, buff_type=BuffType.DEFENCE, buff_value=10, buff_round=3, level_required=0, price=1) InventoryUtil.addItemToShop(guild_id=1, item_name="hello", amount=10) InventoryUtil.addItemToShop(guild_id=1, item_name="hey", amount=10) InventoryUtil.changeShopitemHiddenStatus(guild_id=1, item_name="hello", hidden=True) result = InventoryUtil.listHiddenShopItem(guild_id=1) assert len(result) == 1 # items InventoryUtil.changeShopitemHiddenStatus(guild_id=1, item_name="hey", hidden=True) result = InventoryUtil.listHiddenShopItem(guild_id=1) assert len(result) == 2
class TestKfpDb(): def setup_method(self, method): self.guild_id = 123 self.database = KfpDb(dbFile=":memory:") self.database.add_member(default_user_id) # add a default member def teardown_method(self, method): self.database.teardown() for file in os.listdir(): if file.endswith("test.db"): os.remove(file) def test_addMember(self): self.database.add_member(12346) member = self.database.get_member(12346) assert member.member_id == 12346 def test_dataIntegrity(self): self.database = KfpDb(dbFile="tmp_test.db") self.database.add_member(12346) self.database = KfpDb(dbFile="tmp_test.db") member = self.database.get_member(12346) assert member.member_id == 12346 def test_getMember_notExist(self): assert not self.database.get_member(100) # user 100 does not exist def test_addMultipleMembers(self): member_ids = [1, 2, 3, 4, 5, 6, 7, 8] self.database.add_members(member_ids) for member_id in member_ids: member = Member.get_by_id(member_id) assert member.member_id == member_id def test_memberHasUniqueId(self): with pytest.raises(peewee.IntegrityError): self.database.add_member(default_user_id) def test_increaseExp_notExist(self): assert -1 == self.database.increase_exp(0, 0, 100, 10) # user 100 does not exist def test_increaseExp(self): self.database.increase_exp(0, 0, default_user_id, 10) member = Member.get_by_id(default_user_id) assert member.exp == 10 def test_ignore_increaseExp(self): self.database.set_ignore_xp_channel(3, 5) self.database.increase_exp(3, 5, default_user_id, 10) member = Member.get_by_id(default_user_id) assert member.exp == 0 def test_after_remove_ignore_increaseExp(self): self.database.remove_ignore_xp_channel(3, 5) self.database.set_ignore_xp_channel(3, 5) self.database.remove_ignore_xp_channel(3, 5) self.database.increase_exp(3, 5, default_user_id, 10) member = Member.get_by_id(default_user_id) assert member.exp == 10 def test_rankUp(self): member = Member.get_by_id(default_user_id) assert member.rank == 0 self.database.increase_exp(0, 0, default_user_id, 100) member = Member.get_by_id(default_user_id) assert member.rank == 1 def test_addCoin_notExist(self): assert not self.database.add_coin(100, 10) # user 100 does not exist def test_addCoin(self): self.database.add_coin(default_user_id, 10) member = self.database.get_member(default_user_id) assert member.coin == 10 def test_subtractCoin(self): self.database.add_coin(default_user_id, 100) self.database.add_coin(default_user_id, -10) member = self.database.get_member(default_user_id) assert member.coin == 90 def test_notEnoughMoney(self): self.database.add_coin(default_user_id, 100) assert not self.database.add_coin(default_user_id, -1000) member = self.database.get_member(default_user_id) assert member.coin == 100 def test_getMemberRankOrder_number1(self): # 只有一人的時候那人是第一名 assert self.database.get_member_rank_order(0) == 1 def test_getMemberRankOrder_last(self): self.database.add_member(1) self.database.add_member(2) self.database.increase_exp(0, 0, 1, 100) self.database.increase_exp(0, 0, 2, 100) assert self.database.get_member_rank_order(0) == 3 def test_getMemberRankOrder_first(self): self.database.add_member(1) self.database.add_member(2) self.database.increase_exp(0, 0, default_user_id, 101) self.database.increase_exp(0, 0, 1, 100) self.database.increase_exp(0, 0, 2, 100) assert self.database.get_member_rank_order(default_user_id) == 1 def test_sameMemberRankOrder_second(self): self.database.add_member(1) self.database.add_member(2) self.database.add_member(3) self.database.increase_exp(0, 0, default_user_id, 100) self.database.increase_exp(0, 0, 1, 100) self.database.increase_exp(0, 0, 2, 1000) self.database.increase_exp(0, 0, 3, 10) # 經驗相同時排名相同, 並列第二 assert self.database.get_member_rank_order(default_user_id) == 2 assert self.database.get_member_rank_order(1) == 2 def test_setRankupChannel_notExist(self): assert not self.database.get_rankup_channel_id(11) def test_setRankupChannel(self): self.database.set_rankup_channel(11, 123) assert self.database.get_rankup_channel_id(11) == 123 self.database.set_rankup_channel(11, 456) assert self.database.get_rankup_channel_id(11) == 456 def test_resetEveryoneToken(self): self.database.add_member(1) self.database.add_member(2) self.database.add_member(3) self.database.reset_everyone_token() assert self.database.get_member(1).token == 100 assert self.database.get_member(2).token == 100 assert self.database.get_member(3).token == 100
class TestRoleUtil(): def setup_method(self, method): self.database = KfpDb(":memory:") pass def teardown_method(self, method): self.database.teardown() pass def test_updateRoleCreateNewOne(self): role: KfpRole = RoleUtil.updateRole(123, 321, "testing", "0x000345") assert role.guild_id == 123 assert role.role_id == 321 assert role.role_name == "testing" assert role.color == "0x000345" def test_updateRoleUpdateOldOne(self): RoleUtil.updateRole(123, 321, "testing", "0x000345") role: KfpRole = RoleUtil.updateRole(123, 321, "onetwothree", "0x000543") assert role.role_name == "onetwothree" assert role.color == "0x000543" def test_updateKfpRoleLevelZero(self): RoleUtil.updateRole(123, 321, "testing", "0x000345") role: KfpRole = RoleUtil.getRole(123, 321) assert role.level == 0 def test_updateKfpRoleLevelSuccess(self): RoleUtil.updateRole(123, 321, "testing", "0x000345") role: KfpRole = RoleUtil.getRole(123, 321) RoleUtil.updateKfpRoleLevel(role, 20) assert role.level == 20 def test_getCurrentRolesEmpty(self): roleList = RoleUtil.getCurrentRoles(123) assert len(roleList) == 0 def test_getCurrentRolesSuccess(self): role1 = RoleUtil.updateRole(123, 321, "testing", "0x000345") role2 = RoleUtil.updateRole(123, 123, "onetwothree", "0x000543") roleList = RoleUtil.getCurrentRoles(123) assert len(roleList) == 2 assert roleList[0] == role1 assert roleList[1] == role2 def test_getCurrentRolesSuccess_level(self): role1 = RoleUtil.updateRole(123, 100, "first", "0x000345", Util.RoleCategory.KFP_DEFAULT) RoleUtil.updateKfpRoleLevel(role1, 0) role2 = RoleUtil.updateRole(123, 101, "second", "0x000543", Util.RoleCategory.KFP_DEFAULT) RoleUtil.updateKfpRoleLevel(role2, 20) role3 = RoleUtil.updateRole(123, 102, "third", "0x000543", Util.RoleCategory.KFP_DEFAULT) RoleUtil.updateKfpRoleLevel(role3, 40) role4 = RoleUtil.updateRole(123, 103, "fourth", "0x000543", Util.RoleCategory.KFP_DEFAULT) RoleUtil.updateKfpRoleLevel(role4, 60) role5 = RoleUtil.updateRole(123, 104, "fifth", "0x000543", Util.RoleCategory.KFP_DEFAULT) RoleUtil.updateKfpRoleLevel(role5, 80) role6 = RoleUtil.updateRole(123, 105, "sixth", "0x000543", Util.RoleCategory.KFP_DEFAULT) RoleUtil.updateKfpRoleLevel(role6, 100) role = RoleUtil.getKfpRoleFromLevel(123, 80) assert role == role5 def test_getRoleBeforeLevel(self): role1 = RoleUtil.updateRole(123, 321, "testing", "0x000345", Util.RoleCategory.KFP_DEFAULT) RoleUtil.updateKfpRoleLevel(role1, 10) role2 = RoleUtil.updateRole(123, 123, "onetwothree", "0x000543", Util.RoleCategory.KFP_DEFAULT) RoleUtil.updateKfpRoleLevel(role2, 20) role = RoleUtil.getKfpRolesBeforeLevel(123, 9) assert len(role) == 0 role = RoleUtil.getKfpRolesBeforeLevel(123, 11) assert role[0] == role1 role = RoleUtil.getKfpRolesBeforeLevel(123, 20) assert role[0] == role1 role = RoleUtil.getKfpRolesBeforeLevel(123, 21) assert role[0] == role2 assert role[1] == role1 def test_getRoleFromLevel(self): role0 = RoleUtil.updateRole(123, 100, "other roles", "0x000345") RoleUtil.updateKfpRoleLevel(role0, 0) role1 = RoleUtil.updateRole(123, 321, "testing", "0x000345", Util.RoleCategory.KFP_DEFAULT) RoleUtil.updateKfpRoleLevel(role1, 10) role2 = RoleUtil.updateRole(123, 123, "onetwothree", "0x000543", Util.RoleCategory.KFP_DEFAULT) RoleUtil.updateKfpRoleLevel(role2, 20) role = RoleUtil.getKfpRoleFromLevel(123, 9) assert not role role = RoleUtil.getKfpRoleFromLevel(123, 11) assert role == role1 role = RoleUtil.getKfpRoleFromLevel(123, 20) assert role == role2