Skip to content

Commit a3c0477

Browse files
authored
Merge pull request #122 from lusergit/push-qmkklxvtnwvx
feat(capabilities): device capabilities
2 parents 710afe5 + bb797ff commit a3c0477

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
### Added
9+
- Device capabilities
10+
- Device capability `purge_property_compression_format`
11+
712
## [1.2.0] - 2024-07-01
813

914
## [1.2.0-rc.0] - 2024-05-28
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#
2+
# This file is part of Astarte.
3+
#
4+
# Copyright 2025 SECO Mind Srl
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
20+
21+
defmodule Astarte.Core.Device.Capabilities do
22+
use TypedEctoSchema
23+
import Ecto.Changeset
24+
25+
alias Astarte.Core.Device.Capabilities
26+
27+
@required_fields []
28+
29+
@permitted_fields [:purge_properties_compression_format] ++ @required_fields
30+
31+
@primary_key false
32+
typed_embedded_schema do
33+
field :purge_properties_compression_format, Ecto.Enum,
34+
values: [zlib: 0, plaintext: 1],
35+
default: :zlib
36+
end
37+
38+
def changeset(%Capabilities{} = capabilities, params \\ %{}) do
39+
capabilities
40+
|> cast(params, @permitted_fields)
41+
|> validate_required(@required_fields)
42+
end
43+
end
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#
2+
# This file is part of Astarte.
3+
#
4+
# Copyright 2025 SECO Mind Srl
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
20+
21+
defmodule Astarte.Core.Device.CapabilitiesTest do
22+
use ExUnit.Case
23+
24+
alias Astarte.Core.Device.Capabilities
25+
26+
test "capabilities with :purge_properties_compression_format :zlib" do
27+
params = %{
28+
"purge_properties_compression_format" => "zlib"
29+
}
30+
31+
changeset = Capabilities.changeset(%Capabilities{}, params)
32+
33+
assert %Ecto.Changeset{valid?: true} = changeset
34+
35+
{:ok, capabilities} = Ecto.Changeset.apply_action(changeset, :insert)
36+
37+
assert %Capabilities{purge_properties_compression_format: :zlib} = capabilities
38+
end
39+
40+
test "capabilities with :purge_properties_compression_format :plaintext" do
41+
params = %{
42+
"purge_properties_compression_format" => "plaintext"
43+
}
44+
45+
changeset = Capabilities.changeset(%Capabilities{}, params)
46+
47+
assert %Ecto.Changeset{valid?: true} = changeset
48+
49+
{:ok, capabilities} = Ecto.Changeset.apply_action(changeset, :insert)
50+
51+
assert %Capabilities{purge_properties_compression_format: :plaintext} = capabilities
52+
end
53+
54+
test "capabilities with invalid :purge_properties_compression_format fails" do
55+
params = %{
56+
purge_properties_compression_format: :invalid
57+
}
58+
59+
changeset = Capabilities.changeset(%Capabilities{}, params)
60+
61+
assert %Ecto.Changeset{valid?: false} = changeset
62+
end
63+
end

0 commit comments

Comments
 (0)