Adding tests for RecordsParser and SemanticHistoryParser.

This commit is contained in:
Sascha Zepter 2024-10-06 22:16:58 +00:00
parent b8172c86e3
commit f1f6d2c715
2 changed files with 179 additions and 10 deletions

View file

@ -7,21 +7,27 @@ RSpec.describe GoogleMaps::RecordsParser do
subject(:parser) { described_class.new(import).call(json) }
let(:import) { create(:import) }
let(:time) { Time.zone.now }
let(:json) do
{
'latitudeE7' => 123_456_789,
'longitudeE7' => 123_456_789,
'timestamp' => Time.zone.now.to_s,
'altitude' => 0,
'velocity' => 0
}
end
it 'creates a point' do
expect { parser }.to change(Point, :count).by(1)
context 'with regular timestamp' do
let(:json) { super().merge('timestamp' => time.to_s) }
it 'creates a point' do
expect { parser }.to change(Point, :count).by(1)
end
end
context 'when point already exists' do
let(:json) { super().merge('timestamp' => time.to_s) }
before do
create(
:point, user: import.user, import:, latitude: 12.3456789, longitude: 12.3456789,
@ -33,5 +39,43 @@ RSpec.describe GoogleMaps::RecordsParser do
expect { parser }.not_to change(Point, :count)
end
end
context 'with timestampMs in milliseconds' do
let(:json) { super().merge('timestampMs' => (time.to_f * 1000).to_i.to_s) }
it 'creates a point using milliseconds timestamp' do
expect { parser }.to change(Point, :count).by(1)
end
end
context 'with ISO 8601 timestamp' do
let(:json) { super().merge('timestamp' => time.iso8601) }
it 'parses ISO 8601 timestamp correctly' do
expect { parser }.to change(Point, :count).by(1)
created_point = Point.last
expect(created_point.timestamp).to eq(DateTime.parse(iso_timestamp).to_i)
end
end
context 'with timestamp in milliseconds' do
let(:json) { super().merge('timestamp' => (time.to_f * 1000).to_i.to_s) }
it 'parses millisecond timestamp correctly' do
expect { parser }.to change(Point, :count).by(1)
created_point = Point.last
expect(created_point.timestamp).to eq(time.to_i)
end
end
context 'with timestamp in seconds' do
let(:json) { super().merge('timestamp' => time.to_i.to_s) }
it 'parses second timestamp correctly' do
expect { parser }.to change(Point, :count).by(1)
created_point = Point.last
expect(created_point.timestamp).to eq(time.to_i)
end
end
end
end
end

View file

@ -7,6 +7,7 @@ RSpec.describe GoogleMaps::SemanticHistoryParser do
subject(:parser) { described_class.new(import, user.id).call }
let(:user) { create(:user) }
let(:time) { Time.zone.now }
context 'when activitySegment is present' do
context 'when startLocation is blank' do
@ -19,7 +20,7 @@ RSpec.describe GoogleMaps::SemanticHistoryParser do
{ 'latE7' => 123_456_789, 'lngE7' => 123_456_789 }
]
},
'duration' => { 'startTimestamp' => Time.zone.now.to_s }
'duration' => { 'startTimestamp' => time.to_s }
}
}
end
@ -32,7 +33,7 @@ RSpec.describe GoogleMaps::SemanticHistoryParser do
let(:activity_segment) do
{
'activitySegment' => {
'duration' => { 'startTimestamp' => Time.zone.now.to_s }
'duration' => { 'startTimestamp' => time.to_s }
}
}
end
@ -49,7 +50,7 @@ RSpec.describe GoogleMaps::SemanticHistoryParser do
{
'activitySegment' => {
'startLocation' => { 'latitudeE7' => 123_456_789, 'longitudeE7' => 123_456_789 },
'duration' => { 'startTimestamp' => Time.zone.now.to_s }
'duration' => { 'startTimestamp' => time.to_s }
}
}
end
@ -57,6 +58,68 @@ RSpec.describe GoogleMaps::SemanticHistoryParser do
it 'creates a point' do
expect { parser }.to change(Point, :count).by(1)
end
context 'with different timestamp formats' do
context 'when timestamp is in ISO format' do
let(:activity_segment) do
{
'activitySegment' => {
'startLocation' => { 'latitudeE7' => 123_456_789, 'longitudeE7' => 123_456_789 },
'duration' => { 'startTimestamp' => time.iso8601 }
}
}
end
it 'creates a point' do
expect { parser }.to change(Point, :count).by(1)
end
end
context 'when timestamp is in seconds format' do
let(:activity_segment) do
{
'activitySegment' => {
'startLocation' => { 'latitudeE7' => 123_456_789, 'longitudeE7' => 123_456_789 },
'duration' => { 'startTimestamp' => (time.to_i).to_s }
}
}
end
it 'creates a point' do
expect { parser }.to change(Point, :count).by(1)
end
end
context 'when timestamp is in milliseconds format' do
let(:activity_segment) do
{
'activitySegment' => {
'startLocation' => { 'latitudeE7' => 123_456_789, 'longitudeE7' => 123_456_789 },
'duration' => { 'startTimestamp' => (time.to_f * 1000).to_i.to_s }
}
}
end
it 'creates a point' do
expect { parser }.to change(Point, :count).by(1)
end
end
context 'when timestampMs is used' do
let(:activity_segment) do
{
'activitySegment' => {
'startLocation' => { 'latitudeE7' => 123_456_789, 'longitudeE7' => 123_456_789 },
'duration' => { 'timestampMs' => (time.to_f * 1000).to_i.to_s }
}
}
end
it 'creates a point' do
expect { parser }.to change(Point, :count).by(1)
end
end
end
end
end
@ -67,7 +130,7 @@ RSpec.describe GoogleMaps::SemanticHistoryParser do
{
'placeVisit' => {
'location' => { 'latitudeE7' => 123_456_789, 'longitudeE7' => 123_456_789 },
'duration' => { 'startTimestamp' => Time.zone.now.to_s }
'duration' => { 'startTimestamp' => time.to_s }
}
}
end
@ -75,6 +138,68 @@ RSpec.describe GoogleMaps::SemanticHistoryParser do
it 'creates a point' do
expect { parser }.to change(Point, :count).by(1)
end
context 'with different timestamp formats' do
context 'when timestamp is in ISO format' do
let(:place_visit) do
{
'placeVisit' => {
'location' => { 'latitudeE7' => 123_456_789, 'longitudeE7' => 123_456_789 },
'duration' => { 'startTimestamp' => time.iso8601 }
}
}
end
it 'creates a point' do
expect { parser }.to change(Point, :count).by(1)
end
end
context 'when timestamp is in seconds format' do
let(:place_visit) do
{
'placeVisit' => {
'location' => { 'latitudeE7' => 123_456_789, 'longitudeE7' => 123_456_789 },
'duration' => { 'startTimestamp' => time.to_i.to_s }
}
}
end
it 'creates a point' do
expect { parser }.to change(Point, :count).by(1)
end
end
context 'when timestamp is in milliseconds format' do
let(:place_visit) do
{
'placeVisit' => {
'location' => { 'latitudeE7' => 123_456_789, 'longitudeE7' => 123_456_789 },
'duration' => { 'startTimestamp' => (time.to_f * 1000).to_i.to_s }
}
}
end
it 'creates a point' do
expect { parser }.to change(Point, :count).by(1)
end
end
context 'when timestampMs is used' do
let(:place_visit) do
{
'placeVisit' => {
'location' => { 'latitudeE7' => 123_456_789, 'longitudeE7' => 123_456_789 },
'duration' => { 'timestampMs' => (time.to_f * 1000).to_i.to_s }
}
}
end
it 'creates a point' do
expect { parser }.to change(Point, :count).by(1)
end
end
end
end
context 'when location with coordinates is blank' do
@ -83,7 +208,7 @@ RSpec.describe GoogleMaps::SemanticHistoryParser do
{
'placeVisit' => {
'location' => {},
'duration' => { 'startTimestamp' => Time.zone.now.to_s }
'duration' => { 'startTimestamp' => time.to_s }
}
}
end
@ -97,7 +222,7 @@ RSpec.describe GoogleMaps::SemanticHistoryParser do
{
'placeVisit' => {
'otherCandidateLocations' => [{ 'latitudeE7' => 123_456_789, 'longitudeE7' => 123_456_789 }],
'duration' => { 'startTimestamp' => Time.zone.now.to_s }
'duration' => { 'startTimestamp' => time.to_s }
}
}
end