-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Variable JSON device readings (#4)
- Loading branch information
Showing
12 changed files
with
269 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
from django.contrib import admin | ||
|
||
from .models import Device, DeviceReading | ||
from .models import Device, DeviceModule, DeviceModuleReading | ||
|
||
|
||
@admin.register(Device) | ||
class DeviceAdmin(admin.ModelAdmin): | ||
list_display = ("id", "name") | ||
|
||
|
||
@admin.register(DeviceReading) | ||
class DeviceReadingAdmin(admin.ModelAdmin): | ||
@admin.register(DeviceModule) | ||
class DeviceModuleAdmin(admin.ModelAdmin): | ||
pass | ||
|
||
|
||
@admin.register(DeviceModuleReading) | ||
class DeviceModuleReadingAdmin(admin.ModelAdmin): | ||
pass |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import json | ||
|
||
from django.forms import JSONField as JSONFormField | ||
from django.forms import widgets | ||
|
||
|
||
class PrettyJSONWidget(widgets.Textarea): | ||
def format_value(self, value): | ||
# Prettify the json | ||
value = json.dumps(json.loads(value), indent=2, sort_keys=True) | ||
|
||
# Calculate the size of the contents | ||
row_lengths = [len(r) for r in value.split("\n")] | ||
content_width = max(len(row_lengths) + 2, 20) | ||
content_height = max(max(row_lengths) + 2, 45) | ||
|
||
# Adjust the size of TextArea to fit to content | ||
self.attrs["rows"] = min(content_width, 60) | ||
self.attrs["cols"] = min(content_height, 155) | ||
|
||
return value | ||
|
||
|
||
class PrettyJsonFormField(JSONFormField): | ||
widget = PrettyJSONWidget |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import pytest | ||
from jsonschema.exceptions import SchemaError, ValidationError | ||
|
||
from shedpi_hub_dashboard.models import DeviceModuleReading | ||
from shedpi_hub_dashboard.tests.utils.factories import DeviceModuleFactory | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_schema_validation_happy_path(): | ||
""" | ||
The data validation should succeed when the schema is valid | ||
""" | ||
schema = { | ||
"$id": "https://example.com/person.schema.json", | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "Person", | ||
"type": "object", | ||
"properties": { | ||
"firstName": {"type": "string", "description": "The person's first name."}, | ||
"lastName": {"type": "string", "description": "The person's last name."}, | ||
"age": { | ||
"description": "Age in years which must be equal to or greater than zero.", | ||
"type": "integer", | ||
"minimum": 0, | ||
}, | ||
}, | ||
} | ||
data = {"firstName": "John", "lastName": "Doe", "age": 21} | ||
device_module = DeviceModuleFactory(schema=schema) | ||
|
||
reading = DeviceModuleReading(device_module=device_module, data=data) | ||
|
||
# By default no validation error is raised | ||
reading.save() | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_json_schema_invalid_data(): | ||
schema = { | ||
"$id": "https://example.com/person.schema.json", | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "Person", | ||
"type": "object", | ||
"properties": { | ||
"age": { | ||
"description": "Age in years which must be equal to or greater than zero.", | ||
"type": "integer", | ||
"minimum": 0, | ||
} | ||
}, | ||
} | ||
data = {"age": 21} | ||
updated_data = {"age": "Some text"} | ||
device_module = DeviceModuleFactory(schema=schema) | ||
reading = DeviceModuleReading(device_module=device_module, data=data) | ||
|
||
# No error occurs with valid data | ||
reading.save() | ||
|
||
# With invalid data it now throws a validation error | ||
with pytest.raises(ValidationError): | ||
reading.data = updated_data | ||
reading.save() | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_json_schema_update_with_invalid_data(): | ||
schema = { | ||
"$id": "https://example.com/person.schema.json", | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "Person", | ||
"type": "object", | ||
"properties": { | ||
"age": { | ||
"description": "Age in years which must be equal to or greater than zero.", | ||
"type": "integer", | ||
"minimum": 0, | ||
} | ||
}, | ||
} | ||
data = {"age": "Some text"} | ||
device_module = DeviceModuleFactory(schema=schema) | ||
reading = DeviceModuleReading(device_module=device_module, data=data) | ||
|
||
with pytest.raises(ValidationError): | ||
reading.save() | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_json_schema_invalid_schema(): | ||
schema = {"type": 1234} | ||
data = {"firstName": "John", "lastName": "Doe", "age": 21} | ||
|
||
device_module = DeviceModuleFactory(schema=schema) | ||
reading = DeviceModuleReading(device_module=device_module, data=data) | ||
|
||
with pytest.raises(SchemaError): | ||
reading.save() |
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import ClassVar | ||
from uuid import uuid4 | ||
|
||
import factory | ||
from factory.django import DjangoModelFactory | ||
|
||
from shedpi_hub_dashboard.models import Device, DeviceModule, DeviceModuleReading | ||
|
||
|
||
class DeviceFactory(DjangoModelFactory): | ||
class Meta: | ||
model = Device | ||
|
||
id = factory.LazyFunction(lambda: str(uuid4())) | ||
name = factory.Faker("word") | ||
location = factory.Faker("word") | ||
|
||
|
||
class DeviceModuleFactory(DjangoModelFactory): | ||
class Meta: | ||
model = DeviceModule | ||
|
||
id = factory.LazyFunction(lambda: str(uuid4())) | ||
device = factory.SubFactory(DeviceFactory) | ||
name = factory.Faker("word") | ||
location = factory.Faker("word") | ||
schema: ClassVar[dict] = {} | ||
|
||
|
||
class DeviceModuleReadingFactory(DjangoModelFactory): | ||
class Meta: | ||
model = DeviceModuleReading | ||
|
||
device_module = factory.SubFactory(DeviceModuleFactory) | ||
data: ClassVar[dict] = {} |