# encoding: utf-8 require 'spec_helper' require 'fedux_org_stdlib/models/class_based_model' RSpec.describe Models::ClassBasedModel do before(:each) do module TestIt module Exceptions class InvalidTestClass < Exception; end end end module Test123 module Models class TestClassForFilesystem < FeduxOrgStdlib::Models::BaseModel include FeduxOrgStdlib::Models::FilesystemBasedModel include FeduxOrgStdlib::Models::ClassBasedModel def self.model_path File.join(examples_directory, 'models', 'class_based.rb') end end end end module Test123 module Models class TestClassForFilesystem2 < FeduxOrgStdlib::Models::BaseModel include FeduxOrgStdlib::Models::FilesystemBasedModel include FeduxOrgStdlib::Models::ClassBasedModel def self.model_path File.join(examples_directory, 'models', 'forbidden.rb') end def self.forbidden_keywords [:forbidden] end end end end end it 'loads all available instances from the file system and make the classes available.' do # all loaded instances need be defined like that # # model: # ------ # class Testmodel # end # # Class to be loaded for instance of model (second level is plural of model name) # Furthermore you need to define a method which is used as a check for a valid # class. That class must not take an argument in #new! All arguments need to be # taken by the '#method'. # -------------------------------------------------------------------------------- # module Bla # module Testmodels # class XY # def method # end # end # end # end test_class = Class.new(Models::BaseModel) do include Models::FilesystemBasedModel include Models::ClassBasedModel def initialize(name, klass) super(name) @klass = klass end def self.model_name 'TestClass' end def self.library_name 'TestIt' end def self.check_method :req_method end def self.path_to_instances File.join(examples_directory, 'models', 'class_based', 'valid_1.rb') end def self.require_path(_name) File.join(examples_directory, 'models', 'class_based', 'valid_1') end end test_class.init expect(test_class.all.first.name).to eq(:valid_1) end it 'raises an error if an instance is invalid: name error' do test_class = Class.new(Models::BaseModel) do include Models::FilesystemBasedModel include Models::ClassBasedModel def initialize(name, klass) super(name) @klass = klass end def self.model_name 'TestClass' end def self.library_name 'TestIt' end def self.check_method :req_method end def self.path_to_instances File.join(examples_directory, 'models', 'class_based', 'invalid_1.rb') end def self.require_path(_name) File.join(examples_directory, 'models', 'class_based', 'invalid_1') end end expect do test_class.init end.to raise_error end it 'an import filter uses a forbidden keywords as name' do test_class = Class.new(Models::BaseModel) do include Models::FilesystemBasedModel include Models::ClassBasedModel def initialize(name, klass) super(name) @klass = klass end def self.model_name 'TestClass' end def self.library_name 'TestIt' end def self.path_to_instances File.join(examples_directory, 'models', 'class_based', 'forbidden_keyword.rb') end def self.require_path(_name) File.join(examples_directory, 'models', 'class_based', 'forbidden_keyword') end def self.forbidden_keywords [:forbidden_keyword] end end expect do test_class.init end.to raise_error FeduxOrgStdlib::Models::Exceptions::UnauthorizedUseOfKeyword end context '#check_method' do it 'raises an error if an import filter is invalid: missing method' do test_class = Class.new(Models::BaseModel) do include Models::FilesystemBasedModel include Models::ClassBasedModel def initialize(name, klass) super(name) @klass = klass end def self.model_name 'TestClass' end def self.library_name 'TestIt' end def self.path_to_instances File.join(examples_directory, 'models', 'class_based', 'invalid_2.rb') end def self.require_path(_name) File.join(examples_directory, 'models', 'class_based', 'invalid_2') end def self.check_method :req_method end end expect do test_class.init end.to raise_error TestIt::Exceptions::InvalidTestClass end it 'supports multiple methods to check for invalid instance and succeeds if both are avaiable.' do test_class = Class.new(Models::BaseModel) do include Models::FilesystemBasedModel include Models::ClassBasedModel def initialize(name, klass) super(name) @klass = klass end def self.model_name 'TestClass' end def self.library_name 'TestIt' end def self.path_to_instances File.join(examples_directory, 'models', 'class_based', 'valid_2.rb') end def self.require_path(_name) File.join(examples_directory, 'models', 'class_based', 'valid_2') end def self.check_method [:req_method1, :req_method2] end end expect do test_class.init end.not_to raise_error end it 'supports multiple methods to check for invalid instance and fails if one is missing.' do test_class = Class.new(Models::BaseModel) do include Models::FilesystemBasedModel include Models::ClassBasedModel def initialize(name, klass) super(name) @klass = klass end def self.model_name 'TestClass' end def self.library_name 'TestIt' end def self.path_to_instances File.join(examples_directory, 'models', 'class_based', 'invalid_3.rb') end def self.require_path(_name) File.join(examples_directory, 'models', 'class_based', 'invalid_3') end def self.check_method [:req_method1, :req_method2] end end expect do test_class.init end.to raise_error TestIt::Exceptions::InvalidTestClass end end it 'returns the require path' do model = Class.new(Models::BaseModel) do include Models::FilesystemBasedModel def self.require_path(_name) File.join(examples_directory, 'models', 'class_based', 'invalid_2') end end expect_result = File.join(examples_directory, 'models', 'class_based', 'invalid_2') result = model.send(:require_path, :name) expect(result).to eq(expect_result) end it 'returns the name for the instance based on the path to the ruby file' do expect(Test123::Models::TestClassForFilesystem.send(:name, Test123::Models::TestClassForFilesystem.send(:model_path))).to eq(:class_based) end it 'check if the name for the instances is forbidden' do expect do Test123::Models::TestClassForFilesystem2.send(:name, Test123::Models::TestClassForFilesystem2.send(:model_path)) end.to raise_error FeduxOrgStdlib::Models::Exceptions::UnauthorizedUseOfKeyword end it 'raises an exception if no valid algorithms could be found for model' do klass = Class.new(FeduxOrgStdlib::Models::BaseModel) do include FeduxOrgStdlib::Models::FilesystemBasedModel include FeduxOrgStdlib::Models::ClassBasedModel def self.module_name 'ProjectXY' end def self.model_name 'Internal' end def self.model_path 'asdfasf' end end expect do klass.send(:load_from_filesystem) end.to raise_error FeduxOrgStdlib::Models::Exceptions::NoImplementationsForModelFound end it '#ignore' do test_class = Class.new(Models::BaseModel) do include Models::FilesystemBasedModel include Models::ClassBasedModel def initialize(name, klass) super(name) @klass = klass end def self.model_name 'TestClass' end def self.library_name 'TestIt' end def self.path_to_instances File.join(examples_directory, 'models', 'class_based', 'ignore') end def self.require_path(_name) File.join(examples_directory, 'models', 'class_based', 'ignore', 'valid_1.rb') end def self.check_method :req_method end def self.ignore 'ignored' end end test_class.init expect(test_class.all.count).to eq(1) end end