def handle(self, request: Request, params: Dict): if not request: raise RuntimeError("Missing request") logger.info(f"Updating expense {params['expense_id']}") expense_id = Optional.of(params['expense_id']) category = Optional.of(params['category']) hide = Optional.of(params['hide']) if expense_id.is_empty(): raise RuntimeError("Expense id should not be empty") try: self.db.update_by_key(expense_id.get(), category=category, hide=hide) return """ <html> <head> <title>Some HTML in here</title> </head> <body> <h1>Successful </h1> </body> </html> """ except Exception as err: logger.error(f"Unable to update expense id: {expense_id} - {err}") raise RuntimeError("Update failed")
def getOriginFromWikidata(name: str): toReturn = Optional.empty() placeOfBirth = None page = wptools.page(name) try: page.get_wikidata() except: print("Couldn't get wikidata from name.") try: placeOfBirth = page.data['wikidata']['place of birth (P19)'] except: None try: placeOfBirth = page.data['wikidata']['location of formation (P740)'] except: None try: index = placeOfBirth.index("(") placeOfBirth = placeOfBirth[0:index] except: None if placeOfBirth != None: toReturn = Optional.of(placeOfBirth) return toReturn
def test_get_or_raise_on_a_populated_optional_returns_value(self): optional = Optional.of("thing") class RandomDomainException(Exception): pass assert optional.get_or_raise(RandomDomainException()) == optional.get()
def test_raises_if_flat_map_function_returns_non_optional(self): def does_not_return_optional(thing): return "PANTS" optional = Optional.of("thing") with pytest.raises(FlatMapFunctionDoesNotReturnOptionalException): optional.flat_map(does_not_return_optional)
def test_flat_map_returns_unwrapped_value_with_map_result(self): def maps_stuff(thing): return Optional.of(thing + "PANTS") optional = Optional.of("thing") res = optional.flat_map(maps_stuff) assert res.is_present() assert res.get() == "thingPANTS"
def test_will_run_consumer_if_present(self): scope = {'seen': False} def some_thing_consumer(thing): scope['seen'] = True optional = Optional.of("thing") optional.if_present(some_thing_consumer) assert scope['seen']
def test_wont_raise_on_or_else_raise_from_if_present_when_present(self): class ShouldNotHappenException(Exception): pass optional = Optional.of("thing") scope = {'seen': False} def some_thing_consumer(thing): scope['seen'] = True optional.if_present(some_thing_consumer).or_else_raise( ShouldNotHappenException) assert scope['seen']
def test_will_not_run_or_else_from_if_present_when_not_empty(self): scope = { 'if_seen': False, 'else_seen': False, } def some_thing_consumer(thing): scope['if_seen'] = True def or_else_procedure(): scope['else_seen'] = True optional = Optional.of(23) value = optional.if_present(some_thing_consumer).or_else( or_else_procedure) assert scope['if_seen'] assert not scope['else_seen'] assert value.get() == 23
def test_is_present_with_content(self): optional = Optional.of("thing") assert optional.is_present()
def test_populated_optionals_are_truthy(self): assert Optional.of('foo')
def test_populated_optionals_are_truthy_even_if_their_value_is_falsy(self): assert Optional.of(False)
def test_get_or_default_on_a_populated_optional_ignores_default_value( self): optional = Optional.of("thing") assert optional.get_or_default("pants") == optional.get()
def test_will_return_optional_of_return_val_when_not_present(self): def or_else_supplier(): return "pants" optional = Optional.empty() assert optional.or_else(or_else_supplier) == Optional.of("pants")
def test_can_instantiate(self): Optional.of(None)
def does_stuff(thing): return Optional.of("PANTS")
def test_map_returns_empty_if_function_returns_none(self): def does_nothing(thing): return None optional = Optional.of("thing") assert optional.map(does_nothing).is_empty()
def test_can_eval_the_representation_of_a_populated_optional(self): optional = Optional.of('23') assert eval(repr(optional)) == optional
def test_empty_optional_not_equal_non_empty_optional(self): assert Optional.empty() != Optional.of("thing")
def get_open_class(self, period: int, subject: Subject) -> Optional.of(Class): newest_class = self.matrix[period][subject.id][-1] if newest_class.is_open(): return Optional.of(newest_class) return Optional.empty()
def test_optional_not_equal_with_non_optional(self): assert "PANTS" != Optional.of("PANTS")
def maps_stuff(thing): return Optional.of(thing + "PANTS")
def test_flat_map_returns_empty_if_function_returns_empty_optional(self): def does_nothing(thing): return Optional.empty() optional = Optional.of("thing") assert optional.flat_map(does_nothing).is_empty()
def test_is_not_present_with_empty(self): optional = Optional.of(None) assert not optional.is_present()
def test_can_instantiate_an_empty_optional_via_the_zero_arity_of(self): assert Optional.of() == Optional.empty()
def test_can_get_when_present_and_have_checked(self): optional = Optional.of("thing") assert optional.is_present() assert optional.get() == "thing"
def test_instantiate_with_none(self): optional = Optional.of(None) assert optional.is_empty()
def test_non_empty_optionals_with_non_equal_content_are_not_equal(self): assert Optional.of("PANTS") != Optional.of("thing")
def test_instantiate_with_content(self): optional = Optional.of("something") assert not optional.is_empty()
def test_can_instantiate_with_any_other_value(self): assert Something(value=23, optional=Optional) == Optional.of(23)
def test_non_empty_optionals_with_equal_content_are_equal(self): assert Optional.of("PANTS") == Optional.of("PANTS")