def t_rdb_delete_by_key(): connect_info = SQLHelper._get_default_connection() rdb_tbl = RDBDataTable("people", connect_info, ["playerID"], commit=False) print("Lines removed:", rdb_tbl.delete_by_key(["abbotgl01"]), "should equal 1") print("Lines removed:", rdb_tbl.delete_by_key(["abbotgl01"]), "should equal 0") print("Lines removed:", rdb_tbl.delete_by_key(["abbeych01"]), "should equal 1")
def t_rdb_insert(): connect_info = SQLHelper._get_default_connection() rdb_tbl = RDBDataTable("people", connect_info, ["playerID"], commit=False) rdb_tbl.insert({"playerID": "abbotgl99"}) if not rdb_tbl.find_by_primary_key(["abbotgl99"]): print("insert test, failed") else: print("insert test, passed")
def t_rdb_find_by_primary_key(): connect_info = SQLHelper._get_default_connection() rdb_tbl = RDBDataTable("people", connect_info, ["playerID"], commit=False) r = { "playerID": "abbotgl01" } print(rdb_tbl.find_by_primary_key(r.values())) print(rdb_tbl.find_by_primary_key(r.values(), ["birthCity", "birthState"]))
def t_rdb_update_by_key(): connect_info = SQLHelper._get_default_connection() rdb_tbl = RDBDataTable("people", connect_info, ["playerID"], commit=False) keys = ["abbotgl01"] new_val = {"deathYear": "123", "deathMonth": "12", "deathDay": "1"} print("Lines updated:", rdb_tbl.update_by_key(keys, new_val), "should equal 1") print("Lines updated:", rdb_tbl.update_by_key(keys, new_val), "should equal 0") print("Lines updated:", rdb_tbl.update_by_key(keys, {"playerID": "abbotgl01"}), "should equal 0")
def t_rdb_delete_by_template(): connect_info = SQLHelper._get_default_connection() rdb_tbl = RDBDataTable("people", connect_info, ["playerID"], commit=False) r = { "playerID": "abbotgl01", "birthYear": "1951", "birthMonth": "2" } print("Lines removed:", rdb_tbl.delete_by_template(r), "should equal 1") print("Lines removed:", rdb_tbl.delete_by_template(r), "should equal 0") print("Lines removed:", rdb_tbl.delete_by_template({"birthYear": "1951"}), "should > 1") print("Lines removed:", rdb_tbl.delete_by_template({"birthYear": "1951"}), "should equal 0")
def t_rdb_find_by_template(): connect_info = SQLHelper._get_default_connection() rdb_tbl = RDBDataTable("people", connect_info, ["playerID"], commit=False) r = { "playerID": "aasedo01", "birthYear": "1954", "birthCity": "Orange" } # print(rdb_tbl.find_by_template({})) print(rdb_tbl.find_by_template(r)) print(rdb_tbl.find_by_template(r, ["weight", "height"])) print(rdb_tbl.find_by_template(r, ["deathYear"]))
def __init__(self, table_name, connect_info, key_columns, commit=False): """ :param table_name: Logical name of the table. :param connect_info: Dictionary of parameters necessary to connect to the data. :param key_columns: List, in order, of the columns (fields) that comprise the primary key. """ if not table_name: table_name = "lahman2019raw" if not key_columns: raise Exception("Key columns can't be empty") self.table_name = table_name self.connect_info = connect_info if connect_info else SQLHelper._get_default_connection( ) self.key_columns = key_columns self.commit = commit # if testing, don't commit