You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: purchase_affair_test.rb
    path of file: ./repos/ekylibre/test/models
    
    the code of the file until where you have to start completion: # = Informations
#
# == License
#
# Ekylibre - Simple agricultural ERP
# Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon
# Copyright (C) 2010-2012 Brice Texier
# Copyright (C) 2012-2014 Brice Texier, David Joulin
# Copyright (C) 2015-2023 Ekylibre SAS
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# bu
    
  def new_valid_purchases_invoice
    supplier = entities(:entities_005)
    journal = Journal.find_by(nature: :purchases)
    nature = PurchaseNature.find_or_initialize_by(
      journal: journal,
    )
    nature.name ||= 'Purchases baby!'
    nature.save!
    items = (0..4).to_a.map do |index|
      variant = ProductNatureVariant.where(category: ProductNatureCategory.where(purchasable: true)).offset(index).first
      PurchaseItem.new(
        conditioning_quantity: 1 + rand(20),
        unit_pretax_amount: 10 + (100 * rand).round(2),
        variant: variant,
        conditioning_unit: variant.guess_conditioning[:unit],
        tax: Tax.all.sample
      )
    end
    purchase = Purchase.create!(supplier: supplier, nature: nature, type: 'PurchaseInvoice', items: items, invoiced_at: DateTime.new(2018, 1, 1))
    assert purchase.amount > 0, "Purchase amount should be greater than 0. Got: #{purchase.amount.inspect}"
    purchase.reload
    assert purchase.affair, 'An affair should be present after invoicing'
    assert purchase.journal_entry, 'A journal entry should exists after purchase invoicing'
    assert_equal purchase.affair.credit, purchase.amount, 'Purchase amount should match exactly affair debit'
    assert purchase.affair.unbalanced?,
           "Affair should not be balanced:\n" +
           purchase.affair.attributes.sort_by(&:first).map { |k, v| " - #{k}: #{v}" }.join("\n")
    assert purchase.affair.letterable_journal_entry_items.any?,
           "Affair should have letterable journal entry items:\n" +
           purchase.affair.deals.map { |d| " - #{d.class.name}: #{d.journal_entry.inspect}" }.join("\n")
    assert purchase.affair.journal_entry_items_unbalanced?,
           "Journal entry items should be unbalanced:\n" +
           purchase.affair.letterable_journal_entry_items.map { |i| " - #{i.account_number.ljust(14)} | #{i.debit.to_s.rjust(10)} | #{i.credit.to_s.rjust(10)}" }.join("\n")
    assert !purchase.affair.multi_thirds?
    assert !purchase.affair.journal_entry_items_already_lettered?

    # REVIEW: This should be confirmed by someone.
    # Test changed by @aquaj because it seems to be the desired behaviour
    # after @lcoq's modifications in code.
    # Can @burisu or @ionosphere confirm ?
    assert purchase.affair.letterable?
    purchase
  end