Skip to content

Commit

Permalink
Fix som_plantmeter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oriolpiera committed Mar 4, 2024
1 parent b691e54 commit 5e15498
Show file tree
Hide file tree
Showing 5 changed files with 350 additions and 342 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/som_plantmeter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This workflow will pass tests of module passed by input

name: som_plantmeter
on:
workflow_dispatch:
pull_request: # PR
jobs:
erp-tests-module:
uses: Som-Energia/openerp_som_addons/.github/workflows/reusable_workflow.yml@MOD_reusable_worflow
with:
module: som_plantmeter
secrets:
ACCESS_TOKEN_GA: ${{ secrets.ACCESS_TOKEN_GA }}
ESIOS_TOKEN: ${{ secrets.ESIOS_TOKEN }}
SRID: ${{ secrets.SRID }}

13 changes: 2 additions & 11 deletions plantmeter/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,8 @@ def sorteddict(d):
return self.assertMultiLineEqual(dict1.dump(), dict2.dump())

def _inProduction():
import erppeek_wst
import dbconfig
c = erppeek_wst.ClientWST(**dbconfig.erppeek)
c.begin()
destructive_testing_allowed = c._execute(
'res.config', 'get', 'destructive_testing_allowed', False)
c.rollback()
c.close()

if destructive_testing_allowed: return False
return True
import socket
return socket.gethostname() in ['erp01', 'erpwork01', 'erpwork02', 'erpwork03', 'erpwork04', 'erpwork05']

def destructiveTest(decorated):
return unittest.skipIf(_inProduction(),
Expand Down
79 changes: 41 additions & 38 deletions som_plantmeter/som_plantmeter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from yamlns import namespace as ns

from datetime import datetime
from plantmeter.resource import ProductionAggregator, ProductionPlant, ProductionMeter
from plantmeter.resource import ProductionAggregator, ProductionPlant, ProductionMeter
from plantmeter.mongotimecurve import MongoTimeCurve, toLocal, asUtc
from somutils.isodates import isodate, naiveisodatetime, localisodate
from somutils.isodates import isodate, localisodate


class GenerationkwhProductionAggregator(osv.osv):
"""
Expand All @@ -20,7 +21,6 @@ class GenerationkwhProductionAggregator(osv.osv):

_name = 'generationkwh.production.aggregator'


_columns = {
'name': fields.char('Name', size=50),
'description': fields.char('Description', size=150),
Expand All @@ -34,7 +34,7 @@ class GenerationkwhProductionAggregator(osv.osv):

def get_kwh(self, cursor, uid, mix_id, start, end, context=None):
'''Get production aggregation'''

if not context:
context = {}
_aggr = self._createAggregator(cursor, uid, mix_id)
Expand Down Expand Up @@ -77,10 +77,10 @@ def extract_attrs(obj, attrs):

aggr = self.browse(cursor, uid, mix_id)
curveProvider = MongoTimeCurve(mdbpool.get_db(),
'tm_profile',
creationField = 'create_date',
timestampField = 'utc_gkwh_timestamp',
)
'tm_profile',
creationField='create_date',
timestampField='utc_gkwh_timestamp',
)

# TODO: Clean initialization method
args = ['id', 'name', 'description', 'enabled']
Expand All @@ -93,15 +93,16 @@ def extract_attrs(obj, attrs):
ProductionMeter(
curveProvider=curveProvider,
**extract_attrs(meter, args + ['first_active_date'])
)
)
for meter in plant.meters
if meter.enabled
],
))
],
))
for plant in aggr.plants
if plant.enabled
],
))
],
))


GenerationkwhProductionAggregator()

Expand All @@ -118,7 +119,7 @@ class GenerationkwhProductionPlant(osv.osv):
'enabled': fields.boolean('Enabled'),
'nshares': fields.integer('Number of shares'),
'aggr_id': fields.many2one('generationkwh.production.aggregator', 'Production aggregator',
required=True),
required=True),
'meters': fields.one2many('generationkwh.production.meter', 'plant_id', 'Meters'),
'first_active_date': fields.date('First operative date'),
'last_active_date': fields.date('Last operative date'),
Expand All @@ -143,19 +144,22 @@ class GenerationkwhProductionMeter(osv.osv):
'enabled': fields.boolean('Enabled'),
'plant_id': fields.many2one('generationkwh.production.plant'),
'first_active_date': fields.date('First operative date'),
}
}
_defaults = {
'enabled': lambda *a: False,
}


GenerationkwhProductionMeter()


class PlantShareProvider(ErpWrapper):
"""
Provides a list for each plant of their active periods and their share value.
To be used as provider for a LayeredShareCurve, that generates
the curves to represent the share value of the active built plants.
"""

def __init__(self, erp, cursor, uid, mixname, context=None):
self.mixname = mixname
super(PlantShareProvider, self).__init__(erp, cursor, uid, context)
Expand All @@ -177,24 +181,24 @@ def items(self):
ns(
mix=self.mixname,
shares=plant['nshares'],
firstEffectiveDate = isodate(plant['first_active_date']),
lastEffectiveDate = isodate(plant['last_active_date']),
firstEffectiveDate=isodate(plant['first_active_date']),
lastEffectiveDate=isodate(plant['last_active_date']),
)
for plant in plants
]



class GenerationkwhProductionMeasurement(osv_mongodb.osv_mongodb):

_name = 'generationkwh.production.measurement'
_order = 'timestamp desc'

_columns = {
'name': fields.integer('Plant identifier'), # NOTE: workaround due mongodb backend
# NOTE: workaround due mongodb backend
'name': fields.integer('Plant identifier'),
'create_at': fields.datetime('Create datetime'),
'datetime': fields.datetime('Exported datetime'),
'daylight': fields.char('Exported datetime daylight',size=1),
'daylight': fields.char('Exported datetime daylight', size=1),
'ae': fields.float('Exported energy (kWh)')
}

Expand All @@ -221,8 +225,8 @@ def search(self, cursor, uid, args, offset=0, limit=0, order=None,
order=order, context=context,
count=count)

GenerationkwhProductionMeasurement()

GenerationkwhProductionMeasurement()


class GenerationkwhProductionAggregatorTesthelper(osv.osv):
Expand All @@ -231,11 +235,10 @@ class GenerationkwhProductionAggregatorTesthelper(osv.osv):
_name = 'generationkwh.production.aggregator.testhelper'
_auto = False


def get_kwh(self, cursor, uid, mix_id, start, end, context=None):
mix = self.pool.get('generationkwh.production.aggregator')
return mix.get_kwh(cursor, uid, mix_id,
isodate(start), isodate(end), context)
isodate(start), isodate(end), context)

def firstActiveDate(self, cursor, uid, mix_id, context=None):
mix = self.pool.get('generationkwh.production.aggregator')
Expand All @@ -258,25 +261,26 @@ def clear_mongo_collections(self, cursor, uid, collections, context=None):

def fillMeasurements(self, cursor, uid, first_date, meter_name, values):
curveProvider = MongoTimeCurve(mdbpool.get_db(),
'tm_profile',
creationField = 'create_date',
timestampField = 'utc_gkwh_timestamp',
)
'tm_profile',
creationField='create_date',
timestampField='utc_gkwh_timestamp',
)
curveProvider.update(
start = localisodate(first_date),
filter = meter_name,
field = 'ae',
data = values,
)
start=localisodate(first_date),
filter=meter_name,
field='ae',
data=values,
)

def fillMeasurementPoint(self, cursor, uid, pointTime, name, value, context=None):
curveProvider = MongoTimeCurve(mdbpool.get_db(),
'tm_profile',
creationField = 'create_date',
timestampField = 'utc_gkwh_timestamp',
)
'tm_profile',
creationField='create_date',
timestampField='utc_gkwh_timestamp',
)
curveProvider.fillPoint(
datetime=toLocal(asUtc(naiveisodatetime(pointTime))),
datetime=toLocal(asUtc(datetime.strptime(
pointTime, "%Y-%m-%d %H:%M:%S"))),
name=name,
ae=value)

Expand All @@ -292,7 +296,6 @@ def plantShareItems(self, cursor, uid, mixname):
]



GenerationkwhProductionAggregatorTesthelper()


Expand Down
1 change: 1 addition & 0 deletions som_plantmeter/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from plantmeter_test import *
Loading

0 comments on commit 5e15498

Please sign in to comment.