2021-04-07 16:04:27 +01:00
|
|
|
"""
|
|
|
|
We are using unittest for troggle.
|
|
|
|
|
|
|
|
Note that the database has not been parsed from the source files when these tests are run,
|
|
|
|
so any path that relies on data being in the database will fail.
|
|
|
|
|
|
|
|
The simple redirections to files which exist, e.g. in
|
|
|
|
/expoweb/
|
|
|
|
/expofiles/
|
|
|
|
/expofiles/documents/
|
|
|
|
etc. will test fine.
|
|
|
|
|
|
|
|
But paths like this:
|
|
|
|
/survey_scans/
|
|
|
|
/caves/
|
|
|
|
which rely on database resolution will fail unless a fixture has been set up for
|
|
|
|
them.
|
|
|
|
|
|
|
|
https://docs.djangoproject.com/en/3.0/topics/testing/tools/
|
|
|
|
"""
|
|
|
|
import unittest
|
|
|
|
import re
|
2021-05-13 21:15:30 +01:00
|
|
|
import subprocess
|
|
|
|
|
2021-04-07 16:04:27 +01:00
|
|
|
from django.test import TestCase, SimpleTestCase, Client
|
|
|
|
|
|
|
|
class SimpleTest(SimpleTestCase):
|
|
|
|
def test_test_setting(self):
|
|
|
|
from django.conf import settings
|
|
|
|
self.assertEqual(settings.EMAIL_BACKEND, 'django.core.mail.backends.locmem.EmailBackend')
|
2021-05-13 21:15:30 +01:00
|
|
|
import troggle.settings as settings
|
2021-04-07 16:04:27 +01:00
|
|
|
def test_import_TroggleModel(self):
|
2021-04-13 00:43:57 +01:00
|
|
|
from troggle.core.models.troggle import TroggleModel
|
2021-04-07 16:04:27 +01:00
|
|
|
def test_import_Cave(self):
|
2021-04-13 00:47:17 +01:00
|
|
|
from troggle.core.models.caves import Cave
|
2021-04-07 16:04:27 +01:00
|
|
|
def test_import_parsers_surveys(self):
|
2022-03-02 21:15:24 +00:00
|
|
|
#from PIL import Image
|
2021-04-13 00:11:08 +01:00
|
|
|
from troggle.core.utils import save_carefully
|
2021-04-07 16:04:27 +01:00
|
|
|
from functools import reduce
|
|
|
|
def test_import_parsers_survex(self):
|
|
|
|
import troggle.settings as settings
|
2021-04-13 00:43:57 +01:00
|
|
|
import troggle.core.models.troggle as models
|
2021-04-13 00:50:12 +01:00
|
|
|
import troggle.core.models.survex as models_survex
|
2021-04-17 20:45:38 +01:00
|
|
|
import troggle.core.models.caves as models_caves
|
2021-04-07 16:04:27 +01:00
|
|
|
from troggle.parsers.people import GetPersonExpeditionNameLookup
|
2021-05-05 00:35:10 +01:00
|
|
|
from troggle.core.views.other import frontpage
|
2021-04-17 20:45:38 +01:00
|
|
|
from troggle.core.views.caves import ent, cavepage
|
2021-05-05 00:35:10 +01:00
|
|
|
from troggle.core.views import scans, drawings, other, caves, statistics, survex, uploads
|
|
|
|
def test_import_views_uploads(self):
|
|
|
|
from troggle.core.views.uploads import dwgupload, scanupload
|
2021-04-07 16:04:27 +01:00
|
|
|
def test_import_parsers_QMs(self):
|
2021-04-13 00:47:17 +01:00
|
|
|
from troggle.core.models.caves import QM, Cave, LogbookEntry
|
2021-04-07 16:04:27 +01:00
|
|
|
def test_import_parsers_people(self):
|
2022-03-02 23:19:48 +00:00
|
|
|
from html import unescape
|
2021-04-07 16:04:27 +01:00
|
|
|
from unidecode import unidecode
|
|
|
|
def test_import_parsers_logbooks(self):
|
|
|
|
from django.template.defaultfilters import slugify
|
|
|
|
from django.utils.timezone import get_current_timezone, make_aware
|
2021-04-13 00:43:57 +01:00
|
|
|
from troggle.core.models.troggle import DataIssue, Expedition
|
2021-04-13 00:47:17 +01:00
|
|
|
from troggle.core.models.caves import Cave, LogbookEntry, PersonTrip
|
2021-04-07 16:04:27 +01:00
|
|
|
from parsers.people import GetPersonExpeditionNameLookup
|
|
|
|
def test_import_core_views_caves(self):
|
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
|
|
|
from django.shortcuts import get_object_or_404, render
|
|
|
|
import troggle.core.views.expo
|
2021-04-13 00:43:57 +01:00
|
|
|
from troggle.core.models.troggle import Expedition
|
2021-04-13 00:47:17 +01:00
|
|
|
from troggle.core.models.caves import CaveSlug, Cave, CaveAndEntrance, QM, EntranceSlug, Entrance, Area, SurvexStation
|
2021-04-26 02:10:45 +01:00
|
|
|
from troggle.core.forms import CaveForm, CaveAndEntranceFormSet, EntranceForm, EntranceLetterForm
|
2021-05-03 20:36:29 +01:00
|
|
|
from troggle.core.views.auth import login_required_if_public
|
2021-04-07 16:04:27 +01:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.conf import settings
|
|
|
|
def test_import_parsers_mix(self):
|
|
|
|
from troggle.parsers.logbooks import GetCaveLookup
|
|
|
|
import troggle.settings
|
|
|
|
import troggle.parsers.caves
|
|
|
|
import troggle.parsers.people
|
2021-05-03 20:36:29 +01:00
|
|
|
import troggle.parsers.drawings
|
|
|
|
import troggle.parsers.scans
|
2021-04-07 16:04:27 +01:00
|
|
|
import troggle.parsers.logbooks
|
|
|
|
import troggle.parsers.QMs
|
|
|
|
import troggle.parsers.survex
|
|
|
|
def test_import_imports(self):
|
|
|
|
from django.core import management
|
|
|
|
from django.db import connection, close_old_connections, connections
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.urls import reverse
|
|
|
|
|
|
|
|
def test_import_urls(self):
|
|
|
|
from django.conf import settings
|
|
|
|
from django.conf.urls import url, include
|
2021-04-17 20:45:38 +01:00
|
|
|
from django.contrib import admin
|
|
|
|
from django.contrib import auth
|
|
|
|
from django.urls import reverse, resolve
|
2021-04-07 16:04:27 +01:00
|
|
|
from django.views.generic.base import RedirectView
|
|
|
|
from django.views.generic.edit import UpdateView
|
|
|
|
from django.views.generic.list import ListView
|
2021-05-03 20:36:29 +01:00
|
|
|
from troggle.core.views import other, caves, statistics, survex
|
2021-04-17 20:45:38 +01:00
|
|
|
from troggle.core.views.auth import expologin, expologout
|
|
|
|
from troggle.core.views.caves import ent, cavepage
|
|
|
|
from troggle.core.views.expo import expofiles_redirect, expofilessingle, expopage, editexpopage, mediapage, map, mapfile
|
|
|
|
from troggle.core.views.logbooks import expedition, personexpedition, Expeditions_tsvListView, Expeditions_jsonListView
|
|
|
|
from troggle.core.views.logbooks import get_logbook_entries, logbookentry, logbookSearch
|
2021-04-30 21:32:53 +01:00
|
|
|
from troggle.core.views.logbooks import notablepersons, person, get_people
|
2021-05-05 00:35:10 +01:00
|
|
|
from troggle.core.views.other import controlpanel
|
2021-04-17 20:45:38 +01:00
|
|
|
from troggle.core.views.prospect import prospecting
|
|
|
|
from troggle.core.views.prospect import prospecting_image
|
|
|
|
from troggle.core.views.statistics import pathsreport, stats, dataissues
|
|
|
|
from troggle.core.views.survex import survexcaveslist, survexcavesingle, svx
|
|
|
|
|
2021-05-13 21:15:30 +01:00
|
|
|
class SubprocessTest(TestCase):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
pass
|
|
|
|
|
2022-06-23 14:03:50 +01:00
|
|
|
def test_utf8(self):
|
|
|
|
'''Expects that utf8 is the default encoding when opening files
|
|
|
|
'''
|
|
|
|
import sys
|
|
|
|
import locale
|
|
|
|
self.assertTrue( sys.getdefaultencoding() == "utf-8", f'{sys.getdefaultencoding()} - UTF8 error in getdefaultencoding')
|
|
|
|
self.assertTrue( sys.getfilesystemencoding() == "utf-8", f'{sys.getfilesystemencoding()} - UTF8 error in getfilesystemencoding')
|
|
|
|
self.assertTrue( locale.getdefaultlocale()[1] == "UTF-8", f'{locale.getdefaultlocale()} - UTF8 error in locale.getdefaultlocale')
|
|
|
|
self.assertTrue( locale.getpreferredencoding() == "UTF-8", f'{locale.getpreferredencoding()} - UTF8 error in locale.getpreferredencoding')
|
|
|
|
|
2021-05-13 21:15:30 +01:00
|
|
|
def test_installs(self):
|
2022-03-05 18:02:01 +00:00
|
|
|
''' Expects external software installed: cavern, survexport, git
|
2021-10-31 17:25:45 +00:00
|
|
|
(but not whether it actually works)
|
2021-10-31 16:01:14 +00:00
|
|
|
'''
|
2021-05-13 21:15:30 +01:00
|
|
|
import troggle.settings as settings
|
|
|
|
|
2021-10-31 16:01:14 +00:00
|
|
|
for i in [settings.CAVERN, settings.SURVEXPORT, settings.GIT]:
|
2021-05-13 21:15:30 +01:00
|
|
|
# Define command as string and then split() into list format
|
|
|
|
cmd = f'which {i}'.split()
|
|
|
|
try:
|
|
|
|
sp = subprocess.check_call(cmd, shell=False)
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
self.assertTrue( False, f'no {i} installed')
|
|
|
|
|
2022-03-05 17:42:12 +00:00
|
|
|
def test_repos_git_status(self):
|
2022-03-05 18:02:01 +00:00
|
|
|
''' Expects clean git repos with no added files and no merge failures
|
2022-03-05 17:42:12 +00:00
|
|
|
'''
|
|
|
|
from pathlib import Path
|
|
|
|
import troggle.settings as settings
|
2022-03-05 18:02:01 +00:00
|
|
|
TROGGLE_PATH = Path(settings.REPOS_ROOT_PATH) / "troggle"
|
|
|
|
for cwd in [settings.EXPOWEB, settings.DRAWINGS_DATA, TROGGLE_PATH]: # add settings.SURVEX_DATA when loser is gitified
|
2022-03-05 17:42:12 +00:00
|
|
|
sp = subprocess.run([settings.GIT, "status"], cwd=cwd, capture_output=True, text=True)
|
2022-03-05 18:02:01 +00:00
|
|
|
scwd = str(cwd)
|
|
|
|
#print(scwd + ":\n\n" + sp.stderr + '\n\n' + sp.stdout + '\n\nreturn code: ' + str(sp.returncode))
|
2022-03-05 17:42:12 +00:00
|
|
|
if sp.returncode != 0:
|
2022-03-05 18:02:01 +00:00
|
|
|
print(scwd + ":\n\n" + sp.stderr + '\n\n' + sp.stdout + '\n\nreturn code: ' + str(sp.returncode))
|
|
|
|
self.assertTrue( sp.returncode == 0, f'{scwd} - git is unhappy')
|
2022-03-05 17:42:12 +00:00
|
|
|
|
|
|
|
content = sp.stdout
|
2022-06-25 01:05:29 +01:00
|
|
|
ph = r'Your branch is up[ -]to[ -]date'
|
2022-03-05 17:42:12 +00:00
|
|
|
phmatch = re.search(ph, content)
|
2022-03-05 18:02:01 +00:00
|
|
|
self.assertIsNotNone(phmatch, f"{scwd} - Failed to find expected git output: '" + ph +"'")
|
2022-03-05 17:42:12 +00:00
|
|
|
|
|
|
|
ph1 = r'no changes added to commit'
|
|
|
|
phmatch1 = re.search(ph1, content)
|
|
|
|
ph2 = r'nothing to commit'
|
|
|
|
phmatch2 = re.search(ph2, content)
|
|
|
|
phmatch = phmatch1 or phmatch2
|
2022-03-05 18:02:01 +00:00
|
|
|
self.assertIsNotNone(phmatch, f"{scwd} - Failed to find expected git output: {ph1} or {ph2}")
|
2021-04-07 16:04:27 +01:00
|
|
|
|
|
|
|
|