Commit e5b46e7f by John Doe

refactoring database parsing

parent ed0a8a8f
......@@ -9,13 +9,13 @@ class UpdateDb
super()
end
def run(db_adapter:)
def run(schema)
# create the root folder if it doesn't exist
@db.root_folder ||= DbFolder.create(name: 'root', path: '/')
@root_folder = @db.root_folder
# create the entry array from the schema
entries = __create_entries(db_adapter.schema)
entries = __create_entries(schema)
updater = UpdateFolder.new(@root_folder, entries)
absorb_status(updater.run)
......
......@@ -16,7 +16,6 @@ class DbSchemaHelper
streams: __build_streams(stream_count)
}
end
# rubocop:enable Metrics/MethodLength
# build stream hash for a file
def __build_streams(count)
......
......@@ -26,13 +26,9 @@ simple_db = [
describe 'UpdateDb' do
describe '*run*' do
def update_with_schema(schema, db: nil)
# stub the database adapter
adapter = instance_double(DbAdapter)
allow(adapter).to receive(:schema).and_return(Array.new(schema))
# run the update
@db = db || Db.new
@service = UpdateDb.new(db: @db)
@service.run(db_adapter: adapter)
@service.run(schema)
@root = @db.root_folder
end
# simple schema parsing
......@@ -187,36 +183,5 @@ describe 'UpdateDb' do
expect(@folder.subfolders.count).to eq(1)
end
end
describe 'given changes to remote metadata' do
it 'updates file info' do
# create Db with 1 folder and file
update_with_schema([helper.entry('/folder1/file1',
metadata: { name: 'old_name' })])
file = DbFile.find_by_name('old_name')
expect(file).to be_present
# run update again with new metadata
update_with_schema([helper.entry('/folder1/file1',
metadata: { name: 'new_name' })],
db: @db)
file.reload
expect(file.name).to eq('new_name')
end
it 'updates folder info' do
# create Db with folder and subfolder
update_with_schema([helper.entry('/folder1/subfolder/info',
metadata: { name: 'old_name' })])
folder = DbFolder.find_by_name('old_name')
expect(folder).to be_present
# run update again with new metadata
update_with_schema([helper.entry('/folder1/subfolder/info',
metadata: { name: 'new_name' })],
db: @db)
folder.reload
expect(folder.name).to eq('new_name')
end
it 'adds new streams'
it 'removes missing streams'
end
end
end
# frozen_string_literal: true
require 'rails_helper'
helper = DbSchemaHelper.new
describe 'UpdateFile service' do
let(:db) { Db.new }
let(:service) { UpdateDb.new(db: db) }
it 'updates file info' do
# create Db with 1 folder and file
service.run([helper.entry('/folder1/file1',
metadata: { name: 'old_name' })])
file = DbFile.find_by_name('old_name')
expect(file).to be_present
# run update again with new metadata
service = UpdateDb.new(db: db)
service.run([helper.entry('/folder1/file1',
metadata: { name: 'new_name' })])
file.reload
expect(file.name).to eq('new_name')
end
it 'updates stream info' do
# create Db with file with 1 stream
schema = [helper.entry('/folder1/subfolder/file',
stream_count: 1)]
schema[0][:streams][0][:name] = 'old_name'
service.run(schema)
stream = DbStream.find_by_name('old_name')
expect(stream).to be_present
# run update again with new metadata
schema[0][:streams][0][:name] = 'new_name'
service = UpdateDb.new(db: db)
service.run(schema)
stream.reload
expect(stream.name).to eq('new_name')
end
end
# frozen_string_literal: true
require 'rails_helper'
helper = DbSchemaHelper.new
describe 'UpdateFolder service' do
let(:db) { Db.new }
let(:service) { UpdateDb.new(db: db) }
it 'updates folder info' do
# create Db with folder and subfolder
service.run([helper.entry('/folder1/subfolder/info',
metadata: { name: 'old_name' })])
folder = DbFolder.find_by_name('old_name')
expect(folder).to be_present
# run update again with new metadata
service = UpdateDb.new(db: db)
service.run([helper.entry('/folder1/subfolder/info',
metadata: { name: 'new_name' })])
folder.reload
expect(folder.name).to eq('new_name')
end
# run update again with different streams
it 'removes missing streams'
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment