Example #1
0
def discover_constraints_from_database(table, constraints_path=None,
                                       conn=None, dbtype=None, db=None,
                                       host=None, port=None, user=None,
                                       password=None, **kwargs):
    """
    Discover constraints in the given database table.

    Writes constraints as JSON to the specified file (or to stdout).
    """
    (table, dbtype) = parse_table_name(table, dbtype)
    db = database_connection(table=table, conn=conn, dbtype=dbtype, db=db,
                             host=host, port=port,
                             user=user, password=password)
    constraints = discover_db_table(dbtype, db, table, **kwargs)
    if constraints is None:
        # should never happen
        return

    output = constraints.to_json(tddafile=constraints_path)

    if constraints_path:
        with open(constraints_path, 'w') as f:
            f.write(output);
    else:
        print(output)
    return output
Example #2
0
def discover_constraints_from_database(table,
                                       constraints_path=None,
                                       conn=None,
                                       dbtype=None,
                                       db=None,
                                       host=None,
                                       port=None,
                                       user=None,
                                       password=None,
                                       **kwargs):
    """
    Discover constraints in the given database table.

    Writes constraints as JSON to the specified file (or to stdout).
    """
    (table, dbtype) = parse_table_name(table, dbtype)
    db = database_connection(table=table,
                             conn=conn,
                             dbtype=dbtype,
                             db=db,
                             host=host,
                             port=port,
                             user=user,
                             password=password)
    constraints = discover_db_table(dbtype, db, table, **kwargs)
    if constraints_path:
        with open(constraints_path, 'w') as f:
            f.write(constraints.to_json())
    else:
        print(constraints.to_json())
Example #3
0
 def test_sqlite_discover_elements_rex(self):
     # build constraints for full 118 element dataset
     constraints = discover_db_table('sqlite',
                                     self.db,
                                     'elements',
                                     inc_rex=True)
     self.assertStringCorrect(constraints.to_json(),
                              'elements118rex.tdda',
                              rstrip=True)
Example #4
0
 def test_discover_elements(self):
     # build constraints for full 118 element dataset
     elements = self.dbh.resolve_table('elements')
     constraints = discover_db_table(self.dbh.dbtype,
                                     self.db,
                                     elements,
                                     inc_rex=False)
     constraints.remove_field('_rowindex')  # hidden field, ignore it
     self.assertStringCorrect(constraints.to_json(),
                              'elements118.tdda',
                              rstrip=True,
                              ignore_substrings=[
                                  '"as_at":', '"local_time":',
                                  '"utc_time":', '"creator":', '"host":',
                                  '"user":'******'"source":', '"rdbms":',
                                  '"dataset":', '"tddafile":'
                              ])
Example #5
0
 def test_discover_elements(self):
     # build constraints for full 118 element dataset
     elements = self.dbh.resolve_table('elements')
     constraints = discover_db_table(self.dbh.dbtype, self.db, elements,
                                     inc_rex=False)
     constraints.remove_field('_rowindex') # hidden field, ignore it
     self.assertStringCorrect(constraints.to_json(),
                              'elements118.tdda',
                              rstrip=True,
                              ignore_substrings=['"as_at":',
                                                 '"local_time":',
                                                 '"utc_time":',
                                                 '"creator":',
                                                 '"host":',
                                                 '"user":'******'"source":',
                                                 '"rdbms":',
                                                 '"dataset":',
                                                 '"tddafile":'])
Example #6
0
 def test_discover_elements_rex(self):
     # build constraints for full 118 element dataset
     elements = self.dbh.resolve_table('elements')
     constraints = discover_db_table(self.dbh.dbtype,
                                     self.db,
                                     elements,
                                     inc_rex=True)
     constraints.remove_field('_rowindex')  # hidden field, ignore it
     j = constraints.to_json()
     # compare against the right expected file, depending on whether the
     # version of python we're running under has escaped commas or not.
     expected_file = ('elements118oldrex.tdda'
                      if '\\,' in j else 'elements118rex.tdda')
     self.assertStringCorrect(j,
                              expected_file,
                              rstrip=True,
                              ignore_substrings=[
                                  '"as_at":', '"local_time":',
                                  '"utc_time":', '"creator":', '"host":',
                                  '"user":'******'"source":', '"rdbms":',
                                  '"dataset":', '"tddafile":'
                              ])
Example #7
0
 def test_discover_elements_rex(self):
     # build constraints for full 118 element dataset
     elements = self.dbh.resolve_table('elements')
     constraints = discover_db_table(self.dbh.dbtype, self.db, elements,
                                     inc_rex=True, seed=827364)
     constraints.remove_field('_rowindex') # hidden field, ignore it
     j = constraints.to_json()
     # compare against the right expected file, depending on whether the
     # version of python we're running under has escaped commas or not.
     expected_file = ('elements118oldrex.tdda' if '\\,' in j
                      else 'elements118rex.tdda')
     self.assertStringCorrect(j, expected_file, rstrip=True,
                              ignore_substrings=['"as_at":',
                                                 '"local_time":',
                                                 '"utc_time":',
                                                 '"creator":',
                                                 '"host":',
                                                 '"user":'******'"source":',
                                                 '"rdbms":',
                                                 '"dataset":',
                                                 '"tddafile":'])