コード例 #1
0
def test_to_yaml(request, instance_single_pk, instance_postgresql,
                 supports_serialization, hybrid_value, max_nesting,
                 current_nesting, serialize_function, warning, error):
    if supports_serialization:
        target = instance_postgresql[0][0]
    else:
        target = instance_single_pk[0]

    target.hybrid = hybrid_value

    if not error:
        if warning:
            with pytest.warns(warning):
                interim_dict = target._to_dict('yaml',
                                               max_nesting=max_nesting,
                                               current_nesting=current_nesting)
        else:
            interim_dict = target._to_dict('yaml',
                                           max_nesting=max_nesting,
                                           current_nesting=current_nesting)

    if not error and not warning:
        result = target.to_yaml(max_nesting=max_nesting,
                                current_nesting=current_nesting,
                                serialize_function=serialize_function)

        print('RESULT:')
        print(result)
        assert isinstance(result, str)

        deserialized_dict = yaml.safe_load(result)
        print('\nDESERIALIZED DICT:')
        print(deserialized_dict)

        assert isinstance(deserialized_dict, dict)

        assert are_dicts_equivalent(interim_dict, deserialized_dict) is True

    elif not warning:
        with pytest.raises(error):
            result = target.to_yaml(max_nesting=max_nesting,
                                    current_nesting=current_nesting,
                                    serialize_function=serialize_function)
    elif not error:
        with pytest.warns(warning):
            result = target.to_yaml(max_nesting=max_nesting,
                                    current_nesting=current_nesting,
                                    serialize_function=serialize_function)

        print('RESULT:')
        print(result)
        assert isinstance(result, str)

        deserialized_dict = yaml.safe_load(result)
        print('\nDESERIALIZED DICT:')
        print(deserialized_dict)

        assert isinstance(deserialized_dict, dict)

        assert are_dicts_equivalent(interim_dict, deserialized_dict) is True
コード例 #2
0
def test__to_dict(request, instance_single_pk, instance_postgresql,
                  supports_serialization, hybrid_value, format, max_nesting,
                  current_nesting, expected_result, warning, error):
    if supports_serialization:
        target = instance_postgresql[0][0]
    else:
        target = instance_single_pk[0]

    target.hybrid = hybrid_value

    if not error and not warning:
        result = target._to_dict(format,
                                 max_nesting=max_nesting,
                                 current_nesting=current_nesting)

        assert isinstance(result, dict)
        print('RESULT:')
        print(result)
        print('\nEXPECTED:')
        print(expected_result)
        for key in result:
            assert key in expected_result
            assert expected_result[key] == result[key]
        for key in expected_result:
            assert key in result
            assert result[key] == expected_result[key]
        assert are_dicts_equivalent(result, expected_result) is True
    elif not warning:
        with pytest.raises(error):
            result = target._to_dict(format,
                                     max_nesting=max_nesting,
                                     current_nesting=current_nesting)
    elif not error:
        with pytest.warns(warning):
            result = target._to_dict(format,
                                     max_nesting=max_nesting,
                                     current_nesting=current_nesting)

        assert isinstance(result, dict)
        for key in result:
            assert key in expected_result
            assert expected_result[key] == result[key]
        for key in expected_result:
            assert key in result
            assert result[key] == expected_result[key]
        assert are_dicts_equivalent(result, expected_result) is True
コード例 #3
0
def test_instance__parse_dict(request, instance_single_pk, instance_postgresql,
                              supports_serialization, hybrid_value, format,
                              max_nesting, current_nesting, extra_keys,
                              error_on_extra_keys, drop_extra_keys,
                              expected_result, warning, error):
    if supports_serialization:
        target = instance_postgresql[0][0]
    else:
        target = instance_single_pk[0]

    target.hybrid = hybrid_value

    if not error and not warning:
        input_data = target._to_dict(format,
                                     max_nesting=max_nesting,
                                     current_nesting=current_nesting)
        if extra_keys:
            for key in extra_keys:
                input_data[key] = extra_keys[key]

        result = target._parse_dict(input_data,
                                    format,
                                    error_on_extra_keys=error_on_extra_keys,
                                    drop_extra_keys=drop_extra_keys)

        if not error_on_extra_keys and drop_extra_keys:
            for key in extra_keys:
                assert key not in result
        elif not error_on_extra_keys and not drop_extra_keys:
            for key in extra_keys:
                assert key in result

        assert are_dicts_equivalent(result, expected_result) is True
    elif not warning:
        with pytest.raises(error):
            input_data = target._to_dict(format,
                                         max_nesting=max_nesting,
                                         current_nesting=current_nesting)
            if extra_keys:
                for key in extra_keys:
                    input_data[key] = extra_keys[key]

            result = target._parse_dict(
                input_data,
                format,
                error_on_extra_keys=error_on_extra_keys,
                drop_extra_keys=drop_extra_keys)
    elif not error:
        with pytest.warns(warning):
            input_data = target._to_dict(format,
                                         max_nesting=max_nesting,
                                         current_nesting=current_nesting)
            if extra_keys:
                for key in extra_keys:
                    input_data[key] = extra_keys[key]

            result = target._parse_dict(
                input_data,
                format,
                error_on_extra_keys=error_on_extra_keys,
                drop_extra_keys=drop_extra_keys)

        assert isinstance(result, dict)
        for key in result:
            assert key in expected_result
            assert expected_result[key] == result[key]
        for key in expected_result:
            assert key in result
            assert result[key] == expected_result[key]

        if not error_on_extra_keys and drop_extra_keys:
            for key in extra_keys:
                assert key not in result
        elif not error_on_extra_keys and not drop_extra_keys:
            for key in extra_keys:
                assert key in result

        assert are_dicts_equivalent(result, expected_result) is True
コード例 #4
0
def test_dump_to_yaml(request, instance_single_pk, instance_postgresql,
                      supports_serialization, hybrid_value, max_nesting,
                      current_nesting, serialize_function, warning, error):
    if supports_serialization:
        target = instance_postgresql[0][0]
    else:
        target = instance_single_pk[0]

    target.hybrid = hybrid_value

    if not error:
        if warning:
            with pytest.warns(warning):
                interim_dict = target._to_dict('yaml',
                                               max_nesting=max_nesting,
                                               current_nesting=current_nesting,
                                               is_dumping=True)
        else:
            interim_dict = target._to_dict('yaml',
                                           max_nesting=max_nesting,
                                           current_nesting=current_nesting,
                                           is_dumping=True)

    if not error and not warning:
        result = target.dump_to_yaml(max_nesting=max_nesting,
                                     current_nesting=current_nesting,
                                     serialize_function=serialize_function)

        print('RESULT:')
        print(result)
        assert isinstance(result, str)

        should_pass = False
        try:
            deserialized_dict = yaml.safe_load(result)
            print('\nDESERIALIZED DICT:')
            print(deserialized_dict)

            assert isinstance(deserialized_dict, dict)
            assert are_dicts_equivalent(interim_dict,
                                        deserialized_dict) is True

        except ConstructorError as raised_error:
            for key in interim_dict:
                if isinstance(interim_dict[key], datetime.timedelta):
                    should_pass = True

            if not should_pass:
                raise raised_error

    elif not warning:
        with pytest.raises(error):
            result = target.dump_to_yaml(max_nesting=max_nesting,
                                         current_nesting=current_nesting,
                                         serialize_function=serialize_function)
            print('THIS IS THE RESULT:')
            print(result)
    elif not error:
        with pytest.warns(warning):
            result = target.dump_to_yaml(max_nesting=max_nesting,
                                         current_nesting=current_nesting,
                                         serialize_function=serialize_function)

        print('RESULT:')
        print(result)
        assert isinstance(result, str)

        should_pass = False
        try:
            deserialized_dict = yaml.safe_load(result)
            print('\nDESERIALIZED DICT:')
            print(deserialized_dict)

            assert isinstance(deserialized_dict, dict)

            assert are_dicts_equivalent(interim_dict,
                                        deserialized_dict) is True
        except ConstructorError as raised_error:
            for key in interim_dict:
                if isinstance(interim_dict[key], datetime.timedelta):
                    should_pass = True

            if not should_pass:
                raise raised_error
コード例 #5
0
def test_dump_to_json(request, instance_single_pk, instance_postgresql,
                      supports_serialization, hybrid_value, max_nesting,
                      current_nesting, serialize_function, warning, error):
    if supports_serialization:
        target = instance_postgresql[0][0]
    else:
        target = instance_single_pk[0]

    target.hybrid = hybrid_value

    if not error:
        if warning:
            with pytest.warns(warning):
                interim_dict = target._to_dict('json',
                                               max_nesting=max_nesting,
                                               current_nesting=current_nesting,
                                               is_dumping=True)
        else:
            interim_dict = target._to_dict('json',
                                           max_nesting=max_nesting,
                                           current_nesting=current_nesting,
                                           is_dumping=True)

    if not error and not warning:
        result = target.dump_to_json(max_nesting=max_nesting,
                                     current_nesting=current_nesting,
                                     serialize_function=serialize_function)

        print('RESULT:')
        print(result)
        assert isinstance(result, str)

        deserialized_dict = json.loads(result)
        print('\nDESERIALIZED DICT:')
        print(deserialized_dict)

        print('\nINTERIM DICT:')
        print(interim_dict)

        assert isinstance(deserialized_dict, dict)

        try:
            assert are_dicts_equivalent(interim_dict,
                                        deserialized_dict) is True
        except AssertionError as error:
            for key in interim_dict:
                if isinstance(interim_dict[key], datetime.timedelta):
                    interim_dict[key] = interim_dict[key].total_seconds()
            try:
                assert are_dicts_equivalent(interim_dict,
                                            deserialized_dict) is True
            except AssertionError:
                raise error

    elif not warning:
        with pytest.raises(error):
            result = target.dump_to_json(max_nesting=max_nesting,
                                         current_nesting=current_nesting,
                                         serialize_function=serialize_function)
    elif not error:
        with pytest.warns(warning):
            result = target.dump_to_json(max_nesting=max_nesting,
                                         current_nesting=current_nesting,
                                         serialize_function=serialize_function)

        print('RESULT:')
        print(result)
        assert isinstance(result, str)

        deserialized_dict = json.loads(result)
        print('\nDESERIALIZED DICT:')
        print(deserialized_dict)

        print('\nINTERIM DICT:')
        print(interim_dict)

        assert isinstance(deserialized_dict, dict)

        try:
            assert are_dicts_equivalent(interim_dict,
                                        deserialized_dict) is True
        except AssertionError as error:
            for key in interim_dict:
                if isinstance(interim_dict[key], datetime.timedelta):
                    interim_dict[key] = interim_dict[key].total_seconds()
            try:
                assert are_dicts_equivalent(interim_dict,
                                            deserialized_dict) is True
            except AssertionError:
                raise error