Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test suite #108 #426

Merged
merged 10 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
],
Comment on lines 68 to 83
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that the tests use their own connection (own database yes, but not own connection). We actually have the connection names micro_power_manager and shard hardcoded in a lot of places in the code base. So, I'm doubtful if using different connection names will even work.

Do you think it's possible to remove the two testing connections and the envs DB_CONNECTION and TEST_DB_CONNECTION?

We could instead use a different DB_DATABASE env when running test.

However, I'm not sure how many side effects this will have. So, I'd say we move this to a different PR

Copy link
Contributor Author

@beesaferoot beesaferoot Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have to give it a try. It will make things cleaner if we have the same connections


// 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
Loading