diff --git a/app/Imports/Sheets/DailyChangesSheet.php b/app/Imports/Sheets/DailyChangesSheet.php index 90b7223..356be9a 100644 --- a/app/Imports/Sheets/DailyChangesSheet.php +++ b/app/Imports/Sheets/DailyChangesSheet.php @@ -85,7 +85,7 @@ class DailyChangesSheet implements ToCollection, WithHeadingRow, WithValidation, public function rules(): array { return [ - 'portfolio_id' => ['required'], + 'portfolio_id' => ['required', 'uuid'], 'date' => ['required', 'date'], 'total_market_value' => ['sometimes', 'nullable', 'numeric'], 'total_cost_basis' => ['sometimes', 'nullable', 'min:0', 'numeric'], diff --git a/app/Imports/Sheets/PortfoliosSheet.php b/app/Imports/Sheets/PortfoliosSheet.php index bf43d98..015ff05 100644 --- a/app/Imports/Sheets/PortfoliosSheet.php +++ b/app/Imports/Sheets/PortfoliosSheet.php @@ -58,7 +58,7 @@ class PortfoliosSheet implements ToCollection, WithValidation, WithHeadingRow, S public function rules(): array { return [ - 'portfolio_id' => ['sometimes', 'nullable'], + 'portfolio_id' => ['sometimes', 'nullable', 'uuid'], 'title' => ['required', 'string'], 'wishlist' => ['sometimes', 'nullable', 'boolean'], 'notes' => ['sometimes', 'nullable', 'string'], diff --git a/app/Imports/Sheets/TransactionsSheet.php b/app/Imports/Sheets/TransactionsSheet.php index e3d21f1..b331830 100644 --- a/app/Imports/Sheets/TransactionsSheet.php +++ b/app/Imports/Sheets/TransactionsSheet.php @@ -106,9 +106,9 @@ class TransactionsSheet implements ToCollection, WithHeadingRow, WithValidation, public function rules(): array { return [ - 'transaction_id' => ['sometimes', 'nullable'], + 'transaction_id' => ['sometimes', 'nullable', 'uuid'], 'symbol' => ['required', 'string'], - 'portfolio_id' => ['required'], + 'portfolio_id' => ['required', 'uuid'], 'quantity' => ['required', 'min:0', 'numeric'], 'transaction_type' => ['required', 'in:BUY,SELL'], 'date' => ['required', 'date'], diff --git a/tests/0000_00_00_import_test.xlsx b/tests/0000_00_00_import_test.xlsx new file mode 100644 index 0000000..a58b90c Binary files /dev/null and b/tests/0000_00_00_import_test.xlsx differ diff --git a/tests/ImportExportTest.php b/tests/ImportExportTest.php new file mode 100644 index 0000000..74882a6 --- /dev/null +++ b/tests/ImportExportTest.php @@ -0,0 +1,81 @@ +actingAs($user = User::factory()->create()); + + Transaction::factory(5)->buy()->lastYear()->symbol('AAPL')->create(); + + Excel::download(new BackupExport, now()->format('Y_m_d') . '_investbrain_backup.xlsx'); + + Excel::assertDownloaded(now()->format('Y_m_d') . '_investbrain_backup.xlsx', function(BackupExport $export) { + return true; + }); + } + + /** + */ + public function test_backup_job_completes(): void + { + $this->actingAs($user = User::factory()->create()); + + $backup_job = BackupImportModel::create([ + 'user_id' => auth()->user()->id, + 'path' => __DIR__.'/0000_00_00_import_test.xlsx' + ]); + + $backup_job->refresh(); + + $this->assertEquals('success', $backup_job->status); + } + + /** + */ + public function test_backup_job_inserts_rows(): void + { + $this->actingAs($user = User::factory()->create()); + + BackupImportModel::create([ + 'user_id' => auth()->user()->id, + 'path' => __DIR__.'/0000_00_00_import_test.xlsx' + ]); + + $this->assertEquals(3, $user->transactions->count()); + } + + /** + */ + public function test_backup_job_calculates_correct_holding_data(): void + { + $this->actingAs($user = User::factory()->create()); + + BackupImportModel::create([ + 'user_id' => auth()->user()->id, + 'path' => __DIR__.'/0000_00_00_import_test.xlsx' + ]); + + $holding = $user->holdings->first(); + + $this->assertEquals('AAPL', $holding->symbol); + $this->assertEquals(6, $holding->quantity); + $this->assertEqualsWithDelta(233.33, $holding->average_cost_basis, 0.01); + } +}