Решение на Пета задача - DataModel от Петър Нетовски

Обратно към всички решения

Към профила на Петър Нетовски

Резултати

  • 0 точки от тестове
  • 0 бонус точки
  • 0 точки общо
  • 2 успешни тест(а)
  • 23 неуспешни тест(а)

Код

class DataModel
class << self
def attributes(*attributes)
return @attributes if attributes.empty?
attr_accessor *attributes
@attributes = attributes
end
def data_store(object = nil)
return @data_store if object.nil?
@data_store = object
end
end
attr_accessor :id
def initialize(values = {})
values.each do |value, key|
:"#{key}=".to_proc.call value if @attributes.include? key
end
end
def save
end
def delete
end
end
class HashStore
def create(data)
end
def find(object)
end
end
class ArrayStore
def create(data)
end
def find(object)
end
end

Лог от изпълнението

..FFFFFFFFFFFFFFFFFFFFFFF

Failures:

  1) DataModel accepts attributes when initializing
     Failure/Error: record = user_model.new(
     NoMethodError:
       undefined method `include?' for nil:NilClass
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:21:in `block in initialize'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `each'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `initialize'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:28:in `new'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:28:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  2) DataModel has #find_by_<attribute> methods
     Failure/Error: record = user_model.new(first_name: 'Ivan', last_name: 'Ivanov')
     NoMethodError:
       undefined method `include?' for nil:NilClass
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:21:in `block in initialize'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `each'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `initialize'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:40:in `new'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:40:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  3) DataModel id generation creates id on first save and does not change it
     Failure/Error: record = user_model.new(first_name: 'Ivan', last_name: 'Ivanov')
     NoMethodError:
       undefined method `include?' for nil:NilClass
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:21:in `block in initialize'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `each'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `initialize'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:49:in `new'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:49:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  4) DataModel id generation does not reuse ids
     Failure/Error: ivan = user_model.new(first_name: 'Ivan')
     NoMethodError:
       undefined method `include?' for nil:NilClass
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:21:in `block in initialize'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `each'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `initialize'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:63:in `new'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:63:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  5) DataModel id generation does not break when there are two models with the same store
     Failure/Error: ivan = user_model.new(first_name: 'Ivan')
     NoMethodError:
       undefined method `include?' for nil:NilClass
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:21:in `block in initialize'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `each'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `initialize'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:81:in `new'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:81:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  6) DataModel equality comparison compares by id if both records are saved
     Failure/Error: ivan = user_model.new(first_name: 'Ivan')
     NoMethodError:
       undefined method `include?' for nil:NilClass
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:21:in `block in initialize'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `each'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `initialize'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:93:in `new'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:93:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  7) DataModel equality comparison uses #equal? if there are no ids
     Failure/Error: first_user  = user_model.new(first_name: 'Ivan')
     NoMethodError:
       undefined method `include?' for nil:NilClass
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:21:in `block in initialize'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `each'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `initialize'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:109:in `new'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:109:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  8) DataModel.where finds records by attributes
     Failure/Error: user_model.new(first_name: 'Ivan', last_name: 'Ivanov').save
     NoMethodError:
       undefined method `include?' for nil:NilClass
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:21:in `block in initialize'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `each'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `initialize'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:119:in `new'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:119:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  9) DataModel.where finds records by multiple attributes
     Failure/Error: user_model.new(first_name: 'Ivan', last_name: 'Ivanov').save
     NoMethodError:
       undefined method `include?' for nil:NilClass
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:21:in `block in initialize'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `each'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `initialize'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:119:in `new'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:119:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  10) DataModel.where returns empty collection when nothing is found
     Failure/Error: user_model.new(first_name: 'Ivan', last_name: 'Ivanov').save
     NoMethodError:
       undefined method `include?' for nil:NilClass
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:21:in `block in initialize'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `each'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `initialize'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:119:in `new'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:119:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  11) DataModel.where raises an error if the query is by an unknown key
     Failure/Error: user_model.new(first_name: 'Ivan', last_name: 'Ivanov').save
     NoMethodError:
       undefined method `include?' for nil:NilClass
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:21:in `block in initialize'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `each'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `initialize'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:119:in `new'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:119:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  12) DataModel#delete deletes only the record for which it is called
     Failure/Error: ivan = user_model.new(first_name: 'Ivan').save
     NoMethodError:
       undefined method `include?' for nil:NilClass
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:21:in `block in initialize'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `each'
     # /tmp/d20161202-15620-1xzzk1l/solution.rb:20:in `initialize'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:152:in `new'
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:152:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  13) DataModel#delete raises an error if the record is not saved
     Failure/Error: DataModel::DeleteUnsavedRecordError
     NameError:
       uninitialized constant DataModel::DeleteUnsavedRecordError
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:164:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  14) HashStore behaves like a data store #create saves a new record
     Failure/Error: expect(store.find(id: 2)).to eq [user]
       
       expected: [{:id=>2, :name=>"Pesho"}]
            got: nil
       
       (compared using ==)
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-1xzzk1l/spec.rb:235
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:178:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  15) HashStore behaves like a data store #find can find elements by attributes
     Failure/Error: expect(store.find(id: 2)).to eq [user]
       
       expected: [{:id=>2, :name=>"Pesho"}]
            got: nil
       
       (compared using ==)
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-1xzzk1l/spec.rb:235
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:187:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  16) HashStore behaves like a data store #update updates the attributes of a record with a given ID
     Failure/Error: store.update(2, {id: 2, name: 'Georgi'})
     NoMethodError:
       undefined method `update' for #<HashStore:0x007fb603bb59e8>
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-1xzzk1l/spec.rb:235
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:199:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  17) HashStore behaves like a data store #update only updates records with the correct IDs
     Failure/Error: store.update(2, {id: 2, name: 'Sasho'})
     NoMethodError:
       undefined method `update' for #<HashStore:0x007fb603bae288>
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-1xzzk1l/spec.rb:235
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:210:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  18) HashStore behaves like a data store #delete can delete multiple records with a single query
     Failure/Error: store.delete(name: 'Pesho')
     NoMethodError:
       undefined method `delete' for #<HashStore:0x007fb603b9bb60>
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-1xzzk1l/spec.rb:235
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:227:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  19) ArrayStore behaves like a data store #create saves a new record
     Failure/Error: expect(store.find(id: 2)).to eq [user]
       
       expected: [{:id=>2, :name=>"Pesho"}]
            got: nil
       
       (compared using ==)
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-1xzzk1l/spec.rb:239
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:178:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  20) ArrayStore behaves like a data store #find can find elements by attributes
     Failure/Error: expect(store.find(id: 2)).to eq [user]
       
       expected: [{:id=>2, :name=>"Pesho"}]
            got: nil
       
       (compared using ==)
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-1xzzk1l/spec.rb:239
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:187:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  21) ArrayStore behaves like a data store #update updates the attributes of a record with a given ID
     Failure/Error: store.update(2, {id: 2, name: 'Georgi'})
     NoMethodError:
       undefined method `update' for #<ArrayStore:0x007fb603b842a8>
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-1xzzk1l/spec.rb:239
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:199:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  22) ArrayStore behaves like a data store #update only updates records with the correct IDs
     Failure/Error: store.update(2, {id: 2, name: 'Sasho'})
     NoMethodError:
       undefined method `update' for #<ArrayStore:0x007fb603b72080>
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-1xzzk1l/spec.rb:239
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:210:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

  23) ArrayStore behaves like a data store #delete can delete multiple records with a single query
     Failure/Error: store.delete(name: 'Pesho')
     NoMethodError:
       undefined method `delete' for #<ArrayStore:0x007fb603b6e840>
     Shared Example Group: "a data store" called from /tmp/d20161202-15620-1xzzk1l/spec.rb:239
     # /tmp/d20161202-15620-1xzzk1l/spec.rb:227:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:7:in `block (2 levels) in <top (required)>'

Finished in 0.01732 seconds
25 examples, 23 failures

Failed examples:

rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:27 # DataModel accepts attributes when initializing
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:39 # DataModel has #find_by_<attribute> methods
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:48 # DataModel id generation creates id on first save and does not change it
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:62 # DataModel id generation does not reuse ids
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:74 # DataModel id generation does not break when there are two models with the same store
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:92 # DataModel equality comparison compares by id if both records are saved
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:108 # DataModel equality comparison uses #equal? if there are no ids
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:124 # DataModel.where finds records by attributes
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:129 # DataModel.where finds records by multiple attributes
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:138 # DataModel.where returns empty collection when nothing is found
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:142 # DataModel.where raises an error if the query is by an unknown key
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:151 # DataModel#delete deletes only the record for which it is called
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:162 # DataModel#delete raises an error if the record is not saved
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:174 # HashStore behaves like a data store #create saves a new record
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:183 # HashStore behaves like a data store #find can find elements by attributes
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:196 # HashStore behaves like a data store #update updates the attributes of a record with a given ID
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:204 # HashStore behaves like a data store #update only updates records with the correct IDs
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:218 # HashStore behaves like a data store #delete can delete multiple records with a single query
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:174 # ArrayStore behaves like a data store #create saves a new record
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:183 # ArrayStore behaves like a data store #find can find elements by attributes
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:196 # ArrayStore behaves like a data store #update updates the attributes of a record with a given ID
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:204 # ArrayStore behaves like a data store #update only updates records with the correct IDs
rspec /tmp/d20161202-15620-1xzzk1l/spec.rb:218 # ArrayStore behaves like a data store #delete can delete multiple records with a single query

История (1 версия и 0 коментара)

Петър обнови решението на 01.12.2016 23:56 (преди около 8 години)

+class DataModel
+ class << self
+ def attributes(*attributes)
+ return @attributes if attributes.empty?
+
+ attr_accessor *attributes
+ @attributes = attributes
+ end
+
+ def data_store(object = nil)
+ return @data_store if object.nil?
+
+ @data_store = object
+ end
+ end
+
+ attr_accessor :id
+
+ def initialize(values = {})
+ values.each do |value, key|
+ :"#{key}=".to_proc.call value if @attributes.include? key
+ end
+ end
+
+ def save
+ end
+
+ def delete
+ end
+end
+
+class HashStore
+ def create(data)
+ end
+
+ def find(object)
+ end
+end
+
+class ArrayStore
+ def create(data)
+ end
+
+ def find(object)
+ end
+end