class ApiTest(TestCase): """ API tests for kingdom projects. This is not complete. It only checks the main api returns, and does not check the other API endpoints. The rationale here is the endpoints only do glue code, and all the logic is done at the signal level. """ def setUp(self): self.u = User(username="******") self.u.set_password("pwd") self.u.save() self.k = Kingdom(user=self.u) self.k.save() self.c = Client() self.c.login(username=self.u.username, password="******") def test_api_is_up(self): """ Check API calls returns with status 200 """ r = self.c.get(reverse('kingdom.views.api.api')) self.assertEqual(200, r.status_code)
class ScriptTest(TestCase): """ Unit tests for event app """ def setUp(self): self.k = Kingdom() self.k.save() self.c = EventCategory( frequency=1, timeout=1, ) self.c.save() self.e = Event( name="Event 1", slug="event_1", category=self.c, text="Event 1", on_fire="" ) self.e.save() self.a = EventAction( event=self.e, text="some text", on_fire="", ) self.a.save() def test_kingdom_create_pending_event(self): """ Check the pending event is created and not started """ pe = self.k.create_pending_event("event_1") self.assertIsNone(pe.started) self.assertEqual(pe.event_id, self.e.pk) self.assertEqual(self.k.pendingevent_set.count(), 1) def test_kingdom_start_pending_event(self): """ Check the pending event is created and started """ pe = self.k.start_pending_event("event_1") self.assertIsNotNone(pe.started) self.assertEqual(pe.event_id, self.e.pk) self.assertEqual(self.k.pendingevent_set.count(), 1)
def test_target(self): """ A target can be defined """ self.m.has_target = True self.m.save() k2 = Kingdom() k2.save() r = self.c.post(reverse('mission.views.pending_mission_set_target', args=(self.pm.pk,)), {'target': k2.pk}) self.assertEqual(200, r.status_code) self.assertEqual(PendingMission.objects.get(pk=self.pm.pk).target, k2)
def test_only_two_kingdoms_per_pending_bargain(self): """ Can't have more than two kingdoms on the same bargain """ k3 = Kingdom() k3.save() pbk3 = PendingBargainKingdom( pending_bargain=self.pb, kingdom=k3 ) self.assertRaises(ValidationError, pbk3.save)
def test_title_folk_kingdom(self): """ Folk must be in the AvailableTitle kingdom. """ k2 = Kingdom() k2.save() at = AvailableTitle( title=self.t, kingdom=k2, folk=self.f ) self.assertRaises(ValidationError, at.save)
def setUp(self): self.k1 = Kingdom() self.k1.save() self.k2 = Kingdom() self.k2.save() self.f = Folk(kingdom=self.k1) self.f.save() self.m = Mission( name="Stub mission", slug="stub", on_resolution="", ) self.m.save() self.mg = MissionGrid( mission=self.m, allow_empty=True, ) self.mg.save() self.pb = PendingBargain() self.pb.save() self.pbk1 = PendingBargainKingdom( pending_bargain=self.pb, kingdom=self.k1 ) self.pbk1.save() self.pbk2 = PendingBargainKingdom( pending_bargain=self.pb, kingdom=self.k2 ) self.pbk2.save() self.pm = PendingMission( kingdom=self.k1, mission=self.m ) self.pm.save() self.pbsm = PendingBargainSharedMission( pending_mission=self.pm, pending_bargain=self.pb ) self.pbsm.save()
def setUp(self): self.k = Kingdom() self.k.save() self.c = EventCategory( frequency=1, timeout=1, ) self.c.save() self.e = Event( name="Event 1", slug="event_1", category=self.c, text="Event 1", on_fire="" ) self.e.save() self.a = EventAction( event=self.e, text="some text", on_fire="", ) self.a.save()
class UnitTest(TestCase): def setUp(self): self.k = Kingdom() self.k.save() @override_settings(DEBUG=True) def test_scriptlog(self): """ Scriptlog store all relevant datas. We need to override_settings with DEBUG=True as the test runner automatically deactivate this option. """ f = Function( slug="create_lastname", ) f.on_fire = """ # Does one direct query LastName(name="la veuve des sept lieux").save() """ f.save() f2 = Function( slug="count_and_create_lastname", ) f2.on_fire = """ # Does 2 direct queries, 1 indirect LastName.objects.count() create_lastname.fire() LastName.objects.count() """ f2.save() # Sanity check self.assertEqual(0, ScriptLog.objects.count()) # Do 3 queries f2.fire(create_lastname=f) self.assertEqual(2, ScriptLog.objects.count()) # First log is the innermost function self.assertEqual(1, ScriptLog.objects.all()[0].direct_queries) self.assertEqual(1, ScriptLog.objects.all()[0].queries) self.assertEqual(2, ScriptLog.objects.all()[1].direct_queries) self.assertEqual(3, ScriptLog.objects.all()[1].queries)
def test_claim_unicity(self): """ You can't have two claims on the same kingdom. """ k2 = Kingdom() k2.save() Claim( offender=self.k, offended=k2 ).save() c = Claim( offender=self.k, offended=k2 ) self.assertRaises(IntegrityError, c.save)
def setUp(self): self.k = Kingdom() self.k.save() self.f = Folk( kingdom=self.k, ) self.f.save()
def setUp(self): self.k = Kingdom() self.k.save() self.f = Folk(first_name="bob", kingdom=self.k) self.f.save() self.m = Mission(name="Stub mission", slug="stub", on_resolution="") self.m.save()
def test_kingdom_add_claim(self): """ Verify if the claim is created """ self.k2 = Kingdom() self.k2.save() self.k.add_claim(self.k2, Claim.REACHABLE) self.assertEqual(self.k, Claim.objects.get(offender=self.k2, level=Claim.REACHABLE).offended)
def test_sanity_folk_in_kingdoms(self): """ The folk in PendingBargainSharedMissionAffectation must be owned by one of the sides of the negotiation. """ k3 = Kingdom() k3.save() f2 = Folk(kingdom=k3) f2.save() pbsma = PendingBargainSharedMissionAffectation( pending_bargain_shared_mission=self.pbsm, mission_grid=self.mg, folk=f2 ) # folk.kingdom != pbk.kingdom self.assertRaises(IntegrityError, pbsma.save)
def setUp(self): self.k = Kingdom() self.k.save() self.f = Folk( kingdom=self.k, first_name="Robert", last_name="Baratheon" ) self.f.save()
def test_mission_target_default(self): """ Check the target with default code (all kingdom but mine) """ k2 = Kingdom( money=50000 ) k2.save() self.m.has_target = True self.m.save() # Assert noRaises self.pm.target = k2 self.pm.save() # Our kingdom is forbidden by default self.pm.target = self.k self.assertRaises(ValidationError, self.pm.save)
def test_mission_target_list_code(self): """ Check the target_list code is runned on affectation """ self.m.has_target = True self.m.target_list = "param = Kingdom.objects.filter(money__gte=10000)" self.m.save() # Check : no kingdom matches. self.assertEqual(len(self.pm.targets()), 0) k2 = Kingdom( money=50000 ) k2.save() # Check : k2 matches self.assertEqual(len(self.pm.targets()), 1) self.assertEqual(self.pm.targets()[0], k2)
def setUp(self): self.u = User(username="******") self.u.set_password("pwd") self.u.save() self.k = Kingdom(user=self.u) self.k.save() self.c = Client() self.c.login(username=self.u.username, password="******")
def test_folk_kingdom_change(self): """ When you change kingdom, you're disaffected from your title. """ at = AvailableTitle( title=self.t, kingdom=self.k, folk=self.f ) at.save() k2 = Kingdom() k2.save() self.f.kingdom = k2 self.f.save() # Reload the title, folk should now be empty at = AvailableTitle.objects.get(pk=at.pk) self.assertIsNone(at.folk)
def test_claim_level(self): """ Claim level should be REACHABLE, CLAIM or VENDETTA. """ k2 = Kingdom() k2.save() c = Claim(offender=self.k, offended=k2) c.save() c.level = c.REACHABLE c.save() c.level = c.CLAIM c.save() c.level = c.VENDETTA c.save() c.level = 10 self.assertRaises(ValidationError, c.save)
def test_sanity_pending_mission_in_kingdoms(self): """ A PendingBargainSharedMission must be owned by one of the sides of the negotiation """ k3 = Kingdom() k3.save() pm = PendingMission( kingdom=k3, mission=self.m ) pm.save() pbsm2 = PendingBargainSharedMission( pending_mission=pm, pending_bargain=self.pb ) # pm.kingdom != pbk.kingdom self.assertRaises(IntegrityError, pbsm2.save)
def test_cant_update_target_after_mission_start(self): """ The target can't be changed after mission start. """ self.m.has_target = True self.m.save() k2 = Kingdom() k2.save() k3 = Kingdom() k3.save() # Sanity check self.pm.target = k2 self.pm.save() self.pm.target = k3 self.pm.save() self.pm.started = datetime.now() self.pm.save() # Can't change target self.pm.target = k2 self.assertRaises(ValidationError, self.pm.save)
def test_token_pending_event_sanity(self): """ Check kingdoms in token and pending_event are similar """ pe = PendingEvent( event=self.e, kingdom=self.k, started=None ) pe.save() k2 = Kingdom() k2.save() pet = PendingEventToken( kingdom=k2, pending_event=pe, category=self.c ) self.assertRaises(IntegrityError, pet.save)
def test_has_claim(self): """ Checks if the has_claim works """ self.k3 = Kingdom() self.k3.save() self.assertIsNone(self.k.has_claim(self.k3)) self.k.add_claim(self.k3, Claim.REACHABLE) self.assertEqual(Claim.REACHABLE, self.k.has_claim(self.k3))
def setUp(self): self.k = Kingdom() self.k.save() self.f = Folk( kingdom=self.k, ) self.f.save() self.qc = QualityCategory( name="Stub quality category", description="A stub." ) self.qc.save()
def test_mission_resolution_with_target(self): """ Check the on_resolution code works with a target. """ k2 = Kingdom() k2.save() self.m.has_target = True self.m.on_resolution = """ if target.pk == %s: status='mission_solved' """ % k2.pk self.m.save() self.pm.target = k2 self.pm.save() self.pm.started = datetime.now() self.pm.save() status = self.pm.resolve() self.assertEqual(status, 'mission_solved')
def setUp(self): self.k = Kingdom() self.k.save() self.f = Folk( kingdom=self.k, ) self.f.save() self.m = Message( kingdom=self.k, content="some content" ) self.m.save()
def setUp(self): self.k = Kingdom() self.k.save() self.f = Folk( kingdom=self.k, ) self.f.save() self.t = Title( name="Dummy title", slug="dummy_title", description="Nothing to say." ) self.t.save()
def setUp(self): self.u = User(username="******") self.u.set_password("pwd") self.u.save() self.k = Kingdom(user=self.u) self.k.save() self.f = Folk( kingdom=self.k, ) self.f.save() self.t = Title( name="Stub title", description="My description.") self.t.save() self.at = AvailableTitle( title=self.t, kingdom=self.k, folk=self.f ) self.at.save() self.m = Mission( name="Stub mission", slug="stub", on_resolution="", title=self.t, ) self.m.save() self.mg = MissionGrid( mission=self.m, slug='stub_grid', allow_empty=True ) self.mg.save() self.pm = PendingMission( mission=self.m, kingdom=self.k ) self.pm.save() self.c = Client() self.c.login(username=self.u.username, password="******")
def setUp(self): self.k = Kingdom() self.k.save() self.f = Folk( kingdom=self.k, ) self.f.save() self.t = Title( name="Stub title", description="My description.") self.t.save() self.at = AvailableTitle( title=self.t, kingdom=self.k, folk=self.f ) self.at.save() self.m = Mission( name="Stub mission", slug="stub", on_resolution="", title=self.t, ) self.m.save() self.mg = MissionGrid( mission=self.m, slug='stub_grid' ) self.mg.save() self.pm = PendingMission( mission=self.m, kingdom=self.k ) self.pm.save() self.pma = PendingMissionAffectation( pending_mission=self.pm, mission_grid=self.mg, folk=self.f ) self.pma.save()
def setUp(self): self.k = Kingdom() self.k.save() self.f = Folk( kingdom=self.k, first_name="Robert", last_name="Baratheon" ) self.f.save() self.t = Trigger( slug='Trigger_internal_test', name='Trigger_internal_test', prestige_threshold=10, population_threshold=10, money_threshold=10, ) self.t.save()