Skip to content

Commit

Permalink
Fixing tests and adding workflows (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
DraKen0009 authored Dec 9, 2024
1 parent 1ae2087 commit e098561
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 61 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/test-plugin-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Run Tests with Docker

on:
push:
branches:
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Print branch name
env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
run: |
echo "Branch name: $BRANCH_NAME"
shell: bash

# Checkout the ohcnetwork/care repository
- name: Checkout ohcnetwork/care repository
uses: actions/checkout@v3
with:
repository: DraKen0009/care
ref: adding-camera-plugin


# Update the plug_config.py file with the required content
- name: Update plug_config.py
env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
run: |
cat > ./plug_config.py <<EOL
from plugs.manager import PlugManager
from plugs.plug import Plug
camera_plugin = Plug(
name="camera",
package_name="git+https://github.com/ohcnetwork/care_camera_asset.git",
version="@${BRANCH_NAME}",
configs={},
)
plugs = [camera_plugin]
manager = PlugManager(plugs)
EOL
# Build and start Docker containers
- name: Build and start Docker containers
run: |
make up
# Run Django management command tests
- name: Run Django management command tests
run: |
docker compose exec backend bash -c "python manage.py test camera --keepdb --parallel --shuffle"
6 changes: 3 additions & 3 deletions camera/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.1.2 on 2024-11-21 09:35
# Generated by Django 5.1.3 on 2024-12-05 21:12

import care.utils.models.validators
import django.db.models.deletion
Expand All @@ -22,12 +22,12 @@ class Migration(migrations.Migration):
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('external_id', models.UUIDField(db_index=True, default=uuid.uuid4, unique=True)),
('created_date', models.DateTimeField(auto_now_add=True, db_index=True, null=True)),
('modified_date', models.DateTimeField(auto_now=True, db_index=True, null=True)),
('deleted', models.BooleanField(db_index=True, default=False)),
('name', models.CharField(max_length=255, null=True)),
('position', models.JSONField(validators=[care.utils.models.validators.JSONFieldSchemaValidator({'$schema': 'http://json-schema.org/draft-07/schema#', 'additionalProperties': False, 'properties': {'x': {'type': 'number'}, 'y': {'type': 'number'}, 'zoom': {'type': 'number'}}, 'required': ['x', 'y', 'zoom'], 'type': 'object'})])),
('is_migrated', models.BooleanField(default=False)),
('created_date', models.DateTimeField()),
('modified_date', models.DateTimeField()),
('asset_bed', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='camera_position_presets', to='facility.assetbed')),
('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to=settings.AUTH_USER_MODEL)),
('updated_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to=settings.AUTH_USER_MODEL)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Generated by Django 5.1.2 on 2024-11-21 09:35
# Generated by Django 5.1.3 on 2024-12-05 21:12

from django.db import migrations


def migrate_camera_preset_to_position_preset(apps, schema_editor):
try:
CameraPreset = apps.get_model("facility", "CameraPreset")
Expand All @@ -23,6 +22,8 @@ def migrate_camera_preset_to_position_preset(apps, schema_editor):
created_by=preset.created_by,
updated_by=preset.updated_by,
is_migrated=preset.is_migrated,
created_date=preset.created_date,
modified_date=preset.modified_date
)
for preset in camera_presets
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.1.3 on 2024-12-05 21:14

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('camera', '0002_auto_20241206_0242'),
]

operations = [
migrations.AlterField(
model_name='positionpreset',
name='created_date',
field=models.DateTimeField(auto_now_add=True, db_index=True, null=True),
),
migrations.AlterField(
model_name='positionpreset',
name='modified_date',
field=models.DateTimeField(auto_now=True, db_index=True, null=True),
),
]
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from care.utils.tests.test_utils import TestUtils
from rest_framework.test import APITestCase
from rest_framework import status
from rest_framework.exceptions import ValidationError
from camera.utils.onvif import OnvifAsset


class AssetBedCameraPresetViewSetTestCase(TestUtils, APITestCase):
Expand Down Expand Up @@ -30,7 +32,11 @@ def setUpTestData(cls):
cls.asset_bed2 = cls.create_assetbed(cls.bed, cls.asset2)

def get_base_url(self, asset_bed_id=None):
return f"/api/camera/assetbed/position_presets/?assetbed_external_id={asset_bed_id or self.asset_bed1.external_id}"
return f"/api/camera/position-presets/?assetbed_external_id={asset_bed_id or self.asset_bed1.external_id}"

def validate_invalid_meta(self, asset_class, meta):
with self.assertRaises(ValidationError):
asset_class(meta)

def test_create_camera_preset_without_position(self):
res = self.client.post(
Expand Down Expand Up @@ -108,14 +114,14 @@ def test_create_camera_preset_and_presence_in_various_preset_list_apis(self):

# Check if preset in asset preset list
res = self.client.get(
f"/api/camera/position_presets/?asset_external_id={asset_bed.asset.external_id}"
f"/api/camera/position-presets/?asset_external_id={asset_bed.asset.external_id}"
)
self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertContains(res, preset_external_id)

# Check if preset in bed preset list
res = self.client.get(
f"/api/camera/position_presets/?bed_external_id={asset_bed.bed.external_id}"
f"/api/camera/position-presets/?bed_external_id={asset_bed.bed.external_id}"
)
self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertContains(res, preset_external_id)
Expand All @@ -136,3 +142,54 @@ def test_create_camera_preset_with_same_name_in_same_bed(self):
self.get_base_url(self.asset_bed2.external_id), data, format="json"
)
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)


def test_meta_validations_for_onvif_asset(self):
valid_meta = {
"local_ip_address": "192.168.0.1",
"camera_access_key": "username:password:access_key",
"middleware_hostname": "middleware.local",
"insecure_connection": True,
}
onvif_asset = OnvifAsset(valid_meta)
self.assertEqual(onvif_asset.middleware_hostname, "middleware.local")
self.assertEqual(onvif_asset.host, "192.168.0.1")
self.assertEqual(onvif_asset.username, "username")
self.assertEqual(onvif_asset.password, "password")
self.assertEqual(onvif_asset.access_key, "access_key")
self.assertTrue(onvif_asset.insecure_connection)

invalid_meta_cases = [
# Invalid format for camera_access_key
{
"id": "123",
"local_ip_address": "192.168.0.1",
"middleware_hostname": "middleware.local",
"camera_access_key": "invalid_format",
},
# Missing username/password in camera_access_key
{
"local_ip_address": "192.168.0.1",
"middleware_hostname": "middleware.local",
"camera_access_key": "invalid_format",
},
# Missing middleware_hostname
{
"local_ip_address": "192.168.0.1",
"camera_access_key": "username:password:access_key",
},
# Missing local_ip_address
{
"middleware_hostname": "middleware.local",
"camera_access_key": "username:password:access_key",
},
# Invalid value for insecure_connection
{
"local_ip_address": "192.168.0.1",
"camera_access_key": "username:password:access_key",
"middleware_hostname": "middleware.local",
"insecure_connection": "invalid_value",
},
]
for meta in invalid_meta_cases:
self.validate_invalid_meta(OnvifAsset, meta)
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
with open("HISTORY.rst") as history_file:
history = history_file.read()

requirements = []
requirements = [
"requests"
]

test_requirements = []

Expand Down
52 changes: 0 additions & 52 deletions tests/test_onvif_validations.py

This file was deleted.

0 comments on commit e098561

Please sign in to comment.