<commit_before>#!/usr/bin/env escript
%% -*- erlang -*-
%%! -pa ./src/couchdb -pa ./src/mochiweb -sasl errlog_type false -noshell

% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

%% XXX: Figure out how to -include("couch_db.hrl")
-record(doc, {id= <<"">>, revs={0, []}, body={[]},
            atts=[], deleted=false, meta=[]}).
-record(att, {name, type, att_len, disk_len, md5= <<>>, revpos=0, data,
            encoding=identity}).

default_config() ->
    test_util:build_file("etc/couchdb/default_dev.ini").

main(_) ->
    test_util:init_code_path(),
    etap:plan(26),
    case (catch test()) of
        ok ->
            etap:end_tests();
        Other ->
            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
            etap:bail()
    end,
    ok.

test() ->
    couch_config:start_link([default_config()]),
    couch_config:set("attachments", "compression_level", "0"),
    ok = test_from_json_success(),
    ok = test_from_json_errors(),
    ok.

test_from_json_success() ->
    Cases = [
        {
            {[]},
            #doc{},
            "Return an empty document for an empty JSON object."
        },
        {
            {[{<<"_id">>, <<"zing!">>}]},
            #doc{id= <<"zing!">>},
            "Parses document ids."
        },
        {
            {[{<<"_id">>, <<"_design/foo">>}]},
            #doc{id= <<"_design/foo">>},
            "_design/document ids."
        },
        {
            {[{<<"_id">>, <<"_local/bam">>}]},
            #doc{id= <<"_local/bam">>},
            "_local/document ids."
        },
        {
            {[{<<"_rev">>, <<"4-230234">>}]},
            #doc{revs={4, [<<"230234">>]}},
            "_rev stored in revs."
        },
        {
            {[{<<"soap">>, 35}]},
            #doc{body={[{<<"soap">>, 35}]}},
            "Non underscore prefixed fields stored in body."
        },
        {
            {[{<<"_attachments">>, {[
                {<<"my_attachment.fu">>, {[
                    {<<"stub">>, true},
                    {<<"content_type">>, <<"application/awesome">>},
                    {<<"length">>, 45}
                ]}},
                {<<"noahs_private_key.gpg">>, {[
                    {<<"data">>, <<"<KEY>">>},
                    {<<"content_type">>, <<"application/pgp-signature">>}
                ]}}
            ]}}]},
            #doc{atts=[
                #att{
                    name = <<"my_attachment.fu">>,
                    data = stub,
                    type = <<"application/awesome">>,
                    att_len = 45,
                    disk_len = 45
                },
                #att{
                    name = <<"noahs_private_key.gpg">>,
                    data = <<"I have a pet fish!">>,
                    type = <<"application/pgp-signature">>,
                    att_len = 18,
                    disk_len = 18
                }
            ]},
            "Attachments are parsed correctly."
        },
        {
            {[{<<"_deleted">>, true}]},
            #doc{deleted=true},
            "_deleted controls the deleted field."
        },
        {<commit_msg>fix test case for revpos-checking commit<commit_after>#!/usr/bin/env escript
%% -*- erlang -*-
%%! -pa ./src/couchdb -pa ./src/mochiweb -sasl errlog_type false -noshell

% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

%% XXX: Figure out how to -include("couch_db.hrl")
-record(doc, {id= <<"">>, revs={0, []}, body={[]},
            atts=[], deleted=false, meta=[]}).
-record(att, {name, type, att_len, disk_len, md5= <<>>, revpos=0, data,
            encoding=identity}).

default_config() ->
    test_util:build_file("etc/couchdb/default_dev.ini").

main(_) ->
    test_util:init_code_path(),
    etap:plan(26),
    case (catch test()) of
        ok ->
            etap:end_tests();
        Other ->
            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
            etap:bail()
    end,
    ok.

test() ->
    couch_config:start_link([default_config()]),
    couch_config:set("attachments", "compression_level", "0"),
    ok = test_from_json_success(),
    ok = test_from_json_errors(),
    ok.

test_from_json_success() ->
    Cases = [
        {
            {[]},
            #doc{},
            "Return an empty document for an empty JSON object."
        },
        {
            {[{<<"_id">>, <<"zing!">>}]},
            #doc{id= <<"zing!">>},
            "Parses document ids."
        },
        {
            {[{<<"_id">>, <<"_design/foo">>}]},
            #doc{id= <<"_design/foo">>},
            "_design/document ids."
        },
        {
            {[{<<"_id">>, <<"_local/bam">>}]},
            #doc{id= <<"_local/bam">>},
            "_local/document ids."
        },
        {
            {[{<<"_rev">>, <<"4-230234">>}]},
            #doc{revs={4, [<<"230234">>]}},
            "_rev stored in revs."
        },
        {
            {[{<<"soap">>, 35}]},
            #doc{body={[{<<"soap">>, 35}]}},
            "Non underscore prefixed fields stored in body."
        },
        {
            {[{<<"_attachments">>, {[
                {<<"my_attachment.fu">>, {[
                    {<<"stub">>, true},
                    {<<"content_type">>, <<"application/awesome">>},
                    {<<"length">>, 45}
                ]}},
                {<<"noahs_private_key.gpg">>, {[
                    {<<"data">>, <<"<KEY>">>},
                    {<<"content_type">>, <<"application/pgp-signature">>}
                ]}}
            ]}}]},
            #doc{atts=[
                #att{
                    name = <<"my_attachment.fu">>,
                    data = stub,
                    type = <<"application/awesome">>,
                    att_len = 45,
                    disk_len = 45,
                    revpos = nil
                },
                #att{
                    name = <<"noahs_private_key.gpg">>,
                    data = <<"I have a pet fish!">>,
                    type = <<"application/pgp-signature">>,
                    att_len = 18,
                    disk_len = 18,
                    revpos = 0
                }
            ]},
            "Attachments are parsed correctly."
        },
        {
            {[{<<"_deleted">>, true}]},
            #doc{deleted=true},
            "_deleted controls the deleted field."
        },
        {
{'fraction_non_alphanumeric': 0.1800734993875051, 'fraction_numerical': 0.010480468218320403, 'mean_word_length': 1.5973842347119123, 'pattern_counts': {'":': 0, '<': 137, '<?xml version=': 0, '>': 149, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '336354', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2194, 'n_words': 495}