<commit_before>require "spec_helper"

require "support/have_persisted_matcher"
require "support/mapper_setup"
require "support/sequel_persistence_setup"
require "support/seed_data_setup"
require "sequel_mapper"

RSpec.describe "Graph persistence" do
  include_context "mapper setup"
  include_context "sequel persistence setup"
  include_context "seed data setup"

  subject(:mapper) { mappers.fetch(:users) }

  let(:user) {
    mapper.where(id: "users/1").first
  }

  context "without accessing associations" do
    let(:modified_email) { "<EMAIL>" }

    it "saves the root object" do
      user.email = modified_email
      mapper.save(user)

      expect(datastore).to have_persisted(
        :users,
        hash_including(
          id: "users/1",
          email: modified_email,
        )
      )
    end

    it "doesn't send associated objects to the database as columns" do
      user.email = modified_email
      mapper.save(user)

      expect(datastore).not_to have_persisted(
        :users,
        hash_including(
          posts: anything,
        )
      )
    end

    # TODO move to a dirty tracking spec?
    context "when mutating entity fields in place" do
      it "saves the object" do
        user.email << "MUTATED"

        mapper.save(user)

        expect(datastore).to have_persisted(
          :users,
          hash_including(
            id: "users/1",
            email: /MUTATED$/,
          )
        )
      end
    end
  end

  context "modify shallow has many associated object" do
    let(:post) { user.posts.first }
    let(:modified_post_body) { "modified ur body" }

    it "saves the associated object" do
      post.body = modified_post_body
      mapper.save(user)

      expect(datastore).to have_persisted(
        :posts,
        hash_including(
          id: post.id,
          subject: post.subject,
          author_id: user.id,
          body: modified_post_body,
        )
      )
    end
  end

  context "modify deeply nested has many associated object" do
    let(:comment) {
      user.posts.first.comments.first
    }

    let(:modified_comment_body) { "body moving, body moving" }

    it "saves the associated object" do
      comment.body = modified_comment_body
      mapper.save(user)

      expect(datastore).to have_persisted(
        :comments,
        hash_including(
          {
            id: "comments/1",
            post_id: "posts/1",
            commenter_id: "users/1",
            body: modified_comment_body,
          }
        )
      )
    end
  end

  context "modify the foreign_key of an object" do
    let(:original_author) { user }
    let(:new_author)      { mapper.where(id: "users/2").first }
    let(:post)            { original_author.posts.first }

    it "persists the change in ownership" do
      post.author = new_author
      mapper.save(user)

      expect(datastore).to have_persisted(
        :posts,
        hash_including(
          id: post.id,
          author_id: new_author.id,
        )
      )
    end

    xit "removes the object from the original graph" do
      post.author = new_author
      mapper.save(user)

      expect(original_author.posts.to_a.map(&:id))
        .not_to include("posts/1")
    end

    it "adds the object to the appropriate graph" do
      post.author = new_author
      mapper.save(user)

      expect(new_author.posts.to_a.map(&:id))
        .to include("posts/1")
    end
  end

  xcontext "add a node to a has many assocation" do
    let(:new_post_attrs) {<commit_msg>Remove tests around foreign key mutation<commit_after>require "spec_helper"

require "support/have_persisted_matcher"
require "support/mapper_setup"
require "support/sequel_persistence_setup"
require "support/seed_data_setup"
require "sequel_mapper"

RSpec.describe "Graph persistence" do
  include_context "mapper setup"
  include_context "sequel persistence setup"
  include_context "seed data setup"

  subject(:mapper) { mappers.fetch(:users) }

  let(:user) {
    mapper.where(id: "users/1").first
  }

  context "without accessing associations" do
    let(:modified_email) { "<EMAIL>" }

    it "saves the root object" do
      user.email = modified_email
      mapper.save(user)

      expect(datastore).to have_persisted(
        :users,
        hash_including(
          id: "users/1",
          email: modified_email,
        )
      )
    end

    it "doesn't send associated objects to the database as columns" do
      user.email = modified_email
      mapper.save(user)

      expect(datastore).not_to have_persisted(
        :users,
        hash_including(
          posts: anything,
        )
      )
    end

    # TODO move to a dirty tracking spec?
    context "when mutating entity fields in place" do
      it "saves the object" do
        user.email << "MUTATED"

        mapper.save(user)

        expect(datastore).to have_persisted(
          :users,
          hash_including(
            id: "users/1",
            email: /MUTATED$/,
          )
        )
      end
    end
  end

  context "modify shallow has many associated object" do
    let(:post) { user.posts.first }
    let(:modified_post_body) { "modified ur body" }

    it "saves the associated object" do
      post.body = modified_post_body
      mapper.save(user)

      expect(datastore).to have_persisted(
        :posts,
        hash_including(
          id: post.id,
          subject: post.subject,
          author_id: user.id,
          body: modified_post_body,
        )
      )
    end
  end

  context "modify deeply nested has many associated object" do
    let(:comment) {
      user.posts.first.comments.first
    }

    let(:modified_comment_body) { "body moving, body moving" }

    it "saves the associated object" do
      comment.body = modified_comment_body
      mapper.save(user)

      expect(datastore).to have_persisted(
        :comments,
        hash_including(
          {
            id: "comments/1",
            post_id: "posts/1",
            commenter_id: "users/1",
            body: modified_comment_body,
          }
        )
      )
    end
  end

  xcontext "add a node to a has many assocation" do
    let(:new_post_attrs) {
{'fraction_non_alphanumeric': 0.09257457846952011, 'fraction_numerical': 0.0024319066147859923, 'mean_word_length': 2.17172236503856, 'pattern_counts': {'":': 0, '<': 9, '<?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': '1982582', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1870, 'n_words': 470}