def __test_ex_defaults(adapted_item_data): assert is_not_a_number(adapted_item_data.get("ex_average_back_price")) assert adapted_item_data.get("ex_back_size") == 0 assert is_not_a_number(adapted_item_data.get("ex_average_lay_price")) assert adapted_item_data.get("ex_lay_size") == 0 assert is_not_a_number(adapted_item_data.get("ex_offered_back_price")) assert is_not_a_number(adapted_item_data.get("ex_offered_lay_price"))
def test_inverse_price(): GIVEN("a price") buy_price = 2 WHEN("we calculate the inverse") sell_price = calc_inverse_price(buy_price) THEN("the correct price is returned") assert sell_price == 2 GIVEN("another valid price") buy_price = 1.5 WHEN("we calculate the sell price") sell_price = calc_inverse_price(buy_price) THEN("the correct price is returned") assert sell_price == 1 / (1 - (1 / buy_price)) GIVEN("a string price") buy_price = "a price" WHEN("we calculate the sell price") sell_price = calc_inverse_price(buy_price) THEN("the sell price is shown to be not a number") assert is_not_a_number(sell_price) GIVEN("a negative price") buy_price = -2 WHEN("we calculate the sell price") sell_price = calc_inverse_price(buy_price) THEN("the sell price is shown to be not a number") assert is_not_a_number(sell_price) GIVEN("a price between 0 and 1") buy_price = 0.99 WHEN("we calculate the sell price") sell_price = calc_inverse_price(buy_price) THEN("the sell price is shown to be not a number") assert is_not_a_number(sell_price)
def __calc_valid_records(self): valid_records = 0 for i, item in enumerate(self.__weights): if (not is_not_a_number(item) and item > 0 and not is_not_a_number(self.__x[i]) and not is_not_a_number(self.__y[i])): valid_records += 1 return valid_records
def test_not_nan(): GIVEN("a value that is a number") number = 1 WHEN("we check if the value is not a number") true = is_not_a_number(number) THEN("it is not") assert not true GIVEN("a None value") none = None WHEN("we check if the value is not a number") true = is_not_a_number(none) THEN("it is not") assert not true
def test_nan(): GIVEN("a value that is not a number") nan = not_a_number() WHEN("we check if the value is not a number") true = is_not_a_number(nan) THEN("it is") assert true
def test_buy_metadata(required_variables): GIVEN("a dictionary with the correct information") item_data = __get_data() variables = [ "id", "removal_date", "sp_back_size", "ex_back_size", "sp_back_price", "ex_average_back_price", "ex_offered_back_price", ] required_variables.return_value = variables WHEN("we instantiate the item handler object") adapted_item_data = ItemAdapter(item_data).get_adapted_data() THEN("the object has all of the correct information") assert adapted_item_data.get("id") == __get_id(item_data) assert is_not_a_number(adapted_item_data.get("removal_date")) assert adapted_item_data.get("sp_back_size") == __calc_sp_back_size(item_data) assert adapted_item_data.get("ex_back_size") == __calc_ex_back_size(item_data) assert adapted_item_data.get("sp_back_price") == __get_sp_back_price(item_data) assert adapted_item_data.get( "ex_average_back_price" ) == __calc_ex_average_back_price(item_data) assert adapted_item_data.get( "ex_offered_back_price" ) == __get_ex_offered_back_price(item_data) assert len(adapted_item_data.keys()) == len(variables)
def test_try_divide_zero(): GIVEN("a numerator and an invalid divisor") numerator = 1 denominator = 0 WHEN("we try to divide the numerator by NaN") result = try_divide(value=numerator, denominator=denominator) THEN("the result is as expected") assert is_not_a_number(result)
def test_try_divide_nan(): GIVEN("a numerator, a divisor and a NaN value") numerator = 1 denominator = 2 nan = not_a_number() WHEN("we try to divide the numerator by NaN") result = try_divide(value=numerator, denominator=nan) THEN("the result is as expected") assert is_not_a_number(result) WHEN("we try to divide NaN by the denominator") result = try_divide(value=nan, denominator=denominator) THEN("the result is as expected") assert is_not_a_number(result) WHEN("we try to divide NaN by NaN") result = try_divide(value=nan, denominator=nan) THEN("the result is as expected") assert is_not_a_number(result)
def test_empty_ex(required_variables): GIVEN("a dictionary which has an empty ex attribute") item_data = __get_data(ex={}) required_variables.return_value = all_variables WHEN("we instantiate the item handler object") adapted_item_data = ItemAdapter(item_data).get_adapted_data() THEN("the object has all of the correct defaults applied") assert adapted_item_data.get("id") == __get_id(item_data) __test_ex_defaults(adapted_item_data=adapted_item_data) __test_sp_values(adapted_item_data=adapted_item_data, item_data=item_data) assert is_not_a_number(adapted_item_data.get("removal_date"))
def test_invalid_near_price(required_variables): GIVEN("a dictionary which has Infinity in place of the nearPrice") item_data = __get_data(sp=__get_inf_sp()) required_variables.return_value = all_variables WHEN("we instantiate the item handler object") adapted_item_data = ItemAdapter(item_data).get_adapted_data() THEN("the object has all of the correct defaults applied") assert adapted_item_data.get("id") == __get_id(item_data) __test_sp_defaults(adapted_item_data=adapted_item_data, item_data=item_data) __test_ex_values(adapted_item_data=adapted_item_data, item_data=item_data) assert is_not_a_number(adapted_item_data.get("removal_date"))
def test_create_item(required_variables): GIVEN("a dictionary with the correct information") item_data = __get_data() required_variables.return_value = all_variables WHEN("we instantiate the item handler object") adapted_item_data = ItemAdapter(item_data).get_adapted_data() THEN("the object has all of the correct information") assert adapted_item_data.get("id") == __get_id(item_data) __test_sp_values(adapted_item_data=adapted_item_data, item_data=item_data) __test_ex_values(adapted_item_data=adapted_item_data, item_data=item_data) assert is_not_a_number(adapted_item_data.get("removal_date"))
def test_nan_comp_data(): GIVEN("a simple set of data and a data transform handler") nan = not_a_number() items = ( { "id": 123, "price": 0.476190476190476 }, { "id": 456, "price": 0.476190476190476 }, { "id": 789, "price": nan }, ) handler = TransformHandler() handler._set_items(items=items) WHEN("we calculate the compositional data") compositional_data = handler._get_compositional_data(price_name="price") THEN("a list of dictionaries with the correct values is returned") assert compositional_data[0:2] == [ { "id": 123, "compositional_probability": 0.5, "compositional_price": 2 }, { "id": 456, "compositional_probability": 0.5, "compositional_price": 2 }, ] nan_entry = compositional_data[2] assert nan_entry.get("id") == 789 assert is_not_a_number(nan_entry.get("compositional_probability")) assert is_not_a_number(nan_entry.get("compositional_price"))
def test_get_last_column_entry(): GIVEN("a simple set of data and a container") data = __get_test_dict() data_container = DataContainer(data) WHEN("we get the last entry for A") last_a = data_container.get_last_column_entry("A") THEN("the correct value is returned") assert last_a == data.get("A")[-1] assert isinstance(last_a, float) WHEN("we get the last entry for B") last_b = data_container.get_last_column_entry("B") THEN("the correct value is returned") assert last_b == data.get("B")[-1] assert isinstance(last_b, str) WHEN("we get the last entry for C") last_c = data_container.get_last_column_entry("C") THEN("the correct value is returned") assert is_not_a_number(last_c)
def test_nan_comp_data(): GIVEN("a simple set of data and a probability handler") nan = not_a_number() items = ( { "id": 123, "sp_probability": 0.476190476190476 }, { "id": 456, "sp_probability": 0.476190476190476 }, { "id": 789, "sp_probability": nan }, ) handler = ProbabilityHandler(items=items, name="sp_probability", correct_probability=1) WHEN("we calculate the compositional probabilities") compositional_probabilities = handler.calc_compositional_probabilities() THEN("a list of dictionaries with the correct values is returned") assert compositional_probabilities[0:2] == [ { "id": 123, "compositional_probability": 0.5 }, { "id": 456, "compositional_probability": 0.5 }, ] nan_entry = compositional_probabilities[2] assert nan_entry.get("id") == 789 assert is_not_a_number(nan_entry.get("compositional_probability"))
def __test_sp_defaults(adapted_item_data, item_data): assert adapted_item_data.get("id") == __get_id(item_data) assert is_not_a_number(adapted_item_data.get("sp_back_price")) assert adapted_item_data.get("sp_back_size") == 0 assert is_not_a_number(adapted_item_data.get("sp_lay_price")) assert adapted_item_data.get("sp_lay_size") == 0
def is_a_number(value): return type(value) in [int, float, complex] and not is_not_a_number(value)