Skip to content
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
33 changes: 33 additions & 0 deletions spec/timestamp_schema_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "json-schema"
require "spec"
require "../src/placeos-models"

module PlaceOS::Model
describe "Epoch timestamp schemas" do
it "describes the Edge heartbeat using its serialized integer type" do
time = Time.unix(1_700_000_000)
edge = Edge.new
edge.last_seen = time
JSON.parse(edge.to_json)["last_seen"].as_i64.should eq time.to_unix

refs = JSON::Schema::Definitions.new("#/components/schemas/")
properties = JSON.parse(Edge.json_schema(true, refs).to_json)["properties"]
properties["last_seen"].should eq JSON.parse(%({"type":"integer","format":"Int64","nullable":true}))
end

it "describes the Shortener validity timestamps using their serialized integer type" do
start = Time.unix(1_700_000_000)
finish = start + 1.hour
shortener = Shortener.new(valid_from: start, valid_until: finish)
json = JSON.parse(shortener.to_json)
json["valid_from"].as_i64.should eq start.to_unix
json["valid_until"].as_i64.should eq finish.to_unix

refs = JSON::Schema::Definitions.new("#/components/schemas/")
properties = JSON.parse(Shortener.json_schema(true, refs).to_json)["properties"]
epoch = JSON.parse(%({"type":"integer","format":"Int64","nullable":true}))
properties["valid_from"].should eq epoch
properties["valid_until"].should eq epoch
end
end
end
3 changes: 1 addition & 2 deletions src/placeos-models/edge.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ module PlaceOS::Model

attribute user_id : String, mass_assignment: false

@[JSON::Field(type: "integer", format: "Int64")]
attribute last_seen : Time?, converter: Time::EpochConverterOptional, mass_assignment: false
attribute last_seen : Time?, converter: Time::EpochConverterOptional, type: "integer", format: "Int64", mass_assignment: false
attribute online : Bool = false, mass_assignment: false

@[JSON::Field(ignore: true)]
Expand Down
4 changes: 2 additions & 2 deletions src/placeos-models/shortener.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module PlaceOS::Model
attribute redirect_count : Int64 = 0
attribute enabled : Bool = true

attribute valid_from : Time? = nil, converter: Time::EpochConverterOptional
attribute valid_until : Time? = nil, converter: Time::EpochConverterOptional
attribute valid_from : Time? = nil, converter: Time::EpochConverterOptional, type: "integer", format: "Int64"
attribute valid_until : Time? = nil, converter: Time::EpochConverterOptional, type: "integer", format: "Int64"

belongs_to Authority

Expand Down
Loading