File: test_registrations.py

package info (click to toggle)
python-openleadr-python 0.5.34%2Bdfsg.1-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,496 kB
  • sloc: python: 6,942; xml: 663; makefile: 32; sh: 18
file content (120 lines) | stat: -rw-r--r-- 3,841 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from openleadr import OpenADRClient, OpenADRServer, enable_default_logging
import datetime
import pytest
import asyncio

enable_default_logging()


def on_create_party_registration_success(registration_info):
    return 'ven123', 'reg123'

def on_create_party_registration_reject(registration_info):
    return False

def on_event(*args, **kwargs):
    return 'optIn'

def event_callback(ven_id, event_id, opt_type):
    pass

@pytest.mark.asyncio
async def test_successful_registration():
    loop = asyncio.get_event_loop()
    client = OpenADRClient(ven_name='myven',
                           vtn_url='http://localhost:8080/OpenADR2/Simple/2.0b')

    client.add_handler('on_event', on_event)
    server = OpenADRServer(vtn_id='myvtn', requested_poll_freq=datetime.timedelta(seconds=1))
    server.add_handler('on_create_party_registration', on_create_party_registration_success)

    await server.run_async()
    await client.run()

    await asyncio.sleep(0.1)

    assert client.registration_id == 'reg123'
    await client.stop()
    await server.stop()

@pytest.mark.asyncio
async def test_rejected_registration():
    loop = asyncio.get_event_loop()
    client = OpenADRClient(ven_name='myven',
                           vtn_url='http://localhost:8080/OpenADR2/Simple/2.0b')

    client.add_handler('on_event', on_event)
    server = OpenADRServer(vtn_id='myvtn', requested_poll_freq=datetime.timedelta(seconds=1))
    server.add_handler('on_create_party_registration', on_create_party_registration_reject)

    await server.run_async()
    await client.run()

    await asyncio.sleep(0.1)

    assert client.registration_id == None
    await client.stop()
    await server.stop()

@pytest.mark.asyncio
async def test_registration_with_prefilled_ven_id():
    loop = asyncio.get_event_loop()
    client = OpenADRClient(ven_name='myven',
                           vtn_url='http://localhost:8080/OpenADR2/Simple/2.0b',
                           ven_id='ven123')

    client.add_handler('on_event', on_event)
    server = OpenADRServer(vtn_id='myvtn', requested_poll_freq=datetime.timedelta(seconds=1))
    server.add_handler('on_create_party_registration', on_create_party_registration_success)

    await server.run_async()
    await client.run()

    await asyncio.sleep(0.1)

    assert client.registration_id == 'reg123'
    await client.stop()
    await server.stop()

@pytest.mark.asyncio
async def test_rejected_registration_with_prefilled_ven_id():
    loop = asyncio.get_event_loop()
    client = OpenADRClient(ven_name='myven',
                           vtn_url='http://localhost:8080/OpenADR2/Simple/2.0b',
                           ven_id='ven123')

    client.add_handler('on_event', on_event)
    server = OpenADRServer(vtn_id='myvtn', requested_poll_freq=datetime.timedelta(seconds=1))
    server.add_handler('on_create_party_registration', on_create_party_registration_reject)

    await server.run_async()
    await client.run()

    await asyncio.sleep(0.1)

    assert client.registration_id == None
    await client.stop()
    await server.stop()

@pytest.mark.asyncio
async def test_registration_with_different_ven_id():
    loop = asyncio.get_event_loop()
    client = OpenADRClient(ven_name='myven',
                           vtn_url='http://localhost:8080/OpenADR2/Simple/2.0b',
                           ven_id='someven')
    client.add_handler('on_event', on_event)
    server = OpenADRServer(vtn_id='myvtn', requested_poll_freq=datetime.timedelta(seconds=1))
    server.add_handler('on_create_party_registration', on_create_party_registration_success)

    await server.run_async()
    await client.run()

    await asyncio.sleep(0.1)

    assert client.registration_id == 'reg123'
    assert client.ven_id == 'ven123'

    await asyncio.sleep(0.1)

    await client.stop()
    await server.stop()