Added test file for commands and redid command decorators

+ Command decorator now puts functions to to one dict
+ Params, name, and the function object are in this dict.
doc_update
Joey Hines 2018-12-17 15:16:21 -06:00
parent 59bac55fd1
commit 83a1b7a175
4 changed files with 61 additions and 71 deletions

View File

@ -2,31 +2,25 @@ from django.db.models import Q, F
from GeoffreyApp.models import * from GeoffreyApp.models import *
from GeoffreyApp.errors import * from GeoffreyApp.errors import *
from GeoffreyApp.minecraft_api import * from GeoffreyApp.minecraft_api import *
import inspect
post_list = [] command_dict = {"GET": {}, "POST": {}, "DELETE": {}}
get_list = []
delete_list = []
def post(func): def getRequiredArgs(func):
def command(): args = inspect.getfullargspec(func)
post_list.append(func)
return command() return args.args + args.kwonlyargs
def delete(func): def command(type):
def command(): def command_dec(func):
delete_list.append(func) def add_command():
command_dict[type][func.__name__] = {"func": func, "params": getRequiredArgs(func)}
return add_command()
return command() return command_dec
def get(func):
def command():
get_list.append(func)
return command()
def get_player(discord_uuid=None, mc_uuid=None): def get_player(discord_uuid=None, mc_uuid=None):
@ -59,7 +53,7 @@ def get_location(owner, name=None, loc_type=Location):
return loc return loc
@post @command("POST")
def register(player_name, discord_uuid): def register(player_name, discord_uuid):
mc_uuid = grab_UUID(player_name) mc_uuid = grab_UUID(player_name)
player = Player.objects.create(name=player_name, mc_uuid=mc_uuid, discord_uuid=discord_uuid) player = Player.objects.create(name=player_name, mc_uuid=mc_uuid, discord_uuid=discord_uuid)
@ -67,16 +61,16 @@ def register(player_name, discord_uuid):
return player.json return player.json
@post @command("POST")
def add_base(x_pos, z_pos, name=None, discord_uuid=None, mc_uuid=None): def add_base(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=Base) return add_location(x_pos, z_pos, name=name, discord_uuid=discord_uuid, mc_uuid=mc_uuid, loc_type=Base)
@post @command("POST")
def add_shop(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) 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): 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) player = get_player(discord_uuid, mc_uuid)
try: try:
@ -96,7 +90,7 @@ def add_location(x_pos, z_pos, name=None, discord_uuid=None, mc_uuid=None, loc_t
return location return location
@post @command("POST")
def add_tunnel(tunnel_direction, tunnel_number, location_name=None, discord_uuid=None, mc_uuid=None): def add_tunnel(tunnel_direction, tunnel_number, location_name=None, discord_uuid=None, mc_uuid=None):
player = get_player(discord_uuid, mc_uuid) player = get_player(discord_uuid, mc_uuid)
if location_name is None: if location_name is None:
@ -108,7 +102,7 @@ def add_tunnel(tunnel_direction, tunnel_number, location_name=None, discord_uuid
return tunnel return tunnel
@get @command("GET")
def find_location(search): def find_location(search):
limit = 25 limit = 25
@ -120,13 +114,13 @@ def find_location(search):
return locations return locations
@delete @command("DELETE")
def delete(name, discord_uuid=None, mc_uuid=None): def delete(name, discord_uuid=None, mc_uuid=None):
owner = get_player(discord_uuid, mc_uuid) owner = get_player(discord_uuid, mc_uuid)
Location.objects.get(name__iexact=name, owner=owner) Location.objects.get(name__iexact=name, owner=owner)
@get @command("GET")
def find_around(x_pos, z_pos, radius=200): def find_around(x_pos, z_pos, radius=200):
locations = Location.objects.get(x_coord__range=(x_pos - radius, x_pos + radius), locations = Location.objects.get(x_coord__range=(x_pos - radius, x_pos + radius),
z_coord__range=(z_pos - radius, z_pos + radius), dimension='O') z_coord__range=(z_pos - radius, z_pos + radius), dimension='O')
@ -134,7 +128,7 @@ def find_around(x_pos, z_pos, radius=200):
return locations return locations
@post @command("POST")
def add_item(item_name, quantity, diamond_price, shop_name=None, discord_uuid=None, mc_uuid=None): def add_item(item_name, quantity, diamond_price, shop_name=None, discord_uuid=None, mc_uuid=None):
player = get_player(discord_uuid, mc_uuid) player = get_player(discord_uuid, mc_uuid)
@ -146,7 +140,7 @@ def add_item(item_name, quantity, diamond_price, shop_name=None, discord_uuid=No
# TODO Re-implement selling shop search # TODO Re-implement selling shop search
@get @command("GET")
def selling(item_name): def selling(item_name):
items = [] items = []
if len(item_name) == 0: if len(item_name) == 0:
@ -172,13 +166,13 @@ def selling(item_name):
return items return items
@get @command("GET")
def info(location_name): def info(location_name):
loc = Location.objects.get(name__iexact=location_name) loc = Location.objects.get(name__iexact=location_name)
return loc return loc.json
@get @command("GET")
def tunnel(player_name): def tunnel(player_name):
tunnels = Tunnel.objects.get(location__owner__name__icontains=player_name) tunnels = Tunnel.objects.get(location__owner__name__icontains=player_name)
@ -188,7 +182,7 @@ def tunnel(player_name):
return tunnel return tunnel
@post @command("POST")
def edit_pos(x, z, loc_name, discord_uuid=None, mc_uuid=None): def edit_pos(x, z, loc_name, discord_uuid=None, mc_uuid=None):
player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid) player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid)
@ -201,7 +195,7 @@ def edit_pos(x, z, loc_name, discord_uuid=None, mc_uuid=None):
return location return location
@post @command("POST")
def edit_tunnel(tunnel_direction, tunnel_number, loc_name, discord_uuid=None, mc_uuid=None): def edit_tunnel(tunnel_direction, tunnel_number, loc_name, discord_uuid=None, mc_uuid=None):
player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid) player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid)
location = get_location(player, loc_name) location = get_location(player, loc_name)
@ -215,7 +209,7 @@ def edit_tunnel(tunnel_direction, tunnel_number, loc_name, discord_uuid=None, mc
return location return location
@post @command("POST")
def edit_name(new_name, loc_name, discord_uuid=None, mc_uuid=None): def edit_name(new_name, loc_name, discord_uuid=None, mc_uuid=None):
player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid) player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid)
location = get_location(player, loc_name) location = get_location(player, loc_name)
@ -226,7 +220,7 @@ def edit_name(new_name, loc_name, discord_uuid=None, mc_uuid=None):
return location return location
@post @command("POST")
def delete_item(item, shop_name, discord_uuid=None, mc_uuid=None): def delete_item(item, shop_name, discord_uuid=None, mc_uuid=None):
player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid) player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid)
@ -237,7 +231,7 @@ def delete_item(item, shop_name, discord_uuid=None, mc_uuid=None):
return shop return shop
@get @command("GET")
def me(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) player = get_player(discord_uuid=discord_uuid, mc_uuid=mc_uuid)

View File

@ -7,37 +7,13 @@ import GeoffreyApp.api.commands as commands
from GeoffreyApp.errors import * from GeoffreyApp.errors import *
def getRequiredArgs(func): def run_command(request, command, req_type):
args = inspect.getfullargspec(func)
'''
if defaults:
args = args[:-len(kwonlyargs)]
'''
return args.args + args.kwonlyargs
GET = []
POST = []
DELETE = []
for command in commands.get_list:
GET.append({"name": command.__name__, "params": getRequiredArgs(command)})
for command in commands.post_list:
POST.append({"name": command.__name__, "params": getRequiredArgs(command)})
for command in commands.delete_list:
DELETE.append({"name": command.__name__, "params": getRequiredArgs(command)})
command_response = {"GET": GET, "POST": POST, "DELETE": DELETE}
def run_command(request, command, command_list):
command = command.lower() command = command.lower()
try: try:
for c in command_list: if command in commands.command_dict[req_type]:
if command == c.__name__: response = commands.command_dict[req_type][command]["func"](**request.dict())
response = c(**request.dict()) else:
return JsonResponse(response, safe=False) raise CommandNotFound
raise CommandNotFound
except Exception as e: except Exception as e:
response = {"error": e.__class__.__name__} response = {"error": e.__class__.__name__}
@ -47,18 +23,18 @@ def run_command(request, command, command_list):
class CommandAPI(View): class CommandAPI(View):
def get(self, request, command): def get(self, request, command):
get = request.GET get = request.GET
if command.lower() != "commands": if command.lower() == "commands":
return run_command(get, command, commands.get_list) return JsonResponse(command.commands_dict)
else: else:
return JsonResponse(command_response) return run_command(get, command, "GET")
def post(self, request, command): def post(self, request, command):
post = request.POST post = request.POST
return run_command(post, command, commands.post_list) return run_command(post, command, "POST")
def delete(self, request, command): def delete(self, request, command):
delete = request.DELETE delete = request.DELETE
return run_command(delete, command, commands.delete_list) return run_command(delete, command, "DELETE")
class SettingsAPI(View): class SettingsAPI(View):

View File

@ -35,7 +35,7 @@ class Location(models.Model):
@property @property
def json(self): def json(self):
return {"Type": str(type(self)), "Name": self.name, "x_coord": self.x_coord, "z_coord": self.z_coord, return {"Type": self.__class__.__name__, "Name": self.name, "x_coord": self.x_coord, "z_coord": self.z_coord,
"dimension": self.dimension, "Owner": self.owner.json} "dimension": self.dimension, "Owner": self.owner.json}

View File

@ -0,0 +1,20 @@
from django.test import TestCase
from GeoffreyApp.api.commands import *
from GeoffreyApp.models import *
# Create your tests here.
MC_UUID = "fe7e84132570458892032b69ff188bc3"
DISCORD_UUID = "143072699567177728"
USERNAME = "ZeroHD"
class CommandsAPITestCase(TestCase):
def setUp(self):
Player.objects.create(name=USERNAME, mc_uuid=MC_UUID, discord_uuid=DISCORD_UUID)
def test_register(self):
command_dict["POST"]["register"]["func"](player_name="Vakky", discord_uuid="229423434256351233")
count = Player.objects.filter(mc_uuid__iexact="7afbf6632bf049ef915f22e81b298d17").count()
self.assertEqual(count, 1)