Skip to content

Commit

Permalink
Merge pull request #8 from Som-Energia/FIX_som_plantmeter_tests
Browse files Browse the repository at this point in the history
Fix som_plantmeter tests
  • Loading branch information
oriolpiera committed Mar 4, 2024
2 parents b691e54 + 3c26080 commit ffdafc5
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 347 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/som_plantmeter.yml
@@ -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@main
with:
module: som_plantmeter
secrets:
ACCESS_TOKEN_GA: ${{ secrets.ACCESS_TOKEN_GA }}
ESIOS_TOKEN: ${{ secrets.ESIOS_TOKEN }}
SRID: ${{ secrets.SRID }}

25 changes: 10 additions & 15 deletions plantmeter/testutils.py
Expand Up @@ -6,6 +6,7 @@
# Readable verbose testcase listing
unittest.TestCase.__str__ = unittest.TestCase.id


def assertNsEqual(self, dict1, dict2):
"""
Asserts that both dict have equivalent structure.
Expand All @@ -22,8 +23,8 @@ def sorteddict(d):
if type(d) in (dict, ns):
return ns(sorted(
(k, sorteddict(v))
for k,v in d.items()
))
for k, v in d.items()
))
if type(d) in (list, tuple):
return [sorteddict(x) for x in d]
return d
Expand All @@ -32,23 +33,17 @@ 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
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
return 'somenergia' in socket.gethostbyaddr(s.getsockname()[0])[0].split('.')


def destructiveTest(decorated):
return unittest.skipIf(_inProduction(),
"Destructive test being run in a production setup!!")(decorated)

"Destructive test being run in a production setup!!")(decorated)


# vim: ts=4 sw=4 et
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -15,7 +15,7 @@

setup(
name = "plantmeter",
version = "1.7.10",
version = "1.7.11",
description =
"OpenERP module and library to manage multisite energy generation",
author = "Som Energia SCCL",
Expand Down
79 changes: 41 additions & 38 deletions som_plantmeter/som_plantmeter.py
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
@@ -0,0 +1 @@
from plantmeter_test import *

0 comments on commit ffdafc5

Please sign in to comment.