| 
									
										
										
										
											2024-08-30 20:30:09 -05:00
										 |  |  | # -*- coding: utf-8; -*- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-30 19:59:59 -06:00
										 |  |  | import uuid as _uuid | 
					
						
							| 
									
										
										
										
											2024-08-30 20:30:09 -05:00
										 |  |  | from unittest import TestCase | 
					
						
							| 
									
										
										
										
											2024-11-30 19:59:59 -06:00
										 |  |  | from unittest.mock import MagicMock | 
					
						
							| 
									
										
										
										
											2024-08-30 20:30:09 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | try: | 
					
						
							|  |  |  |     import sqlalchemy as sa | 
					
						
							| 
									
										
										
										
											2024-11-30 19:59:59 -06:00
										 |  |  |     from sqlalchemy.dialects.postgresql import UUID as PGUUID | 
					
						
							| 
									
										
										
										
											2024-08-30 20:30:09 -05:00
										 |  |  |     from wuttjamaican.db import util as mod | 
					
						
							|  |  |  |     from wuttjamaican.db.model.base import Setting | 
					
						
							| 
									
										
										
										
											2024-11-30 19:59:59 -06:00
										 |  |  |     from wuttjamaican.util import make_true_uuid | 
					
						
							| 
									
										
										
										
											2024-12-03 21:53:30 -06:00
										 |  |  |     from wuttjamaican.testing import DataTestCase | 
					
						
							| 
									
										
										
										
											2024-08-30 20:30:09 -05:00
										 |  |  | except ImportError: | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | else: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class TestModelBase(TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def test_dict_behavior(self): | 
					
						
							|  |  |  |             setting = Setting() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.assertEqual(list(iter(setting)), [("name", None), ("value", None)]) | 
					
						
							| 
									
										
										
										
											2025-08-30 16:36:52 -05:00
										 |  |  |             self.assertIsNone(setting.name) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.assertIsNone(setting["name"]) | 
					
						
							|  |  |  |             setting.name = "foo" | 
					
						
							|  |  |  |             self.assertEqual(setting["name"], "foo") | 
					
						
							|  |  |  |             self.assertRaises(KeyError, lambda: setting["notfound"]) | 
					
						
							| 
									
										
										
										
											2024-08-30 20:30:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-30 19:59:59 -06:00
										 |  |  |     class TestUUID(TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def test_load_dialect_impl(self): | 
					
						
							|  |  |  |             typ = mod.UUID() | 
					
						
							|  |  |  |             dialect = MagicMock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # TODO: this doesn't really test anything, but gives us | 
					
						
							|  |  |  |             # coverage at least.. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # postgres | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             dialect.name = "postgresql" | 
					
						
							| 
									
										
										
										
											2024-11-30 19:59:59 -06:00
										 |  |  |             dialect.type_descriptor.return_value = 42 | 
					
						
							|  |  |  |             result = typ.load_dialect_impl(dialect) | 
					
						
							|  |  |  |             self.assertTrue(dialect.type_descriptor.called) | 
					
						
							|  |  |  |             self.assertEqual(result, 42) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # other | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             dialect.name = "mysql" | 
					
						
							| 
									
										
										
										
											2024-11-30 19:59:59 -06:00
										 |  |  |             dialect.type_descriptor.return_value = 43 | 
					
						
							|  |  |  |             dialect.type_descriptor.reset_mock() | 
					
						
							|  |  |  |             result = typ.load_dialect_impl(dialect) | 
					
						
							|  |  |  |             self.assertTrue(dialect.type_descriptor.called) | 
					
						
							|  |  |  |             self.assertEqual(result, 43) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def test_process_bind_param_postgres(self): | 
					
						
							|  |  |  |             typ = mod.UUID() | 
					
						
							|  |  |  |             dialect = MagicMock() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             dialect.name = "postgresql" | 
					
						
							| 
									
										
										
										
											2024-11-30 19:59:59 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # null | 
					
						
							|  |  |  |             result = typ.process_bind_param(None, dialect) | 
					
						
							|  |  |  |             self.assertIsNone(result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # string | 
					
						
							|  |  |  |             uuid_str = make_true_uuid().hex | 
					
						
							|  |  |  |             result = typ.process_bind_param(uuid_str, dialect) | 
					
						
							|  |  |  |             self.assertEqual(result, uuid_str) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # uuid | 
					
						
							|  |  |  |             uuid_true = make_true_uuid() | 
					
						
							|  |  |  |             result = typ.process_bind_param(uuid_true, dialect) | 
					
						
							|  |  |  |             self.assertEqual(result, str(uuid_true)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def test_process_bind_param_other(self): | 
					
						
							|  |  |  |             typ = mod.UUID() | 
					
						
							|  |  |  |             dialect = MagicMock() | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             dialect.name = "mysql" | 
					
						
							| 
									
										
										
										
											2024-11-30 19:59:59 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # null | 
					
						
							|  |  |  |             result = typ.process_bind_param(None, dialect) | 
					
						
							|  |  |  |             self.assertIsNone(result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # string | 
					
						
							|  |  |  |             uuid_str = make_true_uuid().hex | 
					
						
							|  |  |  |             result = typ.process_bind_param(uuid_str, dialect) | 
					
						
							|  |  |  |             self.assertEqual(result, uuid_str) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # uuid | 
					
						
							|  |  |  |             uuid_true = make_true_uuid() | 
					
						
							|  |  |  |             result = typ.process_bind_param(uuid_true, dialect) | 
					
						
							|  |  |  |             self.assertEqual(result, uuid_true.hex) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def test_process_result_value(self): | 
					
						
							|  |  |  |             typ = mod.UUID() | 
					
						
							|  |  |  |             dialect = MagicMock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # null | 
					
						
							|  |  |  |             result = typ.process_result_value(None, dialect) | 
					
						
							|  |  |  |             self.assertIsNone(result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # string | 
					
						
							|  |  |  |             uuid_str = make_true_uuid().hex | 
					
						
							|  |  |  |             result = typ.process_result_value(uuid_str, dialect) | 
					
						
							|  |  |  |             self.assertIsInstance(result, _uuid.UUID) | 
					
						
							|  |  |  |             self.assertEqual(result.hex, uuid_str) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # uuid | 
					
						
							|  |  |  |             uuid_true = make_true_uuid() | 
					
						
							|  |  |  |             result = typ.process_result_value(uuid_true, dialect) | 
					
						
							|  |  |  |             self.assertIs(result, uuid_true) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-30 20:30:09 -05:00
										 |  |  |     class TestUUIDColumn(TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def test_basic(self): | 
					
						
							|  |  |  |             column = mod.uuid_column() | 
					
						
							|  |  |  |             self.assertIsInstance(column, sa.Column) | 
					
						
							| 
									
										
										
										
											2024-12-07 23:45:47 -06:00
										 |  |  |             self.assertIsInstance(column.type, mod.UUID) | 
					
						
							| 
									
										
										
										
											2024-08-30 20:30:09 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     class TestUUIDFKColumn(TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def test_basic(self): | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             column = mod.uuid_fk_column("foo.bar") | 
					
						
							| 
									
										
										
										
											2024-08-30 20:30:09 -05:00
										 |  |  |             self.assertIsInstance(column, sa.Column) | 
					
						
							| 
									
										
										
										
											2024-12-07 23:45:47 -06:00
										 |  |  |             self.assertIsInstance(column.type, mod.UUID) | 
					
						
							| 
									
										
										
										
											2024-12-03 21:53:30 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     class TestMakeTopoSortkey(DataTestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def test_basic(self): | 
					
						
							|  |  |  |             model = self.app.model | 
					
						
							|  |  |  |             sortkey = mod.make_topo_sortkey(model) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             original = ["User", "Person", "UserRole", "Role"] | 
					
						
							| 
									
										
										
										
											2024-12-03 21:53:30 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # models are sorted so dependants come later | 
					
						
							|  |  |  |             result = sorted(original, key=sortkey) | 
					
						
							| 
									
										
										
										
											2025-08-30 21:25:44 -05:00
										 |  |  |             self.assertTrue(result.index("Role") < result.index("UserRole")) | 
					
						
							|  |  |  |             self.assertTrue(result.index("User") < result.index("UserRole")) | 
					
						
							|  |  |  |             self.assertTrue(result.index("Person") < result.index("User")) |