-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests for raw message parser, refactor message handler to use it
- Loading branch information
1 parent
2b441bd
commit 39a52e3
Showing
5 changed files
with
150 additions
and
112 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
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe RawMqttMessageParser do | ||
subject(:parser) { | ||
RawMqttMessageParser.new | ||
} | ||
|
||
it "parses empty messages" do | ||
message = "{}" | ||
parsed = parser.parse(message) | ||
expect(parsed).to eq({ data: [ { recorded_at: nil, sensors: [] }]}) | ||
end | ||
|
||
it "parses messages with a timestamp" do | ||
message = "{t:2024-09-25T13:19:38Z}" | ||
parsed = parser.parse(message) | ||
expect(parsed).to eq({ data: [ { recorded_at: "2024-09-25T13:19:38Z", sensors: [] }]}) | ||
end | ||
|
||
it "parses messages with a timestamp and one postitive integer value" do | ||
message = "{t:2024-09-25T13:19:38Z,1:2}" | ||
parsed = parser.parse(message) | ||
expect(parsed).to eq({ data: [ { recorded_at: "2024-09-25T13:19:38Z", sensors: [{id: "1", value: "2"}] }]}) | ||
end | ||
|
||
it "parses messages with a timestamp and one negative integer value" do | ||
message = "{t:2024-09-25T13:19:38Z,2:-3}" | ||
parsed = parser.parse(message) | ||
expect(parsed).to eq({ data: [ { recorded_at: "2024-09-25T13:19:38Z", sensors: [{id: "2", value: "-3"}] }]}) | ||
end | ||
|
||
it "parses messages with a timestamp and one positive float value" do | ||
message = "{t:2024-09-25T13:19:38Z,10:3.12345}" | ||
parsed = parser.parse(message) | ||
expect(parsed).to eq({ data: [ { recorded_at: "2024-09-25T13:19:38Z", sensors: [{id: "10", value: "3.12345"}] }]}) | ||
end | ||
|
||
it "parses messages with a timestamp and one negative float value" do | ||
message = "{t:2024-09-25T13:19:38Z,100:-2000.12345}" | ||
parsed = parser.parse(message) | ||
expect(parsed).to eq({ data: [ { recorded_at: "2024-09-25T13:19:38Z", sensors: [{id: "100", value: "-2000.12345"}] }]}) | ||
end | ||
|
||
it "parses messages with a timestamp and multiple values" do | ||
message = "{t:2024-09-25T13:19:38Z,100:-2000.12345,21:12345.23450}" | ||
parsed = parser.parse(message) | ||
expect(parsed).to eq({ data: [ { recorded_at: "2024-09-25T13:19:38Z", sensors: [{id: "100", value: "-2000.12345"}, { id: "21", value: "12345.23450"}] }]}) | ||
end | ||
|
||
it "strips non-ascii characters from messages" do | ||
message = "{t:2024-09-25T13:19:38Z💣💣💣💣,100:-2000.12345,21:12345.23450}" | ||
parsed = parser.parse(message) | ||
expect(parsed).to eq({ data: [ { recorded_at: "2024-09-25T13:19:38Z", sensors: [{id: "100", value: "-2000.12345"}, { id: "21", value: "12345.23450"}] }]}) | ||
end | ||
|
||
it "returns nil if no valid message parsed" do | ||
message = "ceci n'est pas un message" | ||
parsed = parser.parse(message) | ||
expect(parsed).to eq(nil) | ||
end | ||
|
||
it "parses timestamps at any position in the packet" do | ||
message = "{100:-2000.12345,t:2024-09-25T13:19:38Z,21:12345.23450}" | ||
parsed = parser.parse(message) | ||
expect(parsed).to eq({ data: [ { recorded_at: "2024-09-25T13:19:38Z", sensors: [{id: "100", value: "-2000.12345"}, { id: "21", value: "12345.23450"}] }]}) | ||
end | ||
|
||
it "parses messages with spaces between entries" do | ||
message = "{t:2024-09-25T13:19:38Z, 100 : -2000.12345, 21 : 12345.23450}" | ||
parsed = parser.parse(message) | ||
expect(parsed).to eq({ data: [ { recorded_at: "2024-09-25T13:19:38Z", sensors: [{id: "100", value: "-2000.12345"}, { id: "21", value: "12345.23450"}] }]}) | ||
end | ||
|
||
it "parses messages with leading spaces after the braces" do | ||
message = "{ t:2024-09-25T13:19:38Z,100:-2000.12345,21:12345.23450 }" | ||
parsed = parser.parse(message) | ||
expect(parsed).to eq({ data: [ { recorded_at: "2024-09-25T13:19:38Z", sensors: [{id: "100", value: "-2000.12345"}, { id: "21", value: "12345.23450"}] }]}) | ||
end | ||
|
||
it "parses messages padded with whitespace" do | ||
message = " {t:2024-09-25T13:19:38Z,100:-2000.12345,21:12345.23450} " | ||
parsed = parser.parse(message) | ||
expect(parsed).to eq({ data: [ { recorded_at: "2024-09-25T13:19:38Z", sensors: [{id: "100", value: "-2000.12345"}, { id: "21", value: "12345.23450"}] }]}) | ||
end | ||
end |