SlideShare ist ein Scribd-Unternehmen logo
1 von 10
Downloaden Sie, um offline zu lesen
Fixtures and Fittings...


mark@ekivemark.com                          @ekivemark
        A Newbie voyage in to Django UnitTesting




                                             @ekivemark
New to Django
import django.experts
AUDIENCE = “Technical”

if not(developer)==True
and techie(dangerous):
    bad_idea = give_talk(AUDIENCE)
Why Fixtures?
fixtures =   ['apps/services/fixtures/services_testdata.json',
             #'apps/accounts/fixtures/accounts_test_data.json',
              'apps/coupons/fixtures/accounts_test_data.json',
              'apps/intake/fixtures/intake_inprocess.json']




      • Compare screen data entry to UnitTest
      • Testing pagination
      • Testing Search
                                                     @ekivemark
Make it readable
python manage.py dumpdata accounts --indent=4 >./apps/accounts/
fixtures/testdata.json



[
    {
        "pk": 1,
        "model": "accounts.userprofile",
        "fields": {
            "mobile_phone_number": "7036232789",
            "vid": "820912028666724",
            "pin": 4829,
            "gender": "",
            "fax_number": "",
            "anonymous_patient_id": "76766c7d-25eb-4118-9a4c-b939c80929ce",
            "user": 2,
            "secondary_insurance_or_payer": "",
            "organization": "",
            "primary_insurance_or_payer": ""
        }
    }

]




                                                                              @ekivemark
Localize variables
  • settings_test.py
            a bunch of frequently used test variables
__author__ = 'mark @ekivemark'
"""
Unit Test Framework Standard Variables used across modules

Add
{from ..test.settings_test import * }
to top of {app name}/tests.py in order to be able to use variables without the
{settings_test.} prefix

This avoids setting variable values in multiple locations.
"""

# Edit this number to your cellphone number for testing purposes.
TESTER_CELL_NUMBER = "9999999999"

# User full name = Harvey Ive
USERNAME_FOR_TEST='mark'
PASSWORD_FOR_TEST='123456789'
EMAIL_FOR_TEST="spam1@nospam.org"

SMSCODE_FOR_TEST='9999'

VALID_LASTNAME="First"
VALID_FIRSTNAME="Arthur"


                                                                                 @ekivemark
USERNAME_NOT_TEST='nottester'
PASSWORD_NOT_TEST='password'

SMSCODE_NOT_TEST='9999'
Build a DRY toolset
        • Printing results to the screen
def Test_Start(message=""):
    print "_______________________________________________"
    caller = inspect.getframeinfo(inspect.currentframe().f_back)[2]
    print "Test Harness:" +
inspect.getframeinfo(inspect.currentframe().f_back)[0]
    print term_color.OKGREEN + "Entering:" + caller +
term_color.ENDC
    if message != "":
        print "Notes:"+message
    print "------------------------------------------------"
    return



   Highlighting helps
   when quickly
   scanning output                                                    @ekivemark
Time and Date stamp
                      your tests
# @unittest.skip
class Accounts_Simple_TestCase(TestCase):
    """Background to this test harness
       and prove the test harness works
    """

   # Add your fixtures here
   # fixtures = ['testdata.json']

   def test_basic_addition_Accounts(self):
       """
       Tests that 1 + 1 always equals 2.
       """
       Test_Start("1+1=2")
       answer = self.assertEqual(1 + 1, 2)
       Test_Msg("RESTCat.apps.accounts.tests.py")
       print "     Test Runtime: "+str(datetime.now())
       if answer == None:
           print "     Test Harness ready"
       else:
           print "     This Test Harness has a problem"
       Test_End("RESTCat.apps.accounts.tests.py")

       return

                                                          @ekivemark
User permissions
def check_permission(usr_name="",showprint=False):
    """
    View permissions for a user profile
    """
    if usr_name=="":def give_permission(usr_name="",permit_this="",showprint=False):
        usr_key = 1     """
    else:               Add a permission to a user
        try:            """
            usr_key = User.objects.get(username=usr_name)
        except:         if usr_name=="":     def remove_permission(usr_name="",not_permitted="",showprint=False):
            usr_key = 1     if showprint!=False: """
                                                 remove a permission from a user
                                print "No permission added"
    usr_permission = Permission.objects.filter(user=usr_key)
                            result = ""          """
                        else:
    if showprint!=False:                         if usr_name=="":
                            usr_key = User.objects.get(username=usr_name)
        print "User:" + usr_name
                            if showprint!=False:     if showprint!=False:
        print "key:" + str(usr_key) "got usr_key"
                                print                    print not_permitted+" permission not revoked"
        print "Permissions:"    print usr_key        result = ""
        print usr_permission                     else:
                                print "Now add permission:["+permit_this+"]"
                                                     usr_key = User.objects.get(username=usr_name)
                                                     if showprint!=False:
                            new_permission = Permission(user=usr_key, permission_name=permit_this)
    return usr_permission new_permission.save()          print "revoking ["+not_permitted+"] for "+usr_key
                            result = new_permission
                                                     kill_permission = Permission.objects.filter(user=usr_key,
                        return result        permission_name=not_permitted)
                                                     if showprint!=False:
                                                         print kill_permission

                                                    kill_permission.delete()
                                                    result = "revoked ["+not_permitted+"] for "+str(usr_key)

                                                return result

                                                                                        @ekivemark
?
mark@ekivemark.com       @ekivemark




                         @ekivemark

Weitere ähnliche Inhalte

Mehr von Mark Scrimshire

Mehr von Mark Scrimshire (20)

The Power of Consumer Directed Health Data
The Power of Consumer Directed Health DataThe Power of Consumer Directed Health Data
The Power of Consumer Directed Health Data
 
The Power of Beneficiary-Directed Data (CMS BlueButton on FHIR API Update)
The Power of Beneficiary-Directed Data (CMS BlueButton on FHIR API Update)The Power of Beneficiary-Directed Data (CMS BlueButton on FHIR API Update)
The Power of Beneficiary-Directed Data (CMS BlueButton on FHIR API Update)
 
BlueButton on FHIR at HIMSS'17 HL7 API Symposium
BlueButton on FHIR at HIMSS'17 HL7 API SymposiumBlueButton on FHIR at HIMSS'17 HL7 API Symposium
BlueButton on FHIR at HIMSS'17 HL7 API Symposium
 
CMS BlueButton On FHIR - HIMSS17 Update
CMS BlueButton On FHIR - HIMSS17 UpdateCMS BlueButton On FHIR - HIMSS17 Update
CMS BlueButton On FHIR - HIMSS17 Update
 
CMS BlueButton on FHIR at Cinderblocks3
CMS BlueButton on FHIR at Cinderblocks3 CMS BlueButton on FHIR at Cinderblocks3
CMS BlueButton on FHIR at Cinderblocks3
 
BlueButton on FHIR - HxRefactored 2016
BlueButton on FHIR - HxRefactored 2016BlueButton on FHIR - HxRefactored 2016
BlueButton on FHIR - HxRefactored 2016
 
Aneesh Chopra - HealthCa.mp/dev Keynote. 2016: the Year to participate in the...
Aneesh Chopra - HealthCa.mp/dev Keynote. 2016: the Year to participate in the...Aneesh Chopra - HealthCa.mp/dev Keynote. 2016: the Year to participate in the...
Aneesh Chopra - HealthCa.mp/dev Keynote. 2016: the Year to participate in the...
 
Entrepreneur attitude or job title?
Entrepreneur attitude or job title?Entrepreneur attitude or job title?
Entrepreneur attitude or job title?
 
CMS BlueButton On FHIR for Researchers - Presentation to NIH and PCORI Resear...
CMS BlueButton On FHIR for Researchers - Presentation to NIH and PCORI Resear...CMS BlueButton On FHIR for Researchers - Presentation to NIH and PCORI Resear...
CMS BlueButton On FHIR for Researchers - Presentation to NIH and PCORI Resear...
 
BlueButton On FHIR Presentation to Attachments Work Group at HL7 Meeting Jan ...
BlueButton On FHIR Presentation to Attachments Work Group at HL7 Meeting Jan ...BlueButton On FHIR Presentation to Attachments Work Group at HL7 Meeting Jan ...
BlueButton On FHIR Presentation to Attachments Work Group at HL7 Meeting Jan ...
 
Tap Your Passion for Opportunity
Tap Your Passion for OpportunityTap Your Passion for Opportunity
Tap Your Passion for Opportunity
 
BlueButtonOnFHIR - Payer Briefing
BlueButtonOnFHIR - Payer BriefingBlueButtonOnFHIR - Payer Briefing
BlueButtonOnFHIR - Payer Briefing
 
B bon fhir_workshop
B bon fhir_workshopB bon fhir_workshop
B bon fhir_workshop
 
A Baptism of FHIR - The Layman's intro to HL7 FHIR
A Baptism of FHIR - The Layman's intro to HL7 FHIRA Baptism of FHIR - The Layman's intro to HL7 FHIR
A Baptism of FHIR - The Layman's intro to HL7 FHIR
 
Health2 stat 2015-08-20
Health2 stat 2015-08-20Health2 stat 2015-08-20
Health2 stat 2015-08-20
 
BlueButton and MyHealth at Connected Health 2015 in San Diego
BlueButton and MyHealth at Connected Health 2015 in San DiegoBlueButton and MyHealth at Connected Health 2015 in San Diego
BlueButton and MyHealth at Connected Health 2015 in San Diego
 
The Patient at the Center of a New Healthcare System - All About You at #Cind...
The Patient at the Center of a New Healthcare System - All About You at #Cind...The Patient at the Center of a New Healthcare System - All About You at #Cind...
The Patient at the Center of a New Healthcare System - All About You at #Cind...
 
HealthCare Heal Thyself - The Patient as the hub of a new health system
HealthCare Heal Thyself - The Patient as the hub of a new health systemHealthCare Heal Thyself - The Patient as the hub of a new health system
HealthCare Heal Thyself - The Patient as the hub of a new health system
 
BlueButton on FHIR @HXRconf
BlueButton on FHIR @HXRconf BlueButton on FHIR @HXRconf
BlueButton on FHIR @HXRconf
 
Mongodb and the Health Care Challenge
Mongodb and the Health Care ChallengeMongodb and the Health Care Challenge
Mongodb and the Health Care Challenge
 

Kürzlich hochgeladen

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 

Django/Python Unit Testing talk for Django District

  • 1. Fixtures and Fittings... mark@ekivemark.com @ekivemark A Newbie voyage in to Django UnitTesting @ekivemark
  • 3. import django.experts AUDIENCE = “Technical” if not(developer)==True and techie(dangerous): bad_idea = give_talk(AUDIENCE)
  • 4. Why Fixtures? fixtures = ['apps/services/fixtures/services_testdata.json', #'apps/accounts/fixtures/accounts_test_data.json', 'apps/coupons/fixtures/accounts_test_data.json', 'apps/intake/fixtures/intake_inprocess.json'] • Compare screen data entry to UnitTest • Testing pagination • Testing Search @ekivemark
  • 5. Make it readable python manage.py dumpdata accounts --indent=4 >./apps/accounts/ fixtures/testdata.json [ { "pk": 1, "model": "accounts.userprofile", "fields": { "mobile_phone_number": "7036232789", "vid": "820912028666724", "pin": 4829, "gender": "", "fax_number": "", "anonymous_patient_id": "76766c7d-25eb-4118-9a4c-b939c80929ce", "user": 2, "secondary_insurance_or_payer": "", "organization": "", "primary_insurance_or_payer": "" } } ] @ekivemark
  • 6. Localize variables • settings_test.py a bunch of frequently used test variables __author__ = 'mark @ekivemark' """ Unit Test Framework Standard Variables used across modules Add {from ..test.settings_test import * } to top of {app name}/tests.py in order to be able to use variables without the {settings_test.} prefix This avoids setting variable values in multiple locations. """ # Edit this number to your cellphone number for testing purposes. TESTER_CELL_NUMBER = "9999999999" # User full name = Harvey Ive USERNAME_FOR_TEST='mark' PASSWORD_FOR_TEST='123456789' EMAIL_FOR_TEST="spam1@nospam.org" SMSCODE_FOR_TEST='9999' VALID_LASTNAME="First" VALID_FIRSTNAME="Arthur" @ekivemark USERNAME_NOT_TEST='nottester' PASSWORD_NOT_TEST='password' SMSCODE_NOT_TEST='9999'
  • 7. Build a DRY toolset • Printing results to the screen def Test_Start(message=""): print "_______________________________________________" caller = inspect.getframeinfo(inspect.currentframe().f_back)[2] print "Test Harness:" + inspect.getframeinfo(inspect.currentframe().f_back)[0] print term_color.OKGREEN + "Entering:" + caller + term_color.ENDC if message != "": print "Notes:"+message print "------------------------------------------------" return Highlighting helps when quickly scanning output @ekivemark
  • 8. Time and Date stamp your tests # @unittest.skip class Accounts_Simple_TestCase(TestCase): """Background to this test harness and prove the test harness works """ # Add your fixtures here # fixtures = ['testdata.json'] def test_basic_addition_Accounts(self): """ Tests that 1 + 1 always equals 2. """ Test_Start("1+1=2") answer = self.assertEqual(1 + 1, 2) Test_Msg("RESTCat.apps.accounts.tests.py") print " Test Runtime: "+str(datetime.now()) if answer == None: print " Test Harness ready" else: print " This Test Harness has a problem" Test_End("RESTCat.apps.accounts.tests.py") return @ekivemark
  • 9. User permissions def check_permission(usr_name="",showprint=False): """ View permissions for a user profile """ if usr_name=="":def give_permission(usr_name="",permit_this="",showprint=False): usr_key = 1 """ else: Add a permission to a user try: """ usr_key = User.objects.get(username=usr_name) except: if usr_name=="": def remove_permission(usr_name="",not_permitted="",showprint=False): usr_key = 1 if showprint!=False: """ remove a permission from a user print "No permission added" usr_permission = Permission.objects.filter(user=usr_key) result = "" """ else: if showprint!=False: if usr_name=="": usr_key = User.objects.get(username=usr_name) print "User:" + usr_name if showprint!=False: if showprint!=False: print "key:" + str(usr_key) "got usr_key" print print not_permitted+" permission not revoked" print "Permissions:" print usr_key result = "" print usr_permission else: print "Now add permission:["+permit_this+"]" usr_key = User.objects.get(username=usr_name) if showprint!=False: new_permission = Permission(user=usr_key, permission_name=permit_this) return usr_permission new_permission.save() print "revoking ["+not_permitted+"] for "+usr_key result = new_permission kill_permission = Permission.objects.filter(user=usr_key, return result permission_name=not_permitted) if showprint!=False: print kill_permission kill_permission.delete() result = "revoked ["+not_permitted+"] for "+str(usr_key) return result @ekivemark
  • 10. ? mark@ekivemark.com @ekivemark @ekivemark