User Tools

Site Tools


devel:hacking

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
devel:hacking [2012/05/07 11:17]
morten fix url
devel:hacking [2013/06/19 07:45]
morten fix jenkins url
Line 66: Line 66:
 long-term goal to rewrite the remaining Java backend code to Python. long-term goal to rewrite the remaining Java backend code to Python.
  
-Currently (as of February 2011), NAV consists mostly of Python code, +Currently (as of September 2012), NAV consists mostly of Python code, 
-with a few remaining backend systems written in Java (there'​s also the +with one remaining backend systems written in Java (eventEngine).
-Netmap Java Applet).+
  
   * We will only accept new code written in Python (except when it   * We will only accept new code written in Python (except when it
Line 125: Line 124:
 # -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
 </​code>​ </​code>​
 +
 +===== Javascript =====
 +
 +When writing javascript code try to focus on modules not pages. If the code is html-related,​ it should take selectors or objects as input and concern itself solely about those. This makes for much easier testing and reuse. And of course - write the tests first. ​
 +
 +When the module is done you write a controller for the page that plugs the needed plugins to the page elements. This should fail gracefully if the needed elements are not present.
 +
 +NAVs javascript uses [[http://​requirejs.org/​|require.js]] - use this to create modules and specify dependencies. ​
 +
 +Pro tip is to create ''​require_config.dev.js''​ in ''​media/​js/''​ and add the following configuration to requirejs: ​
 +<​code>​require.urlArgs = "​bust="​ +  (new Date()).getTime();</​code>​ This makes sure your not using cached resources in your browser when developing, which browsers loves to do! See [[http://​requirejs.org/​docs/​api.html#​config-urlArgs|config-urlArgs]] in requirejs documentation for «details». The ''​require_config.dev.js''​ is added in global HG ignore.
 +
 +==== Accessing resources with ajax requiring authentication ====
 +
 +As your authenticated session might have timed out due to idle (no activity), resources will return 500 Internal Error if you do not supply the important ''//​X-NAV-AJAX//''​ header on your ajax requests.
 +
 +So make sure to include the ''​**X-NAV-AJAX**''​ header so you will get proper HTTP response code in the reponse from your request. ​
 +
 +NAV has a shortcut for fixing this in ''​default.js''​ (adds a function in the public namespace NAV) which attaches the required handlers for jQuery by doing:
 +<​code>​NAV.addGlobalAjaxHandlers()</​code>​
  
 ====== Database ====== ====== Database ======
Line 352: Line 371:
 </​code>​ </​code>​
  
-===== Hudson/​Jenkins ===== 
-We use //Hudson// (soon-to-be //​Jenkins//​) for Continuous Integration 
-of NAV.  All the automated tests are run each time new changesets are 
-pushed to the NAV repositories. ​ Hudson also runs pylint to create 
-stats on code quality. 
  
-Our Hudson installation is available on http://​nav.uninett.no/​hudson ​.+===== Javascript testing ===== 
 + 
 +Testing of javascript is in its infancy in NAV. We are currently using [[http://​busterjs.org/​|buster.js]] as testing toolkit. 
 + 
 +To install buster.js install [[http://​nodejs.org/​|node]] and then: 
 +<​code>​ 
 +npm install -g buster 
 +</​code>​ 
 + 
 +As we use [[http://​requirejs.org/​|require.js]] you need the AMD module of buster aswell. Install it in the /media/js directory:​ 
 +<​code>​ 
 +npm install buster-amd 
 +</​code>​ 
 + 
 +To run the tests you need to 
 +  - Start a buster server by typing ''​buster-server''​ 
 +  - Capture browsers by pointing browsers to the buster-server (default localhost:​1111) 
 +  - Go to /media/js 
 +  - Run the tests by typing ''​buster-test''​ 
 + 
 +All tests are located under ''​media/​js/​tests/''​. Create new tests there. For syntax, assertions and related stuff take a look at the tests already there and [[http://​busterjs.org/​docs/​|the buster docs]]. 
 +===== Jenkins ===== 
 + 
 +We use //Jenkins// (formerly //Hudson//) for Continuous Integration testing of 
 +NAV.  All the automated tests are run each time new changesets are pushed to 
 +the NAV repositories. ​ Jenkins also runs pylint to create stats on code 
 +quality. 
 + 
 +Our Jenkins ​installation is available on https://ci.nav.uninett.no/​ . 
 + 
 +===== Tips and tricks ===== 
 + 
 +===== Make fixtures for integration testing ===== 
 + 
 +<​code>​ 
 +from django.core import serializers 
 +from nav.models.manage import Netbox 
 + 
 +fixtures = serializers.serialize("​xml",​ Netbox.objects.all()[:​2]) 
 +</​code>​ 
 + 
 +Fixtures can so be used in your integration tests by extending 
 +the test case DjangoTransactionTestCase in nav.tests.cases 
 + 
 +See nav.tests.integration.l2trace_test for an example on applying 
 +fixtures for your particular test case.  
 + 
 +Also keep in mind you have to make sure you have the model 
 +dependency in correct order when importing. 
 +Example: Netbox contains a location to a Room where it is located, 
 +you have to make sure Room's are imported first before importing 
 +Netbox'​s 
 + 
 +See https://​docs.djangoproject.com/​en/​dev/​topics/​serialization/​ 
 + 
 +TODO: Be able to use [[https://​docs.djangoproject.com/​en/​dev/​ref/​django-admin/#​dumpdata-appname-appname-appname-model|django-admin'​s management command: dumpdata]] 
 +to create fixtures.  
  
  
Line 383: Line 454:
     addressed to the //nav-dev// mailing list.  Please **do not     addressed to the //nav-dev// mailing list.  Please **do not
     patchbomb** the mailing list with multiple emails.     patchbomb** the mailing list with multiple emails.
- 
devel/hacking.txt · Last modified: 2014/11/05 10:57 by morten