Skip to content

Commit

Permalink
Fix test suite #108 (#426)
Browse files Browse the repository at this point in the history
* Fix broken test command #108

- Get php artisan to working

- Refactor model relationship

* refactor broken relation with Asset Model #108

* Create test suite workflow #108

* fix test suite workflow #108

* add instruction to run test locally #108

* fix lint error #108
  • Loading branch information
beesaferoot authored Dec 23, 2024
1 parent 53cb612 commit 4ca2a7a
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 17 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/test_suite.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Code Test Suite

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
tests:
runs-on: ubuntu-latest

strategy:
matrix:
php-version: ["8.2"]

services:
mariadb:
image: mariadb:10.11
env:
MYSQL_DATABASE: testing_test_company_db
MYSQL_ROOT_PASSWORD: wF9zLp2qRxaS2e
ports:
- 3306:3306

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, dom, fileinfo, mysql
coverage: xdebug

- name: Install Composer Dependencies
run: |
composer install --no-interaction --prefer-dist
composer dump-autoload
working-directory: ./src/backend

# - name: Generate key
# run: php artisan key:generate
# working-directory: ./src/backend

- name: Run Migrations
run: |
php artisan migrate:fresh --seed
working-directory: ./src/backend
env:
DB_CONNECTION: testing
DB_HOST: 127.0.0.1
DB_PORT: 3306
DB_DATABASE: testing_test_company_db
DB_USERNAME: root
DB_PASSWORD: wF9zLp2qRxaS2e

- name: Execute tests
run: php artisan test
working-directory: ./src/backend
continue-on-error: true
env:
DB_CONNECTION: testing
DB_HOST: 127.0.0.1
DB_PORT: 3306
DB_DATABASE: testing_test_company_db
DB_USERNAME: root
DB_PASSWORD: wF9zLp2qRxaS2e
10 changes: 10 additions & 0 deletions docs/development/development-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,13 @@ We recommend the following settings
- CPU: 4 or more
- Memory: 4GB or more
- Disk: 50GB or more

### Running test suite locally (backend)

To run the backend tests use the command:

```sh
php artisan test
```

Note: The test suite is being worked up, so expect many failling tests.
2 changes: 1 addition & 1 deletion src/backend/app/Models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function assetType(): BelongsTo {
}

public function agentAssignedAppliance(): HasMany {
return $this->hasMany(AgentAssignedAppliances::class, 'appliance_type_id', 'id');
return $this->hasMany(AgentAssignedAppliances::class, 'appliance_id', 'id');
}

public function rates(): HasMany {
Expand Down
3 changes: 2 additions & 1 deletion src/backend/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"Inensus\\GomeLongMeter\\": "packages/inensus/gome-long-meter/src",
"Inensus\\WavecomPaymentProvider\\": "packages/inensus/wavecom-payment-provider/src",
"Inensus\\DalyBms\\": "packages/inensus/daly-bms/src",
"Inensus\\AngazaSHS\\": "packages/inensus/angaza-shs/src"
"Inensus\\AngazaSHS\\": "packages/inensus/angaza-shs/src",
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
Expand Down
30 changes: 26 additions & 4 deletions src/backend/config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,38 @@

// PHPunit testing main connection
'testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
// 'driver' => 'sqlite',
// 'database' => ':memory:',
// 'prefix' => '',
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'testing_test_company_db'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'wF9zLp2qRxaS2e'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],

// PHPunit testing test_db connection
'testing_test_company_db' => [
'driver' => 'sqlite',
'database' => ':memory:',
// 'driver' => 'sqlite',
// 'database' => ':memory:',
// 'prefix' => '',
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'testing_test_company_db'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'wF9zLp2qRxaS2e'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],

Expand Down
2 changes: 1 addition & 1 deletion src/backend/database/factories/AgentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function definition() {
'balance' => 0,
'commission_revenue' => 0,
'due_to_energy_supplier' => 0,
'connection' => 'test_company_db',
'connection' => 'testing_test_company_db',
];
}
}
2 changes: 1 addition & 1 deletion src/backend/database/factories/CompanyDatabaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CompanyDatabaseFactory extends Factory {
public function definition() {
return [
'company_id' => 1,
'database_name' => 'test_company_db',
'database_name' => 'testing_test_company_db',
];
}
}
8 changes: 6 additions & 2 deletions src/backend/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true">
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
Expand All @@ -26,6 +26,10 @@
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="testing"/>
<env name="TEST_DB_CONNECTION" value="testing_test_company_db"/>

<env name="DB_HOST" value="127.0.0.1"/>
<env name="DB_PORT" value="3306"/>
<env name="DB_DATABASE" value="testing_test_company_db"/>
<env name="DB_USERNAME" value="root"/>
<env name="DB_PASSWORD" value="wF9zLp2qRxaS2e"/>
</php>
</phpunit>
4 changes: 2 additions & 2 deletions src/backend/tests/RefreshMultipleDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected function refreshInMemoryDatabase() {
);
Artisan::call(
'migrate:fresh',
['--database' => 'test_company_db', '--path' => '/database/migrations/test_company_db']
['--database' => 'testing_test_company_db', '--path' => '/database/migrations/testing_test_company_db']
);
app(Kernel::class)->setArtisan(null);
$this->app[Kernel::class]->setArtisan(null);
Expand All @@ -32,7 +32,7 @@ protected function refreshTestDatabase() {

Artisan::call(
'migrate:fresh',
['--database' => 'test_company_db', '--path' => '/database/migrations/test_company_db']
['--database' => 'testing_test_company_db', '--path' => '/database/migrations/testing_test_company_db']
);

app(Kernel::class)->setArtisan(null);
Expand Down
11 changes: 6 additions & 5 deletions src/backend/tests/Unit/AgentSellApplianceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use App\Models\AgentAssignedAppliances;
use App\Models\AgentCommission;
use App\Models\AgentSoldAppliance;
use App\Models\AssetType;
use App\Models\Asset;
use App\Models\Cluster;
use App\Models\MiniGrid;
use App\Models\PaymentHistory;
use Database\Factories\PersonFactory;
use Database\Factories\Person\PersonFactory;
use Database\Factories\UserFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
Expand Down Expand Up @@ -81,7 +81,7 @@ public function initData() {
'agent_commission_id' => 1,
'device_id' => 1,
'name' => 'alper',
'email' => 'a@b.com',
'email' => 'a@a.com',
'fire_base_token' => 'sadadadasd3',
'password' => '123123',
]);
Expand All @@ -93,15 +93,16 @@ public function initData() {
'risk_balance' => -3,
]);

AssetType::query()->create([
Asset::query()->create([
'name' => 'test',
'price' => 100,
'asset_type_id' => 1,
]);

AgentAssignedAppliances::query()->create([
'agent_id' => 1,
'user_id' => 1,
'appliance_type_id' => 1,
'appliance_id' => 1,
'cost' => 100,
]);
}
Expand Down

0 comments on commit 4ca2a7a

Please sign in to comment.