Exemplo n.º 1
0
class Test(unittest.TestCase):
    def setUp(self):
        print "Inside setUp ..."
        self.sa_helper = SaHelper(config.testing.db.url)
        self.sa_helper.create_all()
        
    def tearDown(self):
        print "Inside tearDown ..."
        
        self.sa_helper.drop_all()
        self.sa_helper.session.remove()
        
    def testDbSchemaExists(self):
        #TODO: to check if query was executed without errors
        result = self.sa_helper.session.execute("select * from book")
        self.assertEqual(result.fetchone(), None)
        
    def testOrmWorks(self):
        author_name = "Martin Fowler"
        author_id   = str(uuid.uuid4())
        
        #TODO: to check if result is ok
        result = self.sa_helper.session.execute(
            "insert into author (id, name) values (:id, :name)",
            {"id": author_id, "name": author_name}
        )
                
        author = self.sa_helper.session.query(Author).all()[0]
        self.assertEqual(author.name, author_name)
        
        print author.id
        
    def testIfManyToManyRelationWorks(self):
        #dictionary of {book: [authors]}
        data = {
            "Refactoring" : ["Martin Fowler", "Kent Beck"],
            "Planning XP" : ["Martin Fowler", "Kent Beck"]
        }
        
        #TODO: to make code below more readable
        #{book_name: book_id}
        processed_books   = {}
        
        #{author_name: author_id}
        processed_authors = {}
        
        for book, authors in data.iteritems():  
            if book in processed_books.keys():
                book_id = processed_books[book]
            else:
                #generates unique id for current book
                book_id = str(uuid.uuid4())
                
                #inserts data into book table
                self.sa_helper.session.execute(
                    "insert into book (id, name) values (:id, :name)",
                    {"id" : book_id, "name": book}
                )
                processed_books[book] = book_id
            
            for author in authors:
                if author in processed_authors.keys():
                    author_id = processed_authors[author]
                else:
                    #generates unique id for current author
                    author_id = str(uuid.uuid4())
                    
                    #inserts data into author table
                    self.sa_helper.session.execute(
                        "insert into author (id, name) values (:id, :name)",
                        {"id" : author_id, "name" : author}
                    )
                    
                    processed_authors[author] = author_id
                
                #inserts corresponding relation into author_book_rel
                self.sa_helper.session.execute(
                    "insert into author_book_rel (author_id, book_id) "
                    "values (:author_id, :book_id)",
                    {"author_id" : author_id, "book_id": book_id}
                )

        books = self.sa_helper.session.query(Book).all()
        for book in books:
            print book
            self.assertTrue(len(book.authors) > 1)
            
            
        authors = self.sa_helper.session.query(Author).all()
        for author in authors:
            print author
            self.assertTrue(len(author.books) > 1)
Exemplo n.º 2
0
 def setUp(self):
     print "Inside setUp ..."
     self.sa_helper = SaHelper(config.testing.db.url)
     self.sa_helper.create_all()
Exemplo n.º 3
0
class Test(unittest.TestCase):
    def setUp(self):
        print "Inside setUp ..."
        self.sa_helper = SaHelper(config.testing.db.url)
        self.sa_helper.create_all()
        
        self.facade = LibraryManager(self.sa_helper.session)
        
    def tearDown(self):
        print "Inside tearDown ..."
        
        self.sa_helper.drop_all()
        self.sa_helper.session.remove()
        
    def testAuthorCreation(self):
        author_name = "Martin Fowler"
        
        #creates author record in db using LibraryManager api
        author_dto  = AuthorDto(name = author_name)
        self.facade.create_author(author_dto)
        
        #checks if record was created
        res = self.sa_helper.session.query(Author)\
            .filter(Author.name == author_name).count()
        
        self.assertEqual(res, 1)
   
    def testBookCreation(self):
        book_name = "Refactoring"
        
        #creates book record in db using LibraryManager api
        book_dto  = BookDto(name = book_name)
        self.facade.create_book(book_dto)
        
        #checks if record was created
        res = self.sa_helper.session.query(Book)\
            .filter(Book.name == book_name).count()
        
        self.assertEqual(res, 1)
        
    def testAuthorDeletion(self):
        author_name = "Martin Fowler"
        
        #creates author record
        author = Author()
        author.name = author_name
        
        self.sa_helper.session.add(author)
        self.sa_helper.session.flush()
        
        #checks if record was created
        res = self.sa_helper.session.query(Author)\
            .filter(Author.name == author_name).count()
            
        self.assertEqual(res, 1)
        
        #deletes this record using LibraryManager api
        author_dto  = AuthorDto(name = author_name)        
        self.facade.delete_author(author_dto)
        
        #checks if author was deleted
        res = self.sa_helper.session.query(Author)\
            .filter(Author.name == author_name).count()
            
        self.assertEqual(res, 0)
               
    def testBookDeletion(self):
        book_name = "Refactoring"
        
        #creates book record
        book = Book()
        book.name = book_name
        
        self.sa_helper.session.add(book)
        self.sa_helper.session.flush()
        
        #checks if record was created 
        res = self.sa_helper.session.query(Book)\
            .filter(Book.name == book_name).count()
        
        self.assertEqual(res, 1)
        
        #deletes this record using LibraryManager api
        book_dto  = BookDto(name = book_name)        
        self.facade.delete_book(book_dto)
        
        #checks if book was deleted
        res = self.sa_helper.session.query(Book)\
            .filter(Book.name == book_name).count()
            
        self.assertEqual(res, 0)
Exemplo n.º 4
0
 def setUp(self):
     print "Inside setUp ..."
     self.sa_helper = SaHelper(config.testing.db.url)
     self.sa_helper.create_all()
     
     self.facade = LibraryManager(self.sa_helper.session)
Exemplo n.º 5
0
import sys
sys.path.append(lup)

from flask import Flask, request, redirect, url_for
from flask.templating import render_template
from library_app.src.code.dto import AuthorDto, BookDto, AuthorRef
from library_app.src.code.facade import LibraryManager
from library_app.src.code.sahelper import SaHelper
from library_app.etc import config
from library_app.src.code.validate import AuthorForm, BookForm
import jsonpickle

app = Flask(__name__)

helper = SaHelper(config.profile.db.url)
helper.create_all()
facade = LibraryManager(helper.session)

@app.route("/")
def welcome():
    return render_template("index.html")

@app.route("/books/validate", methods=["POST"])
def validate_book():
    form = BookForm(name = request.form["name"])
    if form.validate():
        return jsonpickle.encode(form.errors)
    
    return jsonpickle.encode(form.errors)