feat: added certain files for watcher_spec with valid and invalid users

This commit is contained in:
GED 2024-12-16 20:53:48 +01:00
parent ab84d282d3
commit 409e3b745d
9 changed files with 1271 additions and 29 deletions

View file

@ -4,6 +4,7 @@ class Imports::Watcher
class UnsupportedSourceError < StandardError; end
WATCHED_DIR_PATH = Rails.root.join('tmp/imports/watched')
SUPPORTED_FORMATS = %w[.gpx .json .rec].freeze
def call
user_directories.each do |user_email|
@ -36,7 +37,7 @@ class Imports::Watcher
def file_names(directory_path)
Dir.entries(directory_path).select do |file|
['.gpx', '.json', '.rec'].include?(File.extname(file))
SUPPORTED_FORMATS.include?(File.extname(file))
end
end
@ -82,7 +83,7 @@ class Imports::Watcher
:google_semantic_history
else
:geojson
end
end
when 'rec' then :owntracks
when 'gpx' then :gpx
else raise UnsupportedSourceError, 'Unsupported source '
@ -103,8 +104,6 @@ class Imports::Watcher
OwnTracks::RecParser.new(file).call
when :gpx
Hash.from_xml(file)
else
JSON.parse(file)
end
end
end
end

View file

@ -0,0 +1 @@
{"type": "FeatureCollection"}

View file

@ -0,0 +1 @@
{"type": "FeatureCollection"}

View file

@ -0,0 +1 @@
{"type": "FeatureCollection"}

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1 @@
{"type": "FeatureCollection"}

View file

@ -0,0 +1,16 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [102.0, 0.5]
},
"properties": {
"timestamp": "2023-01-01T00:00:00Z",
"accuracy": 10
}
}
]
}

View file

@ -5,16 +5,13 @@ require 'rails_helper'
RSpec.describe Imports::Watcher do
describe '#call' do
subject(:service) { described_class.new.call }
let(:watched_dir_path) { Rails.root.join('spec/fixtures/files/watched') }
let(:user) { create(:user, email: 'user@domain.com') }
before do
FileUtils.mkdir_p(watched_dir_path.join(user.email))
stub_const('Imports::Watcher::WATCHED_DIR_PATH', watched_dir_path)
end
after do
FileUtils.rm_rf(watched_dir_path)
Sidekiq::Testing.inline!
end
context 'when there are no files in the watched directory' do
@ -28,39 +25,25 @@ RSpec.describe Imports::Watcher do
context 'when there are files in the watched directory' do
context 'when the file has a valid user email' do
it 'creates an import for the user' do
Sidekiq::Testing.inline!
File.write(watched_dir_path.join(user.email, 'location-history.json'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join(user.email, 'Records.json'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join(user.email, '2023_January.json'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join(user.email, 'owntracks.rec'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join(user.email, 'gpx_track_single_segment.gpx'), '{"type": "FeatureCollection"}')
expect { service }.to change(user.imports, :count).by(5)
expect { service }.to change(user.imports, :count).by(6)
end
end
context 'when the file has an invalid user email' do
it 'does not create an import' do
FileUtils.mkdir_p(watched_dir_path.join('invalid@domain.com'))
File.write(watched_dir_path.join('invalid@domain.com', 'location-history.json'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join('invalid@domain.com', 'Records.json'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join('invalid@domain.com', '2023_January.json'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join('invalid@domain.com', 'owntracks.rec'), '{"type": "FeatureCollection"}')
File.write(watched_dir_path.join('invalid@domain.com', 'gpx_track_single_segment.gpx'), '{"type": "FeatureCollection"}')
expect { service }.not_to change(Import, :count)
expect { service }.not_to change(Import, :count)
end
end
context 'when the import already exists' do
it 'does not create a new import' do
create(:import, user:, name: '2023_January.json')
create(:import, user:, name: 'export_same_points.json')
create(:import, user:, name: 'gpx_track_single_segment.gpx')
create(:import, user:, name: 'location-history.json')
create(:import, user:, name: 'owntracks.rec')
create(:import, user:, name: 'Records.json')
create(:import, user:, name: '2023_January.json')
create(:import, user:, name: 'data.geojson')
expect { service }.not_to change(Import, :count)
end
end