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

class JsonapiPostTest < MiniTest::Spec
  Author = Struct.new(:id, :email, :name) do
    def self.find_by(options)
      AuthorNine if options[:id].to_s=="9"
    end
  end
  AuthorNine = Author.new(9, "<EMAIL>")

  Article = Struct.new(:id, :title, :author, :comments)

  Comment = Struct.new(:id)



  class CrudArticleCreateDecorator < Roar::Decorator
    include Roar::JSON::JSONAPI
    type :articles

    property :id
    property :title

    link :self do
      "http://api/articles/#{id}"
    end

include Representable::Debug::Representable
    property :author, class: Author, populator: ::Representable::FindOrInstantiate, type: "people" do
      include Roar::JSON
      include Roar::Hypermedia

      property :id
      property :name
    end

    collection :comments, class: Comment, populator: ::Representable::FindOrInstantiate do # FIXME: type?
      property :id
    end
  end

  describe "Parse" do
    let(:post_article) {
      {
        "data": {
          "type": "articles",
          "attributes": {
            "title": "Ember Hamster",
          },
          # that does do `photo.photographer= Photographer.find(9)`
          "relationships": {
            "author": {
              "data": { "type": "people", "id": "9", "name": "Celsito" } # FIXME: what should happen if i add `"name": "Celsito"` here? should that be read or not?
            }
          }
        }
      }
    }

    subject { CrudArticleCreateDecorator.new(Article.new).from_json(post_article.to_json) }

    it { subject.title.must_equal "Ember Hamster"  }
    it do
      subject.author.id.must_equal "9"
      subject.author.email.must_equal "<EMAIL>"
      # subject.author.name.must_equal nil
    end
  end

  describe "Render" do
    let(:rendered_post_photos) {<commit_msg>test parsing of collection via "relationships". works.<commit_after>require "test_helper"
require "roar/json/json_api"
require "json"

class JsonapiPostTest < MiniTest::Spec
  Author = Struct.new(:id, :email, :name) do
    def self.find_by(options)
      AuthorNine if options[:id].to_s=="9"
    end
  end
  AuthorNine = Author.new(9, "<EMAIL>")

  Article = Struct.new(:id, :title, :author, :comments)

  Comment = Struct.new(:id) do
    def self.find_by(options)
      new
    end
  end



  class CrudArticleCreateDecorator < Roar::Decorator
    include Roar::JSON::JSONAPI
    type :articles

    property :id
    property :title

    link :self do
      "http://api/articles/#{id}"
    end

include Representable::Debug::Representable
    property :author, class: Author, populator: ::Representable::FindOrInstantiate, type: "people" do
      include Roar::JSON
      include Roar::Hypermedia

      property :id
      property :name
    end

    collection :comments, class: Comment, populator: ::Representable::FindOrInstantiate do # FIXME: type?
      property :id
    end
  end

  describe "Parse" do
    let(:post_article) {
      {
        "data": {
          "type": "articles",
          "attributes": {
            "title": "<NAME>",
          },
          # that does do `photo.photographer= Photographer.find(9)`
          "relationships": {
            "author": {
              "data": { "type": "people", "id": "9", "name": "Celsito" } # FIXME: what should happen if i add `"name": "Celsito"` here? should that be read or not?
            },
            "comments": {
              "data": [
                { "type": "comment", "id": "2" },
                { "type": "comment", "id": "3" },
              ]
            }

          }
        }
      }
    }

    subject { CrudArticleCreateDecorator.new(Article.new(nil, nil, nil, [])).from_json(post_article.to_json) }

    it { subject.title.must_equal "<NAME>"  }
    it do
      subject.author.id.must_equal "9"
      subject.author.email.must_equal "<EMAIL>"
      # subject.author.name.must_equal nil

      subject.comments.must_equal [Comment.new("2"), Comment.new("3")]
    end
  end

  describe "Render" do
    let(:rendered_post_photos) {
{'fraction_non_alphanumeric': 0.13379073756432247, 'fraction_numerical': 0.003430531732418525, 'mean_word_length': 2.5008576329331045, 'pattern_counts': {'":': 28, '<': 13, '<?xml version=': 0, '>': 9, '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': '5417546', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1277, 'n_words': 308}