domingo, 2 de noviembre de 2008

Introduction to Python/Django testing: Doctests

Nota: copié todo el artículo porque el link al post original no me funcionó... (este es el link original)

Eric Holscher: Introduction to Python/Django testing: Doctests

This is the first in a series of blog posts and screencasts that will walk you through how to test your Django application. These posts will focus more on how to get things done in Django, but note that a lot of the content is applicable to pure python as well. A lot of best practices are codified into Django's testing framework, so that we don't have to worry about them! I will try to point them out as we are using them through, because they are good things to know.

The currently planned structure for this series is below. Please comment if there is something that you think is missing, or something that I shouldn't do. This is subject to change, a lot, as well, so your feedback will help direct it. Also note that most or all of this content is available in the Django and Python documentation, and I will try and point there and not re-invent the wheel. I hope that these posts will take a more practical look, and try to point out some pit falls and other things that are useful.

Outline

  • Basic Doc tests
  • Basic Unit tests
  • Comparison of Unit tests vs. Doc test
  • Fixtures
  • Using Mock objects
  • Third party testing tools
  • Writing your own test runner
  • Getting code coverage for your tests

Where to start

I'm assuming that you already have a project that you're working on that you would like to test. There are two different ways of putting tests inside of your django project. You can add a tests.py file and put your tests inside of there. You can also define a tests/ directory and put your tests in files inside of that. For these tutorials it is assumed that the second is the way things are done. It makes it a lot easier when you can break your tests out into logical files.

Doctests

These can go in two places inside your django project. You can put them in your models.py file, in the Docstring for your modules. This is a good way to show usage of your models, and to provide basic testing. The official docs have some great examples of this.

The other place your Doctests can go is inside your tests directory. A doctests file is usually pretty simple. A doctest is just a large string, so there isn't much else to put besides a string. Usually you want to use the triple quote, multi-line string delimiter to define them. That way your " and 's inside of your doctests don't break anything.

"""
This is my worthless test.
>>> print "wee"
wee
>>> print False
False
"""

You can go ahead and put that in a file in your tests/ directory, I named it doctst. My application that I'm writing tests for is mine, because it's the code for my website. Make sure that directory has an __init__.py as well, to signify that it is a python module.

Now here is the tricky part; go ahead and try and run your test suite. In your project directory run ./manage.py test APPNAME. It will show you that you have passed 0 tests. 0 tests? We just defined one.

You need to go into your init.py file and put some stuff in there.

import doctst
__test__ = {
'Doctest': doctst
}

You are importing the doc test module and then adding it to the __test__ dictionary. You have to do this because of the way that python handles looking for doc tests. It looks for a __test__ dictionary inside of your module, and if that exists it looks through it, executing all docstrings as doctests. For more information look at the Python docs.

Now you should be able to go ahead and run the tests and see the magical Ran 1 test in 0.003s OK that all testers live for. This is little bit of overhead really threw me off when I was trying to break my tests.py out into the tests/ directory.

So now we have a test suite that is worthless, but you know how to use doc tests. If you didn't notice, the doctest format is simply the output of your default python shell, so when you are testing your code on the command line and it works, you can simple copy and paste it into your tests. This makes writing doc tests almost trivial. Note however, that they are somewhat fragile, and shouldn't be used for everything. In the next segment, we will talk about unit tests. Then we will compare the two and see when you should use each.

jueves, 16 de octubre de 2008

Como cambiar lista de opciones en un ModelForm

# Model
class Branch(models.Model):
nombre = models.CharField(max_length=32)
archivado = models.BooleanField()

# Model
class SolicitudPP(models.Model):
branch = models.ForeignKey(Branch)

# ModelForm
class SolicitudPPForm(ModelForm):
class Meta:
model = SolicitudPP

# Como cambiar lista de "branches" a mostrar:
f = SolicitudPPForm()
f.fields['branch'].queryset = Branch.objects.filter(archivado=False)


Artículo original: http://oebfare.com/blog/2008/feb/23/changing-modelchoicefield-queryset/

sábado, 30 de agosto de 2008

FindBugs, Hudson, Groovy y errores de alta prioridad

Usando Groovy encontré una manera fácil de forzar el fallo de un "build" cuando FindBugs haya encontrado problemas con prioridad alta. Este script Groovy puede agregarse en un scritp Ant o como un ultimo paso dentro de un proyecto Hudson. Esto es necesario porque el plugin de Findbugs para Hudson (en la versión estable) no se puede configurar para que marque el build como inestable dependiendo de la cantidad de warnings de ALTA prioridad (aunque esto ya está en desarrollo, e implementado en HEAD).

Usando Groovy + XPath:


import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.*

content = new File("ant_build/findbugs/findbugs-report.xml").getText("UTF-8")

builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
doc = builder.parse(new ByteArrayInputStream(content.bytes))
xpath = '/BugCollection/BugInstance[@priority="1"]'
expr = XPathFactory.newInstance().newXPath().compile(xpath)
nodes = expr.evaluate(doc, XPathConstants.NODESET)
if(nodes.length>0) {
throw new RuntimeException("FindBugs reporto ${nodes.length} warning(s) de prioridad ALTA")
}


Usando GPath es mucho más fácil:


def bugCollection = new XmlParser().parseText(content)
highPriorityBugs = bugCollection.BugInstance.findAll { it['@priority'] == '1' }
if(highPriorityBugs.size()>0) {
throw new RuntimeException("FindBugs reporto ${highPriorityBugs.size()} warning(s) de prioridad ALTA")
}


El 'target' de Ant puede definirse así:


<target name="groovy">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy">
<classpath>
<fileset dir="lib/groovy">
<include name="*.jar"></include>
</fileset>
</classpath>
<groovy>
<!--[CDATA[ // SCRIPT GROOVY AQUI ]]-->
</groovy>
</taskdef>
</target>

.

lunes, 18 de agosto de 2008

Tuning Your PostgreSQL Server

Encontré un artículo con buenas referencias, con información para afinar la configuración de PostgreSql.

http://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server

sábado, 3 de mayo de 2008

Relaciones (1:1, 1:n: m:n) con AppEngine

Encontré un link donde se explican las distintas formas de relacionar entidades. Está muy simple y describe los pro y contras de cada opción.
http://daily.profeth.de/2008/04/er-modeling-with-google-app-engine.html

jueves, 6 de marzo de 2008

Salto de páginas con OpenOffice

En la página de OpenOffice están las instrucciones para incorporar una carátula a un documento, pero de forma tal que NO afecte a la numeración de nuestras páginas.

http://wiki.services.openoffice.org/wiki/Documentation/OOoAuthors_User_Manual/Writer_Guide/Numbering_pages

lunes, 11 de febrero de 2008

Limpieza de paquetes de Ubuntu / Debian

Buscando algunos consejos para limpiar mi instalación de Ubuntu 7.10 (luego de hacer el upgrade desde 7.04) encontré http://www.tuxapuntes.com/tux/content/view/416/122/.

En resumen, el comando más útil para esta tarea es deborphan. Por ejemplo, para limpiar los archivos de configuración de paquetes eliminados podemos usar:

deborphan --find-config | xargs -r aptitude -y purge

Para ver la información de los paquetes que purgaríamos con el comando anterior, podemos usar:

deborphan --find-config | xargs -r dpkg -l

El comando deborphan posee varias opciones más de "busqueda" de paquetes ('deborphan --help' las lista)

jueves, 7 de febrero de 2008

Herramientas de testing para Python

Encontré un link muy bueno con una listado bastante extenso de las herramientas existentes para realizar el testing de programas desarrollados con Python.

http://pycheesecake.org/wiki/PythonTestingToolsTaxonomy

lunes, 7 de enero de 2008

Cheat sheet de PostgreSql

Encontré 2 páginas con "cheat sheets" de PostgreSql (no sé cuál sería la traducción de "cheat sheets", quizá "machetes", pero para lo que no saben: son resúmenes sobre lenguajes, programas, etc. con información normalmente usada de referencia cuando no nos acordamos cómo especificar ciertas cosas).
Estas "cheat sheets" las bajé de aqui y aqui.

StaticGenerator para aplicaciones Django

Esta herramienta convierte un sitio dinámico desarrollando de Django en un conjunto de HTMLs listos para servir. Es una forma muy común de mejorar significativamente la performance de un sitio. Es distribuido bajo la licencia MIT. http://superjared.com/projects/static-generator/

En el sitio declaran que se puede mejorar la performance de 500 request por segundo a 7000 req/s:

  • mod_python, no caching: 100 req/s
  • mod_python, memcached: 500 req/s
  • StaticGenerator file: 7,000 req/s

sábado, 29 de diciembre de 2007

Tamaño de tablas/BD en PostgreSql

PostgreSql posee algunas funciones muy útiles para determinar el tamaño en disco que ocupa una tabla o una base de datos. Estas son:
  • pg_relation_size
  • pg_total_relation_size
  • pg_database_size
Para visualizar mas facilmente el resultado de las funciones anteriores, de puede utilizar pg_size_pretty. Por ejemplo:

intranet=# select pg_size_pretty(pg_total_relation_size('requerimiento'));
pg_size_pretty
----------------
23 MB
(1 row)


Estas y otras funciones están documentadas en el sitio de PostgreSql.

miércoles, 12 de diciembre de 2007

Threads en JavaScript usando XPCOM

Encontré una pagina con algunos ejemplos de como utilizar threads con XPCOM, muy útil para desarrollar extensiones de Firefox (y supongo también Thunderbird y el resto de los "primos" de Mozilla).

Las instrucciones están en http://skrul.com/blog/projects/threaddemo y el código accesible via SVN en http://skrul.com/svn/threaddemo/trunk/

lunes, 10 de diciembre de 2007

Download Statusbar + Checksum para bajar

He "empaquetado" en un XPI los cambios en Download Statusbar... Es realmente facil! Simplemente descomprimi el XPI original, le aplique los parches, cambie el nro. de versión y lo volví a comprimir!

Pueden bajar Download Statusbar + Checksum desde aqui.

Tooltips con prototip

Encontré una librería para manejar tooltips, está bastante completa y permite generarlos de varias formas: http://www.nickstakenburg.com/projects/prototip/ (utiliza scriptaculous y prototype).

Download Statusbar + Checksum

Empecé a investigar para aprender a desarrollar extensiones para Firefox, y se me ocurrió empezar implementado una mejora a la extensión "Download Statusbar", que consista en mostrar las opciones para calcular el MD5 o SHA1 del archivo bajado.
Inicialmente mi idea era utilizar el comando correspondiente de linux (md5sum, sha1sum, etc), pero luego descubrí que Firefox ya posee una implementación de varios algoritmos de hashing.

En esta primera versión la interface se ve así:




En el caso anterior, elegí calcular el SHA256 del archivo bajado. El resultado se muestra en un popup:



Pueden bajar el diff (a aplicar al código en CVS del día 2007-12-08), o ver el diff coloreado.

Si te interesa empezar en el desarrollo de extensiones, te recomiendo que inicies con las siguientes páginas (y sigas los links de esas páginas... hay mucha información!!):

http://developer.mozilla.org/en/docs/Setting_up_extension_development_environment
http://kb.mozillazine.org/Getting_started_with_extension_development
http://kb.mozillazine.org/Dev_:_Extensions

miércoles, 28 de noviembre de 2007

Cómo configurar Gnome para que parezca MacOSX

Encontré un HOWTO muy detallado sobre como configurar todos los aspectos de tu linux para que parezca MacOSX (incluyendo sonidos, temas de firefox, splash de gnome y grub, etc.).

http://www.howtoforge.com/mac4lin_make_linux_look_like_a_mac

viernes, 23 de noviembre de 2007

viernes, 9 de noviembre de 2007

Prototype checklist

Para los que estamos aprendiendo Prototype, es bueno ver artículos que te enseñan buenas prácticas, ya que normalmente hay muchas formas de hacer lo mismo, pero entre las diversas formas hay unas mejores que otras.

Estos dos artículos tratan del buen y mal uso de ciertas funcionalidades de Prototype
http://thinkweb2.com/projects/prototype-checklist/
http://thinkweb2.com/projects/prototype/?p=3

jueves, 8 de noviembre de 2007

La NSA accede a productos y redes de Windows

"Los amplios rangos de IP publicados por Cryptome son utilizados por la NSA, por contratistas del sector privado que trabajan con la NSA y por agencias gubernamentales no estadounidenses amigas de la NSA para acceder tanto a sistemas independientes como a redes ejecutando productos de Microsoft. Esto incluye el espionaje de "smart phones" ejecutando Microsoft Mobile."

Cada vez existen más razones para pasarse a Linux!!!!

Artículo completo

viernes, 5 de octubre de 2007

Comunicación Java / Python con XML-RPC

Estuve investigando cómo conectar Java y Python. Lo primero que se me ocurrió fue XMLRPC, y verdaderamente fue muy facil!
La info la saqué de:

El servidor Java:



1 package ar.com.hgdeoro.xmlrpc;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Random;
8
9 import org.apache.xmlrpc.server.PropertyHandlerMapping;
10 import org.apache.xmlrpc.server.XmlRpcServer;
11 import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
12 import org.apache.xmlrpc.webserver.WebServer;
13
14 public class Server {
15
16 private static final int port = 8000;
17
18 public static void main(String[] args) throws Exception {
19
20 final WebServer webServer = new WebServer(port);
21 final XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
22 final PropertyHandlerMapping phm = new PropertyHandlerMapping();
23
24 phm.addHandler("Calculator", Calculator.class);
25
26 xmlRpcServer.setHandlerMapping(phm);
27 XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
28 serverConfig.setEnabledForExtensions(true);
29 serverConfig.setContentLengthOptional(false);
30 webServer.start();
31 }
32
33 public static class Calculator {
34
35 public int add(int i1, int i2) {
36 return i1 + i2;
37 }
38
39 public List<Integer> randomList(int num) {
40 if (num <= 0) {
41 throw new RuntimeException("num debe ser > 0");
42 }
43 final List<Integer> list = new ArrayList<Integer>();
44 for (int i = 0; i < num; i++) {
45 list.add(new Random().nextInt());
46 }
47 return list;
48 }
49
50 public Map<Integer, Integer> randomMap(int num) {
51 if (num <= 0) {
52 throw new RuntimeException("num debe ser > 0");
53 }
54 final Map<Integer, Integer> list = new HashMap<Integer, Integer>();
55 for (int i = 0; i < num; i++) {
56 list.put(Integer.valueOf(i), Integer.valueOf(new Random().nextInt()));
57 }
58 return list;
59 }
60
61 public Map<String, Integer> randomMap2(int num) {
62 if (num <= 0) {
63 throw new RuntimeException("num debe ser > 0");
64 }
65 final Map<String, Integer> list = new HashMap<String, Integer>();
66 for (int i = 0; i < num; i++) {
67 list.put(Integer.valueOf(i).toString(), Integer.valueOf(new Random().nextInt()));
68 }
69 return list;
70 }
71
72 }
73 }



El cliente Java:



1 package ar.com.hgdeoro.xmlrpc;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5
6 import org.apache.xmlrpc.XmlRpcException;
7 import org.apache.xmlrpc.client.XmlRpcClient;
8 import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
9
10 public class Client {
11
12 public static void dump(Object obj) {
13 System.out.println("======================================================================");
14 System.out.println("Clase: " + obj.getClass());
15 System.out.println("toString(): " + obj.toString());
16
17 if (obj.getClass().isArray()) {
18 Object[] objs = (Object[]) obj;
19 System.out.println("---------- ARRAY ----------");
20 System.out.println("Tamanio: " + objs.length);
21 for (int i = 0; i < objs.length; i++) {
22 System.out.println("Object[" + i + "]: " + objs[i]);
23 }
24 }
25 }
26
27 public static void main(String args[]) throws MalformedURLException, XmlRpcException {
28 final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
29 config.setServerURL(new URL("http://127.0.0.1:8000"));
30 final XmlRpcClient client = new XmlRpcClient();
31 client.setConfig(config);
32
33 Object obj = client.execute("add", new Object[] { new Integer(33), new Integer(9) });
34 dump(obj);
35
36 obj = client.execute("random_list", new Object[] { new Integer(14) });
37 dump(obj);
38
39 obj = client.execute("random_map", new Object[] { new Integer(9) });
40 dump(obj);
41
42 try {
43 obj = client.execute("random_map", new Object[] { new Integer(-1) });
44 throw new RuntimeException("La llamada remote deberia haber generado excepcion");
45 } catch (XmlRpcException e) {
46 e.printStackTrace();
47 }
48 }
49
50 }



El servidor Python:



1 from SimpleXMLRPCServer import SimpleXMLRPCServer
2 import random
3
4 def add(x,y):
5 return x + y
6
7 def random_list(num):
8 num = int(num)
9 if(num <= 0):
10 raise Exception("num debe ser mayor a 0")
11 list = []
12 for i in range(num):
13 list.append(random.randint(1, 999999))
14 return list
15
16 def random_map(num):
17 num = int(num)
18 if(num <= 0):
19 raise Exception("num debe ser mayor a 0")
20 map = {}
21 for i in range(num):
22 key = "elem_%d" % i
23 map[key] = random.randint(1, 999999)
24 return map
25
26 if __name__ == '__main__':
27 server = SimpleXMLRPCServer(("127.0.0.1", 8000))
28 server.register_function(add, 'add')
29 server.register_function(random_list, 'random_list')
30 server.register_function(random_map, 'random_map')
31 server.serve_forever()



El cliente Python:



1 import xmlrpclib
2 from pprint import pprint
3
4 if __name__ == '__main__':
5 proxy = xmlrpclib.Server('http://localhost:8000/xmlrpc')
6 pprint(proxy.Calculator.add(2,3))
7 pprint(proxy.Calculator.randomList(8))
8 pprint(proxy.Calculator.randomMap2(8))



Al ejecutar el cliente Python, obtengo:


5
[-816678161,
1435519307,
-648064553,
1198345419,
-332555131,
1349182312,
1286868314,
1756261972]
{'0': -1238582501,
'1': -711045786,
'2': 1242324380,
'3': 453035820,
'4': 727377110,
'5': 1443025388,
'6': -280665009,
'7': -1336015917}


y al ejecutar el cliente Java (esa excepcion que se muestra es un error producido adrede, para ejemplificar qué pasa cuando en el servidor se produce una excepción):


======================================================================
Clase: class java.lang.Integer
toString(): 42
======================================================================
Clase: class [Ljava.lang.Object;
toString(): [Ljava.lang.Object;@11a698a
---------- ARRAY ----------
Tamanio: 14
Object[0]: 140980
Object[1]: 917718
Object[2]: 951678
Object[3]: 405149
Object[4]: 758315
Object[5]: 412932
Object[6]: 488689
Object[7]: 721836
Object[8]: 658873
Object[9]: 590256
Object[10]: 667364
Object[11]: 552500
Object[12]: 784894
Object[13]: 865306
======================================================================
Clase: class java.util.HashMap
toString(): {elem_8=85992, elem_7=47193, elem_6=599464, elem_5=331449, elem_4=982397, elem_3=88299, elem_2=723048, elem_1=462681, elem_0=565753}
org.apache.xmlrpc.XmlRpcException: :num debe ser mayor a 0
at org.apache.xmlrpc.client.XmlRpcStreamTransport.readResponse(XmlRpcStreamTransport.java:186)
at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:145)
at org.apache.xmlrpc.client.XmlRpcHttpTransport.sendRequest(XmlRpcHttpTransport.java:94)
at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.sendRequest(XmlRpcSunHttpTransport.java:44)
at org.apache.xmlrpc.client.XmlRpcClientWorker.execute(XmlRpcClientWorker.java:53)
at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:166)
at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:136)
at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:125)
at ar.com.hgdeoro.xmlrpc.Client.main(Client.java:43)