async def test_data_points_not_added_if_only_partial_success(
     self,
     mock_db_session,
     sensors: t.List[Sensor[t.Any]],
     mut,
     http_server: str,
     bad_api_key_http_server: str,
 ) -> None:
     with pytest.raises(
         ValueError,
         match=f"Error loading data from {bad_api_key_http_server}: Supply API key in X-API-Key header",
     ):
         await mut(
             mock_db_session,
             [
                 Deployment(
                     id=None,
                     colour=None,
                     name=None,
                     uri=http_server,
                     api_key="testing",
                 ),
                 Deployment(
                     id=None,
                     colour=None,
                     name=None,
                     uri=bad_api_key_http_server,
                     api_key="testing",
                 ),
             ],
         )
     assert mock_db_session.execute.call_count == 0
    async def test_data_points_added_if_only_partial_success(
        self,
        mock_db_session,
        sensors: t.List[Sensor[t.Any]],
        mut,
        http_server: str,
        bad_api_key_http_server: str,
        caplog,
    ) -> None:
        await mut(
            mock_db_session,
            [
                Deployment(
                    id=None,
                    colour=None,
                    name=None,
                    uri=http_server,
                    api_key="testing",
                ),
                Deployment(
                    id=None,
                    colour=None,
                    name=None,
                    uri=bad_api_key_http_server,
                    api_key="testing",
                ),
            ],
        )
        # We expect Python Version and AC status for one endpoint
        assert mock_db_session.execute.call_count == 2
        insertion_calls = mock_db_session.execute.call_args_list
        params = [call[0][0]._values for call in insertion_calls]
        assert {insertion["sensor_name"].value
                for insertion in params} == {
                    "PythonVersion",
                    "ACStatus",
                }
        assert {insertion["deployment_id"].value
                for insertion in params} == {
                    uuid.UUID("a46b1d1207fd4cdcad39bbdf706dfe29"),
                }

        # We should also have a log message showing details of the failing server and the failure
        assert len(caplog.records) == 1
        assert caplog.records[0].message == "Data retrieval failed"
        assert bad_api_key_http_server in caplog.records[0].exc_text
        assert "Supply API key in X-API-Key header" in caplog.records[
            0].exc_text
 async def test_get_get_data_from_sensors_with_multiple_servers(
     self, mock_db_session, sensors: t.List[Sensor[t.Any]], mut, http_server: str
 ) -> None:
     results = await mut(
         mock_db_session,
         [
             Deployment(
                 id=None, colour=None, name=None, uri=http_server, api_key="testing"
             ),
             Deployment(
                 id=None, colour=None, name=None, uri=http_server, api_key="testing"
             ),
         ],
     )
     assert mock_db_session.execute.call_count == len(sensors) * 2
     assert len(results) == len(sensors) * 2
Example #4
0
async def get_deployment_by_id(deployment_id: UUID) -> Deployment:
    db_session = db_session_var.get()
    loop = asyncio.get_running_loop()
    query = db_session.query(deployment_table).filter(
        deployment_table.c.id == deployment_id)
    return [
        Deployment.from_sql_result(row)
        for row in await loop.run_in_executor(None, query.all)
    ][0]
 async def test_datapoints_are_added_to_the_session(self, db_session, table) -> None:
     datapoints = await apd.aggregation.collect.add_data_from_sensors(
         db_session,
         [
             Deployment(
                 id=None, colour=None, name=None, uri="http://localhost", api_key=""
             )
         ],
     )
     num_points = db_session.query(table).count()
     assert num_points == len(datapoints) == 2
 async def test_datapoints_are_added_to_the_session(self, mut, db_session) -> None:
     assert db_session.execute.call_count == 0
     datapoints = await mut(
         db_session,
         [
             Deployment(
                 id=None, colour=None, name=None, uri="http://localhost", api_key=""
             )
         ],
     )
     assert db_session.execute.call_count == len(datapoints)
 async def test_datapoints_can_be_mapped_back_to_DataPoints(
     self, mut, db_session, table, model
 ) -> None:
     datapoints = await mut(
         db_session,
         [
             Deployment(
                 id=None, colour=None, name=None, uri="http://localhost", api_key=""
             )
         ],
     )
     db_points = [
         model.from_sql_result(result) for result in db_session.query(table)
     ]
     assert db_points == datapoints
def test_add_deployment_to_db(db_uri, db_session, migrated_db):
    runner = CliRunner()
    deployment_id = uuid.UUID("76587cdd42dc489ea1d220e9b0bc62ca")
    with mock.patch(
            "apd.aggregation.collect.get_deployment_id") as get_deployment_id:
        get_deployment_id.return_value = deployment_id
        runner.invoke(
            apd.aggregation.cli.deployments,
            [
                "add", "http://otherhost", "Other", "--db", db_uri,
                "--api-key", "key"
            ],
        )
    deployments = db_session.query(deployment_table).all()
    assert len(deployments) == 1
    deployment = Deployment.from_sql_result(deployments[0])
    assert deployment.uri == "http://otherhost"
    assert deployment.api_key == "key"
    assert deployment.id == deployment_id
    async def test_daily_summary_view_matches_query(
        self, db_session, model, table, daily_summary_view
    ) -> None:
        await apd.aggregation.collect.add_data_from_sensors(
            db_session,
            [
                Deployment(
                    id=None, colour=None, name=None, uri="http://localhost", api_key=""
                )
            ],
        )

        headers = table.c.sensor_name, table.c.data
        value_counts = (
            db_session.query(*headers, sqlalchemy.func.count(table.c.id))
            .filter(model.collected_on_date == sqlalchemy.func.current_date())
            .group_by(*headers)
        )

        daily_summary_view = db_session.query(daily_summary_view)
        assert value_counts.all() == daily_summary_view.all()