put real test code in tlib_... file (similar to py3 version)
This commit is contained in:
		
							parent
							
								
									f100a18f22
								
							
						
					
					
						commit
						573d4f0198
					
				
					 2 changed files with 132 additions and 106 deletions
				
			
		
							
								
								
									
										111
									
								
								scopes/tests.py
									
										
									
									
									
								
							
							
						
						
									
										111
									
								
								scopes/tests.py
									
										
									
									
									
								
							|  | @ -2,127 +2,32 @@ | ||||||
| 
 | 
 | ||||||
| """The real test implementations""" | """The real test implementations""" | ||||||
| 
 | 
 | ||||||
| from datetime import datetime |  | ||||||
| import unittest | import unittest | ||||||
| from scopes.storage import concept, folder, topic, tracking | from scopes import tlib_storage | ||||||
| 
 | 
 | ||||||
| import config | import config | ||||||
| config.dbengine = 'postgresql' | config.dbengine = 'postgresql' | ||||||
| config.dbname = 'ccotest' | config.dbname = 'ccotest' | ||||||
| config.dbuser = 'ccotest' | config.dbuser = 'ccotest' | ||||||
| config.dbpassword = 'cco' | config.dbpassword = 'cco' | ||||||
|  | config.dbschema = 'testing' | ||||||
| 
 | 
 | ||||||
| # PostgreSQL-specific settings | # PostgreSQL-specific settings | ||||||
| from scopes.storage.db.postgres import StorageFactory  | from scopes.storage.db.postgres import StorageFactory  | ||||||
| factory = StorageFactory(config) | config.storageFactory = StorageFactory(config) | ||||||
| storage = factory(schema='testing') | #storage = factory(schema='testing') | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class Test(unittest.TestCase): | class Test(unittest.TestCase): | ||||||
| 
 | 
 | ||||||
|     def test_001_tracking(self): |     def test_001_tracking(self): | ||||||
|         storage.dropTable('tracks') |         tlib_storage.test_tracking(self, config) | ||||||
|         tracks = storage.create(tracking.Container) |  | ||||||
|      |  | ||||||
|         tr01 = tracking.Track('t01', 'john') |  | ||||||
|         tr01.update(dict(activity='testing')) |  | ||||||
|         self.assertEqual(tr01.head, {'taskId': 't01', 'userName': 'john'}) |  | ||||||
|         self.assertEqual(tr01.taskId, 't01') |  | ||||||
|         self.assertEqual(tr01.userName, 'john') |  | ||||||
|      |  | ||||||
|         self.assertTrue(tracks.getTable() is not None) |  | ||||||
|      |  | ||||||
|         trid01 = tracks.save(tr01) |  | ||||||
|         self.assertTrue(trid01 > 0) |  | ||||||
|      |  | ||||||
|         #tr01a = tracks.get(trid01) |  | ||||||
|         tr01a = tracks['%07i' % trid01] |  | ||||||
|         self.assertEqual(tr01a.head, tr01.head) |  | ||||||
|         self.assertEqual(tr01a.trackId, trid01) |  | ||||||
|         self.assertEqual(tr01a.data.get('activity'), 'testing') |  | ||||||
|      |  | ||||||
|         tr01a.update(dict(text='Set up unit tests.')) |  | ||||||
|         tr01a.timeStamp = None |  | ||||||
|         self.assertTrue(tracks.save(tr01a) > 0) |  | ||||||
|      |  | ||||||
|         tr01b = tracks.queryLast(taskId='t01') |  | ||||||
|         self.assertEqual(tr01b.head, tr01.head) |  | ||||||
|         self.assertNotEqual(tr01b.trackId, trid01) |  | ||||||
|         self.assertEqual(tr01b.data.get('activity'), 'testing') |  | ||||||
|      |  | ||||||
|         tr02 = tracking.Track('t02', 'jim', trackId=31, timeStamp=datetime(2023, 11, 30), |  | ||||||
|                             data=dict(activity='concept')) |  | ||||||
|         trid02 = tracks.upsert(tr02) |  | ||||||
|         self.assertEqual(trid02, 31) |  | ||||||
|         self.assertEqual(tr02.uid, 'rec-31') |  | ||||||
|         tr02.trackId = trid01 |  | ||||||
|         trid021 = tracks.upsert(tr02) |  | ||||||
|         self.assertEqual(trid021, trid01) |  | ||||||
|         self.assertEqual(tr02.uid, 'rec-' + str(trid01)) |  | ||||||
|      |  | ||||||
|         tr03 = storage.getItem('rec-31') |  | ||||||
|         self.assertEqual(tr03.trackId, 31) |  | ||||||
|      |  | ||||||
|         n = tracks.remove(31) |  | ||||||
|         self.assertEqual(n, 1) |  | ||||||
|         self.assertEqual(tracks.get(31), None) |  | ||||||
|      |  | ||||||
|         storage.commit() |  | ||||||
|     |     | ||||||
|     def test_002_folder(self): |     def test_002_folder(self): | ||||||
|         storage.dropTable('folders') |         tlib_storage.test_folder(self, config) | ||||||
|         root = folder.Root(storage) |  | ||||||
|         self.assertEqual(list(root.keys()), []) |  | ||||||
|         root['top'] = folder.Folder() |  | ||||||
|         self.assertEqual(list(root.keys()), ['top']) |  | ||||||
|         top = root['top'] |  | ||||||
|         top['child1'] = folder.Folder(data=dict(title='First Child')) |  | ||||||
|         self.assertEqual(list(top.keys()), ['child1']) |  | ||||||
|         ch1 = top['child1'] |  | ||||||
|         self.assertEqual(ch1.parent, top.rid) |  | ||||||
|         self.assertEqual(list(top.keys()), ['child1']) |  | ||||||
|      |  | ||||||
|         storage.commit() |  | ||||||
|     |     | ||||||
|     def test_003_type(self): |     def test_003_type(self): | ||||||
|         storage.dropTable('types') |         tlib_storage.test_type(self, config) | ||||||
|         concept.setupCoreTypes(storage) |  | ||||||
|      |  | ||||||
|         types = storage.getContainer(concept.Type) |  | ||||||
|         tps = list(types.query()) |  | ||||||
|         self.assertEqual(len(tps), 6) |  | ||||||
|         self.assertEqual(tps[0].name, 'topic') |  | ||||||
|      |  | ||||||
|         tfolder = types.queryLast(name='folder') |  | ||||||
|         fldrs = list(tfolder.values()) |  | ||||||
|         self.assertEqual(len(fldrs), 2) |  | ||||||
|         self.assertEqual(fldrs[0].name, 'top') |  | ||||||
|      |  | ||||||
|         storage.commit() |  | ||||||
|     |     | ||||||
|     def test_004_topic(self): |     def test_004_topic(self): | ||||||
|         storage.dropTable('topics') |         tlib_storage.test_topic(self, config) | ||||||
|         topics = storage.getContainer(topic.Topic) |  | ||||||
|         types = storage.getContainer(concept.Type) |  | ||||||
|         concept.storePredicate(storage, concept.defaultPredicate) |  | ||||||
|         root = folder.Root(storage) |  | ||||||
|         root['top']['topics'] = ftopics = folder.Folder() |  | ||||||
|         ttopic = types.queryLast(name='topic') |  | ||||||
|         self.assertEqual(ttopic.name, 'topic') |  | ||||||
|         ftopics.setTarget(ttopic) |  | ||||||
|         self.assertEqual(ftopics.ref, 'type-1') |  | ||||||
|      |  | ||||||
|         tp_itc = topic.Topic('itc', data=dict( |  | ||||||
|             title='ITC', description='Information and Communication Technology')) |  | ||||||
|         topics.save(tp_itc) |  | ||||||
|         tp_proglang = topic.Topic('prog_lang', data=dict( |  | ||||||
|             title='Programming Languages',  |  | ||||||
|             description='Programming Languages')) |  | ||||||
|         topics.save(tp_proglang) |  | ||||||
|         tp_itc.addChild(tp_proglang) |  | ||||||
|      |  | ||||||
|         c = list(tp_itc.children()) |  | ||||||
|         self.assertEqual(c[0].name, 'prog_lang') |  | ||||||
|      |  | ||||||
|         storage.commit() |  | ||||||
|   |  | ||||||
|  |  | ||||||
							
								
								
									
										121
									
								
								scopes/tlib_storage.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										121
									
								
								scopes/tlib_storage.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,121 @@ | ||||||
|  | # scopes/tlib_storage.py | ||||||
|  | 
 | ||||||
|  | """Test implementation for the `scopes.storage` package.""" | ||||||
|  | 
 | ||||||
|  | from datetime import datetime | ||||||
|  | from scopes.storage import concept, folder, topic, tracking | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_tracking(self, config): | ||||||
|  |     storage = config.storageFactory(config.dbschema) | ||||||
|  |     storage.dropTable('tracks') | ||||||
|  |     tracks = storage.create(tracking.Container) | ||||||
|  | 
 | ||||||
|  |     tr01 = tracking.Track('t01', 'john') | ||||||
|  |     tr01.update(dict(activity='testing')) | ||||||
|  |     self.assertEqual(tr01.head, {'taskId': 't01', 'userName': 'john'}) | ||||||
|  |     self.assertEqual(tr01.taskId, 't01') | ||||||
|  |     self.assertEqual(tr01.userName, 'john') | ||||||
|  | 
 | ||||||
|  |     self.assertTrue(tracks.getTable() is not None) | ||||||
|  | 
 | ||||||
|  |     trid01 = tracks.save(tr01) | ||||||
|  |     self.assertTrue(trid01 > 0) | ||||||
|  | 
 | ||||||
|  |     #tr01a = tracks.get(trid01) | ||||||
|  |     tr01a = tracks['%07i' % trid01] | ||||||
|  |     self.assertEqual(tr01a.head, tr01.head) | ||||||
|  |     self.assertEqual(tr01a.trackId, trid01) | ||||||
|  |     self.assertEqual(tr01a.data.get('activity'), 'testing') | ||||||
|  | 
 | ||||||
|  |     tr01a.update(dict(text='Set up unit tests.')) | ||||||
|  |     tr01a.timeStamp = None | ||||||
|  |     self.assertTrue(tracks.save(tr01a) > 0) | ||||||
|  | 
 | ||||||
|  |     tr01b = tracks.queryLast(taskId='t01') | ||||||
|  |     self.assertEqual(tr01b.head, tr01.head) | ||||||
|  |     self.assertNotEqual(tr01b.trackId, trid01) | ||||||
|  |     self.assertEqual(tr01b.data.get('activity'), 'testing') | ||||||
|  | 
 | ||||||
|  |     tr02 = tracking.Track('t02', 'jim', trackId=31, timeStamp=datetime(2023, 11, 30), | ||||||
|  |                         data=dict(activity='concept')) | ||||||
|  |     trid02 = tracks.upsert(tr02) | ||||||
|  |     self.assertEqual(trid02, 31) | ||||||
|  |     self.assertEqual(tr02.uid, 'rec-31') | ||||||
|  |     tr02.trackId = trid01 | ||||||
|  |     trid021 = tracks.upsert(tr02) | ||||||
|  |     self.assertEqual(trid021, trid01) | ||||||
|  |     self.assertEqual(tr02.uid, 'rec-' + str(trid01)) | ||||||
|  | 
 | ||||||
|  |     tr03 = storage.getItem('rec-31') | ||||||
|  |     self.assertEqual(tr03.trackId, 31) | ||||||
|  | 
 | ||||||
|  |     n = tracks.remove(31) | ||||||
|  |     self.assertEqual(n, 1) | ||||||
|  |     self.assertEqual(tracks.get(31), None) | ||||||
|  | 
 | ||||||
|  |     storage.commit() | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_folder(self, config): | ||||||
|  |     storage = config.storageFactory(config.dbschema) | ||||||
|  |     storage.dropTable('folders') | ||||||
|  |     root = folder.Root(storage) | ||||||
|  |     self.assertEqual(list(root.keys()), []) | ||||||
|  |     root['top'] = folder.Folder() | ||||||
|  |     self.assertEqual(list(root.keys()), ['top']) | ||||||
|  |     top = root['top'] | ||||||
|  |     top['child1'] = folder.Folder(data=dict(title='First Child')) | ||||||
|  |     self.assertEqual(list(top.keys()), ['child1']) | ||||||
|  |     ch1 = top['child1'] | ||||||
|  |     self.assertEqual(ch1.parent, top.rid) | ||||||
|  |     self.assertEqual(list(top.keys()), ['child1']) | ||||||
|  | 
 | ||||||
|  |     storage.commit() | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_type(self, config): | ||||||
|  |     storage = config.storageFactory(config.dbschema) | ||||||
|  |     storage.dropTable('types') | ||||||
|  |     concept.setupCoreTypes(storage) | ||||||
|  | 
 | ||||||
|  |     types = storage.getContainer(concept.Type) | ||||||
|  |     tps = list(types.query()) | ||||||
|  |     self.assertEqual(len(tps), 6) | ||||||
|  |     self.assertEqual(tps[0].name, 'topic') | ||||||
|  | 
 | ||||||
|  |     tfolder = types.queryLast(name='folder') | ||||||
|  |     fldrs = list(tfolder.values()) | ||||||
|  |     self.assertEqual(len(fldrs), 2) | ||||||
|  |     self.assertEqual(fldrs[0].name, 'top') | ||||||
|  | 
 | ||||||
|  |     storage.commit() | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def test_topic(self, config): | ||||||
|  |     storage = config.storageFactory(config.dbschema) | ||||||
|  |     storage.dropTable('topics') | ||||||
|  |     topics = storage.getContainer(topic.Topic) | ||||||
|  |     types = storage.getContainer(concept.Type) | ||||||
|  |     concept.storePredicate(storage, concept.defaultPredicate) | ||||||
|  |     root = folder.Root(storage) | ||||||
|  |     root['top']['topics'] = ftopics = folder.Folder() | ||||||
|  |     ttopic = types.queryLast(name='topic') | ||||||
|  |     self.assertEqual(ttopic.name, 'topic') | ||||||
|  |     ftopics.setTarget(ttopic) | ||||||
|  |     self.assertEqual(ftopics.ref, 'type-1') | ||||||
|  | 
 | ||||||
|  |     tp_itc = topic.Topic('itc', data=dict( | ||||||
|  |         title='ITC', description='Information and Communication Technology')) | ||||||
|  |     topics.save(tp_itc) | ||||||
|  |     tp_proglang = topic.Topic('prog_lang', data=dict( | ||||||
|  |         title='Programming Languages',  | ||||||
|  |         description='Programming Languages')) | ||||||
|  |     topics.save(tp_proglang) | ||||||
|  |     tp_itc.addChild(tp_proglang) | ||||||
|  | 
 | ||||||
|  |     c = list(tp_itc.children()) | ||||||
|  |     self.assertEqual(c[0].name, 'prog_lang') | ||||||
|  | 
 | ||||||
|  |     storage.commit() | ||||||
|  |      | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue