class TestClass(object): cwd = os.path.dirname(os.path.realpath(__file__)) cwms = CWMS() cwms.connect() def test_one(self): """ """ level = self.cwms.retrieve_location_level_values( "DWR.Elev-Forebay.Inst.0.0.CENWDP-FRM-FCST", "01/01/2000", "05/01/2000", "F", df=False, ) print(level) assert level def test_final(self): """ close: Testing good close from db for cleanup """ c = self.cwms.close() assert c == True
class TestClass(object): cwd = os.path.dirname(os.path.realpath(__file__)) cwms = CWMS() load_dotenv() user = os.getenv("USER") password = os.getenv("PASSWORD") host = os.getenv("HOST") service_name = os.getenv("SERVICE_NAME") def test_one(self): """ connect: Testing bad connection to db """ c = self.cwms.connect( host="BAD_HOST", service_name="BAD_SERVICE_NAME", port=1521, user=self.user, password=self.password, ) assert c == False def test_two(self): """ connect: Testing good connection to db """ c = self.cwms.connect( host=self.host, service_name=self.service_name, port=1521, user=self.user, password=self.password, ) assert c == True def test_connect_using_name(self): """ connect: Testing good connection to db """ c = self.cwms.connect(name="pt7") assert c == True def test_final(self): """ close: Testing good close from db """ c = self.cwms.close() assert c == True
class TestClass(object): cwd = os.path.dirname(os.path.realpath(__file__)) cwms = CWMS() cwms.connect() def test_one(self): """ store_location delete_location: Testing successful store_location """ self.cwms.store_location("TST") cur = self.cwms.conn.cursor() loc = self.cwms.retrieve_location("TST")[0][2] cur.close() if "TST" == loc: assert self.cwms.delete_location("TST") else: print(loc) raise ValueError() cur = self.cwms.conn.cursor() sql = """ select base_location_id from cwms_20.at_base_location where base_location_id = 'TST' """ loc = cur.execute(sql).fetchall() cur.close() if loc: raise ValueError() def test_final(self): """ close: Testing good close from db for cleanup """ c = self.cwms.close() assert c == True
def cwms(): cwms = CWMS(verbose=True) yield cwms c = cwms.close()
def connection(name): cwms = CWMS(verbose=True) cwms.connect(name=name) yield cwms cwms.close()
class TestClass(object): cwd = os.path.dirname(os.path.realpath(__file__)) cwms = CWMS() cwms.connect() def test_one(self): """ get_ts_code: Testing successful delete_window """ # adding fake location and data to test with df = self.cwms.retrieve_ts( "LWG.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) if not self.cwms.retrieve_location("TST"): self.cwms.store_location("TST") p_cwms_ts_id = "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV" p_units = "cms" values = list(df["value"]) p_qualities = list(df["quality_code"]) times = list(df["date_time"]) self.cwms.store_ts(p_cwms_ts_id, p_units, times, values, p_qualities) df2 = self.cwms.retrieve_ts( "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2018/1/1", "2019/9/1", "cms", return_df=True, ) start_time = "2018/1/1" end_time = "2019/2/1" self.cwms.delete_ts_window( p_cwms_ts_id, start_time, end_time, p_override_protection="F", p_version_date=None, p_db_office_code=26, ) df3 = self.cwms.retrieve_ts( "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2018/1/1", "2019/9/1", "cms", return_df=True, ) assert df3.set_index("date_time").equals( df2.set_index("date_time").loc["2019-02-01":]) self.cwms.delete_ts("TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "DELETE TS DATA") self.cwms.delete_ts("TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "DELETE TS ID") self.cwms.delete_location("TST") def test_two(self): """ get_extents: Testing successful get_extents """ min_date = self.cwms.get_ts_min_date( "LWG.Flow-Out.Ave.~1Day.1Day.CBT-REV") max_date = self.cwms.get_ts_max_date( "LWG.Flow-Out.Ave.~1Day.1Day.CBT-REV") mn, mx = self.cwms.get_extents("LWG.Flow-Out.Ave.~1Day.1Day.CBT-REV") assert mn == min_date assert mx == max_date def test_three(self): """ get_por: Testing successful get_por """ l = self.cwms.get_por("ALF.Elev-Forebay.Ave.~1Day.1Day.CBT-RAW", return_df=False) assert isinstance(l, list) assert np.floor(l[0][1] + 1) == 626 df = self.cwms.get_por("ALF.Elev-Forebay.Ave.~1Day.1Day.CBT-RAW", return_df=True) assert isinstance(df, pd.core.frame.DataFrame) assert np.floor(df["value"].values[0] + 1) == 626 assert df.shape[0] > 20000 def test_four(self): """ retrieve_multi_ts: Testing successful retrieve_multi_ts """ p_cwms_ts_id_list = [ "LWG.Flow-Out.Ave.~1Day.1Day.CBT-REV", "TDA.Flow-Out.Ave.~1Day.1Day.CBT-REV", ] df = self.cwms.retrieve_multi_ts(p_cwms_ts_id_list, "2019/1/1", "2019/9/1", pivot=True) assert isinstance(df, pd.core.frame.DataFrame) assert np.floor(df[p_cwms_ts_id_list[0]][0] + 1) == 575.0 lwg = self.cwms.retrieve_ts(p_cwms_ts_id_list[0], "2019/1/1", "2019/9/1") assert list(lwg["value"].dropna().values) == list( df[p_cwms_ts_id_list[0]].dropna().values) def test_final(self): """ close: Testing good close from db for cleanup """ c = self.cwms.close() assert c == True
def loc(verbose, name, loc, d): cwms = CWMS(verbose=verbose) cwms.connect(name=name) try: if d: cwms.delete_location(loc, "DELETE TS DATA") cwms.delete_location(loc, "DELETE TS ID") cwms.delete_location(loc) else: cwms.store_location(p_location_id=loc) except: cwms.close() return 0 cwms.close()
def cwms(): cwms = CWMS(verbose=True) cwms.connect() try: cwms.delete_location("CWMSPY", "DELETE TS DATA") cwms.delete_location("CWMSPY", "DELETE TS ID") cwms.delete_location("CWMSPY") except: pass alter_session_sql = "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'" cur = cwms.conn.cursor() cur.execute(alter_session_sql) cur.close() cwms.store_location("CWMSPY") yield cwms # test try: cwms.delete_location("CWMSPY", "DELETE TS DATA") cwms.delete_location("CWMSPY", "DELETE TS ID") cwms.delete_location("CWMSPY") except: pass c = cwms.close()
def cwms_data(request): cwms = CWMS(verbose=True) cwms.connect() units, tz = ["cms", "UTC"] units, tz = request.param alter_session_sql = "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'" cur = cwms.conn.cursor() cur.execute(alter_session_sql) cur.close() try: cwms.delete_location("CWMSPY", "DELETE TS DATA") cwms.delete_location("CWMSPY", "DELETE TS ID") cwms.delete_location("CWMSPY") except: pass cwms.store_location("CWMSPY") p_cwms_ts_id = "CWMSPY.Flow.Inst.0.0.REV" p_units = units times = [ x.strftime("%Y/%m/%d") for x in pd.date_range(datetime(2016, 12, 31), periods=400) ] values = [math.sin(x) for x in range(len(times))] try: cwms.store_ts( p_cwms_ts_id=p_cwms_ts_id, p_units=p_units, times=times, values=values, qualities=None, p_override_prot="T", timezone=tz, ) except Exception as e: try: cwms.delete_location("CWMSPY", "DELETE TS DATA") cwms.delete_location("CWMSPY", "DELETE TS ID") cwms.delete_location("CWMSPY") except: pass c = cwms.close() raise e yield cwms, times, values, p_cwms_ts_id, units, tz cwms.delete_location("CWMSPY", "DELETE TS DATA") cwms.delete_location("CWMSPY", "DELETE TS ID") cwms.delete_location("CWMSPY") c = cwms.close()
def cwms(request): cwms = CWMS(verbose=True) name = request.param cwms.connect(name=name) yield cwms try: cwms.delete_location("CWMSPY", "DELETE TS DATA") cwms.delete_location("CWMSPY", "DELETE TS ID") cwms.delete_location("CWMSPY") except: pass cwms.close()
class TestClass(object): cwd = os.path.dirname(os.path.realpath(__file__)) cwms = CWMS() cwms.connect() def test_one(self): """ get_ts_code: Testing successful ts code """ ts_code = self.cwms.get_ts_code( "ALF.Elev-Forebay.Ave.~1Day.1Day.CBT-RAW") assert ts_code == "23318028" def test_three(self): """ get_ts_code: Testing unsuccessful ts code bc of bad p_cwms_ts_id """ try: self.cwms.get_ts_code("Not a code") except ValueError as e: msg = 'ORA-20001: TS_ID_NOT_FOUND: The timeseries identifier "Not a code"' assert msg in e.__repr__() def test_four(self): """ get_ts_code: Testing unsuccessful ts code bc of bad p_db_office_code """ try: self.cwms.get_ts_code("ALF.Elev-Forebay.Ave.~1Day.1Day.CBT-RAW", 35) except ValueError as e: msg = 'ORA-20001: TS_ID_NOT_FOUND: The timeseries identifier "ALF.Elev-Forebay.Ave.~1Day.1Day.CBT-RAW" was not found for office "POH"' assert msg in e.__repr__() def test_five(self): """ retrieve_ts: Testing successful retrieve_ts """ l = self.cwms.retrieve_ts( "TDA.Flow-Spill.Ave.1Hour.1Hour.CBT-RAW", "2019/1/1", "2019/9/1", return_df=False, ) assert isinstance(l, list) assert np.floor(l[1600][1] + 1) == 40 df = self.cwms.retrieve_ts("TDA.Flow-Spill.Ave.1Hour.1Hour.CBT-RAW", "2019/1/1", "2019/9/1") assert isinstance(df, pd.core.frame.DataFrame) assert np.floor(df["value"].values[1600] + 1) == 40 def test_six(self): """ retrieve_ts: Testing unsuccessful retrieve_ts """ try: self.cwms.retrieve_ts("this is not valid", "2019/1/1", "2019/9/1", "cms", return_df=False) except Exception as e: msg = 'ORA-06502: PL/SQL: numeric or value error\nORA-06512: at "CWMS_20.CWMS_TS"' assert msg in e.__str__() def test_seven(self): """ retrieve_ts: Testing convert to local_tz the LWG.Flow-Out.Ave.~1Day.1Day.CBT-REV pathname inputs data at 12:00 local time, so it should only have 1 hour when on local, but 2 hours when utc """ df = self.cwms.retrieve_ts( "LWG.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, local_tz=True, ) assert len(set([x.hour for x in df["date_time"]])) == 1 df = self.cwms.retrieve_ts( "LWG.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, local_tz=False, ) assert len(set([x.hour for x in df["date_time"]])) == 2 def test_eight(self): """ store_ts: Testing store_ts and delete_ts """ df = self.cwms.retrieve_ts( "LWG.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) if not self.cwms.retrieve_location("TST"): self.cwms.store_location("TST") p_cwms_ts_id = "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV" p_units = "cms" values = list(df["value"]) p_qualities = list(df["quality_code"]) times = list(df["date_time"]) self.cwms.store_ts(p_cwms_ts_id, p_units, times, values, p_qualities, p_override_prot="T") df2 = self.cwms.retrieve_ts( "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) assert df.equals(df2) self.cwms.delete_ts("TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "DELETE TS DATA") self.cwms.delete_ts("TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "DELETE TS ID") try: df2 = self.cwms.retrieve_ts( "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) except ValueError as e: msg = 'TS_ID_NOT_FOUND: The timeseries identifier "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV"' assert msg in e.__str__() # testing store_ts with times as string and qualities = None times = [x.strftime("%Y-%m-%d %H:%M:%S") for x in times] self.cwms.store_ts(p_cwms_ts_id, p_units, times, values, qualities=None) df2 = self.cwms.retrieve_ts( "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) assert df.equals(df2) self.cwms.delete_ts("TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "DELETE TS DATA") self.cwms.delete_ts("TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "DELETE TS ID") try: df2 = self.cwms.retrieve_ts( "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) except ValueError as e: msg = 'TS_ID_NOT_FOUND: The timeseries identifier "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV"' assert msg in e.__str__() # testing store_ts with times as datetime.datetime and qualities = None times = [ datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S") for x in times ] self.cwms.store_ts(p_cwms_ts_id, p_units, times, values, qualities=None) df2 = self.cwms.retrieve_ts( "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) assert df.equals(df2) self.cwms.delete_ts("TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "DELETE TS DATA") self.cwms.delete_ts("TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "DELETE TS ID") self.cwms.delete_location("TST") try: df2 = self.cwms.retrieve_ts( "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) except ValueError as e: msg = 'TS_ID_NOT_FOUND: The timeseries identifier "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV"' assert msg in e.__str__() def test_nine(self): """ store_ts: Testing rename_ts """ df = self.cwms.retrieve_ts( "LWG.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) if not self.cwms.retrieve_location("TST"): self.cwms.store_location("TST") p_cwms_ts_id_old = "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV" p_cwms_ts_id_new = "TST.Flow-In.Ave.~1Day.1Day.CBT-REV" p_units = "cms" values = list(df["value"]) p_qualities = list(df["quality_code"]) times = list(df["date_time"]) self.cwms.store_ts(p_cwms_ts_id_old, p_units, times, values, p_qualities) df2 = self.cwms.retrieve_ts(p_cwms_ts_id_old, "2019/1/1", "2019/9/1", "cms", return_df=True) assert df.equals(df2) self.cwms.rename_ts(p_cwms_ts_id_old, p_cwms_ts_id_new, p_utc_offset_new=None, p_office_id=None) df3 = self.cwms.retrieve_ts(p_cwms_ts_id_new, "2019/1/1", "2019/9/1", "cms", return_df=True) assert df2.equals(df3) self.cwms.delete_ts(p_cwms_ts_id_new, "DELETE TS DATA") self.cwms.delete_ts(p_cwms_ts_id_new, "DELETE TS ID") self.cwms.delete_location("TST") def test_ten(self): """ get_max_date/get_min_date: Testing for successful min/max dates """ min_date = self.cwms.get_ts_min_date( "LWG.Flow-Out.Ave.~1Day.1Day.CBT-REV") max_date = self.cwms.get_ts_max_date( "LWG.Flow-Out.Ave.~1Day.1Day.CBT-REV") assert isinstance(min_date, datetime.datetime) assert isinstance(max_date, datetime.datetime) def test_eleven(self): """ delete_by_df: Testing for successful delete_by_df """ df = self.cwms.retrieve_ts( "LWG.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) if not self.cwms.retrieve_location("TST"): self.cwms.store_location("TST") p_cwms_ts_id = "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV" p_units = "cms" values = list(df["value"]) p_qualities = list(df["quality_code"]) times = list(df["date_time"]) self.cwms.store_ts(p_cwms_ts_id, p_units, times, values, p_qualities) df2 = self.cwms.retrieve_ts( "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) assert df.equals(df2) sample = df2.sample(frac=0.3) sample["ts_id"] = "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV" self.cwms.delete_by_df(sample) df3 = self.cwms.retrieve_ts( "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) not_deleted = df2[False == df2["date_time"].isin( sample["date_time"])].reset_index(drop=True) assert not_deleted.equals(df3) self.cwms.delete_ts("TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "DELETE TS DATA") self.cwms.delete_ts("TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "DELETE TS ID") self.cwms.delete_location("TST") try: df2 = self.cwms.retrieve_ts( "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) except ValueError as e: msg = 'TS_ID_NOT_FOUND: The timeseries identifier "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV"' assert msg in e.__str__() def test_eleven(self): """ store_by_df: Testing for successful store_by_df """ df = self.cwms.retrieve_ts( "LWG.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) if not self.cwms.retrieve_location("TST"): self.cwms.store_location("TST") p_cwms_ts_id = "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV" p_units = "cms" test_df = df.copy() test_df["ts_id"] = p_cwms_ts_id test_df["units"] = p_units self.cwms.store_by_df(test_df) df2 = self.cwms.retrieve_ts( "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) assert df.equals(df2) # test for protected test_df["quality_code"].iloc[0] = 2147483649 test_df["value"].iloc[0] = 9999 self.cwms.store_by_df(test_df) test_df["quality_code"].iloc[0] = 0 test_df["value"].iloc[0] = -9999 df3 = self.cwms.retrieve_ts( "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) assert df3["value"].iloc[0] == 9999 self.cwms.delete_ts("TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "DELETE TS DATA") self.cwms.delete_ts("TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "DELETE TS ID") self.cwms.delete_location("TST") try: df2 = self.cwms.retrieve_ts( "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV", "2019/1/1", "2019/9/1", "cms", return_df=True, ) except ValueError as e: msg = 'TS_ID_NOT_FOUND: The timeseries identifier "TST.Flow-Out.Ave.~1Day.1Day.CBT-REV"' assert msg in e.__str__() def test_final(self): """ close: Testing good close from db for cleanup """ c = self.cwms.close() assert c == True