Esempio n. 1
0
	def test_folk_unicity(self):
		"""
		A folk can only be affected in one Title at a time.
		"""
		at = AvailableTitle(
			title=self.t,
			kingdom=self.k,
			folk=self.f
		)
		at.save()

		t2 = Title(
			name="Dummy title2",
			slug="dummy_title2",
			description="Nothing to say."
		)
		t2.save()

		at2 = AvailableTitle(
			title=t2,
			kingdom=self.k,
			folk=self.f
		)
		
		self.assertRaises(IntegrityError, at2.save)
Esempio n. 2
0
 def create(self, validated_data):
     print(validated_data)
     titleName = validated_data.pop('title')
     user = super(UserSerializer,
                  self).create(validated_data=validated_data)
     user.set_password(validated_data['password'])
     user.save()
     title = Title(title_name=titleName, user=user)
     title.save()
     category = Category.objects.get(name='320症状')
     entryInfo = EntryInfo(category=category, title=title, user=user)
     entryInfo.save()
     return user
Esempio n. 3
0
def book_detail(request, title_id):
    data = Title.objects.filter(id__exact=title_id).first()
    detail = Title.book_info_as_dict(data)
    return render(request, 'site/book/detail.html', {
        'detail': detail,
        'title_id': title_id
    })
Esempio n. 4
0
	def test_templates_and_title_context(self):
		"""
		Check templating works on event and EventAction with default context title.
		"""
		from title.models import Title, AvailableTitle
		t = Title(name="Maître espion", slug="maitre_espion", description=" ")
		t.save()
		f = Folk(first_name="septon", kingdom=self.k)
		f.save()
		at = AvailableTitle(kingdom=self.k, title=t, folk=f)
		at.save()

		self.e.text = "{{ title.maitre_espion.first_name }}"
		self.e.save()

		pe = PendingEvent(
			event=self.e,
			kingdom=self.k,
		)
		pe.save()

		self.assertEqual(pe.text, "septon")
Esempio n. 5
0
	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()
Esempio n. 6
0
	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="******")
Esempio n. 7
0
def get_recommendation_hybrid(request):
    if not request.user.is_authenticated:
        return JsonResponse({'status': 401, 'error': "Please login to get suggestion"})
    current_user_id = 5
    title_rs_df = pd.DataFrame(rs.get_top_recs_using_hybrid(current_user_id))
    title_id_list = list(title_rs_df.title_id)
    title_list = Title.objects.filter(id__in=title_id_list)
    title_list = [Title.book_info_as_dict(book) for book in title_list]
    rs_df = pd.DataFrame.from_dict(title_list)
    rs_df = pd.merge(rs_df, title_rs_df, left_on='id', right_on='title_id')
    rs_df = rs_df.sort_values(by=['rating'], ascending=False).reset_index(
        drop=True)
    json_data = list(rs_df.apply(lambda x: x.to_json(), axis=1))
    # print(json_data)
    data = {'book_list': json.dumps(json_data)}
    return JsonResponse({'status': 200, 'data': data})
Esempio n. 8
0
	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()
Esempio n. 9
0
def get_recommendation_cb(request):
    title_id = request.GET["title_id"]
    book_id_list = rs.get_top_recs_using_content_based(title_id)
    string_book_list = str(book_id_list).replace('[', '').replace(']', '')
    query = """
        SELECT `title`.* FROM `title`
        LEFT JOIN `book_similarity`
        ON `title`.`id` = `book_similarity`.`target`
        WHERE `title`.id IN ({0}) and `book_similarity`.source = {1}
        ORDER BY `book_similarity`.similarity DESC;
    """
    query = query.format(string_book_list, title_id)
    book_list = Title.objects.raw(query)
    # book_list = Title.objects.filter(id__in=book_id_list)
    book_list = [Title.book_info_as_dict(book) for book in book_list]
    data = {'book_list': json.dumps(book_list)}
    return JsonResponse({'status': 200, 'data': data})
Esempio n. 10
0
def get_popular_book(request):
    popular_book = rs.get_popular_book()
    popular_book_list = Title.objects.all().filter(id__in=popular_book)
    popular_book_list = [Title.book_info_as_dict(book) for book in popular_book_list]
    data = {'popular_book_list': json.dumps(popular_book_list)}
    return JsonResponse({'status': 200, 'data': data})
 def load_data():
     db_book_list = Title.objects.all().order_by('id')
     db_book_list = [Title.book_info_as_dict(book) for book in db_book_list]
     db_book_list = pd.DataFrame.from_dict(db_book_list, orient='columns')
     db_book_list['type'] = 'database'
     return db_book_list
Esempio n. 12
0
class ApiTest(TestCase):
	"""
	API tests for missions
	"""

	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 test_grid_affect(self):
		"""
		A folk can be affected
		"""

		r = self.c.post(reverse('mission.views.pending_mission_grid_affect', args=(self.pm.pk, self.mg.pk)), {'folk': self.f.pk})
		self.assertEqual(200, r.status_code)
		self.assertEqual(PendingMissionAffectation.objects.all().count(), 1)

	def test_grid_defect(self):
		"""
		A folk can be defected
		"""
		pma = PendingMissionAffectation(
			pending_mission=self.pm,
			mission_grid=self.mg,
			folk=self.f
		)
		pma.save()

		r = self.c.post(reverse('mission.views.pending_mission_grid_defect', args=(pma.pk,)))
		self.assertEqual(200, r.status_code)
		self.assertEqual(PendingMissionAffectation.objects.all().count(), 0)

	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_value(self):
		"""
		A value can be defined
		"""
		self.m.has_value = True
		self.m.save()

		r = self.c.post(reverse('mission.views.pending_mission_set_value', args=(self.pm.pk,)), {'value': 100})
		self.assertEqual(200, r.status_code)
		self.assertEqual(PendingMission.objects.get(pk=self.pm.pk).value, 100)

	def test_start(self):
		"""
		PendingMission can be started
		"""
		r = self.c.post(reverse('mission.views.pending_mission_start', args=(self.pm.pk,)))
		self.assertEqual(200, r.status_code)
		self.assertIsNotNone(PendingMission.objects.get(pk=self.pm.pk).started)

	def test_cancel(self):
		"""
		PendingMission can be cancelled
		"""
		r = self.c.post(reverse('mission.views.pending_mission_cancel', args=(self.pm.pk,)))
		self.assertEqual(200, r.status_code)
		self.assertRaises(PendingMission.DoesNotExist, lambda: PendingMission.objects.get(pk=self.pm.pk))

	def test_availablemission_start(self):
		"""
		AvailableMission can be started
		"""
		am = AvailableMission(
			mission=self.m,
			kingdom=self.k
		)
		am.save()

		r = self.c.post(reverse('mission.views.available_mission_start', args=(am.pk,)))
		self.assertEqual(200, r.status_code)
		self.assertEqual(PendingMission.objects.all().count(), 2)
Esempio n. 13
0
class UnitTest(TestCase):
	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 test_cant_affect_twice(self):
		"""
		A folk can't be affected twice to a mission.
		"""

		pma2 = PendingMissionAffectation(
			pending_mission=self.pm,
			mission_grid=self.mg,
			folk=self.f
		)

		self.assertRaises(IntegrityError, pma2.save)

	def test_cant_affect_disabled(self):
		"""
		By default, grids do not allow disabled to be affected.
		"""

		self.pma.delete()
		self.f.disabled = True
		self.f.save()

		pma = PendingMissionAffectation(
			pending_mission=self.pm,
			mission_grid=self.mg,
			folk=self.f
		)

		self.assertRaises(ValidationError, pma.save)

	def test_grid_allow_disabled(self):
		"""
		Specific grids allows disabled folks.
		"""

		self.pma.delete()
		self.f.disabled = True
		self.f.save()
		self.mg.allow_disabled = True
		self.mg.save()

		pma = PendingMissionAffectation(
			pending_mission=self.pm,
			mission_grid=self.mg,
			folk=self.f
		)
		pma.save()

	def test_cant_affect_dead(self):
		"""
		A folk can't be affected when he is dead.
		"""

		self.pma.delete()
		self.f.die()

		pma = PendingMissionAffectation(
			pending_mission=self.pm,
			mission_grid=self.mg,
			folk=self.f
		)

		self.assertRaises(ValidationError, pma.save)

	def test_auto_remove_on_death(self):
		"""
		Dead peoples are removed from their affectations
		"""

		self.f.die()
		self.assertRaises(PendingMissionAffectation.DoesNotExist, lambda: PendingMissionAffectation.objects.get(pk=self.pma.pk))

	def test_cant_remove_after_mission_start(self):
		"""
		A folk can't be removed from a mission after it started.
		"""

		# Start the pendingmission
		self.pm.started = datetime.now()
		self.pm.save()

		# Can't remove affectation
		self.assertRaises(ValidationError, self.pma.delete)

	def test_cant_affect_after_mission_start(self):
		"""
		A folk can't be affected to a mission after it started.
		"""

		# Start the pendingmission
		self.pm.started = datetime.now()
		self.pm.save()

		f2 = Folk(
			first_name="Hot",
			last_name="Pie",
			kingdom=self.k
		)
		f2.save()

		pma2 = PendingMissionAffectation(
			pending_mission=self.pm,
			mission_grid=self.mg,
			folk=f2
		)

		# Can't affect after start
		self.assertRaises(ValidationError, pma2.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_cant_update_value_after_mission_start(self):
		"""
		The value can't be changed after mission start.
		"""

		self.m.has_value = True
		self.m.save()

		# Sanity check
		self.pm.value = 10
		self.pm.save()

		self.pm.started = datetime.now()
		self.pm.save()

		# Can't change value
		self.pm.value = 20
		self.assertRaises(ValidationError, self.pm.save)

	def test_grid_condition(self):
		"""
		Check condition is triggered.
		"""

		self.pma.delete()
		self.mg.condition = """
status="not_allowed"
"""
		self.mg.save()

		self.pma = PendingMissionAffectation(
			pending_mission=self.pm,
			mission_grid=self.mg,
			folk=self.f
		)

		# Can't affect folk
		self.assertRaises(ValidationError, self.pma.save)

	def test_grid_length(self):
		"""
		Check grid length constraint.
		"""

		self.mg.length = 1
		self.mg.save()

		f2 = Folk(
			kingdom=self.k,
			first_name="Cersei",
			last_name="Lannister"
		)
		f2.save()

		pma2 = PendingMissionAffectation(
			pending_mission=self.pm,
			mission_grid=self.mg,
			folk=f2
		)

		# Too many people
		self.assertRaises(ValidationError, pma2.save)

		# Update length, now can be saved
		self.mg.length = 2
		self.mg.save()
		pma2.save()

	def test_emptygrids_not_allowed(self):
		"""
		Check that a grid with argument allow_empty to False (default) raises an error if no one is in it
		"""

		self.pma.delete()

		self.pm.started = datetime.now()
		self.assertRaises(ValidationError, self.pm.save)

	def test_emptygrids_allowed(self):
		"""
		Check that a grid with argument allow_empty can have no one in it
		"""
		
		self.mg.allow_empty = True
		self.mg.save()

		self.pma.delete()

		self.pm.started = datetime.now()
		self.pm.save()

	def test_sanity_grid_is_from_mission(self):
		"""
		Check the grid is part of the current mission.
		"""

		m2 = Mission(
			name="Stub mission2",
			on_resolution="",
			title=self.t,
		)
		m2.save()

		pm2 = PendingMission(
			mission=m2,
			kingdom=self.k
		)
		pm2.save()

		# pending_mission refers to mission2,
		# mission_grid refers to mission
		pma2 = PendingMissionAffectation(
			pending_mission=pm2,
			mission_grid=self.mg,
			folk=self.f
		)

		self.assertRaises(IntegrityError, pma2.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 test_mission_target_in_list(self):
		"""
		Check the target is in target_list
		"""

		self.m.has_target = True
		self.m.target_list = "param = Kingdom.objects.filter(money__gte=10000)"
		self.m.save()

		# Invalid assignment, our kingdom does not match condition
		self.pm.target = self.k
		self.assertRaises(ValidationError, self.pm.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_allowed(self):
		"""
		Check the target is allowed on affectation.
		"""

		# Invalid assignment, self.m does not define has_target=True
		self.pm.target = self.k
		self.assertRaises(ValidationError, self.pm.save)

	def test_mission_value_allowed(self):
		"""
		Check the value is allowed on affectation.
		"""

		# Invalid assignment, self.m does not define has_value=True
		self.pm.value = 100
		self.assertRaises(ValidationError, self.pm.save)

	def test_mission_target_provided(self):
		"""
		Check target is not None if mission has_target
		"""

		self.m.has_target = True
		self.m.save()

		self.pm.started = datetime.now()
		self.assertRaises(ValidationError, self.pm.save)

	def test_mission_on_init(self):
		"""
		Check the on_init code can cancel the mission before it is launched.
		"""

		m2 = Mission(
			name="Stub mission2",
			slug="stub_2",
			on_resolution="",
			on_init="status='Abort Mission'",
			title=self.t
		)
		m2.save()

		pm2 = PendingMission(
			mission=m2,
			kingdom=self.k
		)

		self.assertRaises(ValidationError, pm2.save)

	def test_mission_on_start_title(self):
		"""
		Check you can't start without a folk in associated title.
		"""

		self.at.folk = None
		self.at.save()

		self.pm.started = datetime.now()
		self.assertRaises(ValidationError, self.pm.save)

	def test_mission_on_start(self):
		"""
		Check the on_start code.
		"""

		m2 = Mission(
			name="Stub mission2",
			slug="stub_2",
			on_resolution="",
			on_start="""
Kingdom().save()
""",
			title=self.t,
		)
		m2.save()

		# Sanity check
		self.assertEqual(Kingdom.objects.count(), 1)

		pm2 = PendingMission(
			mission=m2,
			kingdom=self.k,
			started=datetime.now()
		)
		pm2.save()

		self.assertEqual(Kingdom.objects.count(), 2)

	def test_mission_on_start_aborted(self):
		"""
		Check the on_start code, aborting the mission.
		"""

		m2 = Mission(
			name="Stub mission2",
			slug="stub_2",
			on_resolution="",
			on_start="""
status='abort'
""",
			title=self.t,
		)
		m2.save()

		pm2 = PendingMission(
			mission=m2,
			kingdom=self.k,
			started=datetime.now()
		)
		
		self.assertRaises(ValidationError, pm2.save)

	def test_mission_start_again(self):
		"""
		Can't start twice the same mission.
		"""

		self.pm.started = datetime.now()
		self.pm.save()

		self.assertRaises(ValidationError, self.pm.start)

	def test_mission_resolution_before_start(self):
		"""
		Can't resolve unstarted mission
		"""

		self.assertRaises(ValidationError, self.pm.resolve)

	def test_mission_resolution(self):
		"""
		Check the on_resolution code.
		"""

		self.m.on_resolution = """
status='mission_solved'
"""
		self.m.save()

		self.pm.started = datetime.now()
		self.pm.save()

		status = self.pm.resolve()
		self.assertEqual(status, 'mission_solved')

	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 test_mission_resolution_with_value(self):
		"""
		Check the on_resolution code works with a value.
		"""

		self.m.has_value = True
		self.m.on_resolution = """
if value == 15:
	status='mission_solved'
"""
		self.m.save()

		self.pm.value = 15
		self.pm.save()

		self.pm.started = datetime.now()
		self.pm.save()

		status = self.pm.resolve()
		self.assertEqual(status, 'mission_solved')

	def test_mission_resolution_delete_pending_mission(self):
		"""
		Pendingmission must be deleted after resolution.
		"""

		self.pm.started = datetime.now()
		self.pm.save()
		self.pm.resolve()

		self.assertTrue(self.pm.is_finished)
		self.assertRaises(PendingMission.DoesNotExist, (lambda: PendingMission.objects.get(pk=self.pm.pk)))

	def test_mission_not_cancellable(self):
		"""
		Check the is_cancellable flag.
		"""
		
		self.m.is_cancellable = False
		self.m.save()

		self.assertRaises(ValidationError, self.pm.delete)

	def test_mission_finished_not_cancellable(self):
		"""
		Check the is_cancellable flag combined with is_finished.
		"""
		self.m.is_cancellable = False
		self.m.save()

		# Fake resolution
		self.pm.is_started = True
		self.pm.is_finished = True
		self.pm.save()

		# AssertNoRaise
		self.pm.delete()

	def test_mission_cancellable(self):
		"""
		Check the inactive is_cancellable flag.
		"""

		# AssertNoRaise
		self.pm.delete()

	def test_mission_on_cancel(self):
		"""
		Check the on_cancel code.
		"""

		self.m.on_cancel = """
kingdom.prestige = 50
kingdom.save()
"""

		# Sanity check
		self.assertEqual(0, Kingdom.objects.get(pk=self.k.pk).prestige)
		
		self.pm.delete()
		
		self.assertEqual(50, Kingdom.objects.get(pk=self.k.pk).prestige)

	def test_mission_on_cancel_status(self):
		"""
		Check the on_cancel code can stop deletion
		"""

		self.m.on_cancel = """
kingdom.prestige = 50
kingdom.save()
"""

		# Sanity check
		self.assertEqual(0, Kingdom.objects.get(pk=self.k.pk).prestige)
		
		self.pm.delete()
		
		self.assertEqual(50, Kingdom.objects.get(pk=self.k.pk).prestige)

	def test_grid_with_two_people(self):
		"""
		Check if folks are well put in the grid
		"""

		self.m.on_resolution = """
status = grids['stub_grid'][0].first_name + " " + grids['stub_grid'][1].first_name
"""
		self.m.save()

		self.f.first_name = "a"
		self.f.save()

		self.f2 = Folk(
			kingdom=self.k,
			first_name="b"
		)
		self.f2.save()

		self.mg.length = 2
		self.mg.save()

		self.pma2 = PendingMissionAffectation(
			pending_mission=self.pm,
			mission_grid=self.mg,
			folk=self.f2
		)
		self.pma2.save()
		self.pm.start()
		status = self.pm.resolve()
		self.assertEqual(status, 'a b')

	def test_grid_with_two_grids(self):
		"""
		Check if folks are well put in the grid
		"""
		mg2 = MissionGrid(
			mission=self.m,
			slug='stub_grid2'
		)
		mg2.save()

		self.m.on_resolution = """
status = grids['stub_grid'][0].first_name + " " + grids['stub_grid2'][0].first_name
"""
		self.m.save()

		self.f.first_name = "a"
		self.f.save()

		self.f2 = Folk(
			kingdom=self.k,
			first_name="b"
		)
		self.f2.save()

		self.pma2 = PendingMissionAffectation(
			pending_mission=self.pm,
			mission_grid=mg2,
			folk=self.f2
		)
		self.pma2.save()

		self.pm.start()
		status = self.pm.resolve()
		self.assertEqual(status, 'a b')

	def test_pendingmission_set_get_value(self):
		"""
		Test that setting and retrieving a context value through a PendingMission works
		"""

		f = Folk(
			kingdom=self.k,
			first_name="Theon",
			last_name="Greyjoy"
		)
		f.save()

		self.pm.set_value("peon", f)
		self.assertEqual(f, self.pm.get_value("peon"))

		self.pm.set_value("Narnia", self.k)
		self.assertEqual(self.k, self.pm.get_value("Narnia"))

		self.pm.set_value("nompourri", "Kevin")
		self.assertEqual(self.pm.get_value("nompourri"), "Kevin")

		self.pm.set_value("beastnum", 666)
		self.assertEqual(self.pm.get_value("beastnum"), 666)

		self.pm.set_value("void", None)
		self.assertEqual(self.pm.get_value("void"), None)

	def test_pendingmission_context_start(self):
		"""
		Test the context access in on_start.
		"""

		self.m.on_start = """
param.set_value('beastnum', 666)
"""
		self.m.save()

		# Internal machinery works to delete.
		self.pm.start()
		self.assertEqual(self.pm.get_value("beastnum"), 666)

	def test_pendingmission_context_resolve(self):
		"""
		Test the context access in on_resolve.
		"""

		self.m.on_start = """
param.set_value('beastnum', 666)
"""
		self.m.on_resolve = """
if param.get_value('beastnum') != 666:
	from django.core.exceptions import ValidationError
	raise ValidationError("HUM")
"""
		self.m.save()

		# Internal machinery works to delete.
		self.pm.start()
		self.pm.resolve()

	def test_pendingmission_cron_timeout(self):
		"""
		Test the cron timeouts pendingmission.
		"""

		self.m.timeout = 10
		self.m.save()

		self.pm.created = datetime.now() - timedelta(minutes=15)
		self.pm.save()

		pm2 = PendingMission(kingdom=self.k, mission=self.m)
		pm2.save()

		cron_minute.send(self, counter=1000)

		self.assertRaises(PendingMission.DoesNotExist, (lambda: PendingMission.objects.get(pk=self.pm.id)))
		# Assert no raises
		PendingMission.objects.get(pk=pm2.id)

	def test_pendingmission_cron_notimeout(self):
		"""
		Test the cron does not timeout pendingmission without timeout.
		"""

		self.m.timeout = None
		self.m.save()

		self.pm.created = datetime.now() - timedelta(minutes=15)
		self.pm.save()

		cron_minute.send(self, counter=1000)

		# assertNoRaises
		PendingMission.objects.get(pk=self.pm.id)

	def test_pendingmission_cron_timeout_cancel_code(self):
		"""
		Test the cron triggers the on_cancel code.
		"""

		self.m.timeout = 10
		self.m.on_cancel = """
kingdom.set_value('pm_deleted', param.pk)
"""
		self.m.save()

		self.pm.created = datetime.now() - timedelta(minutes=15)
		self.pm.save()

		cron_minute.send(self, counter=1000)

		self.assertEqual(self.k.get_value('pm_deleted'), self.pm.id)

	def test_pendingmission_cron_duration(self):
		"""
		Test the cron resolves pendingmission and deletes them.
		"""

		self.m.duration = 10
		self.m.save()

		self.pm.started = datetime.now() - timedelta(minutes=15)
		self.pm.save()

		pm2 = PendingMission(kingdom=self.k, mission=self.m)
		pm2.started = datetime.now()
		pm2.save()

		cron_minute.send(self, counter=1000)

		self.assertRaises(PendingMission.DoesNotExist, (lambda: PendingMission.objects.get(pk=self.pm.id)))
		# Assert no raises
		PendingMission.objects.get(pk=pm2.id)

	def test_pendingmission_cron_duration_resolution_code(self):
		"""
		Test the cron resolves pendingmission and execute the code.
		"""

		self.m.duration = 10
		self.m.on_resolution = """
kingdom.set_value('pm_resolved', param.pk)
"""
		self.m.save()

		self.pm.started = datetime.now() - timedelta(minutes=15)
		self.pm.save()

		cron_minute.send(self, counter=1000)

		self.assertEqual(self.k.get_value('pm_resolved'), self.pm.id)

	def test_team_cant_start(self):
		"""
		Test teams can't be started
		"""

		self.m.is_team = True
		self.m.save()

		self.pm.started = datetime.now()
		self.assertRaises(ValidationError, self.pm.save)
Esempio n. 14
0
class ScriptTest(TestCase):
	"""
	Unit tests for title script
	"""

	def setUp(self):
		self.k = Kingdom()
		self.k.save()

		self.f = Folk(
			first_name="bob",
			kingdom=self.k,
		)
		self.f.save()

		self.t = Title(
			name="Dummy title",
			slug="dummy_title",
			description="Nothing to say."
		)
		self.t.save()

	def test_kingdom_get_folk_in_title(self):
		"""
		Check folk is returned
		"""
		at2 = AvailableTitle(
			title=self.t,
			kingdom=self.k,
			folk=self.f
		)
		at2.save()
		self.assertEqual(self.f, self.k.get_folk_in_title("dummy_title"))

	def test_kingdom_get_folk_in_title_fail(self):
		"""
		Check None is returned
		"""
		at2 = AvailableTitle(
			title=self.t,
			kingdom=self.k,
			folk=self.f
		)
		at2.save()
		self.assertIsNone(self.k.get_folk_in_title("zerfzef"))

	def test_kingdom_get_folk_in_title_empty(self):
		"""
		Check None is returned
		"""
		at2 = AvailableTitle(
			title=self.t,
			kingdom=self.k,
		)
		at2.save()
		self.assertIsNone(self.k.get_folk_in_title("dummy_title"))

	def test_kingdom_unlock_title(self):
		"""
		Check available title is created
		"""

		# Sanity check
		self.assertRaises(AvailableTitle.DoesNotExist, (lambda: AvailableTitle.objects.get(kingdom=self.k, title=self.t)))

		self.k.unlock_title("dummy_title")
		
		# AssertNoRaises
		AvailableTitle.objects.get(kingdom=self.k, title=self.t)

	def test_kingdom_unlock_title_already_available(self):
		"""
		Check you can unlock twice
		"""

		self.k.unlock_title("dummy_title")
		self.k.unlock_title("dummy_title")

	def test_folk_add_title(self):
		"""
		Check the title is added
		"""
		self.assertRaises(AvailableTitle.DoesNotExist, self.k.get_folk_in_title("dummy_title"))

		self.at = AvailableTitle(
			title=self.t,
			kingdom=self.k,
			folk=self.f
		)
		self.at.save()
		self.f.add_title("dummy_title")
		
		self.assertEqual("bob", self.k.get_folk_in_title("dummy_title").first_name)

	def test_folk_remove_title(self):
		"""
		Check the title is removed
		"""
		self.assertRaises(AvailableTitle.DoesNotExist, self.k.get_folk_in_title("dummy_title"))

		self.at = AvailableTitle(
			title=self.t,
			kingdom=self.k,
			folk=self.f
		)
		self.at.save()
		self.f.add_title("dummy_title")
		
		self.assertEqual("bob", self.k.get_folk_in_title("dummy_title").first_name)
		self.f.remove_title()
		self.assertEqual(None, self.k.get_folk_in_title("dummy_title"))
Esempio n. 15
0
class UnitTest(TestCase):
	"""
	Unit tests for title app
	"""

	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 test_folk_unicity(self):
		"""
		A folk can only be affected in one Title at a time.
		"""
		at = AvailableTitle(
			title=self.t,
			kingdom=self.k,
			folk=self.f
		)
		at.save()

		t2 = Title(
			name="Dummy title2",
			slug="dummy_title2",
			description="Nothing to say."
		)
		t2.save()

		at2 = AvailableTitle(
			title=t2,
			kingdom=self.k,
			folk=self.f
		)
		
		self.assertRaises(IntegrityError, at2.save)

	def test_title_kingdom_unicity(self):
		"""
		The same title can't appear twice in the same kingdom
		"""
		at = AvailableTitle(
			title=self.t,
			kingdom=self.k,
		)
		at.save()

		at2 = AvailableTitle(
			title=self.t,
			kingdom=self.k,
		)
		
		self.assertRaises(IntegrityError, at2.save)

	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_folk_death(self):
		"""
		When you change kingdom, you're disaffected from your title.
		"""
		at = AvailableTitle(
			title=self.t,
			kingdom=self.k,
			folk=self.f
		)
		at.save()

		self.f.die()
		
		# Reload the title, folk should now be empty
		at = AvailableTitle.objects.get(pk=at.pk)
		self.assertIsNone(at.folk)

	def test_title_unlock(self):
		"""
		The on_unlock code is executed
		"""
		self.t.on_unlock = """
kingdom.prestige = 50
kingdom.save()
"""

		# Sanity check
		self.assertEqual(self.k.prestige, 0)

		at = AvailableTitle(
			title=self.t,
			kingdom=self.k,
		)
		at.save()

		self.assertEqual(self.k.prestige, 50)

	def test_title_unlock_fail(self):
		"""
		The on_unlock code is executed, and can stop the title creation.
		"""
		self.t.on_unlock = """
status="no"
"""

		# Sanity check
		self.assertEqual(self.k.prestige, 0)

		at = AvailableTitle(
			title=self.t,
			kingdom=self.k,
		)
		self.assertRaises(ValidationError, at.save)

	def test_title_condition(self):
		"""
		The condition can abort an affection
		"""
		self.t.condition = 'status="not_possible"'
		self.t.save()

		at = AvailableTitle(
			title=self.t,
			kingdom=self.k,
			folk=self.f
		)
		self.assertRaises(ValidationError, at.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 test_title_affect_direct(self):
		"""
		You can create and affect in the same time.
		"""

		self.t.on_affect = """
folk.loyalty = 50
folk.save()
"""
		self.t.save()

		# Sanity check
		self.assertNotEquals(self.f.loyalty, 50)

		at = AvailableTitle(
			title=self.t,
			kingdom=self.k,
			folk=self.f
		)
		at.save()

		self.assertEqual(self.f.loyalty, 50)

	def test_title_affect(self):
		"""
		Test affect code is run
		"""

		self.t.on_affect = """
folk.loyalty = 50
folk.save()
"""
		self.t.save()

		# Sanity check
		self.assertNotEquals(self.f.loyalty, 50)

		at = AvailableTitle(
			title=self.t,
			kingdom=self.k,
		)
		at.save()

		# Sanity check
		self.assertNotEquals(self.f.loyalty, 50)

		at.folk = self.f
		at.save()

		self.assertEqual(self.f.loyalty, 50)

	def test_title_defect(self):
		"""
		Test defect code is run
		"""

		self.t.on_defect = """
folk.loyalty = 50
folk.save()
"""
		self.t.save()

		at = AvailableTitle(
			title=self.t,
			kingdom=self.k,
			folk=self.f
		)
		at.save()

		# Sanity check
		self.assertNotEquals(self.f.loyalty, 50)

		at = AvailableTitle.objects.get(pk=at.pk)
		at.folk = None
		at.save()

		self.assertEqual(Folk.objects.get(pk=self.f.pk).loyalty, 50)

	def test_title_affect_defect(self):
		"""
		Test affect AND defect code are run
		"""

		self.t.on_affect = """
folk.loyalty = 80
folk.save()
"""
		self.t.on_defect = """
folk.loyalty = 20
folk.save()
"""
		self.t.save()

		f2 = Folk(
			first_name="Robb",
			last_name="Stark",
			kingdom=self.k
		)
		f2.save()

		# Sanity check
		self.assertEqual(self.f.loyalty, 0)
		self.assertEqual(f2.loyalty, 0)

		at = AvailableTitle(
			title=self.t,
			kingdom=self.k,
			folk=self.f
		)
		at.save()

		self.assertEqual(self.f.loyalty, 80)

		at.folk = f2
		at.save()

		self.assertEqual(self.f.loyalty, 20)
		self.assertEqual(f2.loyalty, 80)