

def testmodel(self):
    for table in ('author', 'books'):
        Query.raw_sql(('DELETE FROM %s' % escape(table)))
    james = Author(first_name='James', last_name='Joyce')
    james.save()
    kurt = Author(first_name='Kurt', last_name='Vonnegut')
    kurt.save()
    tom = Author(first_name='Tom', last_name='Robbins')
    tom.save()
    Book(title='Ulysses', author_id=james.id).save()
    Book(title='Slaughter-House Five', author_id=kurt.id).save()
    Book(title='Jitterbug Perfume', author_id=tom.id).save()
    slww = Book(title='Still Life with Woodpecker', author_id=tom.id)
    slww.save()
    self.assertEqual(slww.author.first_name, 'Tom')
    self.assertEqual(len(list(tom.books)), 2)
    kid = kurt.id
    del (james, kurt, tom, slww)
    b = Book.get(title='Ulysses')[0]
    a = Author.get(id=b.author_id)[0]
    self.assertEqual(a.id, b.author_id)
    a = Author.get(id=b.id)[:]
    self.assert_(isinstance(a, list))
    new_last_name = 'Vonnegut, Jr.'
    a = Author.get(id=kid)[0]
    a.last_name = new_last_name
    a.save()
    a = Author.get(kid)
    self.assertEqual(a.last_name, new_last_name)
    self.assertEqual(Author.get().count(), 3)
    self.assertEqual(len(Book.get()[1:4]), 3)
    a.delete()
    self.assertEqual(Author.get().count(), 2)
    a = Author(first_name='', last_name='Ted')
    try:
        a.save()
        raise Exception('Validation not caught')
    except Model.ValidationError:
        pass
    a.first_name = 'Bill and'
    a.save()
    self.assertEqual(a.bio, 'No bio available')
    try:
        Author(first_name='I am a', last_name='BadGuy!').save()
        raise Exception('Validation not caught')
    except Model.ValidationError:
        pass

dataset/ETHPy150Open mimecuvalo/helloworld/packages/autumn/tests/run.py TestModels.testmodel/original