Commit 365f9a4d by John Doe

more validation tests fixed create_nilm

parent 6c82e8d4
# frozen_string_literal: true
# validate that the file has the appropriate number
# of streams given the format string
class DbDataTypeValidator < ActiveModel::Validator
def validate(record)
# streams might not be built yet
return if record.db_streams.count == 0
# TODO: check for valid format strings (float32, uint8, etc)
unless record.db_streams.count == record.column_count
record.errors[:base] << "must have #{record.column_count} \
streams for format #{record.data_type}"
end
end
end
# A file in the database, contains one or more Streams
class DbFile < ActiveRecord::Base
belongs_to :db_folder
has_many :db_streams, dependent: :destroy
has_many :db_decimations, dependent: :destroy
validates_with DbDataTypeValidator
def defined_attributes
[:name, :name_abbrev, :description, :hidden]
end
......@@ -15,6 +31,14 @@ class DbFile < ActiveRecord::Base
destroy
end
def data_format
/^(\w*)_\d*$/.match(data_type)[1]
end
def column_count
/^\w*_(\d*)$/.match(data_type)[1].to_i
end
def as_json(_options = {})
file = super(except: [:created_at, :updated_at])
file[:streams] = db_streams.map(&:as_json)
......
......@@ -46,11 +46,13 @@ class UpdateFile
# create or update DbStreams for the
# specified DbFile
def __build_streams(file:, stream_data:)
return if stream_data.empty?
stream_data.each do |entry|
stream = file.db_streams.find_by_column(entry[:column])
file.column_count.times do |x|
stream = file.db_streams.find_by_column(x)
stream ||= DbStream.new(db_file: file)
stream.update_attributes(entry)
# check if there is stream metadata for column x
entry = stream_data.select { |meta| meta[:column] == x }
# use the metadata if present
stream.update_attributes(entry[0] || {})
stream.save!
end
end
......
......@@ -18,6 +18,6 @@ class CreateNilm
db = Db.create(nilm: @nilm, url: db_url)
service = UpdateDb.new(db: db)
adapter = DbAdapter.new(db.url)
service.run(db_adapter: adapter)
service.run(adapter.schema)
end
end
......@@ -4,11 +4,11 @@
# are usually returned by DbAdapter.schema
class DbSchemaHelper
# schema data
def entry(path, type: 'uint8_1', metadata: {}, stream_count: 0)
def entry(path, metadata: {}, stream_count: 1)
{
path: path,
attributes: {
data_type: type,
data_type: "float32_#{stream_count}",
start_time: 0,
end_time: 0,
total_rows: 0,
......
......@@ -11,7 +11,8 @@ RSpec.describe 'DbFile' do
specify { expect(db_file).to respond_to(:hidden) }
end
it 'removes streams destroyed' do
describe 'child streams' do
it 'are destroyed with parent file' do
stream = DbStream.create
file = DbFile.create
file.db_streams << stream
......@@ -19,24 +20,19 @@ RSpec.describe 'DbFile' do
expect(DbStream.find_by_id(stream.id)).to be nil
end
describe 'remove' do
let(:db_streams) { FactoryGirl.build_list(:db_stream, 5) }
let(:db_file) { FactoryGirl.create(:db_file) }
let(:db_service) { double(remove_file: true) }
it 'destroys itself' do
db_file.remove(db_service: db_service)
expect(db_file).to be_destroyed
end
it 'destroys its db_streams' do
db_file.db_streams << db_streams
db_file.remove(db_service: db_service)
db_streams.each do |stream|
expect(stream).to be_destroyed
it 'exist for every column in file datatype' do
file = DbFile.create(data_type: 'float32_3')
file.db_streams << DbStream.new
# missing 3 streams
expect(file.valid?).to be false
end
it 'do not exist for columns not in file datatype' do
file = DbFile.create(data_type: 'float32_1')
2.times do |x|
file.db_streams << DbStream.new(column: x)
end
it 'removes itself from the remote system using DbService' do
db_file.remove(db_service: db_service)
expect(db_service).to have_received(:remove_file)
expect(file.valid?).to be false
end
end
end
......@@ -20,7 +20,4 @@ describe 'UpdateFolder service' do
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