All bot commands now have their basic functionality reimplemented
+All commands should return a json or list of jsons +Basic tests have been added for each commanddoc_update
parent
83a1b7a175
commit
61433c271f
|
@ -36,7 +36,10 @@ def get_player(discord_uuid=None, mc_uuid=None):
|
|||
|
||||
def get_location(owner, name=None, loc_type=Location):
|
||||
if name is None:
|
||||
loc_list = Location.objects.all().select_related(loc_type.__name__).get(owner=owner)
|
||||
if loc_type == Location:
|
||||
loc_list = Location.objects.all().filter(owner=owner)
|
||||
else:
|
||||
loc_list = Location.objects.all().select_related(loc_type.__name__.lower()).filter(owner=owner)
|
||||
if len(loc_list) == 1:
|
||||
loc = loc_list[0]
|
||||
elif len(loc_list) == 0:
|
||||
|
@ -44,7 +47,10 @@ def get_location(owner, name=None, loc_type=Location):
|
|||
else:
|
||||
raise EntryNameNotUniqueError
|
||||
else:
|
||||
loc_list = Location.objects.all().select_related(loc_type.__name__).get(owner=owner, name=name)
|
||||
if loc_type == Location:
|
||||
loc_list = Location.objects.all().filter(owner=owner)
|
||||
else:
|
||||
loc_list = Location.objects.all().select_related(loc_type.__name__.lower()).filter(owner=owner)
|
||||
if len(loc_list) == 1:
|
||||
loc = loc_list[0]
|
||||
else:
|
||||
|
@ -70,13 +76,13 @@ def add_base(x_pos, z_pos, name=None, discord_uuid=None, mc_uuid=None):
|
|||
def add_shop(x_pos, z_pos, name=None, discord_uuid=None, mc_uuid=None):
|
||||
return add_location(x_pos, z_pos, name=name, discord_uuid=discord_uuid, mc_uuid=mc_uuid, loc_type=Shop)
|
||||
|
||||
@command("POST")
|
||||
|
||||
def add_location(x_pos, z_pos, name=None, discord_uuid=None, mc_uuid=None, loc_type=Location):
|
||||
player = get_player(discord_uuid, mc_uuid)
|
||||
try:
|
||||
get_location(player, name, loc_type=loc_type)
|
||||
raise EntryNameNotUniqueError
|
||||
except (NoLocationsInDatabase, LocationLookUpError):
|
||||
except Location.DoesNotExist:
|
||||
if name is None:
|
||||
name = "{}'s {}".format(player.name, loc_type.__name__)
|
||||
|
||||
|
@ -97,7 +103,8 @@ def add_tunnel(tunnel_direction, tunnel_number, location_name=None, discord_uuid
|
|||
loc = get_location(player, name=location_name)
|
||||
location_name = loc.name
|
||||
|
||||
tunnel = Tunnel.objects.create(tunnel_direction, tunnel_number, Location=get_location(player, location_name))
|
||||
tunnel = Tunnel.objects.create(tunnel_direction=tunnel_direction, tunnel_number=tunnel_number,
|
||||
location=get_location(player, location_name))
|
||||
|
||||
return tunnel
|
||||
|
||||
|
@ -106,18 +113,22 @@ def add_tunnel(tunnel_direction, tunnel_number, location_name=None, discord_uuid
|
|||
def find_location(search):
|
||||
limit = 25
|
||||
|
||||
locations = Location.objects.filter(Q(name__icontains=search) | Q(owner__name__icontains=search))[:limit]
|
||||
locations_obj = Location.objects.filter(Q(name__icontains=search) | Q(owner__name__icontains=search)).all()[:limit]
|
||||
|
||||
if len(locations) == 0:
|
||||
if len(locations_obj) == 0:
|
||||
raise LocationLookUpError
|
||||
|
||||
return locations
|
||||
locations_json = []
|
||||
for location in locations_obj:
|
||||
locations_json.append(location.json)
|
||||
|
||||
return locations_json
|
||||
|
||||
|
||||
@command("DELETE")
|
||||
def delete(name, discord_uuid=None, mc_uuid=None):
|
||||
owner = get_player(discord_uuid, mc_uuid)
|
||||
Location.objects.get(name__iexact=name, owner=owner)
|
||||
Location.objects.get(name__iexact=name, owner=owner).delete()
|
||||
|
||||
|
||||
@command("GET")
|
||||
|
@ -132,9 +143,9 @@ def find_around(x_pos, z_pos, radius=200):
|
|||
def add_item(item_name, quantity, diamond_price, shop_name=None, discord_uuid=None, mc_uuid=None):
|
||||
player = get_player(discord_uuid, mc_uuid)
|
||||
|
||||
shop = get_location(player, shop_name, Shop)
|
||||
shop = get_location(player, shop_name, Shop).shop
|
||||
|
||||
item_listing = ItemListing.objects.create(shop=shop, quantity=quantity, price=diamond_price, item_name=item_name)
|
||||
item_listing = ItemListing.objects.create(shop=shop, amount=quantity, price=diamond_price, item_name=item_name)
|
||||
|
||||
return item_listing
|
||||
|
||||
|
@ -154,8 +165,12 @@ def selling(item_name):
|
|||
|
||||
for shop_id in shops:
|
||||
shop = Shop.objects.get(pk=shop_id['shop_id']).json
|
||||
item_query = ItemListing.objects.annotate(normalized_price=F('price') / F('amount')).\
|
||||
filter(item_name__icontains=item_name).order_by('normalized_price').all()
|
||||
|
||||
item_query = ItemListing.objects.annotate(normalized_price=F('price') / F('amount')) \
|
||||
.filter(item_name__icontains=item_name, shop_id=shop_id) \
|
||||
.order_by('normalized_price') \
|
||||
.values("item_name", "amount", "price")
|
||||
|
||||
item_list = []
|
||||
for item in item_query:
|
||||
item_list.append(item)
|
||||
|
@ -168,18 +183,30 @@ def selling(item_name):
|
|||
|
||||
@command("GET")
|
||||
def info(location_name):
|
||||
loc = Location.objects.get(name__iexact=location_name)
|
||||
return loc.json
|
||||
location = Location.objects.get(name__iexact=location_name)
|
||||
|
||||
if location is None:
|
||||
location = Location.objects.get(name__iregex="*{}*".format(location_name))
|
||||
|
||||
if location is not None:
|
||||
return location.json
|
||||
else:
|
||||
raise LocationLookUpError
|
||||
|
||||
|
||||
@command("GET")
|
||||
def tunnel(player_name):
|
||||
tunnels = Tunnel.objects.get(location__owner__name__icontains=player_name)
|
||||
tunnels_obj = Tunnel.objects.filter(location__owner__name__icontains=player_name).all()
|
||||
|
||||
if len(tunnels) == 0:
|
||||
if len(tunnels_obj) == 0:
|
||||
raise LocationLookUpError
|
||||
|
||||
return tunnel
|
||||
tunnels_json = []
|
||||
|
||||
for t in tunnels_obj:
|
||||
tunnels_json.append(t.json)
|
||||
|
||||
return tunnels_json
|
||||
|
||||
|
||||
@command("POST")
|
||||
|
@ -188,8 +215,8 @@ def edit_pos(x, z, loc_name, discord_uuid=None, mc_uuid=None):
|
|||
player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid)
|
||||
location = get_location(player, loc_name)
|
||||
|
||||
location.x = x
|
||||
location.z = z
|
||||
location.x_coord = x
|
||||
location.z_coord = z
|
||||
location.save()
|
||||
|
||||
return location
|
||||
|
@ -220,7 +247,7 @@ def edit_name(new_name, loc_name, discord_uuid=None, mc_uuid=None):
|
|||
return location
|
||||
|
||||
|
||||
@command("POST")
|
||||
@command("DELETE")
|
||||
def delete_item(item, shop_name, discord_uuid=None, mc_uuid=None):
|
||||
player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid)
|
||||
|
||||
|
@ -235,10 +262,13 @@ def delete_item(item, shop_name, discord_uuid=None, mc_uuid=None):
|
|||
def me(discord_uuid=None, mc_uuid=None):
|
||||
player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid)
|
||||
|
||||
locations = Location.objects.get(owner=player)
|
||||
locations_obj = Location.objects.filter(owner=player).all()
|
||||
|
||||
if len(locations) == 0:
|
||||
if len(locations_obj) == 0:
|
||||
raise PlayerNotFound
|
||||
|
||||
return locations
|
||||
locations_json = []
|
||||
for location in locations_obj:
|
||||
locations_json.append(location.json)
|
||||
|
||||
return locations_json
|
||||
|
|
|
@ -82,3 +82,8 @@ class Tunnel(models.Model):
|
|||
|
||||
def __str__(self):
|
||||
return "Tunnel: %s %d" % (self.tunnel_direction, self.tunnel_number)
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
return {"Location_name": self.location.name, "tunnel_direction": self.tunnel_direction,
|
||||
"tunnel_number": self.tunnel_number}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from django.test import TestCase
|
||||
from GeoffreyApp.api.commands import *
|
||||
from GeoffreyApp.models import *
|
||||
|
||||
# Create your tests here.
|
||||
|
||||
MC_UUID = "fe7e84132570458892032b69ff188bc3"
|
||||
|
@ -10,7 +11,23 @@ USERNAME = "ZeroHD"
|
|||
|
||||
class CommandsAPITestCase(TestCase):
|
||||
def setUp(self):
|
||||
Player.objects.create(name=USERNAME, mc_uuid=MC_UUID, discord_uuid=DISCORD_UUID)
|
||||
self.player = Player.objects.create(name=USERNAME, mc_uuid=MC_UUID, discord_uuid=DISCORD_UUID)
|
||||
|
||||
def tearDown(self):
|
||||
Player.objects.all().delete()
|
||||
Base.objects.all().delete()
|
||||
Location.objects.all().delete()
|
||||
Shop.objects.all().delete()
|
||||
ItemListing.objects.all().delete()
|
||||
Tunnel.objects.all().delete()
|
||||
|
||||
def populate(self):
|
||||
base = Base.objects.create(owner=self.player, name="test", x_coord=0, z_coord=0)
|
||||
shop = Shop.objects.create(owner=self.player, name="test shop", x_coord=0, z_coord=0)
|
||||
|
||||
item = ItemListing.objects.create(shop=shop, price=1, amount=5, item_name="sed")
|
||||
|
||||
tunnel = Tunnel.objects.create(tunnel_number="42", tunnel_direction="S", location=base)
|
||||
|
||||
def test_register(self):
|
||||
command_dict["POST"]["register"]["func"](player_name="Vakky", discord_uuid="229423434256351233")
|
||||
|
@ -18,3 +35,96 @@ class CommandsAPITestCase(TestCase):
|
|||
count = Player.objects.filter(mc_uuid__iexact="7afbf6632bf049ef915f22e81b298d17").count()
|
||||
|
||||
self.assertEqual(count, 1)
|
||||
|
||||
def test_add_base(self):
|
||||
command_dict["POST"]["add_base"]["func"](x_pos=0, z_pos=0, name=None, discord_uuid=DISCORD_UUID)
|
||||
|
||||
base = Base.objects.filter(name__icontains=USERNAME).all().first()
|
||||
|
||||
self.assertEqual(base.owner.name, "ZeroHD")
|
||||
|
||||
def test_add_shop(self):
|
||||
command_dict["POST"]["add_shop"]["func"](x_pos=0, z_pos=0, name=None, discord_uuid=DISCORD_UUID)
|
||||
|
||||
shop = Shop.objects.filter(name__icontains=USERNAME).all().first()
|
||||
|
||||
self.assertEqual(shop.owner.name, "zerohd")
|
||||
|
||||
def test_add_tunnel(self):
|
||||
base = Base.objects.create(owner=self.player, name="Test", x_coord=0, z_coord=0)
|
||||
command_dict["POST"]["add_tunnel"]["func"](tunnel_direction="N", tunnel_number=500, discord_uuid=DISCORD_UUID)
|
||||
|
||||
qbase = Base.objects.filter(tunnel_location__tunnel_direction="N").all().first()
|
||||
|
||||
self.assertEqual(base.id, qbase.id)
|
||||
|
||||
def test_add_item(self):
|
||||
shop = Shop.objects.create(owner=self.player, name="Test", x_coord=0, z_coord=0)
|
||||
command_dict["POST"]["add_item"]["func"](item_name="sed", quantity=5, diamond_price=5,
|
||||
discord_uuid=DISCORD_UUID)
|
||||
|
||||
item = ItemListing.objects.filter(shop_id=shop.id).all().first()
|
||||
|
||||
self.assertEqual(item.item_name, "sed")
|
||||
|
||||
def test_edit_post(self):
|
||||
shop = Shop.objects.create(owner=self.player, name="Test", x_coord=0, z_coord=0)
|
||||
|
||||
command_dict["POST"]["edit_pos"]["func"](x=500, z=500, loc_name="Test", discord_uuid=DISCORD_UUID)
|
||||
|
||||
qshop = Shop.objects.filter(name__exact="Test").all().first()
|
||||
|
||||
self.assertEqual(qshop.x_coord, 500)
|
||||
|
||||
def test_find_location(self):
|
||||
self.populate()
|
||||
locations = command_dict["GET"]["find_location"]["func"](search="Test")
|
||||
|
||||
count = len(locations)
|
||||
|
||||
self.assertEqual(count, 2)
|
||||
|
||||
def test_selling(self):
|
||||
self.populate()
|
||||
|
||||
items = command_dict["GET"]["selling"]["func"](item_name="sED")
|
||||
|
||||
self.assertEqual(len(items), 1)
|
||||
|
||||
def test_info(self):
|
||||
self.populate()
|
||||
location = command_dict["GET"]["info"]["func"](location_name="test")
|
||||
|
||||
self.assertEqual(location["Name"], "test")
|
||||
|
||||
def test_tunnel(self):
|
||||
self.populate()
|
||||
|
||||
tunnels = command_dict["GET"]["tunnel"]["func"](player_name=USERNAME)
|
||||
|
||||
self.assertEqual(len(tunnels), 1)
|
||||
|
||||
def test_me(self):
|
||||
self.populate()
|
||||
|
||||
locations = command_dict["GET"]["me"]["func"](discord_uuid=DISCORD_UUID)
|
||||
|
||||
self.assertEqual(len(locations), 2)
|
||||
|
||||
def test_delete(self):
|
||||
self.populate()
|
||||
|
||||
command_dict["DELETE"]["delete"]["func"](name="test", discord_uuid=DISCORD_UUID)
|
||||
|
||||
locations = Location.objects.filter(name__icontains="test").all()
|
||||
|
||||
self.assertEqual(len(locations), 1)
|
||||
|
||||
def delete_item(self):
|
||||
self.populate()
|
||||
|
||||
command_dict["DELETE"]["delete_item"]["func"](item="sed", shop_name="test shop", discord_uuid=DISCORD_UUID)
|
||||
|
||||
items = ItemListing.objects.filter(item_name__iexact="sed").all()
|
||||
|
||||
self.assertEqual(len(items), 0)
|
||||
|
|
Loading…
Reference in New Issue