<commit_before>import functools
import itertools
import json
import operator
from copy import deepcopy
from unittest import mock

import pytest

from jsonref import (
    JsonRef,
    JsonRefError,
    dump,
    dumps,
    jsonloader,
    load,
    loads,
    replace_refs,
)
from proxytypes import CallbackProxy, LazyProxy, Proxy, notproxied


def cmp(a, b):
    return (a > b) - (a < b)


@pytest.fixture(
    params=[{"lazy_load": True}, {"lazy_load": False}, {"proxies": False}],
    ids=["lazy_load", "no lazy_load", "no proxies"],
)
def parametrized_replace_refs(request):
    return functools.partial(replace_refs, **request.param)


class TestJsonRef(object):
    def test_non_ref_object_throws_error(self):
        with pytest.raises(ValueError):
            JsonRef({"ref": "aoeu"})

    def test_non_string_is_not_ref(self, parametrized_replace_refs):
        json = {"$ref": [1]}
        assert parametrized_replace_refs(json) == json

    def test_local_object_ref(self, parametrized_replace_refs):
        json = {"a": 5, "b": {"$ref": "#/a"}}
        assert parametrized_replace_refs(json)["b"] == json["a"]

    def test_local_array_ref(self, parametrized_replace_refs):
        json = [10, {"$ref": "#/0"}]
        assert parametrized_replace_refs(json)[1] == json[0]

    def test_local_mixed_ref(self, parametrized_replace_refs):
        json = {"a": [5, 15], "b": {"$ref": "#/a/1"}}
        assert parametrized_replace_refs(json)["b"] == json["a"][1]

    def test_local_escaped_ref(self, parametrized_replace_refs):
        json = {"a/~a": ["resolved"], "b": {"$ref": "#/a~1~0a"}}
        assert parametrized_replace_refs(json)["b"] == json["a/~a"]

    def test_local_nonexistent_ref(self):
        json = {
            "data": [1, 2],
            "a": {"$ref": "#/x"},
            "b": {"$ref": "#/0"},
            "c": {"$ref": "#/data/3"},
            "d": {"$ref": "#/data/b"},
        }
        result = replace_refs(json)
        for key in "abcd":
            with pytest.raises(JsonRefError):
                result[key].__subject__

    def test_actual_references_not_copies(self):
        json = {
            "a": ["foobar"],
            "b": {"$ref": "#/a"},
            "c": {"$ref": "#/a"},
            "d": {"$ref": "#/c"},
        }
        result = replace_refs(json)
        assert result["b"].__subject__ is result["a"]
        assert result["c"].__subject__ is result["a"]
        assert result["d"].__subject__ is result["a"]

    def test_lazy_load(self):
        json = {
            "a": {"$ref": "#/fake"},
        }
        # No errors should be raised when we replace the references
        result = replace_refs(json, lazy_load=True)
        assert result["a"].__reference__ == json["a"]
        # The error should happen when we access the attribute
        with pytest.raises(JsonRefError):
            result["a"].__subject__

    def test_no_lazy_load(self):
        json = {
            "a": {"$ref": "#/fake"},
        }
        # Error should raise straight away without lazy loading
        with pytest.raises(JsonRefError):
            result = replace_refs(json, lazy_load=False)

    def test_no_lazy_load_recursive(self):
        json = {
            "a": {"1": {"$ref": "#/b"}},
            "b": {"$ref": "#/a"},
        }
        # If resolution happens too early, the recursion won't work
        # Make sure we don't break recursion when we aren't being lazy
        result = replace_refs(json, lazy_load=False)<commit_msg>failing test for extra attributes<commit_after>import functools
import itertools
import json
import operator
from copy import deepcopy
from unittest import mock

import pytest

from jsonref import (
    JsonRef,
    JsonRefError,
    dump,
    dumps,
    jsonloader,
    load,
    loads,
    replace_refs,
)
from proxytypes import CallbackProxy, LazyProxy, Proxy, notproxied


def cmp(a, b):
    return (a > b) - (a < b)


@pytest.fixture(
    params=[{"lazy_load": True}, {"lazy_load": False}, {"proxies": False}],
    ids=["lazy_load", "no lazy_load", "no proxies"],
)
def parametrized_replace_refs(request):
    return functools.partial(replace_refs, **request.param)


class TestJsonRef(object):
    def test_non_ref_object_throws_error(self):
        with pytest.raises(ValueError):
            JsonRef({"ref": "aoeu"})

    def test_non_string_is_not_ref(self, parametrized_replace_refs):
        json = {"$ref": [1]}
        assert parametrized_replace_refs(json) == json

    def test_local_object_ref(self, parametrized_replace_refs):
        json = {"a": 5, "b": {"$ref": "#/a"}}
        assert parametrized_replace_refs(json)["b"] == json["a"]

    def test_local_array_ref(self, parametrized_replace_refs):
        json = [10, {"$ref": "#/0"}]
        assert parametrized_replace_refs(json)[1] == json[0]

    def test_local_mixed_ref(self, parametrized_replace_refs):
        json = {"a": [5, 15], "b": {"$ref": "#/a/1"}}
        assert parametrized_replace_refs(json)["b"] == json["a"][1]

    def test_local_escaped_ref(self, parametrized_replace_refs):
        json = {"a/~a": ["resolved"], "b": {"$ref": "#/a~1~0a"}}
        assert parametrized_replace_refs(json)["b"] == json["a/~a"]

    def test_local_nonexistent_ref(self):
        json = {
            "data": [1, 2],
            "a": {"$ref": "#/x"},
            "b": {"$ref": "#/0"},
            "c": {"$ref": "#/data/3"},
            "d": {"$ref": "#/data/b"},
        }
        result = replace_refs(json)
        for key in "abcd":
            with pytest.raises(JsonRefError):
                result[key].__subject__

    def test_actual_references_not_copies(self):
        json = {
            "a": ["foobar"],
            "b": {"$ref": "#/a"},
            "c": {"$ref": "#/a"},
            "d": {"$ref": "#/c"},
        }
        result = replace_refs(json)
        assert result["b"].__subject__ is result["a"]
        assert result["c"].__subject__ is result["a"]
        assert result["d"].__subject__ is result["a"]

    def test_ignored_sibling_attributes(self):
        json = {
            "a": {"type": "object", "properties": {"foo": {"type": "string"}}},
            "b": {"extra": "foobar", "$ref": "#/a"},
        }
        result = replace_refs(json)
        assert result["b"].__subject__ is {
            "extra": "foobar",
            "type": "object",
            "properties": {"foo": {"type": "string"}},
        }

    def test_lazy_load(self):
        json = {
            "a": {"$ref": "#/fake"},
        }
        # No errors should be raised when we replace the references
        result = replace_refs(json, lazy_load=True)
        assert result["a"].__reference__ == json["a"]
        # The error should happen when we access the attribute
        with pytest.raises(JsonRefError):
            result["a"].__subject__

    def test_no_lazy_load(self):
        json = {
            "a": {"$ref": "#/fake"},
        }
        # Error should raise straight away without lazy loading
        with pytest.raises(JsonRefError):
            result = replace_refs(json, lazy_load=False)

    def test_no_lazy_load_recursive(self):
        json = {
            "a": {"1": {"$ref": "#/b"}},
            "b": {"$ref": "#/a"},
        }
        # If resolution happens too early, the recursion won't work
        # Make sure we don't break recursion when we aren't being lazy
        result = replace_refs(json, lazy_load=False)
{'fraction_non_alphanumeric': 0.162574203993524, 'fraction_numerical': 0.005126821370750135, 'mean_word_length': 2.5639423076923076, 'pattern_counts': {'":': 95, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4636048', 'n_tokens_mistral': 2522, 'n_tokens_neox': 2388, 'n_words': 563}