waldhart-availability

Read and process availability from Waldhart software
git clone https://git.in0rdr.ch/waldhart-availability.git
Log | Files | Refs | Pull requests |Archive | README

waldhart_availability_test.py (8188B)


      1 import logging
      2 import pytest
      3 import requests
      4 import requests_mock
      5 from datetime import datetime
      6 
      7 from waldhart_availability import Duration
      8 from waldhart_availability import WaldhartAvailability
      9 
     10 logger = logging.getLogger(__name__)
     11 
     12 class TestWaldhartAvailability:
     13     """
     14     Test WaldhartAvailability functionality.
     15     """
     16 
     17     @requests_mock.Mocker(kw="mock")
     18     def test_login(self, **kwargs):
     19         kwargs['mock'].post("http://waldhart/login", text="OK")
     20         kwargs['mock'].get("http://caldav", text="dav")
     21 
     22         waldhart = WaldhartAvailability(waldhart_url="http://waldhart",
     23                                         waldhart_username="test",
     24                                         waldhart_password="123",
     25                                         caldav_url="http://caldav",
     26                                         caldav_username="testc",
     27                                         caldav_password="1234")
     28         r = waldhart.login()
     29         assert r
     30 
     31     @requests_mock.Mocker(kw="mock")
     32     def test_read_availability(self, **kwargs):
     33         kwargs['mock'].post("http://waldhart/login", text="OK")
     34         kwargs['mock'].get("http://waldhart/myavailability/myavailability_month/get_availability_json?year=2025&month=1", text=JAN_3)
     35         kwargs['mock'].get("http://caldav", text="dav")
     36 
     37         waldhart = WaldhartAvailability(waldhart_url="http://waldhart",
     38                                         waldhart_username="test",
     39                                         waldhart_password="123",
     40                                         caldav_url="http://caldav",
     41                                         caldav_username="testc",
     42                                         caldav_password="1234")
     43         waldhart.login()
     44 
     45         day = waldhart.read_availability(2025, 1)["3"]
     46         logger.info(day)
     47 
     48     @requests_mock.Mocker(kw="mock")
     49     def test_duration(self, **kwargs):
     50         kwargs['mock'].post("http://waldhart/login", text="OK")
     51         kwargs['mock'].get("http://caldav", text="dav")
     52 
     53         waldhart = WaldhartAvailability(waldhart_url="http://waldhart",
     54                                         waldhart_username="test",
     55                                         waldhart_password="123",
     56                                         caldav_url="http://caldav",
     57                                         caldav_username="testc",
     58                                         caldav_password="1234")
     59         waldhart.login()
     60 
     61         kwargs['mock'].get("http://waldhart/myavailability/myavailability_month/get_availability_json?year=2025&month=1", text=JAN_3)
     62         day = waldhart.read_availability(2025, 1)["3"]
     63         duration = waldhart.duration_type(day)
     64         assert duration == Duration.ALLDAY
     65 
     66         kwargs['mock'].get("http://waldhart/myavailability/myavailability_month/get_availability_json?year=2025&month=1", text=JAN_6)
     67         day = waldhart.read_availability(2025, 1)["6"]
     68         duration = waldhart.duration_type(day)
     69         assert duration == Duration.AM
     70 
     71         kwargs['mock'].get("http://waldhart/myavailability/myavailability_month/get_availability_json?year=2025&month=1", text=JAN_20)
     72         day = waldhart.read_availability(2025, 1)["20"]
     73         duration = waldhart.duration_type(day)
     74         assert duration == Duration.PM
     75 
     76         kwargs['mock'].get("http://waldhart/myavailability/myavailability_month/get_availability_json?year=2025&month=1", text=JAN_26)
     77         day = waldhart.read_availability(2025, 1)["26"]
     78         duration = waldhart.duration_type(day)
     79         assert duration == Duration.NV
     80 
     81     @requests_mock.Mocker(kw="mock")
     82     def test_update_availability(self, **kwargs):
     83         kwargs['mock'].post("http://waldhart/login", text="OK")
     84         kwargs['mock'].get("mock://caldav", text="dav")
     85 
     86         # TODO: mock PROPFIND requests
     87         #session = requests.Session()
     88         #adapter = requests_mock.Adapter()
     89         #adapter.register_uri("PROPFIND", "mock://caldav", text="dav")
     90         #session.mount("mock://", adapter)
     91 
     92         waldhart = WaldhartAvailability(waldhart_url="http://waldhart",
     93                                         waldhart_username="test",
     94                                         waldhart_password="123",
     95                                         caldav_url="mock://caldav",
     96                                         caldav_username="testc",
     97                                         caldav_password="1234")
     98         waldhart.login()
     99 
    100         # expect ALLDAY
    101         kwargs['mock'].get("http://waldhart/myavailability/myavailability_month/get_availability_json?year=2025&month=1", text=JAN_3)
    102         day = waldhart.read_availability(2025, 1)["3"]
    103         duration = waldhart.duration_type(day)
    104         waldhart.update_availability(2025, 1, 3, duration)
    105 
    106         # expect forenoon (AM)
    107         kwargs['mock'].get("http://waldhart/myavailability/myavailability_month/get_availability_json?year=2025&month=1", text=JAN_6)
    108         day = waldhart.read_availability(2025, 1)["6"]
    109         duration = waldhart.duration_type(day)
    110         waldhart.update_availability(2025, 1, 6, duration)
    111 
    112         # expect afternoon (PM)
    113         kwargs['mock'].get("http://waldhart/myavailability/myavailability_month/get_availability_json?year=2025&month=1", text=JAN_20)
    114         day = waldhart.read_availability(2025, 1)["20"]
    115         duration = waldhart.duration_type(day)
    116         waldhart.update_availability(2025, 1, 20, duration)
    117 
    118         # expect not available (NV)
    119         kwargs['mock'].get("http://waldhart/myavailability/myavailability_month/get_availability_json?year=2025&month=1", text=JAN_26)
    120         day = waldhart.read_availability(2025, 1)["26"]
    121         duration = waldhart.duration_type(day)
    122         waldhart.update_availability(2025, 1, 26, duration)
    123 
    124 
    125 # Waldhart json response mock
    126 
    127 JAN_3 = """
    128 {
    129   "3": {
    130     "has_course_pm": false,
    131     "has_course_md": false,
    132     "pm": true,
    133     "on_call": false,
    134     "ready_3": null,
    135     "ready_1": null,
    136     "to_2": null,
    137     "has_course_am": false,
    138     "md": true,
    139     "group_type": null,
    140     "to_1": null,
    141     "vacation": false,
    142     "from_2": null,
    143     "has_course_all_day": false,
    144     "am": true,
    145     "ready_2": null,
    146     "from_3": null,
    147     "ready_state": 1,
    148     "from_1": null,
    149     "ill": false,
    150     "courses": null,
    151     "show_hatching": true,
    152     "not_available": false,
    153     "group_type_2": null,
    154     "to_3": null,
    155     "has_course": false
    156   }
    157 }
    158 """
    159 
    160 JAN_6 = """
    161 {
    162   "6": {
    163     "has_course_pm": false,
    164     "has_course_md": false,
    165     "pm": false,
    166     "on_call": false,
    167     "ready_3": 0,
    168     "ready_1": 1,
    169     "to_2": 840,
    170     "has_course_am": false,
    171     "md": false,
    172     "group_type": null,
    173     "to_1": 720,
    174     "vacation": false,
    175     "from_2": 720,
    176     "has_course_all_day": false,
    177     "am": true,
    178     "ready_2": 0,
    179     "from_3": 720,
    180     "ready_state": 5,
    181     "from_1": 480,
    182     "ill": false,
    183     "courses": null,
    184     "show_hatching": true,
    185     "not_available": false,
    186     "group_type_2": null,
    187     "to_3": 960,
    188     "has_course": false
    189   }
    190 }
    191 """
    192 
    193 JAN_20 = """
    194 {
    195   "20": {
    196     "has_course_pm": false,
    197     "has_course_md": false,
    198     "pm": true,
    199     "on_call": false,
    200     "ready_3": 1,
    201     "ready_1": 0,
    202     "to_2": 840,
    203     "has_course_am": false,
    204     "md": false,
    205     "group_type": null,
    206     "to_1": 720,
    207     "vacation": false,
    208     "from_2": 720,
    209     "has_course_all_day": false,
    210     "am": false,
    211     "ready_2": 0,
    212     "from_3": 720,
    213     "ready_state": 5,
    214     "from_1": 480,
    215     "ill": false,
    216     "courses": null,
    217     "show_hatching": true,
    218     "not_available": false,
    219     "group_type_2": null,
    220     "to_3": 960,
    221     "has_course": false
    222   }
    223 }
    224 """
    225 
    226 JAN_26 = """
    227 {
    228   "26": {
    229     "has_course_pm": false,
    230     "has_course_md": false,
    231     "pm": false,
    232     "on_call": false,
    233     "ready_3": null,
    234     "ready_1": null,
    235     "to_2": null,
    236     "has_course_am": false,
    237     "md": false,
    238     "group_type": null,
    239     "to_1": null,
    240     "vacation": false,
    241     "from_2": null,
    242     "has_course_all_day": false,
    243     "am": false,
    244     "ready_2": null,
    245     "from_3": null,
    246     "ready_state": 0,
    247     "from_1": null,
    248     "ill": false,
    249     "courses": null,
    250     "show_hatching": true,
    251     "not_available": true,
    252     "group_type_2": null,
    253     "to_3": null,
    254     "has_course": false
    255   }
    256 }
    257 """