def test_group_does_not_have_codes(self): ctx = FakeContext() PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') asyncio.run(list_code(ctx, group_name='foo')) self.assertTrue(ctx.send_called) self.assertEqual(ctx.send_parameters, "Grupo foo não possui códigos")
def test_code_does_not_exist(self): ctx = FakeContext() PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') asyncio.run(remove_code(ctx, group_name='foo', code='ASDF-1234')) self.assertTrue(ctx.send_called) self.assertEqual(ctx.send_parameters, "Código ASDF-1234 não encontrado no grupo foo")
def test_group_exist_in_another_guild(self): ctx = FakeContext() guild2 = FakeGuild2() PromoCodeGroup.create(guild_id=guild2.id, name='foo') asyncio.run(list_code(ctx, group_name='foo')) self.assertTrue(ctx.send_called) self.assertEqual(ctx.send_parameters, "Grupo foo não existe")
def test_user_does_not_exist_in_this_guild(self): ctx = FakeContext() guild2 = FakeGuild2() PromoCodeGroup.create(guild_id=guild2.id, name='foo') asyncio.run(remove_group(ctx, group_name='foo')) self.assertTrue(ctx.send_called) self.assertEqual(ctx.send_parameters, "Grupo foo não existe!")
def test_group_exists(self): ctx = FakeContext() PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') asyncio.run(remove_group(ctx, group_name='foo')) self.assertTrue(ctx.send_called) self.assertEqual(ctx.send_parameters, "Grupo foo removido")
def test_group_does_not_exist_in_this_guild(self): ctx = FakeContext() guild2 = FakeGuild2() PromoCodeGroup.create(guild_id=guild2.id, name='foo') asyncio.run(add_code(ctx, group_name='foo', code='ASDF-1234')) self.assertTrue(ctx.send_called) self.assertEqual(ctx.send_parameters, "Grupo de códigos promocionais não encontrado: foo")
def test_has_groups_in_different_guild(self): ctx = FakeContext() guild2 = FakeGuild2() PromoCodeGroup.create(guild_id=guild2.id, name='foo') asyncio.run(list_group(ctx)) self.assertTrue(ctx.send_called) self.assertEqual(ctx.send_parameters, "Não há grupos de código promocional cadastrados")
def test_group_exists_in_this_guild(self): ctx = FakeContext() PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') asyncio.run(add_code(ctx, group_name='foo', code='ASDF-1234')) self.assertTrue(ctx.send_called) self.assertEqual( ctx.send_parameters, "Código ASDF-1234 cadastrado no grupo foo com sucesso!" )
def test_invalid_code(self): ctx = FakeContext() PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') asyncio.run(add_code(ctx, group_name='foo', code='ASDF$1234')) self.assertTrue(ctx.send_called) self.assertEqual( ctx.send_parameters, "Código inválido: o código deve ser apenas letras, números e traços (-)" # noqa E501 )
def test_has_results_in_same_guild(self): ctx = FakeContext() PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') asyncio.run(list_group(ctx)) self.assertTrue(ctx.send_called) self.assertEqual( ctx.send_parameters, "Estes são os grupos de código promocional existentes: \n- foo")
async def add_group(ctx, group_name): """Creates a group of promo codes. No code can live outside of a group (they get lonely!).""" logging.info("Tentando adicionar grupo '%s'", group_name) if not validate_group_name(group_name): await ctx.send( "Nome de grupo inválido. Use apenas letras, números, traços (-) e underscore (_)" ) # noqa E501 return try: PromoCodeGroup.create(guild_id=ctx.guild.id, name=group_name) await ctx.send("Grupo {} criado".format(group_name)) except IntegrityError: await ctx.send("Grupo já existente")
def test_group_exists_in_this_guild(self): ctx = FakeContext() PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') asyncio.run(add_code_bulk(ctx, group_name='foo', code_bulk='ASDF-1234 QWER-5678,ZXCV-9012')) self.assertTrue(ctx.send_called) self.assertEqual(ctx.send_parameters, "Códigos adicionados ao grupo foo") promo_codes = PromoCode.select() resulting_codes = ['ASDF-1234', 'QWER-5678', 'ZXCV-9012'] for (promo_code, resulting_code) in zip(promo_codes, resulting_codes): self.assertEqual(promo_code.code, resulting_code)
def test_group_has_code_and_user_has_not_yet_received_one(self): ctx = FakeContext() user = FakeUser() group = PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') promo_code = PromoCode.create(group=group, code='ASDF-1234') asyncio.run(send_code(ctx, group_name='foo', users=[user], is_authorized_or_owner=returns_false)) self.assertTrue(ctx.author.send_called) self.assertEqual( ctx.author.send_parameters, "Código {} enviado para o usuário {}".format(promo_code.code, user.name) ) self.assertTrue(user.send_called) self.assertEqual( user.send_parameters, "Olá! Você ganhou um código: {}".format(promo_code.code) ) saved_promo_code = PromoCode.get(group=group, code=promo_code.code) self.assertEqual(saved_promo_code.sent_to_name, user.name) self.assertEqual(saved_promo_code.sent_to_id, user.id) self.assertIsNotNone(saved_promo_code.sent_at)
def test_code_already_registered(self): ctx = FakeContext() group = PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') PromoCode.create(group=group, code="ASDF-1234") asyncio.run(add_code(ctx, group_name='foo', code='ASDF-1234')) self.assertTrue(ctx.send_called) self.assertEqual(ctx.send_parameters, "Código ASDF-1234 já cadastrado no grupo foo")
def test_delete_group_removes_its_keys(self): ctx = FakeContext() group = PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') PromoCode.create(group=group, code='ASDF-1234') asyncio.run(remove_group(ctx, group_name='foo')) self.assertTrue(ctx.send_called) self.assertEqual(ctx.send_parameters, "Grupo foo removido") self.assertEqual(PromoCode.select().count(), 0)
def test_code_exists_in_current_guild(self): ctx = FakeContext() group = PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') PromoCode.create(group=group, code="ASDF-1234") asyncio.run(remove_code(ctx, group_name='foo', code='ASDF-1234')) self.assertTrue(ctx.send_called) self.assertEqual(ctx.send_parameters, "Código ASDF-1234 excluído do grupo foo")
def test_group_has_codes(self): ctx = FakeContext() group = PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') PromoCode.create(group=group, code='ASDF-1234') asyncio.run(list_code(ctx, group_name='foo')) self.assertTrue(ctx.author.send_called) self.assertEqual(ctx.author.send_parameters, "Códigos para o grupo foo: \n- ASDF-1234")
def test_code_exists_in_another_guild(self): ctx = FakeContext() guild2 = FakeGuild2() group = PromoCodeGroup.create(guild_id=guild2.id, name='foo') PromoCode.create(group=group, code="ASDF-1234") asyncio.run(remove_code(ctx, group_name='foo', code='ASDF-1234')) self.assertTrue(ctx.send_called) self.assertEqual(ctx.send_parameters, "Código ASDF-1234 não encontrado no grupo foo")
def test_group_has_code_and_user_has_already_received_one(self): ctx = FakeContext() user = FakeUser() group = PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') PromoCode.create(group=group, code='ASDF-1234', sent_to_id=user.id) asyncio.run(send_code(ctx, group_name='foo', users=[user], is_authorized_or_owner=returns_false)) self.assertTrue(ctx.send_called) self.assertEqual( ctx.send_parameters, "Usuário {0} já resgatou código do grupo {1}".format(user.name, 'foo') )
def test_group_doesnt_have_codes_available(self): ctx = FakeContext() user = FakeUser() user2 = FakeUser2() group = PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') PromoCode.create(group=group, code='ASDF-1234', sent_to_name=user.name, sent_to_id=user.id) asyncio.run(send_code(ctx, group_name='foo', users=[user2], is_authorized_or_owner=returns_false)) self.assertTrue(ctx.author.send_called) self.assertEqual( ctx.send_parameters, "Grupo {} não possui mais códigos disponíveis".format( group.name ) )
def test_user_has_codes(self): ctx = FakeContext() author = ctx.author sent_at = datetime.now() group = PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') promo_code = PromoCode.create( group=group, code='ASDF-1234', sent_to_id=author.id, sent_to_name=author.name, sent_at=sent_at ) asyncio.run(my_codes(ctx)) self.assertTrue(ctx.author.send_called) self.assertEqual( ctx.author.send_parameters, "Seus códigos: \n- {0} (recebido em {1})".format( promo_code.code, sent_at.astimezone(LOCAL_TIMEZONE).strftime(DATETIME_FORMAT) ) )
def test_group_has_sent_codes(self): ctx = FakeContext() user = FakeUser() group = PromoCodeGroup.create(guild_id=ctx.guild.id, name='foo') sent_at = datetime.now() promo_code = PromoCode.create( group=group, code='ASDF-1234', sent_to_name=user.name, sent_to_id=user.id, sent_at=sent_at ) asyncio.run(list_code(ctx, group_name='foo')) self.assertTrue(ctx.author.send_called) self.assertEqual( ctx.author.send_parameters, "Códigos para o grupo foo: \n- {0} enviado para o usuário {1} em {2}".format( # noqa E501 promo_code.code, user.name, sent_at.astimezone(LOCAL_TIMEZONE).strftime(DATETIME_FORMAT) ) )