Guide: Pyramid for People - Part 4

    Part 3: unit and functional testing, Hello World at Chameleon

    Step 04: View Framework

    Enough with us “hello world”, now let's get down to work on the Projector. A UX person usually has a number of views that need to be prototyped and displayed in a URL structure.

    We want to make this process fast and productive.

    In this step, we copy the structure of the site map such: ... and make a series of URLs that will do this. Along the way, we create more views and more templates.
    /
    /about.html
    /acme
    /people


    Goals
    • Introduction to UX workflow, for example, using arbitrary data
    What is expected
    • “Standard” and “named” views
    • More ZPT designs
    Steps

    $ cd ../../creatingux; mkdir step04; cd step04

    (Unchanged) Copy one to another - step04/application.py:
    from  wsgiref.simple_server  import  make_server

    from  pyramid.config  import  Configurator

    def  main ():
        config = Configurator ()
        config.scan ("views")
        app = config.make_wsgi_app ()
        return  app

    if  __name__ == '__main__':
        app = main ()
        server = make_server ('0.0.0.0', 8080, app)
        server.serve_forever ()
    Further also here - step04/views.py:
     PEOPLE} # Dummy data COMPANY = "ACME, Inc." PEOPLE = [ 



        



        



        



        





            {'name': 'sstanton', 'title': 'Susan Stanton'},
            {'name': 'bbarker', 'title': 'Bob Barker'},
    ]

    PROJECTS = [
            {'name': 'sillyslogans' , 'title': 'Silly Slogans'},
            {'name': 'meaninglessmissions', 'title': 'Meaningless Missions'},
    ]
    And here - step04/index.pt:


        </strong>Projector - Home<strong>



          
    •  href = "/" > Home

    •     
    •  href = "/ about.html" > About Projector

    •     
    •  href = "/ acme" > ACME, Inc.

    •     
    •  href = "/ people" > People


    Projector - Home




    Further here - step04/about.pt:


        </strong>Projector - About<strong>



          
    •  href = "/" > Home

    •     
    •  href = "/ about.html" > About Projector

    •     
    •  href = "/ acme" > ACME, Inc.

    •     
    •  href = "/ people" > People


    Projector - About


    Projector is a simple project management tool capable of hosting
        multiple projects for multiple independent companies,
        sharing a developer pool between autonomous companies.




    Here is step04/company.pt:


        </strong>Projector - People<strong>



          
    •  href = "/" > Home

    •     
    •  href = "/ about.html" > About Projector

    •     
    •  href = "/ acme" > ACME Inc.

    •     
    •  href = "/ people" > People


    People



           tal: repeat = "person people" >
               href = "$ {person.name}" > $ {person.title}
          



    And finally, here - step04/tests.py:
    import  unittest

    class  ProjectorViewsUnitTests (unittest.TestCase):
        def  test_hello_view (self):
            from  views  import  index_view
            result = index_view ({})
            self.assertEqual (len (result.keys ()), 0)

        def  test_about_view (self):
            from  views  import  about_view
            result = about_view ({})
            self.assertEqual (len (result.keys ()), 0)

        def  test_company_view (self):
            from  views  import  company_view
            result = company_view ({})
            self.assertEqual (result ["company" ], "ACME, Inc.")
            self.assertEqual (len (result ["projects"]), 2)

        def  test_people_view (self):
            from  views  import  people_view
            result = people_view ({})
            self.assertEqual (result ["company"], "ACME, Inc." )
            self.assertEqual (len (result ["people"]), 2)

    class  ProjectorFunctionalTests (unittest.TestCase):
        def  setUp (self):
            from  application  import  main
            app = main ()
            from  webtest  import  TestApp
            self.testapp = TestApp ( app)

        def  test_home (self):
            res = self.testapp.get ('/',status = 200)
            self.failUnless ('Home'  in  res.body)

        def  test_it (self):
            res = self.testapp.get ('/', status = 200)
            self.failUnless ('Home'  in  res.body)
            res = self. testapp.get ('/ about.html', status = 200)
            self.failUnless ('autonomous'  in  res.body)
            res = self.testapp.get (' / people ', status = 200)
            self.failUnless (' Susan '  in  res.body)
            res = self.testapp.get (' / acme ', status = 200)
            self.failUnless (' Silly Slogans'  in  res.body)
    Then we write:
    $ nosetests
    what should give us a report on 6 tests.
    Launch the application:
    $ python application.py
    And enjoy the result in the browser -127.0.0.1:8080

    Additional questions

    What happens if you have two view registrations without the @ name attribute, meaning both as the default?
    Is it true that Chameleon (now, if we are talking about the second version) is in any case better to provide you with error messages? Try this by putting some bugs in your Python expressions.
    Will WebTest correctly respond to these errors?
    Does adding .html to your URLs affect anything?

    Analysis

    We started the process of constructing a space of URLs that map to objects and hierarchy in our application. At the moment, we have modeled this using representations.

    Despite the increase in the number of our tests, each of them is still very small. Even such a simple test will still catch most of the stupid errors that pop up during the initial development process. We hope you find nosetests more productive than just clicking.

    Abstracts

    How do the registrations happen under the hood?
    Chameleon, caching, and putting ready-made versions to disk

    Step 05: Create a Master Template

    Also popular now: