예제 #1
0
 def test_db_output_get_empty(self):
     output_db = OutputDB()
     # get all items
     result = output_db.get_all()
     assert list(result) == []
     # try to get an inexistant item
     with pytest.raises(ItemNotFoundError):
         output_db.get_by_id('not_existing')
예제 #2
0
    def test_db_output_create(self, init_models, init_sites, init_buildings):
        output_db = OutputDB()
        # check that database is empty
        result = output_db.get_all()
        assert list(result) == []

        #get service and model id
        models = list(ModelDB().get_all())
        model_id, service_id = models[0].id, models[0].service_id

        # create an item
        output = OutputEvent(service_id, model_id, id=str(gen_uuid()))
        new_output_id_evt = output_db.create(output)
        assert new_output_id_evt is not None

        # check that database is not empty now
        result = output_db.get_all()
        outputs = list(result)
        assert len(outputs) == 1
        assert outputs[0].module_id == service_id
        assert outputs[0].model_id == model_id

        #test with time series
        output = OutputTimeSeries(service_id,
                                  model_id,
                                  init_buildings[0],
                                  ValuesDescription('Temperature',
                                                    'DegreeCelsius', 20),
                                  id=str(gen_uuid()))
        new_output_id_ts = output_db.create(output)
        assert new_output_id_ts is not None

        result = output_db.get_all()
        outputs = list(result)
        assert len(outputs) == 2
        output_event = outputs[0] if isinstance(
            outputs[0], OutputTimeSeries) else outputs[1]
        assert output_event.module_id == service_id
        assert output_event.model_id == model_id
        assert output_event.localization == output.localization
        assert output_event.values_desc.unit == output.values_desc.unit
        assert output_event.values_desc.kind == output.values_desc.kind
        assert output_event.values_desc.sampling == output.values_desc.sampling

        #test relation between service and output
        model = ModelDB().get_by_id(model_id)
        assert set(model.event_output_ids) == {new_output_id_evt}
        assert set(model.timeseries_output_ids) == {new_output_id_ts}
예제 #3
0
    def test_db_output_update(self, init_models, init_sites, init_buildings):
        models = list(ModelDB().get_all())
        model_id, service_id = models[0].id, models[0].service_id

        output_db = OutputDB()
        output = OutputTimeSeries(service_id,
                                  model_id,
                                  init_buildings[0],
                                  ValuesDescription('Temperature',
                                                    'DegreeCelsius', 20),
                                  id=str(gen_uuid()))
        new_output_id_ts = output_db.create(output)

        # get all items
        result = output_db.get_all()
        outputs = list(result)
        assert len(outputs) == 1
        assert [item.id for item in outputs] == [new_output_id_ts]

        # get an item by its ID!
        # for a output, the ID to be used is the URL
        output = output_db.get_by_id(outputs[0].id)

        # update item data
        new_sampling = 400
        new_model_id = models[1].id
        new_kind = 'Energy'
        new_unit = 'Joule'
        output.sampling = new_sampling
        output.model_id = new_model_id
        output.values_desc.kind = new_kind
        output.values_desc.unit = new_unit
        output_db.update(output.id, output)

        # check that item has really been updated in database
        updated_output = output_db.get_by_id(output.id)
        assert updated_output.id == output.id
        assert updated_output.model_id == new_model_id
        assert updated_output.module_id == output.module_id
        assert updated_output.localization == output.localization
        assert updated_output.values_desc.unit == new_unit
        assert updated_output.values_desc.kind == new_kind
        assert updated_output.values_desc.sampling == output.values_desc.sampling

        # delete an item by its ID
        output_db.remove(output.id)

        # get an item by its ID
        with pytest.raises(ItemNotFoundError):
            # it has been removed...
            output_db.get_by_id(output.id)
예제 #4
0
    def test_db_output_filter(self, init_models, init_sites, init_buildings):
        output_db = OutputDB()

        #get service and model id
        models = list(ModelDB().get_all())
        model_id, service_id = models[0].id, models[0].service_id

        # check that database is empty
        result = output_db.get_all(module_id=service_id)
        assert list(result) == []
        result = output_db.get_all(kind='Temperature')
        assert list(result) == []

        # create an item
        output_db.create(OutputEvent(service_id, model_id))
        output_db.create(
            OutputTimeSeries(
                service_id, model_id, init_buildings[0],
                ValuesDescription('Temperature', 'DegreeCelsius', 20)))

        outputs = list(output_db.get_all())
        assert len(outputs) == 2
        result = output_db.get_all(module_id=service_id)
        assert len(list(result)) == 2

        result = output_db.get_all(localization=init_buildings[0])
        assert len(list(result)) == 1

        result = output_db.get_all(value_type='Temperature')
        assert len(list(result)) == 1