def test_add_hook_to_db_new_job(self): if os.path.isfile('/tmp/test.db'): os.remove('/tmp/test.db') # Test writer = Writer(base_path + '/static/config_valid.yml') writer.set_prop('name', self._job_name) writer.set_prop('input', self._job_input) writer.set_prop('output', self._job_output) writer.hooks_writer.add_hook(name='test', method='method', when='never', description='Hook from Test', priority=10) writer.hooks_writer.add_hook(name='test2', method='method2', when='always', active=False) writer.save() self.assertTrue(os.path.isfile('/tmp/test.db')) jobInDb = self._get_job() self.assertEqual(self._job_name, jobInDb['name']) self.assertEqual(self._job_name, writer.get_prop('name')) hooksInDb = self._get_hooks(jobInDb['id'], 2) self.assertEqual('test', hooksInDb[0]['name']) self.assertEqual('Hook from Test', hooksInDb[0]['description']) self.assertIs(10, hooksInDb[0]['priority']) self.assertEqual('method', hooksInDb[0]['method']) self.assertEqual(1, hooksInDb[0]['active']) self.assertEqual('never', hooksInDb[0]['when']) self.assertEqual('test2', hooksInDb[1]['name']) self.assertIs(None, hooksInDb[1]['description']) self.assertIs(1, hooksInDb[1]['priority']) self.assertEqual('method2', hooksInDb[1]['method']) self.assertEqual(0, hooksInDb[1]['active']) self.assertEqual('always', hooksInDb[1]['when'])
def test_new_job_then_update(self): if os.path.isfile('/tmp/test.db'): os.remove('/tmp/test.db') # Test writer = Writer(base_path + '/static/config_valid.yml') writer.set_prop('name', self._job_name) writer.set_prop('input', self._job_input) writer.set_prop('input_parameters', self._job_input_params) writer.set_prop('output', self._job_output) writer.set_prop('output_parameters', self._job_output_params) job = writer.save() self.assertTrue(os.path.isfile('/tmp/test.db')) jobInDb = self._get_job() self.assertEqual(self._job_name, jobInDb['name']) self.assertTrue(jobInDb['active']) self.assertEqual(self._job_input, jobInDb['input']) self.assertEqual(self._job_output, jobInDb['output']) writer = Writer(base_path + '/static/config_valid.yml', job.name) job = writer.get_job() self.assertEqual(job.name, jobInDb['name']) self.assertTrue(job.active) self.assertEqual(job.input, jobInDb['input']) self.assertEqual(job.output, jobInDb['output']) writer = Writer(base_path + '/static/config_valid.yml', job.name) writer.set_prop('active', False) job = writer.save() jobInDb = self._get_job() self.assertEqual(self._job_name, jobInDb['name']) self.assertFalse(jobInDb['active'])
def test_add_hook_to_db_existing_job(self): if os.path.isfile('/tmp/test.db'): os.remove('/tmp/test.db') # Test writer = Writer(base_path + '/static/config_valid.yml') writer.set_prop('name', self._job_name) writer.set_prop('input', self._job_input) writer.set_prop('output', self._job_output) writer.hooks_writer.add_hook(name='test', method='method', when='never', description='Hook from Test', priority=10) writer.hooks_writer.add_hook(name='test2', method='method2', when='always', active=False) writer.save() self.assertTrue(os.path.isfile('/tmp/test.db')) jobInDb = self._get_job() self._get_hooks(jobInDb['id'], 2) # Reinit the writer now to see if I have my job writer = Writer(base_path + '/static/config_valid.yml', jobInDb['name']) hook_test = writer.hooks_writer.get_hook('test') self.assertEqual('test', hook_test['name']) self.assertEqual('Hook from Test', hook_test['description']) self.assertIs(10, hook_test['priority']) self.assertEqual('method', hook_test['method']) self.assertEqual(1, hook_test['active']) self.assertEqual('never', hook_test['when']) hook_test2 = writer.hooks_writer.get_hook('test2') self.assertEqual('test2', hook_test2['name']) self.assertIs(None, hook_test2['description']) self.assertIs(1, hook_test2['priority']) self.assertEqual('method2', hook_test2['method']) self.assertEqual(0, hook_test2['active']) self.assertEqual('always', hook_test2['when']) # Now add a hook writer.hooks_writer.add_hook(name='test3', method='method3', when='sometimes') writer.save() hooksInDb = self._get_hooks(jobInDb['id'], 3) self.assertEqual('test2', hooksInDb[1]['name']) self.assertEqual('test3', hooksInDb[2]['name']) self.assertIs(None, hooksInDb[2]['description']) self.assertIs(1, hooksInDb[2]['priority']) self.assertEqual('method3', hooksInDb[2]['method']) self.assertEqual('sometimes', hooksInDb[2]['when']) writer = Writer(base_path + '/static/config_valid.yml', jobInDb['name']) hooksInObj = writer.hooks_writer.get_hooks() self.assertEqual(3, len(hooksInObj)) self.assertEqual('test', hooksInObj['test']['name']) self.assertEqual('test2', hooksInObj['test2']['name']) self.assertEqual('test3', hooksInObj['test3']['name'])
def test_new_job(self): if os.path.isfile('/tmp/test.db'): os.remove('/tmp/test.db') # Test writer = Writer(base_path + '/static/config_valid.yml') # Name + Desc + Prio writer.set_prop('name', self._job_name) writer.set_prop('description', self._job_desc) writer.set_prop('priority', self._job_prio) writer.set_prop('mode', self._job_mode) writer.set_prop('active', True) writer.set_prop('input', self._job_input) writer.set_prop('input_parameters', self._job_input_params) writer.set_prop('output', self._job_output) writer.set_prop('output_parameters', self._job_output_params) job = writer.save() self.assertTrue(os.path.isfile('/tmp/test.db')) reader = Reader(base_path + '/static/config_valid.yml', job.name) job = reader.get_job() self.assertEqual(self._job_name, job.name) self.assertEqual(self._job_desc, job.description) self.assertEqual(self._job_prio, job.priority) self.assertEqual(self._job_mode, job.mode) self.assertTrue(job.active) self.assertEqual(self._job_input, job.input) self.assertEqual(self._job_input_params, job.input_parameters) self.assertEqual(self._job_output, job.output) self.assertEqual(self._job_output_params, job.output_parameters) self.assertEqual(self._job_prio, job.priority)
def test_add_job_without_field_then_retrieve(self): if os.path.isfile('/tmp/test.db'): os.remove('/tmp/test.db') # Test writer = Writer(base_path + '/static/config_valid.yml') writer.set_prop('name', self._job_name) writer.set_prop('input', self._job_input) writer.set_prop('output', self._job_output) writer.save() self.assertTrue(os.path.isfile('/tmp/test.db')) jobInDb = self._get_job() # Reinit the writer now to make sure I have no error writer = Writer(base_path + '/static/config_valid.yml', jobInDb['name']) with self.assertRaisesRegex(KeyError, "Field test does not exist"): writer.fields_writer.get_field('test')
def test_new_job(self): if os.path.isfile('/tmp/test.db'): os.remove('/tmp/test.db') # Test writer = Writer(base_path + '/static/config_valid.yml') # Name + Desc + Prio writer.set_prop('name', self._job_name) writer.set_prop('description', self._job_desc) writer.set_prop('priority', self._job_prio) # mode : u=update | c=create | cu=create+update | d=delete writer.set_prop('mode', self._job_mode) # Inactive writer.set_prop('active', True) # Input type + Parameters writer.set_prop('input', self._job_input) writer.set_prop('input_parameters', self._job_input_params) # Output type + Parameters writer.set_prop('output', self._job_output) writer.set_prop('output_parameters', self._job_output_params) writer.save() self.assertTrue(os.path.isfile('/tmp/test.db')) job = self._get_job() self.assertEqual(self._job_name, job['name']) self.assertEqual(self._job_name, writer.get_prop('name')) self.assertEqual(self._job_desc, job['description']) self.assertEqual(self._job_desc, writer.get_prop('description')) self.assertEqual(self._job_prio, job['priority']) self.assertEqual(self._job_mode, job['mode']) self.assertTrue(job['active']) self.assertEqual(self._job_input, job['input']) self.assertEqual(self._job_input_params, json.loads(job['input_parameters'])) self.assertEqual(self._job_output, job['output']) self.assertEqual(self._job_output_params, json.loads(job['output_parameters'])) self.assertEqual(self._job_prio, job['priority'])
def test_new_job_then_delete(self): if os.path.isfile('/tmp/test.db'): os.remove('/tmp/test.db') # Test writer = Writer(base_path + '/static/config_valid.yml') writer.set_prop('name', 'To Delete') writer.set_prop('input', self._job_input) writer.set_prop('output', self._job_output) job = writer.save() writer = Writer(base_path + '/static/config_valid.yml', job.name) writer.delete() with self.assertRaisesRegex(ValueError, "Can't retrieve Job .+"): Writer(base_path + '/static/config_valid.yml', job.name)
def test_new_job_missing_data(self): if os.path.isfile('/tmp/test.db'): os.remove('/tmp/test.db') writer = Writer(base_path + '/static/config_valid.yml') with self.assertRaisesRegex(ValueError, "Property name is required"): writer.save() writer.set_prop('name', 'test') with self.assertRaisesRegex(ValueError, "Property input is required"): writer.save() writer.set_prop('input', 'test') with self.assertRaisesRegex(ValueError, "Property output is required"): writer.save() writer.set_prop('output', 'test') writer.save() self.assertTrue(os.path.isfile('/tmp/test.db'))
def test_new_job_with_fields_and_rules(self): if os.path.isfile('/tmp/test.db'): os.remove('/tmp/test.db') writer = Writer(base_path + '/static/config_valid.yml') writer.set_prop('name', self._job_name) writer.set_prop('input', self._job_input) writer.set_prop('input_parameters', {'delimiter': ';'}) writer.set_prop('output', self._job_output) writer.fields_writer.add_field('input_test', 'output_test') writer.fields_writer.add_rule(output_field='output_test', name='test', method='my_method', params={'a': 'b'}, blocking=True, priority=1) writer.fields_writer.add_rule(output_field='output_test', name='test2', method='my_method2', params={'b': 'c'}, blocking=False, priority=100) job = writer.save() self.assertTrue(os.path.isfile('/tmp/test.db')) reader = Reader(base_path + '/static/config_valid.yml', job.name) job = reader.get_job() self.assertEqual(job.input_parameters, {'delimiter': ';'}) fields = reader.get_fields() self.assertIs(len(fields), 1) self.assertEqual(fields[0].output, 'output_test') rules = fields[0].rules self.assertIs(len(rules), 2) self.assertEqual(rules[0].name, 'test') self.assertEqual(rules[1].name, 'test2')
def test_add_rule_to_db_existing_job(self): if os.path.isfile('/tmp/test.db'): os.remove('/tmp/test.db') writer = Writer(base_path + '/static/config_valid.yml') writer.set_prop('name', self._job_name) writer.set_prop('input', self._job_input) writer.set_prop('output', self._job_output) writer.fields_writer.add_field('input_test', 'output_test') writer.fields_writer.add_field('input_test2', 'output_test2') writer.fields_writer.add_rule(output_field='output_test', name='test', method='my_method', params={'a': 'b'}, blocking=True) writer.fields_writer.add_rule(output_field='output_test', name='test2', method='my_method2', params={'b': 'c'}, blocking=False) writer.save() self.assertTrue(os.path.isfile('/tmp/test.db')) jobInDb = self._get_job() fieldsInDb = self._get_fields(jobInDb['id'], 2) self._get_rules(fieldsInDb[0]['id'], 2) # Reinit the writer now to see if I have my job writer = Writer(base_path + '/static/config_valid.yml', jobInDb['name']) field_w_rules = writer.fields_writer.get_field('output_test') self.assertEqual(field_w_rules['input'], 'input_test') # Read Rules rules = writer.fields_writer.get_rules('output_test') rule_test = rules['test'] self.assertEqual('test', rule_test['name']) self.assertEqual('my_method', rule_test['method']) self.assertTrue(rule_test['blocking']) self.assertEqual({'a': 'b'}, rule_test['params']) rule_test2 = rules['test2'] self.assertEqual('test2', rule_test2['name']) self.assertEqual('my_method2', rule_test2['method']) self.assertFalse(rule_test2['blocking']) # Now add more rules / fields writer.fields_writer.add_rule(output_field='output_test', name='test3', method='my_method3', blocking=False) writer.fields_writer.add_field(input='input_test3', output='output_test3') writer.fields_writer.add_rule(output_field='output_test3', name='test4', method='my_method4', blocking=True) writer.save() fieldsInDb = self._get_fields(jobInDb['id'], 3) # for the previous fields, saved the first time rulesInDb = self._get_rules(fieldsInDb[0]['id'], 3) self.assertEqual('test', rulesInDb[0]['name']) self.assertEqual('test2', rulesInDb[1]['name']) self.assertEqual('test3', rulesInDb[2]['name']) self.assertIs(None, rulesInDb[2]['description']) self.assertIs(1, rulesInDb[2]['priority']) self.assertEqual('my_method3', rulesInDb[2]['method']) # For the new field rulesInDb = self._get_rules(fieldsInDb[2]['id'], 1) self.assertEqual('test4', rulesInDb[0]['name']) # Get the same from the writer writer = Writer(base_path + '/static/config_valid.yml', jobInDb['name']) rulesInObj = writer.fields_writer.get_rules('output_test') self.assertEqual(len(rulesInObj), 3) self.assertEqual('test', rulesInObj['test']['name']) self.assertEqual('test2', rulesInObj['test2']['name']) self.assertEqual('test3', rulesInObj['test3']['name']) rulesInObj = writer.fields_writer.get_rules('output_test3') self.assertEqual(len(rulesInObj), 1) self.assertEqual('test4', rulesInObj['test4']['name']) # Get a field to make sure we also have rules with it fieldInObj = writer.fields_writer.get_field('output_test') self.assertEqual(len(fieldInObj['rules']), 3)
def test_add_rules_to_db_new_job(self): if os.path.isfile('/tmp/test.db'): os.remove('/tmp/test.db') # Test writer = Writer(base_path + '/static/config_valid.yml') writer.set_prop('name', self._job_name) writer.set_prop('input', self._job_input) writer.set_prop('output', self._job_output) writer.fields_writer.add_field('input_test', 'output_test') writer.fields_writer.add_field('input_test2', 'output_test2') writer.fields_writer.add_rule(output_field='output_test', name='lowercase', method='lwrcs', description='Lower the case !', active=False) writer.fields_writer.add_rule(output_field='output_test', name='uppercase', method='uprc', params={'a': 'b'}, blocking=True, priority=10) job = writer.save() writer = Writer(base_path + '/static/config_valid.yml', job.name) self.assertTrue(os.path.isfile('/tmp/test.db')) jobInDb = self._get_job() self.assertEqual(self._job_name, jobInDb['name']) self.assertEqual(self._job_name, writer.get_prop('name')) fieldsInDb = self._get_fields(jobInDb['id'], 2) self.assertEqual('input_test', fieldsInDb[0]['input']) self.assertEqual('output_test', fieldsInDb[0]['output']) rulesInDb = self._get_rules(fieldsInDb[0]['id'], 2) self.assertEqual('lowercase', rulesInDb[0]['name']) self.assertEqual('Lower the case !', rulesInDb[0]['description']) self.assertEqual(0, rulesInDb[0]['active']) self.assertEqual('lwrcs', rulesInDb[0]['method']) self.assertEqual({}, json.loads(rulesInDb[0]['params'])) self.assertIs(0, rulesInDb[0]['blocking']) self.assertIs(1, rulesInDb[0]['priority']) self.assertEqual('uppercase', rulesInDb[1]['name']) self.assertIs(None, rulesInDb[1]['description']) self.assertEqual(1, rulesInDb[1]['active']) self.assertEqual('uprc', rulesInDb[1]['method']) self.assertEqual({'a': 'b'}, json.loads(rulesInDb[1]['params'])) self.assertIs(1, rulesInDb[1]['blocking']) self.assertIs(10, rulesInDb[1]['priority']) job = writer.get_job() self.assertEqual(self._job_name, job.name) fields = writer.fields_writer.get_fields() self.assertTrue('output_test' in fields) self.assertTrue('output' in fields['output_test']) self.assertEqual(fields['output_test']['output'], 'output_test') self.assertTrue('rules' in fields['output_test']) self.assertTrue('lowercase' in fields['output_test']['rules']) self.assertTrue('method' in fields['output_test']['rules']['lowercase']) self.assertTrue('params' in fields['output_test']['rules']['lowercase']) self.assertEqual(fields['output_test']['rules']['lowercase']['params'], {}) rules = writer.fields_writer.get_rules('output_test') self.assertTrue('lowercase' in rules) self.assertTrue('method' in rules['lowercase']) self.assertTrue('params' in rules['lowercase']) self.assertEqual({}, rules['lowercase']['params']) self.assertTrue('uppercase' in rules) self.assertTrue('method' in rules['uppercase']) self.assertTrue('params' in rules['uppercase']) self.assertEqual({'a': 'b'}, rules['uppercase']['params'])