Compare commits
No commits in common. "main" and "0.1.0.4" have entirely different histories.
|
@ -20,6 +20,11 @@ class Configuration:
|
|||
"""
|
||||
|
||||
def __init__(self, db_name: str, dotenv_path: str, app_env='TEST'):
|
||||
"""
|
||||
:param db_name: database name
|
||||
:param app_env: application environment, test or production
|
||||
:param dotenv_path: the absolute path to dotenv file
|
||||
"""
|
||||
try:
|
||||
# load environmental variables
|
||||
if not Path(dotenv_path).exists():
|
||||
|
|
|
@ -148,27 +148,27 @@ class DBControl:
|
|||
|
||||
def update_city(self, city_id, city):
|
||||
"""
|
||||
Update an existing city in the database
|
||||
:param city_id: the id of the city to update
|
||||
:param city: the updated city object
|
||||
Update an existing city in the database
|
||||
:param city_id: the id of the city to update
|
||||
:param city: the updated city object
|
||||
"""
|
||||
return self._city.update(city_id, city)
|
||||
|
||||
def persist_application(self, name: str, description: str, application_uuid: str):
|
||||
"""
|
||||
Creates information for an application in the database
|
||||
:param name: name of application
|
||||
:param description: the description of the application
|
||||
:param application_uuid: the uuid of the application to be created
|
||||
:param name: name of application
|
||||
:param description: the description of the application
|
||||
:param application_uuid: the uuid of the application to be created
|
||||
"""
|
||||
return self._application.insert(name, description, application_uuid)
|
||||
|
||||
def update_application(self, name: str, description: str, application_uuid: str):
|
||||
"""
|
||||
Update the application information stored in the database
|
||||
:param name: name of application
|
||||
:param description: the description of the application
|
||||
:param application_uuid: the uuid of the application to be created
|
||||
:param name: name of application
|
||||
:param description: the description of the application
|
||||
:param application_uuid: the uuid of the application to be created
|
||||
"""
|
||||
return self._application.update(application_uuid, name, description)
|
||||
|
||||
|
@ -236,13 +236,6 @@ class DBControl:
|
|||
def delete_application(self, application_uuid):
|
||||
"""
|
||||
Deletes a single application from the database
|
||||
:param application_uuid: the id of the application to delete
|
||||
:param application_uuid: the id of the application to get
|
||||
"""
|
||||
self._application.delete(application_uuid)
|
||||
|
||||
def get_city(self, city_id):
|
||||
"""
|
||||
Get a single city by id
|
||||
:param city_id: the id of the city to get
|
||||
"""
|
||||
return self._city.get_by_id(city_id)
|
||||
|
|
|
@ -23,6 +23,14 @@ class DBSetup:
|
|||
"""
|
||||
|
||||
def __init__(self, db_name, app_env, dotenv_path, admin_password, application_uuid):
|
||||
"""
|
||||
Creates database tables a default admin user and a default admin app with the given password and uuid
|
||||
:param db_name: database name
|
||||
:param app_env: application environment type [TEST|PROD]
|
||||
:param dotenv_path: .env file path
|
||||
:param admin_password: administrator password for the application uuid
|
||||
:application_uuid: application uuid
|
||||
"""
|
||||
repository = Repository(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
|
||||
|
||||
# Create the tables using the models
|
||||
|
|
|
@ -16,7 +16,7 @@ from cerc_persistence.configuration import Models
|
|||
|
||||
class Application(Models):
|
||||
"""
|
||||
Application(Models) class
|
||||
A model representation of an application
|
||||
"""
|
||||
__tablename__ = 'application'
|
||||
id = Column(Integer, Sequence('application_id_seq'), primary_key=True)
|
||||
|
|
|
@ -14,8 +14,7 @@ from cerc_persistence.configuration import Models
|
|||
|
||||
|
||||
class City(Models):
|
||||
"""
|
||||
City(Models) class
|
||||
"""A model representation of a city
|
||||
"""
|
||||
__tablename__ = 'city'
|
||||
id = Column(Integer, Sequence('city_id_seq'), primary_key=True)
|
||||
|
|
|
@ -17,7 +17,7 @@ from cerc_persistence.configuration import Models
|
|||
|
||||
class CityObject(Models):
|
||||
"""
|
||||
CityObject(Models) class
|
||||
A model representation of an application
|
||||
"""
|
||||
__tablename__ = 'city_object'
|
||||
id = Column(Integer, Sequence('city_object_id_seq'), primary_key=True)
|
||||
|
|
|
@ -15,7 +15,7 @@ from cerc_persistence.configuration import Models
|
|||
|
||||
class SimulationResults(Models):
|
||||
"""
|
||||
SimulationResults(Models) class
|
||||
A model representation of an application
|
||||
"""
|
||||
__tablename__ = 'simulation_results'
|
||||
id = Column(Integer, Sequence('simulation_results_id_seq'), primary_key=True)
|
||||
|
|
|
@ -24,7 +24,7 @@ class UserRoles(enum.Enum):
|
|||
|
||||
class User(Models):
|
||||
"""
|
||||
User(Models) class
|
||||
A model representation of a city
|
||||
"""
|
||||
__tablename__ = 'user'
|
||||
id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
|
||||
|
|
|
@ -18,7 +18,7 @@ from cerc_persistence.models import Application as Model
|
|||
|
||||
class Application(Repository):
|
||||
"""
|
||||
Application(Repository) class
|
||||
Application repository
|
||||
"""
|
||||
_instance = None
|
||||
|
||||
|
@ -26,6 +26,9 @@ class Application(Repository):
|
|||
super().__init__(db_name, dotenv_path, app_env)
|
||||
|
||||
def __new__(cls, db_name, dotenv_path, app_env):
|
||||
"""
|
||||
Implemented for a singleton pattern
|
||||
"""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(Application, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
|
|
@ -20,7 +20,7 @@ from cerc_persistence.models import CityObject
|
|||
|
||||
class City(Repository):
|
||||
"""
|
||||
City(Repository) class
|
||||
City repository
|
||||
"""
|
||||
_instance = None
|
||||
|
||||
|
@ -28,6 +28,9 @@ class City(Repository):
|
|||
super().__init__(db_name, dotenv_path, app_env)
|
||||
|
||||
def __new__(cls, db_name, dotenv_path, app_env):
|
||||
"""
|
||||
Implemented for a singleton pattern
|
||||
"""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(City, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
@ -133,7 +136,3 @@ class City(Repository):
|
|||
except SQLAlchemyError as err:
|
||||
logging.error('Error while fetching city by name %s', err)
|
||||
raise SQLAlchemyError from err
|
||||
|
||||
def get_by_id(self, city_id) -> Model:
|
||||
with Session(self.engine) as session:
|
||||
return session.execute(select(Model).where(Model.id == city_id)).first()[0]
|
|
@ -19,7 +19,7 @@ from cerc_persistence.repository import Repository
|
|||
|
||||
class CityObject(Repository):
|
||||
"""
|
||||
CityObject(Repository) class
|
||||
City object repository
|
||||
"""
|
||||
_instance = None
|
||||
|
||||
|
@ -27,6 +27,9 @@ class CityObject(Repository):
|
|||
super().__init__(db_name, dotenv_path, app_env)
|
||||
|
||||
def __new__(cls, db_name, dotenv_path, app_env):
|
||||
"""
|
||||
Implemented for a singleton pattern
|
||||
"""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(CityObject, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
|
|
@ -20,7 +20,7 @@ from cerc_persistence.models import SimulationResults as Model
|
|||
|
||||
class SimulationResults(Repository):
|
||||
"""
|
||||
SimulationResults(Repository) class
|
||||
Simulation results repository
|
||||
"""
|
||||
_instance = None
|
||||
|
||||
|
@ -28,6 +28,9 @@ class SimulationResults(Repository):
|
|||
super().__init__(db_name, dotenv_path, app_env)
|
||||
|
||||
def __new__(cls, db_name, dotenv_path, app_env):
|
||||
"""
|
||||
Implemented for a singleton pattern
|
||||
"""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(SimulationResults, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
|
|
@ -18,7 +18,7 @@ from cerc_persistence.models import User as Model, Application as ApplicationMod
|
|||
|
||||
class User(Repository):
|
||||
"""
|
||||
User(Repository) class
|
||||
User class
|
||||
"""
|
||||
_instance = None
|
||||
|
||||
|
@ -26,6 +26,9 @@ class User(Repository):
|
|||
super().__init__(db_name, dotenv_path, app_env)
|
||||
|
||||
def __new__(cls, db_name, dotenv_path, app_env):
|
||||
"""
|
||||
Implemented for a singleton pattern
|
||||
"""
|
||||
if cls._instance is None:
|
||||
cls._instance = super(User, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
"""
|
||||
CERC Persistence version number
|
||||
"""
|
||||
__version__ = '1.0.0.2'
|
||||
__version__ = '0.1.0.4'
|
||||
|
|
9
setup.py
9
setup.py
|
@ -20,13 +20,8 @@ with open(version) as f:
|
|||
setup(
|
||||
name='cerc-persistence',
|
||||
version=main_ns['__version__'],
|
||||
description="CERC Persistence consist of a set of classes to store and retrieve Cerc Hub cities and results",
|
||||
long_description="CERC Persistence consist of a set of classes to store and retrieve Cerc Hub cities and results.\n\n"
|
||||
"Developed at Concordia university in Canada as part of the research group from the Next Generation "
|
||||
"Cities Institute, our aim among others is to provide a comprehensive set of tools to help "
|
||||
"researchers and urban developers to make decisions to improve the livability and efficiency of our "
|
||||
"cities, cerc persistence will store the simulation results and city information to make those "
|
||||
"available for other researchers.",
|
||||
description="",
|
||||
long_description="",
|
||||
classifiers=[
|
||||
"License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)",
|
||||
"Programming Language :: Python",
|
||||
|
|
|
@ -5,6 +5,7 @@ Copyright © 2022 Concordia CERC group
|
|||
Project Coder Peter Yefi peteryefi@gmail.com
|
||||
"""
|
||||
import glob
|
||||
import json
|
||||
import os
|
||||
import unittest
|
||||
from unittest import TestCase
|
||||
|
@ -15,7 +16,6 @@ from unittests.control import Control
|
|||
|
||||
control = Control()
|
||||
|
||||
|
||||
class TestDBFactory(TestCase):
|
||||
"""
|
||||
TestDBFactory
|
||||
|
@ -59,9 +59,6 @@ TestDBFactory
|
|||
control.application_id,
|
||||
control.user_id)
|
||||
city_objects_id = []
|
||||
request_values = {
|
||||
'scenarios': [{'current status': ['1']}]
|
||||
}
|
||||
for building in control.city.buildings:
|
||||
_building = control.database.building_info(building.name, city_id)
|
||||
if cte.MONTH not in building.cooling_demand:
|
||||
|
@ -99,36 +96,36 @@ TestDBFactory
|
|||
for x in building.onsite_electrical_production[cte.MONTH]]
|
||||
yearly_on_site_electrical_production = [x * cte.WATTS_HOUR_TO_JULES
|
||||
for x in building.onsite_electrical_production[cte.YEAR]]
|
||||
results = {cte.INSEL_MEB: {
|
||||
'monthly_cooling_peak_load': monthly_cooling_peak_load,
|
||||
'yearly_cooling_peak_load': yearly_cooling_peak_load,
|
||||
'monthly_heating_peak_load': monthly_heating_peak_load,
|
||||
'yearly_heating_peak_load': yearly_heating_peak_load,
|
||||
'monthly_lighting_peak_load': monthly_lighting_peak_load,
|
||||
'yearly_lighting_peak_load': yearly_lighting_peak_load,
|
||||
'monthly_appliances_peak_load': monthly_appliances_peak_load,
|
||||
'yearly_appliances_peak_load': yearly_appliances_peak_load,
|
||||
'monthly_cooling_demand': monthly_cooling_demand,
|
||||
'yearly_cooling_demand': yearly_cooling_demand,
|
||||
'monthly_heating_demand': monthly_heating_demand,
|
||||
'yearly_heating_demand': yearly_heating_demand,
|
||||
'monthly_lighting_electrical_demand': monthly_lighting_electrical_demand,
|
||||
'yearly_lighting_electrical_demand': yearly_lighting_electrical_demand,
|
||||
'monthly_appliances_electrical_demand': monthly_appliances_electrical_demand,
|
||||
'yearly_appliances_electrical_demand': yearly_appliances_electrical_demand,
|
||||
'monthly_domestic_hot_water_heat_demand': monthly_domestic_hot_water_heat_demand,
|
||||
'yearly_domestic_hot_water_heat_demand': yearly_domestic_hot_water_heat_demand,
|
||||
'monthly_heating_consumption': monthly_heating_consumption,
|
||||
'yearly_heating_consumption': yearly_heating_consumption,
|
||||
'monthly_cooling_consumption': monthly_cooling_consumption,
|
||||
'yearly_cooling_consumption': yearly_cooling_consumption,
|
||||
'monthly_domestic_hot_water_consumption': monthly_domestic_hot_water_consumption,
|
||||
'yearly_domestic_hot_water_consumption': yearly_domestic_hot_water_consumption,
|
||||
'monthly_distribution_systems_electrical_consumption': monthly_distribution_systems_electrical_consumption,
|
||||
'yearly_distribution_systems_electrical_consumption': yearly_distribution_systems_electrical_consumption,
|
||||
'monthly_on_site_electrical_production': monthly_on_site_electrical_production,
|
||||
'yearly_on_site_electrical_production': yearly_on_site_electrical_production
|
||||
}}
|
||||
results = json.dumps({cte.INSEL_MEB: [
|
||||
{'monthly_cooling_peak_load': monthly_cooling_peak_load},
|
||||
{'yearly_cooling_peak_load': yearly_cooling_peak_load},
|
||||
{'monthly_heating_peak_load': monthly_heating_peak_load},
|
||||
{'yearly_heating_peak_load': yearly_heating_peak_load},
|
||||
{'monthly_lighting_peak_load': monthly_lighting_peak_load},
|
||||
{'yearly_lighting_peak_load': yearly_lighting_peak_load},
|
||||
{'monthly_appliances_peak_load': monthly_appliances_peak_load},
|
||||
{'yearly_appliances_peak_load': yearly_appliances_peak_load},
|
||||
{'monthly_cooling_demand': monthly_cooling_demand},
|
||||
{'yearly_cooling_demand': yearly_cooling_demand},
|
||||
{'monthly_heating_demand': monthly_heating_demand},
|
||||
{'yearly_heating_demand': yearly_heating_demand},
|
||||
{'monthly_lighting_electrical_demand': monthly_lighting_electrical_demand},
|
||||
{'yearly_lighting_electrical_demand': yearly_lighting_electrical_demand},
|
||||
{'monthly_appliances_electrical_demand': monthly_appliances_electrical_demand},
|
||||
{'yearly_appliances_electrical_demand': yearly_appliances_electrical_demand},
|
||||
{'monthly_domestic_hot_water_heat_demand': monthly_domestic_hot_water_heat_demand},
|
||||
{'yearly_domestic_hot_water_heat_demand': yearly_domestic_hot_water_heat_demand},
|
||||
{'monthly_heating_consumption': monthly_heating_consumption},
|
||||
{'yearly_heating_consumption': yearly_heating_consumption},
|
||||
{'monthly_cooling_consumption': monthly_cooling_consumption},
|
||||
{'yearly_cooling_consumption': yearly_cooling_consumption},
|
||||
{'monthly_domestic_hot_water_consumption': monthly_domestic_hot_water_consumption},
|
||||
{'yearly_domestic_hot_water_consumption': yearly_domestic_hot_water_consumption},
|
||||
{'monthly_distribution_systems_electrical_consumption': monthly_distribution_systems_electrical_consumption},
|
||||
{'yearly_distribution_systems_electrical_consumption': yearly_distribution_systems_electrical_consumption},
|
||||
{'monthly_on_site_electrical_production': monthly_on_site_electrical_production},
|
||||
{'yearly_on_site_electrical_production': yearly_on_site_electrical_production}
|
||||
]})
|
||||
|
||||
db_building_id = _building.id
|
||||
city_objects_id.append(db_building_id)
|
||||
|
@ -137,13 +134,6 @@ TestDBFactory
|
|||
results, city_object_id=db_building_id)
|
||||
self.assertEqual(17, len(city_objects_id), 'wrong number of results')
|
||||
self.assertIsNotNone(city_objects_id[0], 'city_object_id is None')
|
||||
results = control.database.results(control.user_id, control.application_id, request_values)
|
||||
self.assertEqual(
|
||||
28,
|
||||
len(results['current status'][0]['insel meb'].keys()),
|
||||
'wrong number of results after retrieve'
|
||||
)
|
||||
|
||||
for _id in city_objects_id:
|
||||
control.database.delete_results_by_name('insel meb', city_object_id=_id)
|
||||
control.database.delete_city(city_id)
|
||||
|
|
|
@ -5,9 +5,18 @@ Copyright © 2022 Concordia CERC group
|
|||
Project Coder Ruben Sanchez ruben.sanchez@mail.concordia.ca
|
||||
"""
|
||||
import datetime
|
||||
import distutils.spawn
|
||||
import os
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import TestCase
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy_utils import database_exists, create_database, drop_database
|
||||
|
||||
from cerc_persistence.db_control import DBControl
|
||||
from cerc_persistence.repository import Repository
|
||||
from cerc_persistence.models import City, Application, CityObject, SimulationResults, User
|
||||
from unittests.control import Control
|
||||
|
||||
control = Control()
|
||||
|
|
Loading…
Reference in New Issue
Block a user