Unit tests, fixes

pull/690/head
Piero Toffanin 2019-07-10 10:34:33 -04:00
rodzic fcc71bbc36
commit 4b45d4dd8f
6 zmienionych plików z 59 dodań i 20 usunięć

Wyświetl plik

@ -52,13 +52,13 @@ def sync_plugin_db():
# Plugins that have a "disabled" file are disabled
disabled_path = plugin.get_path("disabled")
disabled = os.path.isfile(disabled_path)
if not disabled:
_, created = Plugin.objects.get_or_create(
name=plugin.get_name(),
defaults={'enabled': not disabled},
)
if created:
logger.info("Added [{}] plugin to database".format(plugin.get_name()))
_, created = Plugin.objects.get_or_create(
name=plugin.get_name(),
defaults={'enabled': not disabled},
)
if created:
logger.info("Added [{}] plugin to database".format(plugin.get_name()))
def clear_plugins_cache():

Wyświetl plik

@ -18,7 +18,6 @@ class PluginBase(ABC):
def register(self):
self.check_requirements()
def check_requirements(self):
"""
Check if Python requirements need to be installed
@ -34,7 +33,8 @@ class PluginBase(ABC):
if os.path.exists(md5_file):
with open(md5_file, 'r') as f:
md5_mismatch = f.read().strip() != req_md5
else:
reqs_installed = False
if not reqs_installed or md5_mismatch:
logger.info("Installing requirements.txt for {}".format(self))

Wyświetl plik

@ -1,6 +1,7 @@
import os
import shutil
import sys
from django.contrib.auth.models import User
from django.test import Client
from rest_framework import status
@ -8,10 +9,11 @@ from rest_framework import status
from app.models import Plugin
from app.models import Project
from app.models import Task
from app.plugins import UserDataStore
from app.plugins import UserDataStore, enable_plugin
from app.plugins import get_plugin_by_name
from app.plugins import sync_plugin_db
from app.plugins import sync_plugin_db, get_plugins_persistent_path
from app.plugins.data_store import InvalidDataStoreValue
from app.plugins.pyutils import parse_requirements, compute_file_md5, requirements_installed
from .classes import BootTestCase
from app.plugins.grass_engine import grass, GrassEngineException
@ -36,6 +38,11 @@ class TestPlugins(BootTestCase):
self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertTemplateUsed(res, 'plugins/test/templates/app.html')
# No python packages have been installed (plugin is disabled)
self.assertFalse(os.path.exists(get_plugins_persistent_path("test", "site-packages")))
enable_plugin("test")
# Form was rendered correctly
self.assertContains(res, '<input type="text" name="testField" class="form-control" required id="id_testField" />', count=1, status_code=200, html=True)
@ -89,6 +96,33 @@ class TestPlugins(BootTestCase):
self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertTrue(res.content.decode('utf-8') == "console.log('Hello WebODM');") # Empty
# Check that the plugins media dirs have been created
self.assertTrue(os.path.exists(get_plugins_persistent_path()))
self.assertTrue(os.path.exists(get_plugins_persistent_path("test", "site-packages")))
self.assertEqual(get_plugins_persistent_path("test", "site-packages"), test_plugin.get_python_packages_path())
# Check MD5 install has been created
self.assertTrue(os.path.exists(test_plugin.get_python_packages_path("install_md5")))
with open(test_plugin.get_python_packages_path("install_md5"), "r") as f:
md5 = f.read().strip()
self.assertTrue(len(md5) > 20)
self.assertEqual(md5, compute_file_md5(test_plugin.get_path("requirements.txt")))
self.assertTrue(requirements_installed(test_plugin.get_path("requirements.txt"), test_plugin.get_python_packages_path()))
# Test python imports context
self.assertFalse(test_plugin.get_python_packages_path() in sys.path)
with test_plugin.python_imports():
self.assertTrue(test_plugin.get_python_packages_path() in sys.path)
self.assertFalse(test_plugin.get_python_packages_path() in sys.path)
# Parse requirements test
self.assertEqual(parse_requirements(test_plugin.get_path("requirements.txt"))[0], "pyodm")
# Current plugin test
self.assertEqual(test_plugin.get_current_plugin_test(), test_plugin)
def test_grass_engine(self):
cwd = os.path.dirname(os.path.realpath(__file__))
@ -115,9 +149,9 @@ class TestPlugins(BootTestCase):
ctx.set_location("EPSG:4326")
result = execute_grass_script.delay(
os.path.join(grass_scripts_dir, "simple_test.grass"),
ctx.serialize()
).get()
os.path.join(grass_scripts_dir, "simple_test.grass"),
ctx.serialize()
).get()
self.assertTrue("Number of points: 1" in result.get('output'))
self.assertTrue(result.get('context') == ctx.serialize())
@ -126,9 +160,9 @@ class TestPlugins(BootTestCase):
self.assertFalse(os.path.exists(ctx.get_cwd()))
error = execute_grass_script.delay(
os.path.join(grass_scripts_dir, "nonexistant_script.grass"),
ctx.serialize()
).get()
os.path.join(grass_scripts_dir, "nonexistant_script.grass"),
ctx.serialize()
).get()
self.assertIsInstance(error, dict)
self.assertIsInstance(error['error'], str)
@ -154,6 +188,7 @@ class TestPlugins(BootTestCase):
self.assertFalse(os.path.exists(ctx.get_cwd()))
def test_plugin_datastore(self):
enable_plugin("test")
test_plugin = get_plugin_by_name("test")
user = User.objects.get(username='testuser')
other_user = User.objects.get(username='testuser2')
@ -221,6 +256,8 @@ class TestPlugins(BootTestCase):
c.login(username='testsuperuser', password='test1234')
enable_plugin("test")
# Test plugin is enabled
res = c.get('/admin/app/plugin/')
self.assertContains(res, '<a class="button" href="#" disabled>Enable</a>')
@ -262,7 +299,7 @@ class TestPlugins(BootTestCase):
self.assertTrue(Plugin.objects.filter(pk='test_copy').count() == 0)
# Get manifest works and parses JSON
p = get_plugin_by_name("test")
p = get_plugin_by_name("test", only_active=False)
self.assertEqual(p.get_manifest()['author'], "Piero Toffanin")

Wyświetl plik

Wyświetl plik

@ -1,7 +1,7 @@
from rest_framework import status
from rest_framework.response import Response
from app.plugins import PluginBase, Menu, MountPoint
from app.plugins import PluginBase, Menu, MountPoint, get_current_plugin
from app.plugins.views import TaskView
from django.shortcuts import render
from django import forms
@ -47,4 +47,5 @@ class Plugin(PluginBase):
MountPoint('/app_dynamic_script.js$', self.get_dynamic_script('dynamic.js', dynamic_cb))
]
def get_current_plugin_test(self):
return get_current_plugin()

Wyświetl plik

@ -0,0 +1 @@
pyodm==1.5.3b1