diff --git a/mesures/parsers/dummy_data.py b/mesures/parsers/dummy_data.py index b84abc5..10837da 100644 --- a/mesures/parsers/dummy_data.py +++ b/mesures/parsers/dummy_data.py @@ -42,7 +42,7 @@ def __init__(self, datas): except ValueError: # str season data['season'] = 0 if data['season'].lower() == 'w' else 1 - if 'kind_fact' in data: + if 'kind_fact' in data and not 'method' in data: try: data['method'] = int(data.pop('kind_fact')) except ValueError: diff --git a/mesures/pmest.py b/mesures/pmest.py index 6652f11..c63e3b5 100644 --- a/mesures/pmest.py +++ b/mesures/pmest.py @@ -17,6 +17,7 @@ def __init__(self, data, distributor=None, compression='bz2', version=0): """ if isinstance(data, list): data = DummyCurve(data).curve_data + self.columns = columns self.file = self.reader(data) self.generation_date = datetime.now() self.prefix = 'PMEST' @@ -93,7 +94,7 @@ def r4(self): def reader(self, filepath): if isinstance(filepath, str): df = pd.read_csv( - filepath, sep=';', names=columns + filepath, sep=';', names=self.columns ) elif isinstance(filepath, list): df = pd.DataFrame(data=filepath) @@ -120,7 +121,7 @@ def reader(self, filepath): 'r4': 'sum', } ).reset_index() - df = df[columns] + df = df[self.columns] return df def writer(self): @@ -138,10 +139,11 @@ def writer(self): self.measures_date = di dataf = self.file[(self.file['timestamp'] >= di) & (self.file['timestamp'] < df)] dataf['timestamp'] = dataf.apply(lambda row: row['timestamp'].strftime('%Y/%m/%d %H'), axis=1) + dataf['timestamp'] = dataf['timestamp'].astype(str) file_path = os.path.join('/tmp', self.filename) kwargs = {'sep': ';', 'header': False, - 'columns': columns, + 'columns': self.columns, 'index': False, check_line_terminator_param(): ';\n' } diff --git a/spec/generation_files_spec.py b/spec/generation_files_spec.py index 3871fc5..f66d2c4 100644 --- a/spec/generation_files_spec.py +++ b/spec/generation_files_spec.py @@ -27,6 +27,7 @@ from mesures.p1d import P1D from mesures.p2d import P2D from mesures.p5d import P5D +from mesures.pmest import PMEST from mesures.potelectro import POTELECTRO from mesures.reobje2 import REOBJE2 from mesures.reobjecil import REOBJECIL @@ -237,6 +238,39 @@ def get_sample_f1qh_data(): return data_f1qh + @staticmethod + def get_sample_data_pmest(): + basic_pmest = { + "pm": "DK029141", + "tipo_medida": 11, + "timestamp": "2024-11-01 01:00:00", + "season": 0, + "method": 4, + "ai": 10, + "ae": 11, + "r1": 12, + "r2": 13, + "r3": 14, + "r4": 15 + } + + data_pmest = [basic_pmest.copy()] + + ts = "2024-11-01 01:00:00" + for x in range(50): + datas = basic_pmest.copy() + ts = (datetime.strptime(ts, '%Y-%m-%d %H:%M:%S') + timedelta(hours=1)).strftime('%Y-%m-%d %H:%M:%S') + ai = randint(0, 5000) + ae = randint(0, 2) + r1 = randint(0, 30) + r2 = randint(0, 4999) + r3 = randint(0, 30) + r4 = randint(0, 4999) + datas.update({'timestamp': ts, 'ai': ai, 'ae': ae, 'r1': r1, 'r2': r2, 'r3': r3, 'r4': r4}) + data_pmest.append(datas) + + return data_pmest + @staticmethod def get_sample_p5d_data(): basic_p5d = { @@ -1606,4 +1640,31 @@ def get_sample_obcups_data(): "ES0291000000004444QR1F;2024/10;100;100;;Paga la energia, " "primer aviso.;N;AE\n" ) - assert f.file[f.columns].to_csv(sep=';', header=None, index=False) == expected \ No newline at end of file + assert f.file[f.columns].to_csv(sep=';', header=None, index=False) == expected + +with description('An PMEST'): + with it('is instance of PMEST Class'): + data = SampleData().get_sample_data_pmest() + f = PMEST(data) + assert isinstance(f, PMEST) + + with it('is a zip of raw files'): + data = SampleData().get_sample_data_pmest() + f = PMEST(data) + res = f.writer() + assert zipfile.is_zipfile(res) + + with it('has its class methods'): + data = SampleData().get_sample_data_pmest() + f = PMEST(data) + res = f.writer() + assert isinstance(f.total, (int, np.int64)) + assert f.ai == f.total + + with it('gets expected content'): + data = SampleData().get_sample_data_pmest() + f = PMEST(data) + res = f.writer() + # WARNING: timestamp is expressed as "yyyy/mm/dd HH" in file, but in dataframe is still ISO formatted + expected = 'DK029141;11;2024-11-01 01:00:00;0;4;10;11;12;13;14;15' + assert f.file[f.columns].to_csv(sep=';', header=None, index=False).split('\n')[0] == expected \ No newline at end of file