def main(global_config, **settings):
    with Configurator(settings=settings) as config:
        engine = engine_from_config(settings, "sqlalchemy.")
        DBSession.configure(bind=engine)
        Base.metadata.bind = engine

        config.include("pyramid_jinja2")
        config.include(".routes")
        this_session_factory = SignedCookieSessionFactory("itsaseekreet")

        config = Configurator(settings=settings,
                              root_factory="quotes.models.Root",
                              session_factory=this_session_factory)

        config.registry.dbmaker = sessionmaker(bind=engine)
        config.add_request_method(db, reify=True)

        config.include("pyramid_chameleon")
        config.include("pyramid_jinja2")
        config.include(".routes")

        session_factory = get_session_factory(
            engine_from_config(settings, prefix="sqlalchemy."))
        config.add_request_method(
            lambda request: get_tm_session(session_factory, request.tm),
            "dbsession",
            reify=True)
        config.scan(".views")

    return config.make_wsgi_app()
Esempio n. 2
0
 def test_delete_quotes(self):
     quote = Quote("quote_test1", description="somequote", created_at=None)
     DBSession.add(quote)
     DBSession.flush()
     request = testing.DummyRequest()
     request.matchdict["id"] = 1
     view_obj = QuoteView(request)
     response = view_obj.delete_quote()
     print(response)
     print(type(response.body))
     self.assertEqual(response.status_code, 200)
     if "200" in str(request.response):
         return True
Esempio n. 3
0
 def create_sessions(self):
     session = self.request.session
     str_page = str(self.request.url)
     try:
         save_session = {"counter": session["counter"], "page": str_page}
         update_entity = DBSession.query(Session).filter_by(
             page=str_page).first()
         update_entity.counter += 1
         logging.debug("Saved session register: ", save_session)
         print("Current visited page: ", self.request.url)
         DBSession.add(update_entity)
         transaction.commit()
     except DBAPIError as exec:
         return {"internal error": {exec.args}}
Esempio n. 4
0
 def random_quote(self):
     try:
         rows = DBSession.query(Quote).count()
         random_id = (randint(1, rows))
         print("Picked quote:", random_id)
         result = DBSession.query(Quote).filter_by(id=random_id).first()
         return Response(status=200,
                         content_type="application/json",
                         charset="UTF-8",
                         body=json.dumps(result.to_dict()))
     except DBAPIError as exec:
         return Response(status=400,
                         content_type="application/json",
                         charset="UTF-8",
                         body=json.dumps({"error": exec.args}))
Esempio n. 5
0
    def test_get_quotes(self):
        quote1 = Quote("quote111", description="somequote111", created_at=None)
        DBSession.add(quote1)
        quote2 = Quote("quote222", description="somequote222", created_at=None)
        DBSession.add(quote2)
        quote3 = Quote("quote333", description="somequote333", created_at=None)
        DBSession.add(quote3)
        DBSession.flush()

        request = testing.DummyRequest()
        view_obj = QuoteView(request=request)
        response = view_obj.get_quotes()
        self.assertEqual(response.status_code, 200)
        if "200" in str(request.response):
            return True
Esempio n. 6
0
 def delete_quote(self):
     id = self.request.matchdict["id"]
     if id:
         obj = DBSession.query(Quote).filter(Quote.id == id).first()
         if obj:
             try:
                 DBSession.delete(obj)
                 transaction.commit()
                 return Response(status=200,
                                 content_type="application/json",
                                 charset="UTF-8",
                                 body=json.dumps({"deleted":
                                                  obj.to_dict()}))
             except DBAPIError as exec:
                 return Response(status=400,
                                 body=json.dumps({"error": exec.args}))
     else:
         return Response(status=400,
                         content_type="application/json",
                         charset="UTF-8",
                         body=json.dumps({
                             "error":
                             "quote {id} not found".format(id=id)
                         }))
Esempio n. 7
0
 def test_serializeModel(self):
     DBSession.remove()
     self.config = testing.setUp()
     id = 10
     for i in range(15, 18):
         quote = Quote("quote_teste{0}".format(id),
                       description="foo{0}".format(id),
                       created_at=None)
         DBSession.add(quote)
         DBSession.flush()
         id += 1
     rows = DBSession.query(Quote).count()
     random_id = (randint(1, rows))
     random_json_query = DBSession.query(Quote).filter_by(
         id=random_id).first().to_json()
     print("Serialized: ", random_json_query)
     self.assertEqual(type(random_json_query), dict)
Esempio n. 8
0
 def test_random_queryDB(self):
     DBSession.remove()
     self.config = testing.setUp()
     id = 20
     for i in range(1, 5):
         quote = Quote(title="quote_teste{0}".format(id),
                       description="foo{0}".format(id),
                       created_at=None)
         DBSession.add(quote)
         DBSession.flush()
         id += 1
     rows = DBSession.query(Quote).count()
     print("Number of table rows", str(rows))
     random_id = (randint(1, rows))
     print("Picked quote:", random_id)
     random_query = DBSession.query(Quote).filter_by(id=random_id).first()
     print(random_query.to_dict())
     self.assertEqual(type(random_query.to_dict()), dict)
Esempio n. 9
0
 def test_get_quote_id(self):
     quote1 = Quote("quote11", description="somequote11", created_at=None)
     DBSession.add(quote1)
     quote2 = Quote("quote22", description="somequote22", created_at=None)
     DBSession.add(quote2)
     DBSession.flush()
     id = 2
     request = testing.DummyRequest()
     request.matchdict["id"] = id
     view_obj = QuoteView(request)
     response_content = view_obj.get_quote()
     self.assertEqual(response_content.status_code, 200)
Esempio n. 10
0
 def get_quotes(self):
     try:
         results = DBSession.query(Quote).order_by(Quote.id).all()
         json_result = []
         for row in results:
             row_dict = dict(title=row.title,
                             description=row.description,
                             created_at=row.created_at)
             json_result.append(row_dict)
         print(json_result)
         return Response(status=200,
                         content_type="application/json",
                         charset="UTF-8",
                         body=json.dumps({"quotes": json_result}))
     except DBAPIError as exec:
         return Response(status=400,
                         content_type="application/json",
                         charset="UTF-8",
                         body=json.dumps({"error": exec.args}))
Esempio n. 11
0
    def test_initDb(self):
        self.config = testing.setUp()

        with transaction.manager:
            model = Quote(title="quoteTest",
                          description="Quality is never an accident;"
                          " it is always the result of intelligent effort.",
                          created_at=None)
            DBSession.add(model)
            DBSession.flush()
            quote_id = DBSession.query(Quote).first().to_json()
            json_quote = DBSession.query(Quote).filter_by(
                id="1").first().to_dict()
            description = DBSession.query(Quote.description).order_by(
                Quote.created_at).first()
        print(quote_id)
        print(json_quote)
        print(description)
Esempio n. 12
0
 def get_quote(self):
     id = self.request.matchdict["id"]
     if id:
         result = DBSession.query(Quote).filter(Quote.id == id).first()
         if result:
             try:
                 result = result.to_json()
                 print(result)
                 return Response(status=200,
                                 content_type="application/json",
                                 charset="UTF-8",
                                 body=json.dumps({"quote": result}))
             except DBAPIError as exec:
                 return Response(status=400,
                                 body=json.dumps({"error": exec.args}))
     return Response(status=400,
                     content_type="application/json",
                     charset="UTF-8",
                     body=json.dumps(
                         {"error": "quote {id} not found".format(id=id)}))
Esempio n. 13
0
 def get_session(self):
     id = self.request.matchdict["id"]
     if id:
         result = DBSession.query(Session).filter(Session.id == id).first()
         if result:
             try:
                 return Response(status=200,
                                 content_type="application/json",
                                 charset="UTF-8",
                                 body=json.dumps(result.to_dict()))
             except DBAPIError as exec:
                 json_db_error = {"error": exec.args}
                 return Response(status=400,
                                 content_type="application/json",
                                 charset="UTF-8",
                                 body=json.dumps(json_db_error))
     return Response(status=400,
                     content_type="application/json",
                     charset="UTF-8",
                     body=json.dumps({"error": "session not found"}))
Esempio n. 14
0
 def get_sessions(self):
     try:
         results = DBSession.query(Session).order_by(Session.id).all()
         json_result = []
         print(results)
         for row in results:
             row_dict = dict(page=row.page,
                             counter=row.counter,
                             created_at=row.created_at)
             json_result.append(row_dict)
         print(json_result)
         return Response(status=200,
                         content_type="application/json",
                         charset="UTF-8",
                         body=json.dumps({"sessions": json_result}))
     except DBAPIError as exec:
         json_db_error = {"error": exec.args}
         return Response(status=400,
                         content_type="application/json",
                         charset="UTF-8",
                         body=json.dumps(json_db_error))
Esempio n. 15
0
 def test_populate_and_queryDB(self):
     self.config = testing.setUp()
     with transaction.manager:
         added_quote1 = Quote(
             title="quote_teste3",
             description="“All code is guilty, until proven innocent.”",
             created_at=None)
         added_quote2 = Quote(
             title="quote_teste4",
             description="“First, solve the problem. Then, write the code.”",
             created_at=None)
         DBSession.add(added_quote1)
         DBSession.add(added_quote2)
         DBSession.flush()
     result = DBSession.query(Quote).all()
     for row in result:
         row_dict = dict(title=row.title,
                         description=row.description,
                         created_at=row.created_at)
         print(row_dict)
         self.assertEqual(type(row_dict), dict)
Esempio n. 16
0
 def test_create_and_populateDB(self):
     self.config = testing.setUp()
     with transaction.manager:
         try:
             DBSession.add(
                 Quote(
                     title="quote_teste1",
                     description=
                     "If you don’t like unit testing your product,"
                     " most likely your customers won’t like to test it either.",
                     created_at=None))
             DBSession.add(
                 Quote(
                     title="quote_teste2",
                     description=
                     "Software testing proves the existence of bugs not their absence.",
                     created_at=None))
             DBSession.flush()
         except DBAPIError as exec:
             print("SQLAlchemy Error:", exec.args)
Esempio n. 17
0
 def test_deleleDb(self):
     DBSession.remove()
     testing.tearDown()
Esempio n. 18
0
class TestSqlAlchemy(unittest.TestCase):

    engine = create_engine("sqlite://")
    Base.metadata.create_all(engine)
    DBSession.configure(bind=engine)

    def setUp(self):
        self.config = testing.setUp()

    def tearDown(self):
        testing.tearDown()

    def test_initDb(self):
        self.config = testing.setUp()

        with transaction.manager:
            model = Quote(title="quoteTest",
                          description="Quality is never an accident;"
                          " it is always the result of intelligent effort.",
                          created_at=None)
            DBSession.add(model)
            DBSession.flush()
            quote_id = DBSession.query(Quote).first().to_json()
            json_quote = DBSession.query(Quote).filter_by(
                id="1").first().to_dict()
            description = DBSession.query(Quote.description).order_by(
                Quote.created_at).first()
        print(quote_id)
        print(json_quote)
        print(description)

    def test_create_and_populateDB(self):
        self.config = testing.setUp()
        with transaction.manager:
            try:
                DBSession.add(
                    Quote(
                        title="quote_teste1",
                        description=
                        "If you don’t like unit testing your product,"
                        " most likely your customers won’t like to test it either.",
                        created_at=None))
                DBSession.add(
                    Quote(
                        title="quote_teste2",
                        description=
                        "Software testing proves the existence of bugs not their absence.",
                        created_at=None))
                DBSession.flush()
            except DBAPIError as exec:
                print("SQLAlchemy Error:", exec.args)

    def test_populate_and_queryDB(self):
        self.config = testing.setUp()
        with transaction.manager:
            added_quote1 = Quote(
                title="quote_teste3",
                description="“All code is guilty, until proven innocent.”",
                created_at=None)
            added_quote2 = Quote(
                title="quote_teste4",
                description="“First, solve the problem. Then, write the code.”",
                created_at=None)
            DBSession.add(added_quote1)
            DBSession.add(added_quote2)
            DBSession.flush()
        result = DBSession.query(Quote).all()
        for row in result:
            row_dict = dict(title=row.title,
                            description=row.description,
                            created_at=row.created_at)
            print(row_dict)
            self.assertEqual(type(row_dict), dict)

    def test_deleleDb(self):
        DBSession.remove()
        testing.tearDown()

    def test_random_queryDB(self):
        DBSession.remove()
        self.config = testing.setUp()
        id = 20
        for i in range(1, 5):
            quote = Quote(title="quote_teste{0}".format(id),
                          description="foo{0}".format(id),
                          created_at=None)
            DBSession.add(quote)
            DBSession.flush()
            id += 1
        rows = DBSession.query(Quote).count()
        print("Number of table rows", str(rows))
        random_id = (randint(1, rows))
        print("Picked quote:", random_id)
        random_query = DBSession.query(Quote).filter_by(id=random_id).first()
        print(random_query.to_dict())
        self.assertEqual(type(random_query.to_dict()), dict)

    def test_serializeModel(self):
        DBSession.remove()
        self.config = testing.setUp()
        id = 10
        for i in range(15, 18):
            quote = Quote("quote_teste{0}".format(id),
                          description="foo{0}".format(id),
                          created_at=None)
            DBSession.add(quote)
            DBSession.flush()
            id += 1
        rows = DBSession.query(Quote).count()
        random_id = (randint(1, rows))
        random_json_query = DBSession.query(Quote).filter_by(
            id=random_id).first().to_json()
        print("Serialized: ", random_json_query)
        self.assertEqual(type(random_json_query), dict)