44

Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Django Data Model Revisited and

Testing Introduction

CS 370 SE Practicum, Cengiz Günay

(Some slides courtesy of Eugene Agichtein and the Internets)

CS 370, Günay (Emory) Django Data and Testing Intro Spring 2014 1 / 8

Page 3: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Agenda

Warm-up project:

Late submissions due midnight today, deploy on DutchDutch compatibility:

I Python 2.7 and 3.2 (python3) availableI Django 1.6.2I Ask for optional packages!

Main project:

Submit ideas, due Tuesday 2/25 before class

Upcoming:

Guest: Student startup LikePlum on Thursday 2/27

Today:

Django data manipulationIntroduction to testing

CS 370, Günay (Emory) Django Data and Testing Intro Spring 2014 3 / 8

Page 4: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Agenda

Warm-up project:

Late submissions due midnight today, deploy on DutchDutch compatibility:

I Python 2.7 and 3.2 (python3) availableI Django 1.6.2I Ask for optional packages!

Main project:

Submit ideas, due Tuesday 2/25 before class

Upcoming:

Guest: Student startup LikePlum on Thursday 2/27

Today:

Django data manipulationIntroduction to testing

CS 370, Günay (Emory) Django Data and Testing Intro Spring 2014 3 / 8

Page 5: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Agenda

Warm-up project:

Late submissions due midnight today, deploy on DutchDutch compatibility:

I Python 2.7 and 3.2 (python3) availableI Django 1.6.2I Ask for optional packages!

Main project:

Submit ideas, due Tuesday 2/25 before class

Upcoming:

Guest: Student startup LikePlum on Thursday 2/27

Today:

Django data manipulationIntroduction to testing

CS 370, Günay (Emory) Django Data and Testing Intro Spring 2014 3 / 8

Page 6: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Agenda

Warm-up project:

Late submissions due midnight today, deploy on DutchDutch compatibility:

I Python 2.7 and 3.2 (python3) availableI Django 1.6.2I Ask for optional packages!

Main project:

Submit ideas, due Tuesday 2/25 before class

Upcoming:

Guest: Student startup LikePlum on Thursday 2/27

Today:

Django data manipulationIntroduction to testing

CS 370, Günay (Emory) Django Data and Testing Intro Spring 2014 3 / 8

Page 7: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Entry/Exit Surveys

Exit survey: Mobile Web & Agile Intro

What is a responsive webapp? What principles does it use?

Now that you know the pros and cons of using mobile webappsvs. native apps, which one would you prefer?

What are the main principles of Agile development?

Entry survey: Testing

What was the worst bug you encountered/heard about in a program?

How do you usually test your code? What was the most advantageyou gained by testing?

CS 370, Günay (Emory) Django Data and Testing Intro Spring 2014 4 / 8

Page 8: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Django Data Model Revisited

Page 9: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Hypothetical Book database

• Entities:

– Author (first_name, last_name, email, photo)

– Book (title, authors, publisher, publication_date)

– Publisher (name, address, city, state, country, website)

• Relationships:

– Author-Book, Book-Publisher

• Logical design (board)

2/12/2013 CS 370, Spring 2012 2

Page 10: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Better with a diagram?

Diamonds indicate aggregation

Made with Dia

CS 370, Günay (Emory) Django Data and Testing Intro Spring 2014 6 / 8

Page 11: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Better with a diagram?

Diamonds indicate aggregation

Made with Dia

CS 370, Günay (Emory) Django Data and Testing Intro Spring 2014 6 / 8

Page 12: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Better with a diagram?

Diamonds indicate aggregation

Made with Dia

CS 370, Günay (Emory) Django Data and Testing Intro Spring 2014 6 / 8

Page 13: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Django Implementation

class Publisher(models.Model): name = models.CharField(maxlength=30) …website = models.URLField() #implicit: ID (primary key)

class Author(models.Model): first_name = models.CharField(maxlength=30) last_name = models.CharField(maxlength=40) email = models.EmailField() headshot = models.ImageField(upload_to='/tmp')

class Book(models.Model): title = models.CharField(maxlength=100) authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) publication_date = models.DateField()

2/12/2013 CS 370, Spring 2012 3

Page 14: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Important Notes

• Table names are automatically generated by combining the name of the app (books) and the lowercase name of the model (publisher, book, and author). You can override this behavior.

• Django adds a primary key for each table automatically — the id fields.

• By convention, Django appends "_id" to the foreign key field name.

• The foreign key relationship is made explicit by a REFERENCES statement – publisher = models.ForeignKey(Publisher)

2/12/2013 CS 370, Spring 2012 4

Page 15: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Basic Data Access (API)

• from books.models import Publisher

• p1 = Publisher(name='Addison-Wesley', address='75 Arlington Street', ... city='Boston', state_province='MA', country='U.S.A.', ... website='http://www.apress.com/')

• p1.save()

• To retrieve objects from the database, use the attribute Publisher.objects

– publisher_list = Publisher.objects.all()

2/12/2013 CS 370, Spring 2012 5

Page 16: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

ToString()

• Add __str__() method to our Publisher object

– class Publisher(models.Model):

def __str__(self): return self.name

2/12/2013 CS 370, Spring 2012 6

Page 17: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Selecting objects

• Publisher.objects.filter(name="Apress Publishing")

– SELECT id, name, address, city, state_province, country, website FROM book_publisher WHERE name = 'ApressPublishing';

• Publisher.objects.filter(name__contains="press")

• Publisher.objects.get(name="Apress Publishing")

– Can cause exceptions, use Try/Except

2/12/2013 CS 370, Spring 2012 7

Page 18: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Deleting objects

• p = Publisher.objects.get(name="Addison-Wesley")• p.delete()

• Deletions are permanent, so be careful! • avoid deleting objects unless you absolutely have to —

relational databases don’t do “undo” so well, and restoring from backups is painful.

• It’s often a good idea to add “active” flags to your data models. You can look up only “active” objects, and simply set the active field to False instead of deleting the object.

2/12/2013 CS 370, Spring 2012 8

Page 19: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Metadata

• Django uses internal class Meta as a place to specify additional metadata about a model. It’s completely optional, but it can do some very useful things.

– class Publisher(models.Model):….class Meta:

ordering = ["name"]

• https://docs.djangoproject.com/en/1.4/ref/models/options/

2/12/2013 CS 370, Spring 2012 9

Page 20: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Making Changes to a Database Schema

• If you add or change a model’s field, or if you delete a model, you’ll need to make the change in your database manually.

• Django will complain if a model contains a field that has not yet been created in the database table. This will cause an error the first time you use the Django database API to query the given table (i.e., it will happen at code execution time, not at compilation time).

• Django does not care if a database table contains columns that are not defined in the model.

• Django does not care if a database contains a table that is not represented by a model.

2/12/2013 CS 370, Spring 2012 10

Page 21: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Making Changes to Database Schema (2)

• http://www.djangobook.com/en/2.0/chapter05.html

2/12/2013 CS 370, Spring 2012 11

Page 22: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Onwards and upwords…

• Django book:

– http://www.djangobook.com/en/2.0/

• Common mistakes/gotchas:

– https://code.djangoproject.com/wiki/NewbieMistakes

2/12/2013 CS 370, Spring 2012 12

Page 23: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Introduction to Testing

Page 24: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

• "Code without tests is broken by design.“ – J. Kaplan-Moss

• Providing automated tests for your code is a way to repeatedly ensure … that the code you wrote … works as advertised.

• When you first get started, writing tests … sounds like extra work. But simple tests are easy to write and having some tests is better than no tests at all.

2/12/2013 CS 370, Spring 2012 13

“Agile” Dev: Test Early and Often

Page 25: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Testing: The First Bugs

© Ammann & Offutt 14

―It has been just so in all of my

inventions. The first step is an

intuition, and comes with a burst,

then difficulties arise—this thing

gives out and [it is] then that

'Bugs'—as such little faults and

difficulties are called—show

themselves and months of

intense watching, study and

labor are requisite. . .‖ – Thomas

Edison

Hopper’s ―bug‖ (moth stuck in a

relay on an early machine)

Page 26: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Costly Software Failures

© Ammann & Offutt 15

NIST report, ―The Economic Impacts of Inadequate Infrastructure for Software Testing‖ (2002)

– Inadequate software testing costs the US alone between $22 and $59 billion annually

– Better approaches could cut this amount in half

Huge losses due to web application failures– Financial services : $6.5 million per hour (just in USA!)

– Credit card sales applications : $2.4 million per hour (in USA)

In Dec 2006, Amazon.com’s BOGO offer turned into a double discount

2007 : Symantec says that most security vulnerabilities are due to faulty software

Page 27: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Spectacular Software Failures

© Ammann & Offutt 16

Major failures: Ariane 5 explosion, Mars Polar Lander, Intel’s Pentium FDIV bug

Poor testing of safety-critical software can cost lives :

THERAC-25 radiation machine: 3 dead

Mars Polar

Lander crash

site?

THERAC-25 design

Ariane 5:

exception-handling

bug : forced self

destruct on maiden

flight (64-bit to 16-bit

conversion: about

370 million $ lost)

NASA’s Mars lander: September 1999, crashed due to a units integration fault

Toyota brakes : Dozens dead, thousands of crashes

Page 28: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Software is a Skin that Surrounds Our Civilization

Introduction to Software Testing (Ch 1)

© Ammann & Offutt 17

Quote due to Dr. Mark Harman

Page 29: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Airbus 319 Safety Critical Software Control

© Ammann & Offutt 18

Loss of autopilot

Loss of both the commander’s and the co-pilot’s

primary flight and navigation displays !

Loss of most flight deck lighting and intercom

Page 30: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Northeast Blackout of 2003

© Ammann & Offutt 19

Affected 10 million

people in Ontario,

Canada

Affected 40 million

people in 8 US

states

Financial losses of

$6 Billion USD

508 generating

units and 256

power plants shut

down

The alarm system in the energy management system failed due

to a software error and operators were not informed of the power

overload in the system

Page 31: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Test Design in Context

• Test Design is the process of designing input values that will effectively test software

• Test design is one of several activitiesfor testing software

– Most mathematical

– Most technically challenging

• http://www.cs.gmu.edu/~offutt/softwaretest/

© Ammann & Offutt 20

Page 32: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Types of Test Activities

• Testing can be broken up into four general types of activities1. Test Design

2. Test Automation

3. Test Execution

4. Test Evaluation

• Each type of activity requires different skills, background knowledge, education and training

© Ammann & Offutt 21

1.a) Criteria-based

1.b) Human-based

Page 33: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

1. Test Design – (a) Criteria-Based

• This is the most technical job in software testing

• Requires knowledge of :

– Discrete math

– Programming

– Testing

• This is intellectually stimulating, rewarding, and challenging

• Test design is analogous to software architecture on the development side

© Ammann & Offutt 22

Page 34: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

2. Test Automation

• Fairly straightforward programming – small pieces and simple algorithms

• Requires very little theory

• Who is responsible for determining and embedding the expected outputs ?

– Test designers may not always know the expected outputs

– Test evaluators need to get involved early to help with this

© Ammann & Offutt 23

Page 35: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

3. Test Execution

• This is easy – and trivial if the tests are automated

• Requires basic computer skills

– Interns

– Employees with no technical background

• If, for example, GUI tests are not well automated, this requires a lot of manual labor

© Ammann & Offutt 24

Page 36: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Types of Test Activities – Summary

• These four general test activities are quite different

• It is a poor use of resources to use people inappropriately

© Ammann & Offutt 25

1a. Design Design test values to satisfy engineering goals

Criteria Requires knowledge of discrete math, programming and testing

1b. Design Design test values from domain knowledge and intuition

Human Requires knowledge of domain, UI, testing

2. Automation

Embed test values into executable scripts

Requires knowledge of scripting

3. Execution Run tests on the software and record the results

Requires very little knowledge

4. Evaluation Evaluate results of testing, report to developers

Requires domain knowledge

Page 37: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Common Types of Testing

• Unit tests cover very small, highly-specific areas of code.

– There's usually relatively few interactions with other areas of the software.

– useful for critical components

• Integration tests cover multiple different facets of the application working together to produce a result.

– ensure that data flow is right

– handle multiple user interactions.2/12/2013 CS 370, Spring 2012 26

Page 38: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

© Ammann & Offutt 27

What is Unit Testing?

• unittest: testing framework used to write and run repeatable automated tests

• A structure for writing test drivers

• unittest features include:– Assertions for testing expected results

– Test features for sharing common test data

– Test suites for easily organizing and running tests

– Graphical and textual test runners

• Junit (equivalent for Java) is widely used in industry

Page 39: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

© Ammann & Offutt 28

Unit Tests (cont’d)

• Can be used to test …– … an entire object– … part of an object – a method or some interacting

methods– … interaction between several objects

• It is primarily for unit and integration testing, not system testing

• Each test is embedded into one test method• A test class contains one or more test methods• Test classes include :

– A test runner to run the tests (main())– A collection of test methods– Methods to set up the state before and update the state

after each test and before and after all tests

Page 40: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Software Testing in Django

• http://toastdriven.com/blog/2011/apr/10/guide-to-testing-in-django/

• References: http://www.slideshare.net/simon/advanced-django

• https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs

• http://djangotesting.com/en/latest/index.html

– Unit tests

– Fixtures

– Practical examples2/12/2013 CS 370, Spring 2012 29

Page 41: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

What To Test?

• If your model has custom methods, you should test that, usually with unit tests.

• Custom views, forms, template tags, context processors

2/12/2013 CS 370, Spring 2012 30

Page 42: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

When to Test?

• Test-first is where you write the necessary tests to demonstrate proper behavior of the code BEFOREyou write the code to solve the problem at hand.

• Test-after is when you've already written the code to solve the problem, then you go back & create tests to make sure the behavior of the code you wrote is correct.

2/12/2013 CS 370, Spring 2012 31

Page 43: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Basic Unit Testing in Django (Tutorial)

• https://docs.djangoproject.com/en/1.4/topics/testing/

• http://djangotesting.com/en/latest/basic_unittests.html

• http://djangotesting.com/en/latest/views.html

2/12/2013 CS 370, Spring 2012 32

Page 44: Django Data Model Revisited and estingT Introductioncengiz/CS370-pract-softeng-sp14/... · 2014-09-18 · Exit survey: Mobile Web & Agile Intro What is a responsive webapp? What principles

Next Class:

User stories, use cases, scenarios, UI design (online make-up class)