Skip to content

Commit

Permalink
Fix PMEST generation and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmunoznovoa committed Oct 16, 2024
1 parent 038e542 commit d6d5d67
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion mesures/parsers/dummy_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions mesures/pmest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand All @@ -120,7 +121,7 @@ def reader(self, filepath):
'r4': 'sum',
}
).reset_index()
df = df[columns]
df = df[self.columns]
return df

def writer(self):
Expand All @@ -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'
}
Expand Down
63 changes: 62 additions & 1 deletion spec/generation_files_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
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

0 comments on commit d6d5d67

Please sign in to comment.