test/shoden_test.rb in shoden-0.3.0 vs test/shoden_test.rb in shoden-0.4.0

- old
+ new

@@ -1,16 +1,39 @@ require 'cutest' require 'shoden' Model = Class.new(Shoden::Model) +class Person < Shoden::Model + attribute :email + attribute :origin + + index :origin + unique :email +end + +class Tree < Shoden::Model + attribute :name + collection :sprouts, :Sprout +end + +class Sprout < Shoden::Model + attribute :leaves + reference :tree, :Tree +end + class User < Shoden::Model attribute :name end +class A < Shoden::Model + attribute :n, ->(x) { x.to_i } +end + setup do - Shoden.destroy_all + Shoden.destroy_tables + Shoden.setup end test 'model' do model = Model.create assert_equal model.id.class, Fixnum @@ -35,72 +58,65 @@ user.update_attributes(name: 'Cyril') assert_equal user.name, 'Cyril' end -test 'relations' do - class Tree < Shoden::Model - attribute :name - collection :sprouts, :Sprout - end +test 'filtering' do + person = { email: 'elcuervo@elcuervo.net' } + Person.create(person) + p = Person.filter(person).first - class Sprout < Shoden::Model - attribute :leaves - reference :tree, :Tree - end + assert p.email == 'elcuervo@elcuervo.net' +end +test 'relations' do tree = Tree.create(name: 'asd') assert tree.id assert_equal tree.name, 'asd' sprout = tree.sprouts.create(leaves: 4) assert sprout.is_a?(Sprout) + assert tree.sprouts.all.each.is_a?(Enumerator) + + assert tree.sprouts[sprout.id].id == sprout.id + + assert_equal tree.sprouts.count, 1 assert_equal sprout.tree.id, tree.id + assert tree.sprouts.first != nil + assert tree.sprouts.last != nil end test 'deletion' do user = User.create(name: 'Damian') id = user.id user.destroy - assert_raise(Shoden::NotFound) { User[id] } + assert_equal User[id], nil end test 'casting' do - class A < Shoden::Model - attribute :n, ->(x) { x.to_i } - end - a = A.create(n: 1) a_prime = A[a.id] assert_equal a_prime.n, 1 end test 'indices' do - class Person < Shoden::Model - attribute :email - attribute :origin - - index :origin - unique :email - end - person = Person.create(email: 'elcuervo@elcuervo.net', origin: 'The internerd') assert person.id assert_raise Shoden::UniqueIndexViolation do Person.create(email: 'elcuervo@elcuervo.net', origin: 'Montevideo City') end end test 'basic querying' do + User.destroy_all 5.times { User.create } assert_equal User.all.size, 5 - assert_equal User.first.id, 1 - assert_equal User.last.id, 5 + end