def check(expect, validities): with requests_mock.mock() as m: m.get( URL, complete_qs=True, json={ "results": [[{ "id": "00000000-0000-0000-0000-000000000000", "registreringer": [{ "tilstande": { "organisationenhedgyldighed": [{ "gyldighed": v, "virkning": { "from": mora_util.to_lora_time(t1, ), "from_included": True, "to": mora_util.to_lora_time(t2, ), "to_included": False } } for t1, t2, v in validities] }, }] }]] }, ) self.assertIs( expect, validator._is_date_range_valid( '00000000-0000-0000-0000-000000000000', util.parsedatetime('01-01-2000'), util.parsedatetime('01-01-3000'), c, 'organisationenhedgyldighed'))
def test_to_lora_time(self): tests = { self.today: '2015-06-01T00:00:00+02:00', self.now: '2015-06-01T01:10:00+02:00', '01-06-2017': '2017-06-01T00:00:00+02:00', '31-12-2017': '2017-12-31T00:00:00+01:00', 'infinity': 'infinity', '-infinity': '-infinity', '2017-07-31T22:00:00+00:00': '2017-08-01T00:00:00+02:00', # the frontend doesn't escape the 'plus' in ISO 8601 dates, so # we get it as a space '2017-07-31T22:00:00 00:00': '2017-08-01T00:00:00+02:00', datetime.date(2015, 6, 1): '2015-06-01T00:00:00+02:00', # check parsing of raw dates '2018-01-01': '2018-01-01T00:00:00+01:00', '2018-06-01': '2018-06-01T00:00:00+02:00', } for value, expected in tests.items(): self.assertEqual(expected, util.to_lora_time(value), 'failed to parse {!r}'.format(value)) # NB: this test used to work, but we now use dateutil, # which tries it best to make of the inputs from the # user... if False: # 15 is not a valid month self.assertRaises(exceptions.HTTPException, util.to_lora_time, '1999-15-11 00:00:00+01') # make sure we can round-trip the edge cases correctly self.assertEqual(util.parsedatetime(util.NEGATIVE_INFINITY), util.NEGATIVE_INFINITY) self.assertEqual(util.parsedatetime(util.POSITIVE_INFINITY), util.POSITIVE_INFINITY) # we frequently get these dates in spreadsheets self.assertEqual(util.parsedatetime('31-12-9999'), util.POSITIVE_INFINITY) # test fallback self.assertEqual(util.parsedatetime('blyf', 'flaf'), 'flaf')
def test_get_effects(self, m): URL = (settings.LORA_URL + 'organisation/organisationenhed?' 'uuid=00000000-0000-0000-0000-000000000000' '&virkningfra=2010-06-01T02%3A00%3A00%2B02%3A00' '&virkningtil=infinity') m.get( URL, complete_qs=True, json={ "results": [[{ "id": "00000000-0000-0000-0000-000000000000", "registreringer": [{ "tilstande": { "organisationenhedgyldighed": [{ "gyldighed": v, "virkning": { "from": mora_util.to_lora_time(t1, ), "from_included": True, "to": mora_util.to_lora_time(t2, ), "to_included": False } } for t1, t2, v in [ ('01-01-1900', '01-01-2100', 'Aktiv'), ('01-01-2100', '01-01-2300', 'Inaktiv'), ('01-01-2300', '01-01-2500', 'Aktiv'), ('01-01-2500', '01-01-2700', 'Inaktiv'), ('01-01-2700', '01-01-2900', 'Aktiv'), ('01-01-2900', '01-01-3100', 'Inaktiv'), ('01-01-3100', '01-01-3300', 'Aktiv'), ]] }, }] }]] }, ) c = lora.Connector(validity='future') self.assertEqual( [("2100-01-01 00:00:00+01:00", "2300-01-01 00:00:00+01:00", { "tilstande": { "organisationenhedgyldighed": [{ "gyldighed": "Inaktiv", "virkning": { "from_included": True, "to_included": False, "from": "2100-01-01T00:00:00+01:00", "to": "2300-01-01T00:00:00+01:00" } }] } }), ("2300-01-01 00:00:00+01:00", "2500-01-01 00:00:00+01:00", { "tilstande": { "organisationenhedgyldighed": [{ "gyldighed": "Aktiv", "virkning": { "from_included": True, "to_included": False, "from": "2300-01-01T00:00:00+01:00", "to": "2500-01-01T00:00:00+01:00" } }] } }), ("2500-01-01 00:00:00+01:00", "2700-01-01 00:00:00+01:00", { "tilstande": { "organisationenhedgyldighed": [{ "gyldighed": "Inaktiv", "virkning": { "from_included": True, "to_included": False, "from": "2500-01-01T00:00:00+01:00", "to": "2700-01-01T00:00:00+01:00" } }] } }), ("2700-01-01 00:00:00+01:00", "2900-01-01 00:00:00+01:00", { "tilstande": { "organisationenhedgyldighed": [{ "gyldighed": "Aktiv", "virkning": { "from_included": True, "to_included": False, "from": "2700-01-01T00:00:00+01:00", "to": "2900-01-01T00:00:00+01:00" } }] } }), ("2900-01-01 00:00:00+01:00", "3100-01-01 00:00:00+01:00", { "tilstande": { "organisationenhedgyldighed": [{ "gyldighed": "Inaktiv", "virkning": { "from_included": True, "to_included": False, "from": "2900-01-01T00:00:00+01:00", "to": "3100-01-01T00:00:00+01:00" } }] } }), ("3100-01-01 00:00:00+01:00", "3300-01-01 00:00:00+01:00", { "tilstande": { "organisationenhedgyldighed": [{ "gyldighed": "Aktiv", "virkning": { "from_included": True, "to_included": False, "from": "3100-01-01T00:00:00+01:00", "to": "3300-01-01T00:00:00+01:00" } }] } })], [(str(start), str(end), entry) for start, end, entry in c.organisationenhed.get_effects( '00000000-0000-0000-0000-000000000000', relevant={ 'tilstande': ('organisationenhedgyldighed', ), }, )], )