<commit_before>require "test_helper"
require "roar/json/json_api"
require "json"
require "jsonapi/representer"

class JSONAPIFieldsetsTest < Minitest::Spec
  Article = Struct.new(:id, :title, :summary, :comments, :author)
  Comment = Struct.new(:id, :body, :good)
  Author = Struct.new(:id, :name, :email)

  let (:comments) { [Comment.new("c:1", "Cool!", true), Comment.new("c:2", "Nah", false)] }

  describe "Single Resource Object With Options" do
    class DocumentSingleResourceObjectDecorator < Roar::Decorator
      include Roar::JSON::JSONAPI
      type :articles

      property :id
      property :title
      property :summary

      has_many :comments do
        type :comments

        property :id
        property :body
        property :good
      end

      has_one :author do
        type :author

        property :id
        property :name
        property :email
      end
    end

    let (:article) { Article.new(1, "My Article", "An interesting read.", comments, Author.new("a:1", "Celso")) }

    it "includes scalars" do
      DocumentSingleResourceObjectDecorator.new(article).
        to_json(include: [:title, :id]).
        must_equal( {
          "data" => {
            "type" => "articles",
            "id" => "1",
            "attributes" => {
              "title" => "My Article"
            }
          }
        }.to_json )
    end

    it "includes compound objects" do
      DocumentSingleResourceObjectDecorator.new(article).
        to_hash(
          include:  [:id, :title, :included],
          included: {include: [:comments]}).
        must_equal Hash[{
          :data=>
            {:type=>"articles",
             :id=>"1",
             :attributes=>{"title"=>"My Article"},
             :included=>
              [{:type=>"comments",
                :id=>"c:1",
                :attributes=>{"body"=>"Cool!", "good"=>true}},
               {:type=>"comments",
                :id=>"c:2",
                :attributes=>{"body"=>"Nah", "good"=>false}}
              ]
            }
        }]
        # must_equal document.to_json
    end

    it "includes other compound objects" do
      DocumentSingleResourceObjectDecorator.new(article).
        to_hash(
          include:  [:id, :title, :included],
          included: {include: [:author]}).
        must_equal Hash[{
          :data=>
            {:type=>"articles",
             :id=>"1",
             :attributes=>{"title"=>"My Article"},
             :included=>
              [{:type=>"author", :id=>"a:1", :attributes=>{"name"=>"Celso"}}]
            }
        }]
        # must_equal document.to_json
    end
  end

  describe "Collection Resources With Options" do
    class CollectionResourceObjectDecorator < Roar::Decorator
      include Roar::JSON::JSONAPI
      type :articles

      property :id
      property :title
      property :summary
    end

    let(:document) {
      {
        "data" => [
          {
            "type" => "articles",
            "id" => "1",
            "attributes" => {
              "title" => "My Article"
            }
          },
          {
            "type" => "articles",
            "id" => "2",
            "attributes" => {
              "title" => "My Other Article"
            }
          }
        ]<commit_msg>test for rendering collection in JSON API with include:.<commit_after>require "test_helper"
require "roar/json/json_api"
require "json"
require "jsonapi/representer"

class JSONAPIFieldsetsTest < Minitest::Spec
  Article = Struct.new(:id, :title, :summary, :comments, :author)
  Comment = Struct.new(:id, :body, :good)
  Author = Struct.new(:id, :name, :email)

  let (:comments) { [Comment.new("c:1", "Cool!", true), Comment.new("c:2", "Nah", false)] }

  describe "Single Resource Object With Options" do
    class DocumentSingleResourceObjectDecorator < Roar::Decorator
      include Roar::JSON::JSONAPI
      type :articles

      property :id
      property :title
      property :summary

      has_many :comments do
        type :comments

        property :id
        property :body
        property :good
      end

      has_one :author do
        type :author

        property :id
        property :name
        property :email
      end
    end

    let (:article) { Article.new(1, "My Article", "An interesting read.", comments, Author.new("a:1", "Celso")) }

    it "includes scalars" do
      DocumentSingleResourceObjectDecorator.new(article).
        to_json(include: [:title, :id]).
        must_equal( {
          "data" => {
            "type" => "articles",
            "id" => "1",
            "attributes" => {
              "title" => "My Article"
            }
          }
        }.to_json )
    end

    it "includes compound objects" do
      DocumentSingleResourceObjectDecorator.new(article).
        to_hash(
          include:  [:id, :title, :included],
          included: {include: [:comments]}).
        must_equal Hash[{
          :data=>
            {:type=>"articles",
             :id=>"1",
             :attributes=>{"title"=>"My Article"},
             :included=>
              [{:type=>"comments",
                :id=>"c:1",
                :attributes=>{"body"=>"Cool!", "good"=>true}},
               {:type=>"comments",
                :id=>"c:2",
                :attributes=>{"body"=>"Nah", "good"=>false}}
              ]
            }
        }]
        # must_equal document.to_json
    end

    it "includes other compound objects" do
      DocumentSingleResourceObjectDecorator.new(article).
        to_hash(
          include:  [:id, :title, :included],
          included: {include: [:author]}).
        must_equal Hash[{
          :data=>
            {:type=>"articles",
             :id=>"1",
             :attributes=>{"title"=>"My Article"},
             :included=>
              [{:type=>"author", :id=>"a:1", :attributes=>{"name"=>"Celso"}}]
            }
        }]
        # must_equal document.to_json
    end

    describe "collection" do
      it "supports :includes" do
        DocumentSingleResourceObjectDecorator.for_collection.new([article]).
          to_hash(
            include:  [:id, :title, :included],
            included: {include: [:author]}).
          must_equal Hash[{
            :data=>[
              {:type=>"articles",
               :id=>"1",
               :attributes=>{"title"=>"My Article"},
              }],
            :included=>
              [{:type=>"author", :id=>"a:1", :attributes=>{"name"=>"Celso"}}]
          }]
      end
    end
  end

  describe "Collection Resources With Options" do
    class CollectionResourceObjectDecorator < Roar::Decorator
      include Roar::JSON::JSONAPI
      type :articles

      property :id
      property :title
      property :summary
    end

    let(:document) {
      {
        "data" => [
          {
            "type" => "articles",
            "id" => "1",
            "attributes" => {
              "title" => "My Article"
            }
          },
          {
            "type" => "articles",
            "id" => "2",
            "attributes" => {
              "title" => "My Other Article"
            }
          }
        ]
{'fraction_non_alphanumeric': 0.14770347619712412, 'fraction_numerical': 0.003629764065335753, 'mean_word_length': 1.7500959692898272, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 93, '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': '7533527', 'n_tokens_mistral': 2128, 'n_tokens_neox': 2039, 'n_words': 459}