adds passing tests
This commit is contained in:
+60
-54
@@ -2,72 +2,78 @@
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User;
|
||||
use App\Models\Holding;
|
||||
use App\Models\Portfolio;
|
||||
use App\Models\Transaction;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class TransactionsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* A basic test example.
|
||||
*/
|
||||
public function test_can_create_a_transaction(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user);
|
||||
$this->actingAs($user = User::factory()->create());
|
||||
|
||||
$transactions = Transaction::factory()->create();
|
||||
$transaction = Transaction::factory()->create();
|
||||
|
||||
$this->assertNotNull($transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function test_sales_calculate_cost_basis(): void
|
||||
{
|
||||
$this->actingAs($user = User::factory()->create());
|
||||
|
||||
Transaction::factory(5)->buy()->symbol('AAPL')->create();
|
||||
|
||||
$transaction = Transaction::factory()->sell()->symbol('AAPL')->create();
|
||||
|
||||
$this->assertNotNull($transaction->cost_basis);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function test_purchases_dont_have_sale_price(): void
|
||||
{
|
||||
$this->actingAs($user = User::factory()->create());
|
||||
|
||||
$transaction = Transaction::factory()->buy()->create();
|
||||
|
||||
$this->assertNull($transaction->sale_price);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function test_transaction_synced_to_holding(): void
|
||||
{
|
||||
$this->actingAs($user = User::factory()->create());
|
||||
|
||||
$portfolio = Portfolio::factory()->create();
|
||||
|
||||
Transaction::factory(5)->buy()->portfolio($portfolio->id)->symbol('AAPL')->create();
|
||||
$transaction = Transaction::factory()->sell()->portfolio($portfolio->id)->symbol('AAPL')->create();
|
||||
|
||||
$this->assertDatabaseHas('holdings', [
|
||||
'portfolio_id' => $portfolio->id,
|
||||
'symbol' => 'AAPL',
|
||||
'quantity' => 4
|
||||
]);
|
||||
|
||||
$holding = Holding::where([
|
||||
'portfolio_id' => $portfolio->id,
|
||||
'symbol' => 'AAPL'
|
||||
])->first();
|
||||
|
||||
$this->assertEqualsWithDelta(
|
||||
$holding->realized_gain_dollars,
|
||||
$transaction->sale_price - $transaction->cost_basis,
|
||||
0.01
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// static::saving(function ($transaction) {
|
||||
|
||||
// if ($transaction->transaction_type == 'SELL') {
|
||||
|
||||
// $transaction->ensureCostBasisIsAddedToSale();
|
||||
// }
|
||||
// });
|
||||
|
||||
// static::saved(function ($transaction) {
|
||||
|
||||
// $transaction->syncToHolding();
|
||||
|
||||
// $transaction->refreshMarketData();
|
||||
|
||||
// cache()->tags(['metrics', $transaction->portfolio_id])->flush();
|
||||
// });
|
||||
|
||||
// public function update()
|
||||
// {
|
||||
|
||||
// $this->transaction->update($this->validate());
|
||||
// // $this->transaction->owner_id = auth()->user()->id;
|
||||
// $this->transaction->save();
|
||||
|
||||
// $this->success(__('Transaction updated'));
|
||||
|
||||
// $this->dispatch('toggle-manage-transaction');
|
||||
// $this->dispatch('transaction-updated');
|
||||
// }
|
||||
|
||||
// public function save()
|
||||
// {
|
||||
// $validated = $this->validate();
|
||||
|
||||
// if (!isset($this->portfolio)) {
|
||||
// $this->portfolio = Portfolio::find($this->portfolio_id);
|
||||
// }
|
||||
|
||||
// $transaction = $this->portfolio->transactions()->create($validated);
|
||||
// $transaction->save();
|
||||
|
||||
// $this->dispatch('transaction-saved');
|
||||
|
||||
// $this->success(__('Transaction created'), redirectTo: route('holding.show', ['portfolio' => $this->portfolio->id, 'symbol' => $this->symbol]));
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user