Exemplo n.º 1
0
async def test_update_installation(test_app_asyncio, init_test_db, test_db,
                                   access_idx, payload, installation_id,
                                   status_code, status_details):

    # Create a custom access token
    auth = None
    if isinstance(access_idx, int):
        auth = await pytest.get_token(
            ACCESS_TABLE[access_idx]['id'],
            ACCESS_TABLE[access_idx]['scope'].split())

    response = await test_app_asyncio.put(f"/installations/{installation_id}/",
                                          data=json.dumps(payload),
                                          headers=auth)
    assert response.status_code == status_code
    if isinstance(status_details, str):
        assert response.json()['detail'] == status_details

    if response.status_code // 100 == 2:
        updated_installation = await get_entry(test_db, db.installations,
                                               installation_id)
        updated_installation = dict(**updated_installation)
        for k, v in updated_installation.items():
            if k == 'start_ts':
                assert v == parse_time(payload[k])
            else:
                assert v == payload.get(
                    k, INSTALLATION_TABLE_FOR_DB[installation_id - 1][k])
Exemplo n.º 2
0
async def test_get_user(test_app_asyncio, init_test_db, test_db, access_idx,
                        user_id, status_code, status_details):

    # Create a custom access token
    auth = None
    if isinstance(access_idx, int):
        auth = await pytest.get_token(
            ACCESS_TABLE[access_idx]['id'],
            ACCESS_TABLE[access_idx]['scope'].split())

    response = await test_app_asyncio.get(f"/users/{user_id}", headers=auth)
    assert response.status_code == status_code

    if isinstance(status_details, str):
        assert response.json()['detail'] == status_details
    if response.status_code // 100 == 2:
        user_in_db = await get_entry(test_db, db.users, user_id)
        user_in_db = dict(**user_in_db)
        response_json = response.json()
        response_json["created_at"] = parse_time(response_json["created_at"])

        assert response_json == {
            k: v
            for k, v in user_in_db.items() if k != "access_id"
        }
Exemplo n.º 3
0
async def test_update_installation(test_app_asyncio, test_db, monkeypatch):
    # Sterilize DB interactions
    await init_test_db(monkeypatch, test_db)

    test_payload = {
        "device_id": 1,
        "site_id": 1,
        "elevation": 123.,
        "lat": 0.,
        "lon": 0.,
        "yaw": 0.,
        "pitch": 0.,
        "start_ts": "2020-07-13T08:18:45.447773"
    }
    response = await test_app_asyncio.put("/installations/1/",
                                          data=json.dumps(test_payload))
    assert response.status_code == 200
    updated_installation_in_db = await get_entry_in_db(test_db,
                                                       db.installations, 1)
    updated_installation_in_db = dict(**updated_installation_in_db)
    for k, v in updated_installation_in_db.items():
        if k == 'start_ts':
            assert v == parse_time(test_payload[k])
        else:
            assert v == test_payload.get(k, INSTALLATION_TABLE_FOR_DB[0][k])
Exemplo n.º 4
0
async def test_get_alert(test_app_asyncio, test_db, monkeypatch):
    await init_test_db(monkeypatch, test_db)

    response = await test_app_asyncio.get("/alerts/1")
    assert response.status_code == 200
    response_json = response.json()
    response_json["created_at"] = parse_time(response_json["created_at"])
    assert response_json == ALERT_TABLE_FOR_DB[0]
Exemplo n.º 5
0
    def test_is_between_time(self):
        start_time = parse_time("22:00")
        end_time = parse_time("06:00")

        self.assertFalse(
            datetime.is_between_time(parse_datetime("2018-02-28T21:57:13Z"),
                                     start_time, end_time))
        self.assertTrue(
            datetime.is_between_time(parse_datetime("2018-02-28T22:00:00Z"),
                                     start_time, end_time))
        self.assertTrue(
            datetime.is_between_time(parse_datetime("2018-02-28T00:00:00Z"),
                                     start_time, end_time))
        self.assertTrue(
            datetime.is_between_time(parse_datetime("2018-02-28T06:00:00Z"),
                                     start_time, end_time))
        self.assertFalse(
            datetime.is_between_time(parse_datetime("2018-02-28T06:01:00Z"),
                                     start_time, end_time))
Exemplo n.º 6
0
async def test_get_alert(test_app_asyncio, init_test_db, access_idx, alert_id, status_code, status_details):

    # Create a custom access token
    auth = None
    if isinstance(access_idx, int):
        auth = await pytest.get_token(ACCESS_TABLE[access_idx]['id'], ACCESS_TABLE[access_idx]['scope'].split())

    response = await test_app_asyncio.get(f"/alerts/{alert_id}", headers=auth)
    assert response.status_code == status_code
    if isinstance(status_details, str):
        assert response.json()['detail'] == status_details

    if response.status_code // 100 == 2:
        response_json = response.json()
        response_json["created_at"] = parse_time(response_json["created_at"])
        assert response_json == ALERT_TABLE_FOR_DB[alert_id - 1]
Exemplo n.º 7
0
async def test_get_user(test_app_asyncio, test_db, monkeypatch):

    # Sterilize DB interactions
    monkeypatch.setattr(crud, "database", test_db)
    await populate_db(test_db, db.accesses, ACCESS_TABLE)
    await populate_db(test_db, db.users, USER_TABLE_FOR_DB)

    response = await test_app_asyncio.get("/users/1")
    user_1_in_db = await get_entry_in_db(test_db, db.users, 1)
    user_1_in_db = dict(**user_1_in_db)
    assert response.status_code == 200
    response_json = response.json()
    response_json["created_at"] = parse_time(response_json["created_at"])

    assert response_json == {k: v for k, v in user_1_in_db.items() if k != "access_id"}

    # personal version
    response = await test_app_asyncio.get("/users/me")
    assert response.status_code == 200
    json_response = response.json()
    for k in ["id", "login"]:
        assert json_response[k] == USER_TABLE_FOR_DB[1][k]
Exemplo n.º 8
0
async def test_heartbeat(test_app_asyncio, init_test_db, test_db, access_idx,
                         status_code, status_details):

    # Create a custom access token
    auth = None
    if isinstance(access_idx, int):
        auth = None
    if isinstance(access_idx, int):
        auth = await pytest.get_token(
            ACCESS_TABLE[access_idx]['id'],
            ACCESS_TABLE[access_idx]['scope'].split())

    utc_dt = datetime.utcnow()

    response = await test_app_asyncio.put("/devices/heartbeat", headers=auth)
    assert response.status_code == status_code
    if isinstance(status_details, str):
        assert response.json()['detail'] == status_details

    if response.status_code // 100 == 2:
        json_response = response.json()
        # Everything should be identical apart from ping
        assert datetime.utcnow() > datetime.fromisoformat(
            json_response['last_ping'])
        assert utc_dt < datetime.fromisoformat(json_response['last_ping'])

        updated_device = await get_entry(test_db, db.devices,
                                         json_response["id"])
        updated_device = dict(**updated_device)

        assert updated_device['last_ping'] > utc_dt
        if DEVICE_TABLE[json_response["id"] - 1]['last_ping'] is not None:
            assert updated_device['last_ping'] > DEVICE_TABLE[
                json_response["id"] - 1]['last_ping']
        assert updated_device['created_at'] == parse_time(
            DEVICE_TABLE[json_response["id"] - 1]['created_at'])
Exemplo n.º 9
0
from tests.utils import parse_time, parse_datetime

START_RANGE = parse_datetime("2018-02-28T21:57:13Z")
END_RANGE = parse_datetime("2018-03-01T22:10:56Z")

STANDARD_CYCLE = {
    "start": parse_time("06:00"),
    "end": parse_time("22:00"),
    "ocurrencies": [
        (parse_datetime("2018-02-28T21:57:13Z"),
         parse_datetime("2018-02-28T22:00:00Z")),
        (parse_datetime("2018-03-01T06:00:00Z"),
         parse_datetime("2018-03-01T22:00:00Z"))
    ]
}

REDUCED_CYCLE = {
    "start": parse_time("22:00"),
    "end": parse_time("06:00"),
    "ocurrencies": [
        (parse_datetime("2018-02-28T22:00:00Z"),
         parse_datetime("2018-03-01T06:00:00Z")),
        (parse_datetime("2018-03-01T22:00:00Z"),
         parse_datetime("2018-03-01T22:10:56Z"))
    ]
}
Exemplo n.º 10
0
from datetime import datetime
from unittest import TestCase
from unittest.mock import patch, Mock
from app.processors import billing
from app.models.enums import ChargeStrategy
from tests.utils import parse_datetime, parse_time

MOCK_CHARGES = [{
    "value": 9,
    "start": parse_time("10:00"),
    "end": parse_time("11:00")
}, {
    "value": 1,
    "start": parse_time("23:00"),
    "end": parse_time("00:30")
}]


def mock_cycle_calculator_return(expected_cycles, return_value):
    def f(start_date, end_date, start_cycle, end_cycle):
        if (start_cycle, end_cycle) in expected_cycles:
            return return_value

        return []

    return f


def mock_is_between_time_return(expected_cycle):
    def f(start_date, start_cycle, end_cycle):
        if (start_cycle, end_cycle) == expected_cycle: