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_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 mission_list(request, cur_page=1, template="admin/mission/mission/list.tpl"): count = Mission.get_count() if count != 0: page = int(cur_page) mission_list = Mission.get_list(page) p = Paginator(page, count, page_size=config.default_page_size) if mission_list: return render_response(template, request=request, mission_list=mission_list, p=p) return render_response(template, request=None, mission_list=None, p=None)
class DependenciesCommandTest(TestCase): """ Test dependencies commands """ def setUp(self): self.k = Kingdom() self.e = Event( name="Event", slug="first_event") self.e.save() self.ea = self.e.eventaction_set.create( on_fire=""" kingdom.next_event("second_event") """) self.ea.save() self.m = Mission( name="Mission", slug="mission", on_resolution=""" kingdom.create_pending_event("second_event") """) self.m.save() def test_dependencies(self): content = StringIO() management.call_command('dependencies', dry=True, interactive=False, stdout=content) content.seek(0) out = content.read() # Check node list self.assertTrue('event_first_event [' in out) self.assertTrue('mission_mission [' in out) self.assertTrue('event_first_event -> event_second_event' in out) self.assertTrue('mission_mission -> event_second_event' in out) def test_dependencies_restrict_object(self): content = StringIO() management.call_command('dependencies', 'mission_mission', dry=True, interactive=False, stdout=content) content.seek(0) out = content.read() # Check node list does not include "event" model. self.assertFalse('event_first_event [' in out) self.assertTrue('mission_mission [' in out)
def update_or_create_single_mission(self, order): purchase_date, earliest_ship_date = order.get( "PurchaseDate"), order.get("EarliestShipDate") earliest_ship_date = datetime.strptime( earliest_ship_date.split("T")[0], '%Y-%m-%d') amazon_order_id = order.get("AmazonOrderId") mission_instance = Mission.objects.filter( channel_order_id=amazon_order_id).first() if mission_instance is None: mission_instance = Mission(delivery_date=earliest_ship_date, channel_order_id=amazon_order_id) mission_instance.save() is_prime, is_business_order = order.get("IsPrime"), order.get( "IsBusinessOrder") is_premium_order = order.get("IsPremiumOrder") order_status = order.get("OrderStatus") mission_instance.status = order_status if order.get("FulfillmentChannel") == "AFN": mission_instance.is_amazon_fba = True else: mission_instance.is_amazon_fba = False return mission_instance
def test_freeze_nolock(self): """ Test freeze restoration is not bound to petty matter, such as "not leaving a mission before it ends". """ from mission.models import Mission, PendingMission, MissionGrid, PendingMissionAffectation m = Mission( name="Stub mission", slug="stub", title=None, ) m.save() mg = MissionGrid( mission=m, slug='stub_grid' ) mg.save() pm = PendingMission( mission=m, kingdom=self.k ) pm.save() pma = PendingMissionAffectation( pending_mission=pm, mission_grid=mg, folk=self.f ) pma.save() # Create the freeze freeze = Freeze(kingdom=self.k) freeze.save() pm.start() # Sanity check self.assertRaises(ValidationError, lambda: pm.delete()) # Unfreeze. # No errors should be raised, even though the PendingMission is still deleted freeze.restore() self.assertFalse(PendingMission.objects.get(pk=pm.pk).is_started)
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_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 add_mission(request, received_data={}): start_time = convert_to_datetime(received_data['start_time']) end_time = convert_to_datetime(received_data['end_time']) mission = Mission(title=received_data['title'], description=received_data['description'], min_member_count=int(received_data['min_member_count']), max_member_count=int(received_data['max_member_count']), start_time=start_time, end_time=end_time, creator=request.user) mission.save() for task in received_data['tasks']: Task(num=task['num'], lng=task['lng'], lat=task['lat'], description=task['description'], mission=mission).save() return true_json_response(msg="Mission added successfully")
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 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.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 mission_edit(request, mission_id=0, template="admin/mission/mission/edit.tpl"): mission = Mission.get_by_id(mission_id) if request.method == "GET": scenario_list = Scenario.get_all() return render_response(template, scenario_list=scenario_list, mission=mission) elif request.method == "POST": form = MissionForm(request.POST) if form.is_valid(): # name = form.cleaned_data['name'] ep = form.cleaned_data['ep'] # vm = form.cleaned_data['vm'] # xp = form.cleaned_data['xp'] description = form.cleaned_data['description'] # sum_count = form.cleaned_data['sum_count'] is_unlock = form.cleaned_data['is_unlock'] scenario_id = form.cleaned_data['scenario_id'] mission_group_id = form.cleaned_data['mission_group_id'] order = form.cleaned_data['order'] level = form.cleaned_data['level'] is_group_last = form.cleaned_data['is_group_last'] vm = 35 sum_count = 10 if is_group_last: ep = 25 vm = 180 sum_count = 1 if scenario_id < 3: ep = form.cleaned_data['ep'] xp = ep * 5 try: # mission.name = name mission.ep = ep mission.vm = vm mission.xp = xp mission.description = description mission.sum_count = sum_count mission.is_unlock = is_unlock mission.scenario_id = scenario_id mission.mission_group_id = mission_group_id mission.order = order mission.level = level mission.is_group_last = is_group_last mission.save() except Exception, e: if config.debug: print e else: return HttpResponse("<script type='text/javascript'>window.top.right.location.reload();window.top.art.dialog({id:'mission_edit'}).close();</script>")
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.e = Event( name="Event", slug="first_event") self.e.save() self.ea = self.e.eventaction_set.create( on_fire=""" kingdom.next_event("second_event") """) self.ea.save() self.m = Mission( name="Mission", slug="mission", on_resolution=""" kingdom.create_pending_event("second_event") """) self.m.save()
def get_amazon_mission_instances(self): not_matchables = {} for i, row in enumerate(self.result): channel_order_id = self.column_from_row("order-id", row) sku = self.column_from_row("sku", row) print(f"this is channel and sku: {channel_order_id} - {sku}") if channel_order_id == "" or sku == "": continue sku_instance = None if sku != "": sku_instance = Sku.objects.filter(sku__iexact=sku.strip(), main_sku__isnull=True).first() delivery_date_from = self.parse_date(self.column_from_row('earliest-delivery-date', row)) delivery_date_to = self.parse_date(self.column_from_row("latest-delivery-date", row)) ship_date_from = self.parse_date(self.column_from_row('earliest-ship-date', row)) ship_date_to = self.parse_date(self.column_from_row("latest-ship-date", row)) purchased_date = self.parse_date(self.column_from_row("purchase-date", row)) payment_date = self.parse_date(self.column_from_row("payments-date", row)) instance_data = {"channel_order_id": channel_order_id, "is_online": True, "purchased_date": purchased_date, "delivery_date_from": delivery_date_from, "delivery_date_to": delivery_date_to, "ship_date_from": ship_date_from, "ship_date_to": ship_date_to, "payment_date": payment_date, "is_amazon": True } channel = None if sku_instance is not None: instance_data["channel"] = sku_instance.channel channel = sku_instance.channel shipping_address_instance = self.get_shipping_address_instance(row) billing_address_instance = self.get_billing_address_instance(row) if shipping_address_instance is not None: instance_data.update({"delivery_address_id": shipping_address_instance.pk}) if channel is not None: business_account = self.get_business_account(shipping_address_instance, channel) if business_account is not None: instance_data.update({"online_business_account_id": business_account.pk}) self.break_down_address_in_street_and_house_number(shipping_address_instance) if billing_address_instance is not None: instance_data.update({"billing_address_id": billing_address_instance.pk}) self.break_down_address_in_street_and_house_number(billing_address_instance) if sku_instance is None or (sku_instance.product.ean is None or sku_instance.product.ean == ""): instance_data["not_matchable"] = True mission_instance = Mission.objects.filter(channel_order_id=channel_order_id).first() if mission_instance is None: if purchased_date != "" and delivery_date_from != "" and delivery_date_to != "" and payment_date != "": mission_instance = Mission(**instance_data) if self.not_sent_report is None: continue if instance_data.get("not_matchable", None) is True: not_matchables[channel_order_id] = True if not_matchables.get(channel_order_id, None) is not True: instance_data["not_matchable"] = None mission_instance.not_matchable = None mission_instance.save() if mission_instance is not None: if self.not_sent_report is None: mission_instance.__dict__.update(**instance_data) mission_instance.save() productmission_data = {"mission": mission_instance} product_description = self.column_from_row("product-name", row) if product_description != "": productmission_data["online_description"] = product_description order_item_id = self.column_from_row("order-item-id", row) if order_item_id != "": productmission_data["online_identifier"] = order_item_id quantity_purchased = self.column_from_row("quantity-purchased", row) try: quantity_purchased = int(quantity_purchased) productmission_data["amount"] = quantity_purchased except ValueError: print(f"- {quantity_purchased} - kann nicht in int geparset werden") instance_data["not_matchable"] = True mission_instance.save() item_price = self.column_from_row("item-price", row) try: item_price = float(item_price) if quantity_purchased != "": productmission_data["brutto_price"] = item_price/quantity_purchased except ValueError: print(f"- {item_price} - kann nicht in float geparset werden") instance_data["not_matchable"] = True mission_instance.save() shipping_price = self.column_from_row("shipping-price", row) try: shipping_price = float(shipping_price) productmission_data["shipping_price"] = shipping_price except ValueError: print(f"- {shipping_price} - kann nicht in float geparset werden") instance_data["not_matchable"] = True mission_instance.save() discount = self.column_from_row("item-promotion-discount", row) try: discount = float(discount) productmission_data["discount"] = discount except ValueError: print(f"- {discount} - kann nicht in float geparset werden") instance_data["not_matchable"] = True mission_instance.save() shipping_discount = self.column_from_row("ship-promotion-discount", row) try: shipping_discount = float(shipping_discount) productmission_data["shipping_discount"] = shipping_discount except ValueError: print(f"- {shipping_discount} - kann nicht in float geparset werden") instance_data["not_matchable"] = True mission_instance.save() productmission_instance = None if sku_instance is not None: productmission_data["sku"] = sku_instance productmission_instance = ProductMission.objects.filter( sku=sku_instance, mission=mission_instance).first() elif sku != "": productmission_data["no_match_sku"] = sku productmission_instance = ProductMission.objects.filter( no_match_sku=sku, mission=mission_instance).first() print(f"ENTON 123456::: {sku} ------- {productmission_instance or 'No'}") if sku != "" or sku_instance is not None: if productmission_instance is None: productmission_instance = ProductMission.objects.create(**productmission_data) else: productmission_instance.__dict__.update(**productmission_data) productmission_instance.save() mission_instance.save() # damit der richtige Status gesetzt wird
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)
class UnitTest(TestCase): 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 test_no_pending_bargain_with_yourself(self): """ Can't bargain with yourself. """ self.pbk2.kingdom = self.k1 self.assertRaises(IntegrityError, self.pbk2.save) 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_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_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 test_sanity_folk_in_one_affectation(self): """ The folk in PendingBargainSharedMissionAffectation must be owned by one of the sides of the negotiation. """ pbsma = PendingBargainSharedMissionAffectation( pending_bargain_shared_mission=self.pbsm, mission_grid=self.mg, folk=self.f ) pbsma.save() pbsma2 = PendingBargainSharedMissionAffectation( pending_bargain_shared_mission=self.pbsm, mission_grid=self.mg, folk=self.f ) # Can't be affected twice self.assertRaises(IntegrityError, pbsma2.save) def test_autoremove_pending_bargain_on_kingdom_deletion(self): """ When a kingdom is removed, all PendingBargain it was part of are deleted """ self.pbk1.delete() self.assertRaises(PendingBargain.DoesNotExist, lambda: PendingBargain.objects.get(pk=self.pb.pk)) def test_cant_share_started_pending_mission(self): """ A PendingBargainSharedMission must not be started """ self.pm.started = datetime.now() self.pm.save() pbsm = PendingBargainSharedMission( pending_mission=self.pm, pending_bargain=self.pb ) self.assertRaises(ValidationError, pbsm.save) def test_pending_mission_removed_on_start(self): """ PendingBargainSharedMission are deleted when the PendingMission is started. """ self.pm.started = datetime.now() self.pm.save() # PBSM must be deleted self.assertRaises(PendingBargainSharedMission.DoesNotExist, (lambda: PendingBargainSharedMission.objects.get(pk=self.pbsm.pk))) def test_affectation_is_checked(self): """ The affectation must trigger the PendingMissionAffectation code (as a dry run of course, since the affectation is at this point purely virtual) """ self.mg.length = 0 self.mg.save() pbsma = PendingBargainSharedMissionAffectation( pending_bargain_shared_mission=self.pbsm, mission_grid=self.mg, folk=self.f ) # This grid is full self.assertRaises(ValidationError, pbsma.save) def test_cant_affect_if_in_mission(self): """ The affectation must check you're not already in a mission. """ # Create a pending mission, not shared pm2 = PendingMission(kingdom=self.k1, mission=self.m) pm2.save() pma = PendingMissionAffectation(pending_mission=pm2, mission_grid=self.mg, folk=self.f) pma.save() pbsma = PendingBargainSharedMissionAffectation( pending_bargain_shared_mission=self.pbsm, mission_grid=self.mg, folk=self.f ) self.assertRaises(ValidationError, pbsma.save) def test_kingdom_state(self): """ PendingBargainKingdom states values are constrained. """ self.pbk1.state = PendingBargainKingdom.PENDING self.pbk1.save() self.pbk1.state = PendingBargainKingdom.OK self.pbk1.save() self.pbk1.state = PendingBargainKingdom.OK_NO_MATTER_WHAT self.pbk1.save() self.pbk1.state = 15 self.assertRaises(ValidationError, self.pbk1.save) def test_validation(self): """ PendingBargain are committed when everyone is OK. """ pbsma = PendingBargainSharedMissionAffectation( pending_bargain_shared_mission=self.pbsm, mission_grid=self.mg, folk=self.f ) pbsma.save() # Sanity check: folk is not in MissionGrid self.assertEqual(0, PendingMissionAffectation.objects.filter(pending_mission=self.pm, mission_grid=self.mg).count()) self.pbk1.state = PendingBargainKingdom.OK self.pbk1.save() self.pbk2.state = PendingBargainKingdom.OK self.pbk2.save() self.assertEqual(1, PendingMissionAffectation.objects.filter(pending_mission=self.pm, mission_grid=self.mg).count()) self.assertEqual(self.f, PendingMissionAffectation.objects.filter(pending_mission=self.pm, mission_grid=self.mg)[0].folk) # PendingBargain has been deleted self.assertRaises(PendingBargain.DoesNotExist, (lambda: PendingBargain.objects.get(pk=self.pb.pk))) def test_validation_failing(self): """ PendingBargain can raise ValidationError, and forbid saving last state to OK. """ pbsma = PendingBargainSharedMissionAffectation( pending_bargain_shared_mission=self.pbsm, mission_grid=self.mg, folk=self.f ) pbsma.save() # Fake case. # In reality, the Exception may be triggered by some code on the grid depending on various contexts. self.mg.length = 0 self.mg.save() self.pbk1.state = PendingBargainKingdom.OK self.pbk1.save() self.pbk2.state = PendingBargainKingdom.OK # This grid is full self.assertRaises(ValidationError, self.pbk2.save) # Assert exists (has not been deleted) PendingBargain.objects.get(pk=self.pb.pk) def test_state_reverted_on_affectation_delete(self): """ PendingBargainKingdom states are reverted if an affectation is deleted. """ pbsma = PendingBargainSharedMissionAffectation( pending_bargain_shared_mission=self.pbsm, mission_grid=self.mg, folk=self.f ) pbsma.save() # Kingdom1 is OK. self.pbk1.state = PendingBargainKingdom.OK self.pbk1.save() # Deal breaker pbsma.delete() self.assertEqual(PendingBargainKingdom.PENDING, PendingBargainKingdom.objects.get(pk=self.pbk1.pk).state) def test_state_reverted_on_affectation_changed(self): """ PendingBargainKingdom states are reverted if an affectation is updated. """ pbsma = PendingBargainSharedMissionAffectation( pending_bargain_shared_mission=self.pbsm, mission_grid=self.mg, folk=self.f ) pbsma.save() # Kingdom1 is OK. self.pbk1.state = PendingBargainKingdom.OK self.pbk1.save() # Deal breaker f2 = Folk(kingdom=self.k2) f2.save() pbsma.folk = f2 pbsma.save() self.assertEqual(PendingBargainKingdom.PENDING, PendingBargainKingdom.objects.get(pk=self.pbk1.pk).state) def test_state_reverted_on_affectation_created(self): """ PendingBargainKingdom states are reverted if an affectation is created. """ # Kingdom1 is OK. self.pbk1.state = PendingBargainKingdom.OK self.pbk1.save() # Deal breaker pbsma = PendingBargainSharedMissionAffectation( pending_bargain_shared_mission=self.pbsm, mission_grid=self.mg, folk=self.f ) pbsma.save() self.assertEqual(PendingBargainKingdom.PENDING, PendingBargainKingdom.objects.get(pk=self.pbk1.pk).state) def test_state_reverted_on_shared_mission_deleted(self): """ PendingBargainKingdom states are reverted if a shared mission with affectations is removed. """ pbsma = PendingBargainSharedMissionAffectation( pending_bargain_shared_mission=self.pbsm, mission_grid=self.mg, folk=self.f ) pbsma.save() # Kingdom1 is OK. self.pbk1.state = PendingBargainKingdom.OK self.pbk1.save() # Deal breaker self.pbsm.delete() self.assertEqual(PendingBargainKingdom.PENDING, PendingBargainKingdom.objects.get(pk=self.pbk1.pk).state)
class ScriptTest(TestCase): """ Unit tests for mission script """ 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_unlock_mission(self): """ Check the available mission is created. """ self.k.unlock_mission("stub") # AssertNoRaises AvailableMission.objects.get(kingdom=self.k, mission=self.m) def test_kingdom_create_pending_mission(self): """ Check the pending mission is created. """ self.k.create_pending_mission("stub") self.assertEqual(self.k.pendingmission_set.count(), 1) def test_kingdom_unlock_title_already_available(self): """ Check no error occurs if you unlock twice """ self.k.unlock_mission("stub") # AssertNoRaises self.k.unlock_mission("stub") def test_kingdom_get_team(self): """ Check you can retrieve team datas """ # Sanity check self.assertRaises(PendingMission.DoesNotExist, lambda: self.k.get_team("stub")) self.m.is_team = True self.m.save() pm = PendingMission(kingdom=self.k, mission=self.m) pm.save() # AssertNoRaises datas = self.k.get_team("stub") self.assertEqual(datas["pendingmission"], pm) self.assertEqual(len(datas["grids"]), 0)
def get_ebay_mission_instances(self): print(len(self.result)) not_matchables = {} self.result = self.result[: -2] # entfernt letzte zwei Zeilen mit Mitgliedsname und Protokolle for i, row in enumerate(self.result): channel_order_id = row.get("Verkaufsprotokollnummer", "") or "" sku = row.get("Bestandseinheit", "") or "" if channel_order_id == "": continue print(f"THIS IS SKU: {sku}") sku_instance = None if sku != "": sku_instance = Sku.objects.filter( sku__iexact=sku.strip(), main_sku__isnull=True).first() print(f'THIS IS ORDER ID: {channel_order_id}') ship_date_from = self.parse_date( row.get("Versanddatum", "") or "") # not needed ship_date_to = self.parse_date(row.get("Versanddatum", "") or "") # not needed purchased_date = self.parse_date( row.get("Datum der Kaufabwicklung", "") or "") payment_date = self.parse_date(row.get("Zahlungsdatum", "") or "") instance_data = { "channel_order_id": channel_order_id, "is_online": True, "purchased_date": purchased_date, "payment_date": payment_date, "is_ebay": True } mission_instance = Mission.objects.filter( channel_order_id=channel_order_id).first() channel = None if sku_instance is not None: channel = sku_instance.channel if channel is not None: instance_data["channel_id"] = channel.pk print(f"channel: {channel}") shipping_address_instance = self.get_shipping_address_instance( row, mission_instance) billing_address_instance = self.get_billing_address_instance( row, mission_instance) print(f"wilson: {shipping_address_instance}") if channel is not None: if shipping_address_instance is not None: business_account = self.get_business_account( shipping_address_instance, channel) if business_account is not None: instance_data.update({ "online_business_account_id": business_account.pk }) if shipping_address_instance is not None: instance_data.update({ "delivery_address_id": shipping_address_instance.pk, }) self.break_down_address_in_street_and_house_number( shipping_address_instance) if billing_address_instance is not None: instance_data.update( {"billing_address_id": billing_address_instance.pk}) self.break_down_address_in_street_and_house_number( billing_address_instance) if sku_instance is None or (sku_instance.product.ean is None or sku_instance.product.ean == ""): instance_data["not_matchable"] = True if mission_instance is None: if purchased_date != "" and payment_date != "": mission_instance = Mission(**instance_data) if instance_data.get("not_matchable", None) is True: not_matchables[channel_order_id] = True if not_matchables.get(channel_order_id, None) is not True: instance_data["not_matchable"] = None mission_instance.not_matchable = None print(f"bankimoon: {instance_data}") if mission_instance is not None: mission_instance.__dict__.update(instance_data) mission_instance.save() print(f"ben 2: {mission_instance.channel}") if sku == "" and sku_instance is None: if mission_instance not in self.missions_none_sku_amounts: self.missions_none_sku_amounts[mission_instance.pk] = {} self.missions_none_sku_amounts[ mission_instance.pk]["amount"] = 1 else: self.missions_none_sku_amounts[ mission_instance.pk]["amount"] += 1 self.missions_none_sku_amounts[ mission_instance.pk]["instance"] = mission_instance else: if mission_instance not in self.missions_none_sku_amounts: self.missions_none_sku_amounts[mission_instance.pk] = { "instance": mission_instance } product_description = row.get("Artikelbezeichnung", None) if mission_instance is not None: productmission_data = { "mission": mission_instance, "online_description": product_description } quantity_purchased = row.get("Stückzahl", "") or "" print(f"klaus: {quantity_purchased}") try: quantity_purchased = int(quantity_purchased) productmission_data["amount"] = quantity_purchased except ValueError: print( f"- {quantity_purchased} - kann nicht in int geparset werden" ) instance_data["not_matchable"] = True mission_instance.save() item_price = row.get("Preis", "") or "" item_price = item_price.replace("EUR ", "") item_price = item_price.replace(",", ".") try: item_price = float(item_price) if quantity_purchased != "": productmission_data[ "brutto_price"] = item_price / quantity_purchased except ValueError: print( f"- {item_price} - kann nicht in float geparset werden" ) instance_data["not_matchable"] = True mission_instance.save() shipping_price = row.get("Verpackung und Versand", "") or "" shipping_price = shipping_price.replace("EUR ", "") shipping_price = shipping_price.replace(",", ".") try: shipping_price = float(shipping_price) productmission_data["shipping_price"] = shipping_price except ValueError: print( f"- {shipping_price} - kann nicht in float geparset werden" ) instance_data["not_matchable"] = True mission_instance.save() if sku_instance is not None: productmission_data["sku"] = sku_instance productmission_instance = ProductMission.objects.filter( sku=sku_instance, mission=mission_instance).first() elif sku != "": productmission_data["no_match_sku"] = sku productmission_instance = ProductMission.objects.filter( no_match_sku=sku, mission=mission_instance).first() print(f"why {channel_order_id} - {productmission_data}") if sku != "" or sku_instance is not None: if productmission_instance is None: productmission_instance = ProductMission.objects.create( **productmission_data) productmission_instance.save() mission_instance.save( ) # damit der richtige Status gesetzt wird
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)