| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  | # -*- coding: utf-8; -*- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:00:18 -06:00
										 |  |  | import datetime | 
					
						
							| 
									
										
										
										
											2024-12-28 20:10:37 -06:00
										 |  |  | import decimal | 
					
						
							| 
									
										
										
										
											2023-11-22 00:01:46 -06:00
										 |  |  | import os | 
					
						
							|  |  |  | import shutil | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2023-11-22 00:01:46 -06:00
										 |  |  | import tempfile | 
					
						
							| 
									
										
										
										
											2024-05-15 14:22:45 -05:00
										 |  |  | import warnings | 
					
						
							| 
									
										
										
										
											2024-11-30 19:59:59 -06:00
										 |  |  | import uuid as _uuid | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  | from unittest import TestCase | 
					
						
							|  |  |  | from unittest.mock import patch, MagicMock | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-04 08:00:42 -05:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  | from mako.template import Template | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-24 10:20:05 -05:00
										 |  |  | import wuttjamaican.enum | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  | from wuttjamaican import app as mod | 
					
						
							| 
									
										
										
										
											2024-08-24 17:19:50 -05:00
										 |  |  | from wuttjamaican.progress import ProgressBase | 
					
						
							| 
									
										
										
										
											2023-11-22 00:01:46 -06:00
										 |  |  | from wuttjamaican.conf import WuttaConfig | 
					
						
							| 
									
										
										
										
											2024-07-11 19:32:44 -05:00
										 |  |  | from wuttjamaican.util import UNSPECIFIED | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  | from wuttjamaican.testing import FileTestCase, ConfigTestCase | 
					
						
							|  |  |  | from wuttjamaican.batch import BatchHandler | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MockBatchHandler(BatchHandler): | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  | class AnotherBatchHandler(BatchHandler): | 
					
						
							|  |  |  |     pass | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-27 19:10:50 -05:00
										 |  |  | class TestAppHandler(FileTestCase): | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							| 
									
										
										
										
											2024-08-25 12:33:52 -05:00
										 |  |  |         self.setup_files() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.config = WuttaConfig(appname="wuttatest") | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  |         self.app = mod.AppHandler(self.config) | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  |         self.config.app = self.app | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_init(self): | 
					
						
							|  |  |  |         self.assertIs(self.app.config, self.config) | 
					
						
							|  |  |  |         self.assertEqual(self.app.handlers, {}) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(self.app.appname, "wuttatest") | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-24 10:20:05 -05:00
										 |  |  |     def test_get_enum(self): | 
					
						
							|  |  |  |         self.assertIs(self.app.get_enum(), wuttjamaican.enum) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-11 19:32:44 -05:00
										 |  |  |     def test_load_object(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # just confirm the method works on a basic level; the | 
					
						
							|  |  |  |         # underlying function is tested elsewhere | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         obj = self.app.load_object("wuttjamaican.util:UNSPECIFIED") | 
					
						
							| 
									
										
										
										
											2024-07-11 19:32:44 -05:00
										 |  |  |         self.assertIs(obj, UNSPECIFIED) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-25 12:33:52 -05:00
										 |  |  |     def test_get_appdir(self): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         mockdir = self.mkdir("mockdir") | 
					
						
							| 
									
										
										
										
											2024-08-25 12:33:52 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # default appdir | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         with patch.object(sys, "prefix", new=mockdir): | 
					
						
							| 
									
										
										
										
											2024-08-25 12:33:52 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # default is returned by default | 
					
						
							|  |  |  |             appdir = self.app.get_appdir() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.assertEqual(appdir, os.path.join(mockdir, "app")) | 
					
						
							| 
									
										
										
										
											2024-08-25 12:33:52 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # but not if caller wants config only | 
					
						
							|  |  |  |             appdir = self.app.get_appdir(configured_only=True) | 
					
						
							|  |  |  |             self.assertIsNone(appdir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # also, cannot create if appdir path not known | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.assertRaises( | 
					
						
							|  |  |  |                 ValueError, self.app.get_appdir, configured_only=True, create=True | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-08-25 12:33:52 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # configured appdir | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.config.setdefault("wuttatest.appdir", mockdir) | 
					
						
							| 
									
										
										
										
											2024-08-25 12:33:52 -05:00
										 |  |  |         appdir = self.app.get_appdir() | 
					
						
							|  |  |  |         self.assertEqual(appdir, mockdir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # appdir w/ subpath | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         appdir = self.app.get_appdir("foo", "bar") | 
					
						
							|  |  |  |         self.assertEqual(appdir, os.path.join(mockdir, "foo", "bar")) | 
					
						
							| 
									
										
										
										
											2024-08-25 12:33:52 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # subpath is created | 
					
						
							|  |  |  |         self.assertEqual(len(os.listdir(mockdir)), 0) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         appdir = self.app.get_appdir("foo", "bar", create=True) | 
					
						
							|  |  |  |         self.assertEqual(appdir, os.path.join(mockdir, "foo", "bar")) | 
					
						
							|  |  |  |         self.assertEqual(os.listdir(mockdir), ["foo"]) | 
					
						
							|  |  |  |         self.assertEqual(os.listdir(os.path.join(mockdir, "foo")), ["bar"]) | 
					
						
							| 
									
										
										
										
											2024-08-25 12:33:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-22 00:01:46 -06:00
										 |  |  |     def test_make_appdir(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # appdir is created, and 3 subfolders added by default | 
					
						
							|  |  |  |         tempdir = tempfile.mkdtemp() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         appdir = os.path.join(tempdir, "app") | 
					
						
							| 
									
										
										
										
											2023-11-22 00:01:46 -06:00
										 |  |  |         self.assertFalse(os.path.exists(appdir)) | 
					
						
							|  |  |  |         self.app.make_appdir(appdir) | 
					
						
							|  |  |  |         self.assertTrue(os.path.exists(appdir)) | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  |         self.assertEqual(len(os.listdir(appdir)), 4) | 
					
						
							| 
									
										
										
										
											2023-11-22 00:01:46 -06:00
										 |  |  |         shutil.rmtree(tempdir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # subfolders still added if appdir already exists | 
					
						
							|  |  |  |         tempdir = tempfile.mkdtemp() | 
					
						
							|  |  |  |         self.assertTrue(os.path.exists(tempdir)) | 
					
						
							|  |  |  |         self.assertEqual(len(os.listdir(tempdir)), 0) | 
					
						
							|  |  |  |         self.app.make_appdir(tempdir) | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  |         self.assertEqual(len(os.listdir(tempdir)), 4) | 
					
						
							| 
									
										
										
										
											2023-11-22 00:01:46 -06:00
										 |  |  |         shutil.rmtree(tempdir) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  |     def test_render_mako_template(self): | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         output_conf = self.write_file("output.conf", "") | 
					
						
							|  |  |  |         template = Template( | 
					
						
							|  |  |  |             """\
 | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  | [wutta] | 
					
						
							|  |  |  | app_title = WuttaTest | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  | """
 | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  |         output = self.app.render_mako_template(template, {}, output_path=output_conf) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual( | 
					
						
							|  |  |  |             output, | 
					
						
							|  |  |  |             """\
 | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  | [wutta] | 
					
						
							|  |  |  | app_title = WuttaTest | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  | """,
 | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         with open(output_conf, "rt") as f: | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  |             self.assertEqual(f.read(), output) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_resource_path(self): | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         result = self.app.resource_path("wuttjamaican:templates") | 
					
						
							|  |  |  |         self.assertEqual( | 
					
						
							|  |  |  |             result, os.path.join(os.path.dirname(mod.__file__), "templates") | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  |     def test_make_session(self): | 
					
						
							| 
									
										
										
										
											2024-07-04 08:00:42 -05:00
										 |  |  |         try: | 
					
						
							|  |  |  |             from wuttjamaican import db | 
					
						
							|  |  |  |         except ImportError: | 
					
						
							|  |  |  |             pytest.skip("test is not relevant without sqlalchemy") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  |         session = self.app.make_session() | 
					
						
							|  |  |  |         self.assertIsInstance(session, db.Session.class_) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_short_session(self): | 
					
						
							|  |  |  |         short_session = MagicMock() | 
					
						
							|  |  |  |         mockdb = MagicMock(short_session=short_session) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         with patch.dict("sys.modules", **{"wuttjamaican.db": mockdb}): | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             with self.app.short_session(foo="bar") as s: | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  |                 short_session.assert_called_once_with( | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |                     foo="bar", factory=self.app.make_session | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_get_setting(self): | 
					
						
							| 
									
										
										
										
											2024-07-04 08:00:42 -05:00
										 |  |  |         try: | 
					
						
							|  |  |  |             import sqlalchemy as sa | 
					
						
							|  |  |  |             from sqlalchemy import orm | 
					
						
							|  |  |  |         except ImportError: | 
					
						
							|  |  |  |             pytest.skip("test is not relevant without sqlalchemy") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  |         Session = orm.sessionmaker() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         engine = sa.create_engine("sqlite://") | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  |         session = Session(bind=engine) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         session.execute( | 
					
						
							|  |  |  |             sa.text( | 
					
						
							|  |  |  |                 """
 | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  |         create table setting ( | 
					
						
							|  |  |  |                 name varchar(255) primary key, | 
					
						
							|  |  |  |                 value text | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         """
 | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  |         session.commit() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         value = self.app.get_setting(session, "foo") | 
					
						
							| 
									
										
										
										
											2023-10-28 17:48:37 -05:00
										 |  |  |         self.assertIsNone(value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         session.execute(sa.text("insert into setting values ('foo', 'bar');")) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         value = self.app.get_setting(session, "foo") | 
					
						
							|  |  |  |         self.assertEqual(value, "bar") | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  |     def test_save_setting(self): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             import sqlalchemy as sa | 
					
						
							|  |  |  |             from sqlalchemy import orm | 
					
						
							|  |  |  |         except ImportError: | 
					
						
							|  |  |  |             pytest.skip("test is not relevant without sqlalchemy") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Session = orm.sessionmaker() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         engine = sa.create_engine("sqlite://") | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  |         session = Session(bind=engine) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         session.execute( | 
					
						
							|  |  |  |             sa.text( | 
					
						
							|  |  |  |                 """
 | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  |         create table setting ( | 
					
						
							|  |  |  |                 name varchar(255) primary key, | 
					
						
							|  |  |  |                 value text | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         """
 | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  |         session.commit() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # value null by default | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         value = self.app.get_setting(session, "foo") | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  |         self.assertIsNone(value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # unless we save a value | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.app.save_setting(session, "foo", "1") | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  |         session.commit() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         value = self.app.get_setting(session, "foo") | 
					
						
							|  |  |  |         self.assertEqual(value, "1") | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_delete_setting(self): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             import sqlalchemy as sa | 
					
						
							|  |  |  |             from sqlalchemy import orm | 
					
						
							|  |  |  |         except ImportError: | 
					
						
							|  |  |  |             pytest.skip("test is not relevant without sqlalchemy") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Session = orm.sessionmaker() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         engine = sa.create_engine("sqlite://") | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  |         session = Session(bind=engine) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         session.execute( | 
					
						
							|  |  |  |             sa.text( | 
					
						
							|  |  |  |                 """
 | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  |         create table setting ( | 
					
						
							|  |  |  |                 name varchar(255) primary key, | 
					
						
							|  |  |  |                 value text | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         """
 | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  |         session.commit() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # value null by default | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         value = self.app.get_setting(session, "foo") | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  |         self.assertIsNone(value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # unless we save a value | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.app.save_setting(session, "foo", "1") | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  |         session.commit() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         value = self.app.get_setting(session, "foo") | 
					
						
							|  |  |  |         self.assertEqual(value, "1") | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # but then if we delete it, should be null again | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.app.delete_setting(session, "foo") | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  |         session.commit() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         value = self.app.get_setting(session, "foo") | 
					
						
							| 
									
										
										
										
											2024-08-06 18:52:34 -05:00
										 |  |  |         self.assertIsNone(value) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-27 20:26:22 -05:00
										 |  |  |     def test_continuum_is_enabled(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # false by default | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         with patch.object(self.app, "providers", new={}): | 
					
						
							| 
									
										
										
										
											2024-08-27 20:26:22 -05:00
										 |  |  |             self.assertFalse(self.app.continuum_is_enabled()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # but "any" provider technically could enable it... | 
					
						
							|  |  |  |         class MockProvider: | 
					
						
							|  |  |  |             def continuum_is_enabled(self): | 
					
						
							|  |  |  |                 return True | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         with patch.object(self.app, "providers", new={"mock": MockProvider()}): | 
					
						
							| 
									
										
										
										
											2024-08-27 20:26:22 -05:00
										 |  |  |             self.assertTrue(self.app.continuum_is_enabled()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-11 12:55:55 -05:00
										 |  |  |     def test_model(self): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             from wuttjamaican.db import model | 
					
						
							|  |  |  |         except ImportError: | 
					
						
							|  |  |  |             pytest.skip("test not relevant without sqlalchemy") | 
					
						
							| 
									
										
										
										
											2024-07-14 23:20:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertNotIn("model", self.app.__dict__) | 
					
						
							| 
									
										
										
										
											2024-07-14 23:20:44 -05:00
										 |  |  |         self.assertIs(self.app.model, model) | 
					
						
							| 
									
										
										
										
											2024-07-11 12:55:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_get_model(self): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             from wuttjamaican.db import model | 
					
						
							|  |  |  |         except ImportError: | 
					
						
							|  |  |  |             pytest.skip("test not relevant without sqlalchemy") | 
					
						
							| 
									
										
										
										
											2024-07-14 23:20:44 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.assertIs(self.app.get_model(), model) | 
					
						
							| 
									
										
										
										
											2024-07-11 12:55:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_get_title(self): | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(self.app.get_title(), "WuttJamaican") | 
					
						
							| 
									
										
										
										
											2024-07-11 12:55:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-20 22:13:33 -05:00
										 |  |  |     def test_get_node_title(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # default | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(self.app.get_node_title(), "WuttJamaican") | 
					
						
							| 
									
										
										
										
											2024-08-20 22:13:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # will fallback to app title | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.config.setdefault("wuttatest.app_title", "WuttaTest") | 
					
						
							|  |  |  |         self.assertEqual(self.app.get_node_title(), "WuttaTest") | 
					
						
							| 
									
										
										
										
											2024-08-20 22:13:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # will read from config | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.config.setdefault("wuttatest.node_title", "WuttaNode") | 
					
						
							|  |  |  |         self.assertEqual(self.app.get_node_title(), "WuttaNode") | 
					
						
							| 
									
										
										
										
											2024-08-20 22:13:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_get_node_type(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # default | 
					
						
							|  |  |  |         self.assertIsNone(self.app.get_node_type()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # will read from config | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.config.setdefault("wuttatest.node_type", "warehouse") | 
					
						
							|  |  |  |         self.assertEqual(self.app.get_node_type(), "warehouse") | 
					
						
							| 
									
										
										
										
											2024-08-20 22:13:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  |     def test_get_distribution(self): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 22:41:58 -05:00
										 |  |  |         try: | 
					
						
							|  |  |  |             from sqlalchemy.orm import Query | 
					
						
							|  |  |  |         except ImportError: | 
					
						
							|  |  |  |             pytest.skip("test is not relevant without sqlalchemy") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 22:39:45 -05:00
										 |  |  |         # works with "non-native" objects | 
					
						
							|  |  |  |         query = Query({}) | 
					
						
							|  |  |  |         dist = self.app.get_distribution(query) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(dist, "SQLAlchemy") | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # can override dist via config | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.config.setdefault("wuttatest.app_dist", "importlib_metadata") | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  |         dist = self.app.get_distribution() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(dist, "importlib_metadata") | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # but the provided object takes precedence | 
					
						
							| 
									
										
										
										
											2024-08-05 22:39:45 -05:00
										 |  |  |         dist = self.app.get_distribution(query) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(dist, "SQLAlchemy") | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_get_distribution_pre_python_3_10(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # the goal here is to get coverage for code which would only | 
					
						
							|  |  |  |         # run on python 3,9 and older, but we only need that coverage | 
					
						
							|  |  |  |         # if we are currently testing python 3.10+ | 
					
						
							|  |  |  |         if sys.version_info.major == 3 and sys.version_info.minor < 10: | 
					
						
							|  |  |  |             pytest.skip("this test is not relevant before python 3.10") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         importlib_metadata = MagicMock() | 
					
						
							|  |  |  |         importlib_metadata.packages_distributions = MagicMock( | 
					
						
							|  |  |  |             return_value={ | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |                 "wuttjamaican": ["WuttJamaican"], | 
					
						
							|  |  |  |                 "config": ["python-configuration"], | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         orig_import = __import__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def mock_import(name, *args, **kwargs): | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             if name == "importlib.metadata": | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  |                 raise ImportError | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             if name == "importlib_metadata": | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  |                 return importlib_metadata | 
					
						
							|  |  |  |             return orig_import(name, *args, **kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         with patch("builtins.__import__", side_effect=mock_import): | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # default should always be WuttJamaican (right..?) | 
					
						
							|  |  |  |             dist = self.app.get_distribution() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.assertEqual(dist, "WuttJamaican") | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # also works with "non-native" objects | 
					
						
							|  |  |  |             from config import Configuration | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  |             config = Configuration({}) | 
					
						
							|  |  |  |             dist = self.app.get_distribution(config) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.assertEqual(dist, "python-configuration") | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # hacky sort of test, just in case we can't deduce the | 
					
						
							|  |  |  |             # package dist based on the obj - easy enough since we | 
					
						
							|  |  |  |             # have limited the packages_distributions() above | 
					
						
							|  |  |  |             dist = self.app.get_distribution(42) | 
					
						
							|  |  |  |             self.assertIsNone(dist) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # can override dist via config | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.config.setdefault("wuttatest.app_dist", "importlib_metadata") | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  |             dist = self.app.get_distribution() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.assertEqual(dist, "importlib_metadata") | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # but the provided object takes precedence | 
					
						
							|  |  |  |             dist = self.app.get_distribution(config) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.assertEqual(dist, "python-configuration") | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # hacky test again, this time config override should win | 
					
						
							|  |  |  |             dist = self.app.get_distribution(42) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.assertEqual(dist, "importlib_metadata") | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_get_version(self): | 
					
						
							|  |  |  |         from importlib.metadata import version | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 22:41:58 -05:00
										 |  |  |         try: | 
					
						
							|  |  |  |             from sqlalchemy.orm import Query | 
					
						
							|  |  |  |         except ImportError: | 
					
						
							|  |  |  |             pytest.skip("test is not relevant without sqlalchemy") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 22:25:43 -05:00
										 |  |  |         # works with "non-native" objects | 
					
						
							| 
									
										
										
										
											2024-08-05 22:39:45 -05:00
										 |  |  |         query = Query({}) | 
					
						
							|  |  |  |         ver = self.app.get_version(obj=query) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(ver, version("SQLAlchemy")) | 
					
						
							| 
									
										
										
										
											2024-08-05 22:25:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 16:36:52 -05:00
										 |  |  |         # random object will not yield a dist nor version | 
					
						
							|  |  |  |         ver = self.app.get_version(obj=42) | 
					
						
							|  |  |  |         self.assertIsNone(ver) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-05 22:25:43 -05:00
										 |  |  |         # can override dist via config | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.config.setdefault("wuttatest.app_dist", "python-configuration") | 
					
						
							| 
									
										
										
										
											2024-08-05 22:25:43 -05:00
										 |  |  |         ver = self.app.get_version() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(ver, version("python-configuration")) | 
					
						
							| 
									
										
										
										
											2024-08-05 22:25:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # but the provided object takes precedence | 
					
						
							| 
									
										
										
										
											2024-08-05 22:39:45 -05:00
										 |  |  |         ver = self.app.get_version(obj=query) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(ver, version("SQLAlchemy")) | 
					
						
							| 
									
										
										
										
											2024-08-05 22:39:45 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # can also specify the dist | 
					
						
							| 
									
										
										
										
											2025-10-19 12:58:34 -05:00
										 |  |  |         ver = self.app.get_version(dist="progress") | 
					
						
							|  |  |  |         self.assertEqual(ver, version("progress")) | 
					
						
							| 
									
										
										
										
											2024-08-05 21:48:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-04 14:23:02 -05:00
										 |  |  |     def test_make_title(self): | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         text = self.app.make_title("foo_bar") | 
					
						
							| 
									
										
										
										
											2024-08-04 14:23:02 -05:00
										 |  |  |         self.assertEqual(text, "Foo Bar") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-06 16:36:27 -06:00
										 |  |  |     def test_make_full_name(self): | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         name = self.app.make_full_name("Fred", "", "Flintstone", "") | 
					
						
							| 
									
										
										
										
											2025-01-06 16:36:27 -06:00
										 |  |  |         self.assertEqual(name, "Fred Flintstone") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-11 12:55:55 -05:00
										 |  |  |     def test_make_uuid(self): | 
					
						
							|  |  |  |         uuid = self.app.make_uuid() | 
					
						
							|  |  |  |         self.assertEqual(len(uuid), 32) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-30 19:59:59 -06:00
										 |  |  |     def test_make_true_uuid(self): | 
					
						
							|  |  |  |         uuid = self.app.make_true_uuid() | 
					
						
							|  |  |  |         self.assertIsInstance(uuid, _uuid.UUID) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-24 17:19:50 -05:00
										 |  |  |     def test_progress_loop(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def act(obj, i): | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # with progress | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.app.progress_loop(act, [1, 2, 3], ProgressBase, message="whatever") | 
					
						
							| 
									
										
										
										
											2024-08-24 17:19:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # without progress | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.app.progress_loop(act, [1, 2, 3], None, message="whatever") | 
					
						
							| 
									
										
										
										
											2024-08-24 17:19:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-14 23:20:44 -05:00
										 |  |  |     def test_get_session(self): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             import sqlalchemy as sa | 
					
						
							|  |  |  |             from sqlalchemy import orm | 
					
						
							|  |  |  |         except ImportError: | 
					
						
							|  |  |  |             pytest.skip("test not relevant without sqlalchemy") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         model = self.app.model | 
					
						
							|  |  |  |         user = model.User() | 
					
						
							|  |  |  |         self.assertIsNone(self.app.get_session(user)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Session = orm.sessionmaker() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         engine = sa.create_engine("sqlite://") | 
					
						
							| 
									
										
										
										
											2024-07-14 23:20:44 -05:00
										 |  |  |         mysession = Session(bind=engine) | 
					
						
							|  |  |  |         mysession.add(user) | 
					
						
							|  |  |  |         session = self.app.get_session(user) | 
					
						
							|  |  |  |         self.assertIs(session, mysession) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-02 19:48:02 -06:00
										 |  |  |     def test_render_boolean(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # null | 
					
						
							|  |  |  |         self.assertEqual(self.app.render_boolean(None), "") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # true | 
					
						
							|  |  |  |         self.assertEqual(self.app.render_boolean(True), "Yes") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # false | 
					
						
							|  |  |  |         self.assertEqual(self.app.render_boolean(False), "No") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-28 20:10:37 -06:00
										 |  |  |     def test_render_currency(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # null | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(self.app.render_currency(None), "") | 
					
						
							| 
									
										
										
										
											2024-12-28 20:10:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # basic decimal example | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         value = decimal.Decimal("42.00") | 
					
						
							|  |  |  |         self.assertEqual(self.app.render_currency(value), "$42.00") | 
					
						
							| 
									
										
										
										
											2024-12-28 20:10:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # basic float example | 
					
						
							|  |  |  |         value = 42.00 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(self.app.render_currency(value), "$42.00") | 
					
						
							| 
									
										
										
										
											2024-12-28 20:10:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # decimal places will be rounded | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         value = decimal.Decimal("42.12345") | 
					
						
							|  |  |  |         self.assertEqual(self.app.render_currency(value), "$42.12") | 
					
						
							| 
									
										
										
										
											2024-12-28 20:10:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # but we can declare the scale | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         value = decimal.Decimal("42.12345") | 
					
						
							|  |  |  |         self.assertEqual(self.app.render_currency(value, scale=4), "$42.1234") | 
					
						
							| 
									
										
										
										
											2024-12-28 20:10:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # negative numbers get parens | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         value = decimal.Decimal("-42.42") | 
					
						
							|  |  |  |         self.assertEqual(self.app.render_currency(value), "($42.42)") | 
					
						
							| 
									
										
										
										
											2024-12-28 20:10:37 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 10:00:18 -06:00
										 |  |  |     def test_render_date(self): | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(self.app.render_date(None), "") | 
					
						
							| 
									
										
										
										
											2024-12-12 10:00:18 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         dt = datetime.date(2024, 12, 11) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(self.app.render_date(dt), "2024-12-11") | 
					
						
							| 
									
										
										
										
											2024-12-12 10:00:18 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_render_datetime(self): | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(self.app.render_datetime(None), "") | 
					
						
							| 
									
										
										
										
											2024-12-12 10:00:18 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         dt = datetime.datetime(2024, 12, 11, 8, 30, tzinfo=datetime.timezone.utc) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(self.app.render_datetime(dt), "2024-12-11 08:30+0000") | 
					
						
							| 
									
										
										
										
											2024-12-12 10:00:18 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-02 19:48:02 -06:00
										 |  |  |     def test_render_error(self): | 
					
						
							| 
									
										
										
										
											2024-12-28 20:10:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # with description | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             raise RuntimeError("just testin") | 
					
						
							|  |  |  |         except Exception as error: | 
					
						
							|  |  |  |             result = self.app.render_error(error) | 
					
						
							|  |  |  |         self.assertEqual(result, "RuntimeError: just testin") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # without description | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             raise RuntimeError | 
					
						
							|  |  |  |         except Exception as error: | 
					
						
							|  |  |  |             result = self.app.render_error(error) | 
					
						
							|  |  |  |         self.assertEqual(result, "RuntimeError") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 19:08:34 -06:00
										 |  |  |     def test_render_percent(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # null | 
					
						
							|  |  |  |         self.assertEqual(self.app.render_percent(None), "") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # typical | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(self.app.render_percent(12.3419), "12.34 %") | 
					
						
							| 
									
										
										
										
											2025-01-11 19:08:34 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # more decimal places | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(self.app.render_percent(12.3419, decimals=3), "12.342 %") | 
					
						
							|  |  |  |         self.assertEqual(self.app.render_percent(12.3419, decimals=4), "12.3419 %") | 
					
						
							| 
									
										
										
										
											2025-01-11 19:08:34 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # negative | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(self.app.render_percent(-12.3419), "(12.34 %)") | 
					
						
							|  |  |  |         self.assertEqual(self.app.render_percent(-12.3419, decimals=3), "(12.342 %)") | 
					
						
							| 
									
										
										
										
											2025-01-11 19:08:34 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-02 19:48:02 -06:00
										 |  |  |     def test_render_quantity(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # null | 
					
						
							|  |  |  |         self.assertEqual(self.app.render_quantity(None), "") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # integer decimals become integers | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         value = decimal.Decimal("1.000") | 
					
						
							| 
									
										
										
										
											2025-01-02 19:48:02 -06:00
										 |  |  |         self.assertEqual(self.app.render_quantity(value), "1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # but decimal places are preserved | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         value = decimal.Decimal("1.234") | 
					
						
							| 
									
										
										
										
											2025-01-02 19:48:02 -06:00
										 |  |  |         self.assertEqual(self.app.render_quantity(value), "1.234") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # zero can be empty string | 
					
						
							|  |  |  |         self.assertEqual(self.app.render_quantity(0), "0") | 
					
						
							|  |  |  |         self.assertEqual(self.app.render_quantity(0, empty_zero=True), "") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-14 19:39:02 -06:00
										 |  |  |     def test_render_time_ago(self): | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         with patch.object(mod, "humanize") as humanize: | 
					
						
							|  |  |  |             humanize.naturaltime.return_value = "now" | 
					
						
							| 
									
										
										
										
											2024-12-14 19:39:02 -06:00
										 |  |  |             now = datetime.datetime.now() | 
					
						
							|  |  |  |             result = self.app.render_time_ago(now) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.assertEqual(result, "now") | 
					
						
							| 
									
										
										
										
											2024-12-14 19:39:02 -06:00
										 |  |  |             humanize.naturaltime.assert_called_once_with(now) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-14 23:20:44 -05:00
										 |  |  |     def test_get_person(self): | 
					
						
							|  |  |  |         people = self.app.get_people_handler() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         with patch.object(people, "get_person") as get_person: | 
					
						
							|  |  |  |             get_person.return_value = "foo" | 
					
						
							|  |  |  |             person = self.app.get_person("bar") | 
					
						
							|  |  |  |             get_person.assert_called_once_with("bar") | 
					
						
							|  |  |  |             self.assertEqual(person, "foo") | 
					
						
							| 
									
										
										
										
											2024-07-14 23:20:44 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_get_auth_handler(self): | 
					
						
							|  |  |  |         from wuttjamaican.auth import AuthHandler | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auth = self.app.get_auth_handler() | 
					
						
							|  |  |  |         self.assertIsInstance(auth, AuthHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  |     def test_get_batch_handler(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # error if handler not found | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertRaises(KeyError, self.app.get_batch_handler, "CannotFindMe!") | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # caller can specify default | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         handler = self.app.get_batch_handler( | 
					
						
							|  |  |  |             "foo", default="wuttjamaican.batch:BatchHandler" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  |         self.assertIsInstance(handler, BatchHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # default can be configured | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.config.setdefault( | 
					
						
							|  |  |  |             "wuttatest.batch.foo.handler.default_spec", | 
					
						
							|  |  |  |             "wuttjamaican.batch:BatchHandler", | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         handler = self.app.get_batch_handler("foo") | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  |         self.assertIsInstance(handler, BatchHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # preference can be configured | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.config.setdefault( | 
					
						
							|  |  |  |             "wuttatest.batch.foo.handler.spec", "tests.test_app:MockBatchHandler" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         handler = self.app.get_batch_handler("foo") | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  |         self.assertIsInstance(handler, MockBatchHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_get_batch_handler_specs(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # empty by default | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         specs = self.app.get_batch_handler_specs("foo") | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  |         self.assertEqual(specs, []) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # caller can specify default as string | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         specs = self.app.get_batch_handler_specs( | 
					
						
							|  |  |  |             "foo", default="wuttjamaican.batch:BatchHandler" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.assertEqual(specs, ["wuttjamaican.batch:BatchHandler"]) | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # caller can specify default as list | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         specs = self.app.get_batch_handler_specs( | 
					
						
							|  |  |  |             "foo", | 
					
						
							|  |  |  |             default=[ | 
					
						
							|  |  |  |                 "wuttjamaican.batch:BatchHandler", | 
					
						
							|  |  |  |                 "tests.test_app:MockBatchHandler", | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.assertEqual( | 
					
						
							|  |  |  |             specs, | 
					
						
							|  |  |  |             ["wuttjamaican.batch:BatchHandler", "tests.test_app:MockBatchHandler"], | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # default can be configured | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.config.setdefault( | 
					
						
							|  |  |  |             "wuttatest.batch.foo.handler.default_spec", | 
					
						
							|  |  |  |             "wuttjamaican.batch:BatchHandler", | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         specs = self.app.get_batch_handler_specs("foo") | 
					
						
							|  |  |  |         self.assertEqual(specs, ["wuttjamaican.batch:BatchHandler"]) | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # the rest come from entry points | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         with patch.object( | 
					
						
							|  |  |  |             mod, | 
					
						
							|  |  |  |             "load_entry_points", | 
					
						
							|  |  |  |             return_value={ | 
					
						
							|  |  |  |                 "mock": MockBatchHandler, | 
					
						
							|  |  |  |                 "another": AnotherBatchHandler, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         ): | 
					
						
							|  |  |  |             specs = self.app.get_batch_handler_specs("foo") | 
					
						
							|  |  |  |             self.assertEqual( | 
					
						
							|  |  |  |                 specs, | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     "wuttjamaican.batch:BatchHandler", | 
					
						
							|  |  |  |                     "tests.test_app:AnotherBatchHandler", | 
					
						
							|  |  |  |                     "tests.test_app:MockBatchHandler", | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-13 17:26:07 -06:00
										 |  |  |     def test_get_db_handler(self): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             from wuttjamaican.db.handler import DatabaseHandler | 
					
						
							|  |  |  |         except ImportError: | 
					
						
							|  |  |  |             pytest.skip("test not relevant without sqlalchemy") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         db = self.app.get_db_handler() | 
					
						
							|  |  |  |         self.assertIsInstance(db, DatabaseHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 13:08:19 -05:00
										 |  |  |     def test_get_email_handler(self): | 
					
						
							|  |  |  |         from wuttjamaican.email import EmailHandler | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         mail = self.app.get_email_handler() | 
					
						
							|  |  |  |         self.assertIsInstance(mail, EmailHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  |     def test_get_install_handler(self): | 
					
						
							|  |  |  |         from wuttjamaican.install import InstallHandler | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         install = self.app.get_install_handler() | 
					
						
							|  |  |  |         self.assertIsInstance(install, InstallHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-14 23:20:44 -05:00
										 |  |  |     def test_get_people_handler(self): | 
					
						
							|  |  |  |         from wuttjamaican.people import PeopleHandler | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         people = self.app.get_people_handler() | 
					
						
							|  |  |  |         self.assertIsInstance(people, PeopleHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-10 11:07:30 -05:00
										 |  |  |     def test_get_problem_handler(self): | 
					
						
							|  |  |  |         from wuttjamaican.problems import ProblemHandler | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         handler = self.app.get_problem_handler() | 
					
						
							|  |  |  |         self.assertIsInstance(handler, ProblemHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 19:04:30 -06:00
										 |  |  |     def test_get_report_handler(self): | 
					
						
							|  |  |  |         from wuttjamaican.reports import ReportHandler | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         handler = self.app.get_report_handler() | 
					
						
							|  |  |  |         self.assertIsInstance(handler, ReportHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 14:34:35 -05:00
										 |  |  |     def test_send_email(self): | 
					
						
							| 
									
										
										
										
											2024-08-26 13:08:19 -05:00
										 |  |  |         from wuttjamaican.email import EmailHandler | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         with patch.object(EmailHandler, "send_email") as send_email: | 
					
						
							|  |  |  |             self.app.send_email("foo") | 
					
						
							|  |  |  |             send_email.assert_called_once_with("foo") | 
					
						
							| 
									
										
										
										
											2024-08-26 13:08:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | class TestAppProvider(TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.config = WuttaConfig(appname="wuttatest") | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  |         self.app = mod.AppHandler(self.config) | 
					
						
							| 
									
										
										
										
											2024-07-04 06:21:38 -05:00
										 |  |  |         self.config._app = self.app | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_constructor(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # config object is expected | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  |         provider = mod.AppProvider(self.config) | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  |         self.assertIs(provider.config, self.config) | 
					
						
							|  |  |  |         self.assertIs(provider.app, self.app) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(provider.appname, "wuttatest") | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # but can pass app handler instead | 
					
						
							| 
									
										
										
										
											2024-05-15 14:22:45 -05:00
										 |  |  |         with warnings.catch_warnings(): | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             warnings.filterwarnings("ignore", category=DeprecationWarning) | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  |             provider = mod.AppProvider(self.app) | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  |         self.assertIs(provider.config, self.config) | 
					
						
							|  |  |  |         self.assertIs(provider.app, self.app) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_get_all_providers(self): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  |         class FakeProvider(mod.AppProvider): | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # nb. we specify *classes* here | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         fake_providers = {"fake": FakeProvider} | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         with patch("wuttjamaican.app.load_entry_points") as load_entry_points: | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  |             load_entry_points.return_value = fake_providers | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # sanity check, we get *instances* back from this | 
					
						
							|  |  |  |             providers = self.app.get_all_providers() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             load_entry_points.assert_called_once_with("wutta.app.providers") | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  |             self.assertEqual(len(providers), 1) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.assertIn("fake", providers) | 
					
						
							|  |  |  |             self.assertIsInstance(providers["fake"], FakeProvider) | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_hasattr(self): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  |         class FakeProvider(mod.AppProvider): | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  |             def fake_foo(self): | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.app.providers = {"fake": FakeProvider(self.config)} | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertTrue(hasattr(self.app, "fake_foo")) | 
					
						
							|  |  |  |         self.assertFalse(hasattr(self.app, "fake_method_does_not_exist")) | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_getattr(self): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-24 10:20:05 -05:00
										 |  |  |         # enum | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertNotIn("enum", self.app.__dict__) | 
					
						
							| 
									
										
										
										
											2024-08-24 10:20:05 -05:00
										 |  |  |         self.assertIs(self.app.enum, wuttjamaican.enum) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # now we test that providers are loaded... | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  |         class FakeProvider(mod.AppProvider): | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  |             def fake_foo(self): | 
					
						
							|  |  |  |                 return 42 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # nb. using instances here | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         fake_providers = {"fake": FakeProvider(self.config)} | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         with patch.object(self.app, "get_all_providers") as get_all_providers: | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  |             get_all_providers.return_value = fake_providers | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.assertNotIn("providers", self.app.__dict__) | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  |             self.assertIs(self.app.providers, fake_providers) | 
					
						
							|  |  |  |             get_all_providers.assert_called_once_with() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-24 10:20:05 -05:00
										 |  |  |     def test_getattr_model(self): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             import wuttjamaican.db.model | 
					
						
							|  |  |  |         except ImportError: | 
					
						
							|  |  |  |             pytest.skip("test not relevant without sqlalchemy") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # model | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertNotIn("model", self.app.__dict__) | 
					
						
							| 
									
										
										
										
											2024-08-24 10:20:05 -05:00
										 |  |  |         self.assertIs(self.app.model, wuttjamaican.db.model) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  |     def test_getattr_providers(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # collection of providers is loaded on demand | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertNotIn("providers", self.app.__dict__) | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  |         self.assertIsNotNone(self.app.providers) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # custom attr does not exist yet | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertRaises(AttributeError, getattr, self.app, "foo_value") | 
					
						
							| 
									
										
										
										
											2023-11-24 22:24:20 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # but provider can supply the attr | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.app.providers["mytest"] = MagicMock(foo_value="bar") | 
					
						
							|  |  |  |         self.assertEqual(self.app.foo_value, "bar") | 
					
						
							| 
									
										
										
										
											2023-11-25 16:28:37 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  | class TestGenericHandler(ConfigTestCase): | 
					
						
							| 
									
										
										
										
											2023-11-25 16:28:37 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  |     def make_config(self, **kw): | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         kw.setdefault("appname", "wuttatest") | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  |         return super().make_config(**kw) | 
					
						
							| 
									
										
										
										
											2023-11-25 16:28:37 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-30 20:11:10 -05:00
										 |  |  |     def make_handler(self, **kwargs): | 
					
						
							|  |  |  |         return mod.GenericHandler(self.config, **kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-25 16:28:37 -06:00
										 |  |  |     def test_constructor(self): | 
					
						
							| 
									
										
										
										
											2024-11-24 10:13:56 -06:00
										 |  |  |         handler = mod.GenericHandler(self.config) | 
					
						
							| 
									
										
										
										
											2023-11-25 16:28:37 -06:00
										 |  |  |         self.assertIs(handler.config, self.config) | 
					
						
							|  |  |  |         self.assertIs(handler.app, self.app) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual(handler.appname, "wuttatest") | 
					
						
							| 
									
										
										
										
											2025-01-13 11:51:36 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_get_spec(self): | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         self.assertEqual( | 
					
						
							|  |  |  |             mod.GenericHandler.get_spec(), "wuttjamaican.app:GenericHandler" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2025-08-30 20:11:10 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_get_provider_modules(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # no providers, no email modules | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         with patch.object(self.app, "providers", new={}): | 
					
						
							| 
									
										
										
										
											2025-08-30 20:11:10 -05:00
										 |  |  |             handler = self.make_handler() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.assertEqual(handler.get_provider_modules("email"), []) | 
					
						
							| 
									
										
										
										
											2025-08-30 20:11:10 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # provider may specify modules as list | 
					
						
							|  |  |  |         providers = { | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             "wuttatest": MagicMock(email_modules=["wuttjamaican.app"]), | 
					
						
							| 
									
										
										
										
											2025-08-30 20:11:10 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         with patch.object(self.app, "providers", new=providers): | 
					
						
							| 
									
										
										
										
											2025-08-30 20:11:10 -05:00
										 |  |  |             handler = self.make_handler() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             modules = handler.get_provider_modules("email") | 
					
						
							| 
									
										
										
										
											2025-08-30 20:11:10 -05:00
										 |  |  |             self.assertEqual(len(modules), 1) | 
					
						
							|  |  |  |             self.assertIs(modules[0], mod) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # provider may specify modules as string | 
					
						
							|  |  |  |         providers = { | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             "wuttatest": MagicMock(email_modules="wuttjamaican.app"), | 
					
						
							| 
									
										
										
										
											2025-08-30 20:11:10 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |         with patch.object(self.app, "providers", new=providers): | 
					
						
							| 
									
										
										
										
											2025-08-30 20:11:10 -05:00
										 |  |  |             handler = self.make_handler() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             modules = handler.get_provider_modules("email") | 
					
						
							| 
									
										
										
										
											2025-08-30 20:11:10 -05:00
										 |  |  |             self.assertEqual(len(modules), 1) | 
					
						
							|  |  |  |             self.assertIs(modules[0], mod) |