def test3RemoveSchema(self): """ Test removing a more complicated schema """ schema_remdomain_model = create_xsd_and_populate("6_nestedrepeats.xsd", "6_nestedrepeats.xml", self.domain) num_handled = SubmissionHandlingOccurrence.objects.all().count() self.assertEquals(1,num_handled) all_meta = Metadata.objects.all() count = all_meta.count() self.assertEquals(1,count) attachment = all_meta[0].attachment su = StorageUtility() su.remove_schema(schema_remdomain_model.id) # Test that children have become orphans num_handled = SubmissionHandlingOccurrence.objects.all().count() self.assertEquals(0,num_handled) count = Metadata.objects.all().count() self.assertEquals(0,count) self.assertTrue(attachment.submission.is_orphaned()) # TODO fix the db call to be more standard here cursor = connection.cursor() cursor.execute("SELECT * FROM xformmanager_formdefmodel") row = cursor.fetchone() self.assertEquals(row,None) cursor.execute("SELECT * FROM xformmanager_elementdefmodel") row = cursor.fetchone() self.assertEquals(row,None) if settings.DATABASE_ENGINE == 'mysql': cursor.execute("show tables like 'schema_remdomain_xml_nestedrepeats%'") row = cursor.fetchone() self.assertEquals(row,None)
def test1ClearFormData(self): """ Tests clear out all forms. """ create_xsd_and_populate("1_basic.xsd", "1_basic.xml", self.domain) create_xsd_and_populate("2_types.xsd", "2_types.xml", self.domain) create_xsd_and_populate("3_deep.xsd", "3_deep.xml", self.domain) create_xsd_and_populate("4_verydeep.xsd", "4_verydeep.xml", self.domain) create_xsd_and_populate("5_singlerepeat.xsd", "5_singlerepeat.xml", self.domain) create_xsd_and_populate("6_nestedrepeats.xsd", "6_nestedrepeats.xml", self.domain) create_xsd_and_populate("data/pf_followup.xsd", "data/pf_followup_1.xml", self.domain) su = StorageUtility() su.clear() cursor = connection.cursor() cursor.execute("SELECT * FROM xformmanager_formdefmodel") row = cursor.fetchone() self.assertEquals(row,None) cursor.execute("SELECT * FROM xformmanager_elementdefmodel") row = cursor.fetchone() self.assertEquals(row,None) cursor.execute("SELECT * FROM xformmanager_metadata") row = cursor.fetchone() self.assertEquals(row,None) if settings.DATABASE_ENGINE == 'mysql': cursor.execute("show tables like 'schema_remdomain_%'") row = cursor.fetchone() self.assertEquals(row,None)
class XFormManager(object): """A central location for managing xforms. This object includes functionality to upload, insert into, and delete XForms. The forms are translated into database tables where submission data is flattened out and saved for easy reporting access. This class makes the process of handling formdefs (our representation of xform schemas) and formdefmodels (db interaction layer), completely transparent to the programmers. """ def __init__(self): self.su = StorageUtility() def remove_schema(self, id, remove_submissions=False): """Remove a schema entirely, dropping its database tables and optionally deleting all submission to it as well.""" self.su.remove_schema(id, remove_submissions) def remove_data(self, formdef_id, id, remove_submission=False): """Deletes a single row of data from the table, optionally deleting the original submission to it as well.""" self.su.remove_instance_matching_schema(formdef_id, id, remove_submission) def save_schema_POST_to_file(self, stream, file_name): """Given a form post (instance) saves that form to disk, without doing any additional processing.""" try: type = file_name.rsplit('.',1)[1] except IndexError: # POSTed file has no extension type = "xform" return self._save_schema_stream_to_file(stream, type) def save_form_data(self, attachment): """Given an attachment attempt to match it to a known (registered) XForm and parse out its data, saving to the flattened tables and creating metadata for it. Returns True on success and False on failure""" return self.su.save_form_data(attachment) def validate_schema(self, file_name): """validate schema Returns a tuple (is_valid, error) is_valid - True if valid, False if not error - Relevant validation error """ fout = open(file_name, 'r') formdef = FormDef(fout) fout.close() try: formdef.validate() except FormDef.FormDefError, e: return False, e except MetaDataValidationError, e: return False, e
def clear_data(): """Clear most of the data in the system: schemas, submissions, and attachments. Useful in the setup and/or teardown methods of tests. """ su = StorageUtility() su.clear() Submission.objects.all().delete() Attachment.objects.all().delete()
def _testErrors(self, schema_file, instance_file, id): su = SU() xfm = XFM() xsd_file_name = os.path.join(os.path.dirname(__file__),schema_file) xml_file_name = os.path.join(os.path.dirname(__file__),instance_file) schema = xfm._add_schema_from_file(xsd_file_name) formdef = su.get_formdef_from_schema_file(xsd_file_name) data_tree = su._get_data_tree_from_file(xml_file_name) populator = XFormDBTablePopulator( formdef, schema ) queries = populator.populate( data_tree ) xfm.remove_schema(schema.id) return populator.errors
def test2RemoveSchema(self): """ Test removing one schema """ schema_remdomain_model = create_xsd_and_populate("1_basic.xsd", "1_basic.xml", self.domain) su = StorageUtility() su.remove_schema(schema_remdomain_model.id) cursor = connection.cursor() cursor.execute("SELECT * FROM xformmanager_formdefmodel") row = cursor.fetchone() self.assertEquals(row,None) cursor.execute("SELECT * FROM xformmanager_elementdefmodel") row = cursor.fetchone() self.assertEquals(row,None) if settings.DATABASE_ENGINE == 'mysql': cursor.execute("show tables like 'schema_remdomain_xml_basic%'") row = cursor.fetchone() self.assertEquals(row,None)
def __init__(self): self.su = StorageUtility()