fix: quantity validation should not count current transaction
This commit is contained in:
@@ -114,6 +114,35 @@ class TransactionsTest extends TestCase
|
||||
->assertJsonValidationErrors(['symbol']);
|
||||
}
|
||||
|
||||
public function test_cannot_sell_more_than_owned()
|
||||
{
|
||||
Artisan::call('db:seed', [
|
||||
'--class' => CurrencySeeder::class,
|
||||
'--force' => true,
|
||||
]);
|
||||
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$portfolio = Portfolio::factory()->create();
|
||||
|
||||
Transaction::factory(5)->buy()->lastYear()->portfolio($portfolio->id)->symbol('AAPL')->create();
|
||||
|
||||
$data = [
|
||||
'symbol' => 'AAPL',
|
||||
'portfolio_id' => $this->portfolio->id,
|
||||
'transaction_type' => 'SELL',
|
||||
'quantity' => 6,
|
||||
'currency' => 'USD',
|
||||
'date' => now()->toDateString(),
|
||||
'sale_price' => 150,
|
||||
];
|
||||
|
||||
$this->actingAs($this->user)
|
||||
->postJson(route('api.transaction.store'), $data)
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors(['quantity']);
|
||||
}
|
||||
|
||||
public function test_can_show_a_transaction()
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
@@ -8,6 +8,7 @@ use App\Models\Holding;
|
||||
use App\Models\Portfolio;
|
||||
use App\Models\Transaction;
|
||||
use App\Models\User;
|
||||
use App\Rules\QuantityValidationRule;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class TransactionsTest extends TestCase
|
||||
@@ -69,4 +70,22 @@ class TransactionsTest extends TestCase
|
||||
0.01
|
||||
);
|
||||
}
|
||||
|
||||
public function test_cannot_sell_more_than_owned(): void
|
||||
{
|
||||
$this->actingAs($user = User::factory()->create());
|
||||
|
||||
$portfolio = Portfolio::factory()->create();
|
||||
|
||||
Transaction::factory(5)->buy()->lastYear()->portfolio($portfolio->id)->symbol('AAPL')->create();
|
||||
$sale_transaction = Transaction::factory()->sell(6)->lastMonth()->portfolio($portfolio->id)->symbol('AAPL')->make();
|
||||
|
||||
$rule = new QuantityValidationRule($portfolio, $sale_transaction->symbol, 'SELL', $sale_transaction->date, $sale_transaction);
|
||||
|
||||
$rule->validate('quantity', $sale_transaction->quantity, function () {
|
||||
$this->assertFalse(false, 'Not permitted to sell more than owned.');
|
||||
});
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user