<commit_before>describe Defile::Attachment do
  let(:post) { Post.new }
  let(:klass) do
    Class.new do
      extend Defile::Attachment

      attr_accessor :image_id, :image_name, :image_size

      attachment :image, type: :image
    end
  end
  let(:instance) { klass.new }

  describe ":name=" do
    it "receives a file, caches it and sets the _id parameter" do
      instance.image = Defile::FileDouble.new("hello")

      expect(Defile.cache.get(instance.image.id).read).to eq("hello")
      expect(Defile.cache.get(instance.image_cache_id).read).to eq("hello")
    end
  end

  describe ":name" do
    it "gets a file from the store" do
      file = Defile.store.upload(Defile::FileDouble.new("hello"))
      instance.image_id = file.id

      expect(instance.image.id).to eq(file.id)
    end
  end

  describe ":name_cache_id" do
    it "doesn't overwrite a cached file" do
      instance.image = Defile::FileDouble.new("hello")
      instance.image_cache_id = "xyz"

      expect(instance.image.read).to eq("hello")
    end
  end

  describe ":name_attachment.store!" do
    it "puts a cached file into the store" do
      instance.image = Defile::FileDouble.new("hello")
      cache = instance.image

      instance.image_attachment.store!

      expect(Defile.store.get(instance.image_id).read).to eq("hello")
      expect(Defile.store.get(instance.image.id).read).to eq("hello")

      expect(instance.image_cache_id).to be_nil
      expect(Defile.cache.get(cache.id).exists?).to be_falsy
    end

    it "does nothing when not cached" do
      file = Defile.store.upload(Defile::FileDouble.new("hello"))
      instance.image_id = file.id

      instance.image_attachment.store!

      expect(Defile.store.get(instance.image_id).read).to eq("hello")
      expect(Defile.store.get(instance.image.id).read).to eq("hello")
    end

    it "overwrites previously stored file" do
      file = Defile.store.upload(Defile::FileDouble.new("hello"))
      instance.image_id = file.id

      instance.image = Defile::FileDouble.new("world")
      cache = instance.image

      instance.image_attachment.store!

      expect(Defile.store.get(instance.image_id).read).to eq("world")
      expect(Defile.store.get(instance.image.id).read).to eq("world")

      expect(instance.image_cache_id).to be_nil
      expect(Defile.cache.get(cache.id).exists?).to be_falsy
      expect(Defile.store.get(file.id).exists?).to be_falsy
    end
  end
end<commit_msg>Rename image->document in spec<commit_after>describe Defile::Attachment do
  let(:post) { Post.new }
  let(:klass) do
    Class.new do
      extend Defile::Attachment

      attr_accessor :document_id, :document_name, :document_size

      attachment :document, type: :document
    end
  end
  let(:instance) { klass.new }

  describe ":name=" do
    it "receives a file, caches it and sets the _id parameter" do
      instance.document = Defile::FileDouble.new("hello")

      expect(Defile.cache.get(instance.document.id).read).to eq("hello")
      expect(Defile.cache.get(instance.document_cache_id).read).to eq("hello")
    end
  end

  describe ":name" do
    it "gets a file from the store" do
      file = Defile.store.upload(Defile::FileDouble.new("hello"))
      instance.document_id = file.id

      expect(instance.document.id).to eq(file.id)
    end
  end

  describe ":name_cache_id" do
    it "doesn't overwrite a cached file" do
      instance.document = Defile::FileDouble.new("hello")
      instance.document_cache_id = "xyz"

      expect(instance.document.read).to eq("hello")
    end
  end

  describe ":name_attachment.store!" do
    it "puts a cached file into the store" do
      instance.document = Defile::FileDouble.new("hello")
      cache = instance.document

      instance.document_attachment.store!

      expect(Defile.store.get(instance.document_id).read).to eq("hello")
      expect(Defile.store.get(instance.document.id).read).to eq("hello")

      expect(instance.document_cache_id).to be_nil
      expect(Defile.cache.get(cache.id).exists?).to be_falsy
    end

    it "does nothing when not cached" do
      file = Defile.store.upload(Defile::FileDouble.new("hello"))
      instance.document_id = file.id

      instance.document_attachment.store!

      expect(Defile.store.get(instance.document_id).read).to eq("hello")
      expect(Defile.store.get(instance.document.id).read).to eq("hello")
    end

    it "overwrites previously stored file" do
      file = Defile.store.upload(Defile::FileDouble.new("hello"))
      instance.document_id = file.id

      instance.document = Defile::FileDouble.new("world")
      cache = instance.document

      instance.document_attachment.store!

      expect(Defile.store.get(instance.document_id).read).to eq("world")
      expect(Defile.store.get(instance.document.id).read).to eq("world")

      expect(instance.document_cache_id).to be_nil
      expect(Defile.cache.get(cache.id).exists?).to be_falsy
      expect(Defile.store.get(file.id).exists?).to be_falsy
    end
  end
end
{'fraction_non_alphanumeric': 0.12876220849113015, 'fraction_numerical': 0.0, 'mean_word_length': 3.3072961373390557, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 4, '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': '7462483', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1586, 'n_words': 310}