def test_DataImport_linear_search_value(self): data_times = [] data_values = [] key_index = random.randint(0, 9) key_time = None with open("test_file.csv", mode="w") as test_data: data_writer = csv.writer(test_data, delimiter=",") data_writer.writerow(["time", "value"]) idate = datetime.datetime.now() for item in range(9): idatestr = idate.strftime("%m/%d/%Y %H:%M") randval = str(random.randint(0, 10000)) data_writer.writerow([idatestr, randval]) data_times.append(idatestr) data_values.append(randval) idate = idate + datetime.timedelta(minutes=2) test_data.close() testdata = ImportData("test_file.csv") data_import.roundTimeArray(testdata, 5) key_time = testdata._roundtime[key_index] os.remove("test_file.csv") self.assertIn(int(data_values[key_index]), testdata.linear_search_value(key_time))
def test_DataImport_readfile(self): data_times = [] data_values = [] with open("test_file.csv", mode="w") as test_data: data_writer = csv.writer(test_data, delimiter=",") data_writer.writerow(["time", "value"]) idate = datetime.datetime.now() for item in range(9): idatestr = idate.strftime("%m/%d/%Y %H:%M") randval = str(random.randint(0, 10000)) data_writer.writerow([idatestr, randval]) data_times.append(idatestr) data_values.append(randval) idate = idate + datetime.timedelta(minutes=2) test_data.close() testdata = ImportData("test_file.csv") os.remove("test_file.csv") str_times = [val.strftime("%m/%d/%Y %H:%M") for val in testdata._time] self.assertEqual(str_times, data_times) self.assertEqual(testdata._value, data_values)
def test_linear_search_missing(self): self.make_linear_search_file() self.test = ImportData("lin_test.csv") self.assertEqual(-1, self.test.linear_search_value("", self.test._time, self.test._value))
def test_linear_search(self): self.make_linear_search_file() self.test = ImportData("lin_test.csv") self.assertEqual(self.values[1:], self.test.linear_search_value(self.test._time[0], self.test._time, self.test._value))
def test_cgm_file_import(self): self.make_cgm_file() self.cgm_import = ImportData("cgm_test.csv") self.assertEqual([40, 300] + self.values[3:], self.cgm_import._value)
def test_valid_file_import(self): self.make_valid_file() self.test_import = ImportData("test.csv") self.assertEqual(self.values[1:], self.test_import._value)
class TestTimeSeries(unittest.TestCase): def make_valid_file(self): self.times = [datetime.datetime.now() for x in range(10)] self.values = [float(random.randint(0, 10)) for x in range(10)] self.times.insert(0, "time") self.values.insert(0, "value") with open('test.csv', 'w+') as file: writer = csv.writer(file) writer.writerows(zip(self.times, self.values)) def make_cgm_file(self): self.times = [datetime.datetime.now() for x in range(10)] self.values = [float(random.randint(0, 10)) for x in range(10)] self.times.insert(0, "time") self.values.insert(0, "value") self.values[1] = "low" self.values[2] = "high" with open('cgm_test.csv', 'w+') as file: writer = csv.writer(file) writer.writerows(zip(self.times, self.values)) def make_linear_search_file(self): self.times = [datetime.datetime(2019, 10, 14, 20, 43, 29, 525680) for x in range(10)] self.values = [float(random.randint(0, 10)) for x in range(10)] self.times.insert(0, "time") self.values.insert(0, "value") with open("lin_test.csv", 'w+') as file: writer = csv.writer(file) writer.writerows(zip(self.times, self.values)) def test_valid_file_import(self): self.make_valid_file() self.test_import = ImportData("test.csv") self.assertEqual(self.values[1:], self.test_import._value) def test_cgm_file_import(self): self.make_cgm_file() self.cgm_import = ImportData("cgm_test.csv") self.assertEqual([40, 300] + self.values[3:], self.cgm_import._value) def test_linear_search(self): self.make_linear_search_file() self.test = ImportData("lin_test.csv") self.assertEqual(self.values[1:], self.test.linear_search_value(self.test._time[0], self.test._time, self.test._value)) def test_linear_search_missing(self): self.make_linear_search_file() self.test = ImportData("lin_test.csv") self.assertEqual(-1, self.test.linear_search_value("", self.test._time, self.test._value)) def test_make_csv(self): self.data_list = [zip([x for x in range(10)], [x for x in range(10)]) for i in range(3)] self.annotation_list = ["test_column1", "test_column2", "test_column3"] self.base_name = "test_output.csv" self.key_file = "test_column1" print_array(self.data_list, self.annotation_list, self.base_name, self.key_file) self.assertEqual(True, os.path.exists(self.base_name))