def prepare_row_for_insert(attrs, doc, include_extra_props=None): _extra_props = {} attrs = init_values(attrs) (attrs, _extra_props) = set_values(attrs, doc, _extra_props) if include_extra_props is True: _extra_props = unnester.cast(_extra_props, 'jsonb') attrs["extraProps"]["value"] = _extra_props attrs_pg = [v["name_cm"] for k, v in attrs.items() if k in doc.keys()] values = [v["value"] for k, v in attrs.items() if k in doc.keys()] if len(doc.keys()) > len(attrs_pg) and include_extra_props: attrs_pg.append('_extra_props') values.append(_extra_props) return attrs_pg, values
def set_values(attr_details, doc, _extra_props=None): """ Casts values for a whole document """ for key, field_value in doc.items(): keys_cm = list(attr_details.keys()) if key in keys_cm: field_type = attr_details[key]["type_cm"] value = unnester.cast(field_value, field_type) if value == 'undefined' and _extra_props is not None: _extra_props.update({key: field_value}) else: attr_details[key]["value"] = value else: if _extra_props is not None: _extra_props.update({key: field_value}) if _extra_props is not None: return attr_details, _extra_props else: return attr_details
def test_cast_number_text_to_double(self): value_old = "2" column_type = "double precision" value_new = unnester.cast(value_old, column_type) assert value_new == 2
def test_cast_jsonb_to_timestamp(self): # TODO check again value_old = {} column_type = "timestamp" value_new = unnester.cast(value_old, column_type) assert value_new == value_old
def test_cast_jsonb_to_bool(self): value_old = {} column_type = "boolean" value_new = unnester.cast(value_old, column_type) assert value_new is False
def test_cast_jsonb_to_bool(self): value_old = {"xyz": "oh no"} column_type = "boolean" value_new = unnester.cast(value_old, column_type) assert value_new == 'undefined'
def test_cast_jsonb_to_double_undefined(self): value_old = {"xyz": "oh no"} column_type = "double precision" value_new = unnester.cast(value_old, column_type) assert value_new == 'undefined'
def test_cast_text_text_to_double(self): value_old = "xyz" column_type = "double precision" value_new = unnester.cast(value_old, column_type) assert value_new == 'undefined'