troggle-unchained/core/TESTS/test_imports.py

259 lines
9.8 KiB
Python
Raw Normal View History

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.
2023-02-01 21:58:48 +00:00
https://docs.djangoproject.com/en/3.2/topics/testing/tools/
2021-04-07 16:04:27 +01:00
"""
import re
2021-05-13 21:15:30 +01:00
import subprocess
2023-01-19 18:35:56 +00:00
import unittest
from django.test import Client, SimpleTestCase, TestCase
2021-05-13 21:15:30 +01:00
2021-04-07 16:04:27 +01:00
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):
# from PIL import Image
2021-04-07 16:04:27 +01:00
from functools import reduce
2023-01-19 18:35:56 +00:00
from troggle.core.utils import save_carefully
2021-04-07 16:04:27 +01:00
def test_import_parsers_survex(self):
2021-04-17 20:45:38 +01:00
import troggle.core.models.caves as models_caves
2023-01-19 18:35:56 +00:00
import troggle.core.models.survex as models_survex
import troggle.core.models.troggle as models
import troggle.settings as settings
from troggle.core.views import caves, drawings, other, scans, statistics, survex, uploads
2023-01-19 18:35:56 +00:00
from troggle.core.views.caves import cavepage, ent
from troggle.core.views.other import frontpage
2023-01-19 18:35:56 +00:00
from troggle.parsers.people import GetPersonExpeditionNameLookup
def test_import_views_uploads(self):
2023-01-31 17:13:41 +00:00
from troggle.core.views.uploads import dwgupload
def test_import_views_walletedit(self):
2023-02-01 21:58:48 +00:00
from troggle.core.views.wallets_edit import walletedit
2021-04-07 16:04:27 +01:00
def test_import_parsers_QMs(self):
2023-01-29 18:17:43 +00:00
from troggle.core.models.logbooks import QM
2021-04-07 16:04:27 +01:00
def test_import_parsers_people(self):
from html import unescape
2023-01-19 18:35:56 +00:00
2021-04-07 16:04:27 +01:00
from unidecode import unidecode
2021-04-07 16:04:27 +01:00
def test_import_parsers_logbooks(self):
from django.template.defaultfilters import slugify
from django.utils.timezone import get_current_timezone, make_aware
2023-01-19 18:35:56 +00:00
2021-04-07 16:04:27 +01:00
from parsers.people import GetPersonExpeditionNameLookup
2023-01-30 16:18:19 +00:00
from troggle.core.models.logbooks import CaveSlug, QM, LogbookEntry, PersonLogEntry
2023-01-19 18:35:56 +00:00
from troggle.core.models.troggle import DataIssue, Expedition
2021-04-07 16:04:27 +01:00
def test_import_core_views_caves(self):
2023-01-19 18:35:56 +00:00
from django.conf import settings
from django.contrib.auth.decorators import login_required
2021-04-07 16:04:27 +01:00
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
2023-01-19 18:35:56 +00:00
import troggle.core.views.expo
from troggle.core.forms import CaveAndEntranceFormSet, CaveForm, EntranceForm, EntranceLetterForm
from troggle.core.models.caves import Area, Cave, CaveAndEntrance, Entrance, EntranceSlug, SurvexStation
2021-04-13 00:43:57 +01:00
from troggle.core.models.troggle import Expedition
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
def test_import_parsers_mix(self):
import troggle.parsers.caves
2021-05-03 20:36:29 +01:00
import troggle.parsers.drawings
2021-04-07 16:04:27 +01:00
import troggle.parsers.logbooks
2023-01-19 18:35:56 +00:00
import troggle.parsers.people
2021-04-07 16:04:27 +01:00
import troggle.parsers.QMs
2023-01-19 18:35:56 +00:00
import troggle.parsers.scans
import troggle.parsers.survex
import troggle.settings
from troggle.parsers.logbooks import GetCaveLookup
2021-04-07 16:04:27 +01:00
def test_import_imports(self):
from django.contrib.auth.models import User
2023-01-19 18:35:56 +00:00
from django.core import management
from django.db import close_old_connections, connection, connections
2021-04-07 16:04:27 +01:00
from django.http import HttpResponse
from django.urls import reverse
def test_import_urls(self):
from django.conf import settings
2023-01-19 18:35:56 +00:00
from django.conf.urls import include, url
from django.contrib import admin, auth
from django.urls import resolve, reverse
from django.views.generic.base import RedirectView
2021-04-07 16:04:27 +01:00
from django.views.generic.edit import UpdateView
from django.views.generic.list import ListView
2023-01-19 18:35:56 +00:00
from troggle.core.views import caves, other, statistics, survex
2021-04-17 20:45:38 +01:00
from troggle.core.views.auth import expologin, expologout
2023-01-19 18:35:56 +00:00
from troggle.core.views.caves import cavepage, ent
from troggle.core.views.expo import (
editexpopage,
expofiles_redirect,
expofilessingle,
expopage,
map,
mapfile,
mediapage,
)
from troggle.core.views.logbooks import (
Expeditions_jsonListView,
Expeditions_tsvListView,
expedition,
get_logbook_entries,
get_people,
logbookentry,
notablepersons,
person,
personexpedition,
)
from troggle.core.views.other import controlpanel
2023-01-19 18:35:56 +00:00
from troggle.core.views.prospect import prospecting, prospecting_image
from troggle.core.views.statistics import dataissues, pathsreport, stats
from troggle.core.views.survex import survexcavesingle, survexcaveslist, svx
2021-04-17 20:45:38 +01:00
2021-05-13 21:15:30 +01:00
class SubprocessTest(TestCase):
2021-05-13 21:15:30 +01:00
@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"""
2022-06-23 14:03:50 +01:00
import locale
2023-01-19 18:35:56 +00:00
import sys
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):
"""Expects external software installed: cavern, survexport, git
(but not whether it actually works)
"""
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()
2021-05-13 21:15:30 +01:00
try:
sp = subprocess.check_call(cmd, shell=False)
except subprocess.CalledProcessError:
self.assertTrue(False, f"no {i} installed")
2021-05-13 21:15:30 +01:00
2022-03-05 17:42:12 +00:00
def test_repos_git_status(self):
"""Expects clean git repos with no added files and no merge failures"""
2022-03-05 17:42:12 +00:00
from pathlib import Path
2023-01-19 18:35:56 +00:00
2022-03-05 17:42:12 +00:00
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.SURVEX_DATA, settings.EXPOWEB, settings.DRAWINGS_DATA, TROGGLE_PATH]:
2022-03-05 17:42:12 +00:00
sp = subprocess.run([settings.GIT, "status"], cwd=cwd, capture_output=True, text=True)
2022-11-23 21:59:42 +00:00
out = str(sp.stdout)
if len(out) > 160:
out = out[:75] + "\n <Long output curtailed>\n" + out[-75:]
print(f"git output: {cwd}:\n # {sp.stderr=}\n # sp.stdout={out} \n # return code: {str(sp.returncode)}")
2022-03-05 17:42:12 +00:00
if sp.returncode != 0:
print(f"git output: {cwd}:\n # {sp.stderr=}\n # sp.stdout={out} \n # return code: {str(sp.returncode)}")
self.assertTrue(sp.returncode == 0, f"{cwd} - git is unhappy")
2022-03-05 17:42:12 +00:00
content = sp.stdout
ph = r"Your branch is up[ -]to[ -]date"
phmatch = re.search(ph, content)
2022-09-12 20:47:31 +01:00
msg = f'{cwd} - Failed to find expected git output: "{ph}"'
self.assertIsNotNone(phmatch, msg)
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)
2022-03-05 17:42:12 +00:00
phmatch = phmatch1 or phmatch2
2022-09-12 20:47:31 +01:00
msg = f'{cwd} - Failed to find expected git output: "{ph1}" or "{ph2}"'
self.assertIsNotNone(phmatch, msg)
2021-04-07 16:04:27 +01:00
def test_loser_survex_status(self):
"""Expects no failures of survex files"""
from pathlib import Path
2023-01-19 18:35:56 +00:00
import troggle.settings as settings
cwd = settings.SURVEX_DATA
for survey in ["1623.svx", "1626.svx"]:
sp = subprocess.run([settings.CAVERN, survey], cwd=cwd, capture_output=True, text=True)
2022-11-23 21:59:42 +00:00
out = str(sp.stdout)
if len(out) > 160:
out = out[:75] + "\n <Long output curtailed>\n" + out[-75:]
# print(f'survex output: {cwd}:\n # {sp.stderr=}\n # sp.stdout={out} \n # return code: {str(sp.returncode)}')
if sp.returncode != 0:
print(
f"survex output: {cwd}:\n # {sp.stderr=}\n # sp.stdout={out} \n # return code: {str(sp.returncode)}"
)
self.assertTrue(sp.returncode == 0, f"{cwd} - survex is unhappy")
2021-04-07 16:04:27 +01:00
content = sp.stdout
ph = r"Total length of survey legs"
phmatch = re.search(ph, content)
msg = f'{cwd} - Failed to find expected survex output: "{ph}"'
self.assertIsNotNone(phmatch, msg)
ph1 = r"Time used"
phmatch1 = re.search(ph1, content)
ph2 = r"vertical length of survey le"
phmatch2 = re.search(ph2, content)
phmatch = phmatch1 or phmatch2
msg = f'{cwd} - Failed to find expected survex output: "{ph1}" or "{ph2}"'
self.assertIsNotNone(phmatch, msg)