Skip to content

Commit

Permalink
added tests for example app configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Amirouche Boubekki committed May 3, 2012
1 parent 37c888c commit 8f914a1
Show file tree
Hide file tree
Showing 27 changed files with 508 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,5 @@
*.sqlite
*~
example_project/tmp/*
example_project/medias
example_project/data.db
Binary file removed example_project/data.db
Binary file not shown.
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import os
import hashlib

from datetime import datetime

Expand Down Expand Up @@ -106,3 +108,10 @@ class AnotherBelovedPartner(BelovedPartner):
"""

packer = ZIPPacker

@property
def archive_name(self):
m = hashlib.sha1()
m.update(os.urandom(24))
filename = m.hexdigest()
return '%s.zip' % filename
16 changes: 0 additions & 16 deletions example_project/example_app/tests.py

This file was deleted.

4 changes: 4 additions & 0 deletions example_project/example_app/tests/__init__.py
@@ -0,0 +1,4 @@
from sequential.zip_packer import *
from sequential.flat_packer import *
from mass.zip_packer import *
from mass.flat_packer import *
58 changes: 58 additions & 0 deletions example_project/example_app/tests/base.py
@@ -0,0 +1,58 @@
import os
import shutil

from django.conf import settings
from django.test import TestCase

from carrier_pigeon.registry import REGISTRY


REGISTRY_COPY = dict(REGISTRY)

class ImplementationBaseTests(TestCase):

@property
def outbox(self):
path = os.path.join(
settings.SITE_ROOT,
'tmp',
'export',
self.tested_configuration_name,
'outbox',
)
return path

def clean_outbox(self):
path = self.outbox
if os.path.exists(path):
shutil.rmtree(path)

def data(self, filename):
path = os.path.join(
os.path.dirname(__file__),
'data',
self.tested_configuration_name,
filename
)
return path

def setUp(self):
# remove from registred configuration
# configuration that we do no test in current test class
REGISTRY.clear()
for key, item in REGISTRY_COPY.iteritems():
print key, item
if key == self.tested_configuration_name:
REGISTRY[key] = item
self.clean_outbox()

def tearDown(self):
self.clean_outbox()

# clean media content
path = os.path.join(
settings.SITE_ROOT,
'medias',
'photo',
)
shutil.rmtree(path)
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<doc><title>An egg &amp; and a chicken</title><updating_date>May 2, 2012</updating_date><content><p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p></content><photo>/medias/1.jpg</photo><linkeds><linked><title>An egg &amp; and a chicken</title><id>1</id></linked><linked><title>The dear and the fawn </title><id>2</id></linked><linked><title>Tazmania devil dog</title><id>3</id></linked></linkeds></doc>
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<doc><title>The dear and the fawn </title><updating_date>May 2, 2012</updating_date><content><p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p></content><photo>/medias/1.jpg</photo><linkeds><linked><title>An egg &amp; and a chicken</title><id>1</id></linked><linked><title>The dear and the fawn </title><id>2</id></linked><linked><title>Tazmania devil dog</title><id>3</id></linked></linkeds></doc>
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<doc><title>An egg &amp; and a chicken</title><updating_date>May 2, 2012</updating_date><content><p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p></content><photo>/medias/1.jpg</photo><linkeds><linked><title>An egg &amp; and a chicken</title><id>1</id></linked><linked><title>The dear and the fawn </title><id>2</id></linked><linked><title>Tazmania devil dog</title><id>3</id></linked></linkeds></doc>
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<doc><title>The dear and the fawn </title><updating_date>May 2, 2012</updating_date><content><p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p></content><photo>/medias/1.jpg</photo><linkeds><linked><title>An egg &amp; and a chicken</title><id>1</id></linked><linked><title>The dear and the fawn </title><id>2</id></linked><linked><title>Tazmania devil dog</title><id>3</id></linked></linkeds></doc>
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<doc type="flatmassexport"><title>An egg &amp; and a chicken</title><updating_date>May 3, 2012</updating_date><content><p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p></content><photo>/photos/1.jpg</photo></doc>
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<doc type="flatmassexport"><title>The dear and the fawn </title><updating_date>May 3, 2012</updating_date><content><p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p></content><photo>/photos/1.jpg</photo></doc>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<doc><title>An egg &amp; and a chicken</title><updating_date>May 2, 2012</updating_date><content><p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p></content><photo>/medias/1.jpg</photo></doc>
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<doc><title>The dear and the fawn </title><updating_date>May 2, 2012</updating_date><content><p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p></content><photo>/medias/1.jpg</photo></doc>
Empty file.
116 changes: 116 additions & 0 deletions example_project/example_app/tests/mass/flat_packer.py
@@ -0,0 +1,116 @@
import os
import shutil
import hashlib
import zipfile
from StringIO import StringIO
from datetime import datetime
from datetime import timedelta

from django.conf import settings
from django.core.files import File
from django.core.management import call_command

from carrier_pigeon.models import ItemToPush

from example_app.models import Photo
from example_app.models import Story
from example_app.tests.base import ImplementationBaseTests


class FlatMassExportTest(ImplementationBaseTests):
"""Check proper behavior of WeeklyDigest mass zip export configuration"""

tested_configuration_name = 'flatmassexport'

def setUp(self):
super(FlatMassExportTest, self).setUp()

settings.CARRIER_PIGEON_PUSH_URLS = {
self.tested_configuration_name: (
"dummy://user:pwd@ftp.weeklydigest.tld",
),
}

# create Photo instance

# build a random filename
m = hashlib.sha1()
m.update(os.urandom(24))
filepath = '/tmp/%s.jpeg' % m.hexdigest()
# open and write photo filename with data
fd = open(filepath, 'w')
fd.write('coucou')
fd.close()
fd = open(filepath)
# create Photo object
photo = Photo(
title='Egg',
credits='Chicken',
caption='An egg created by a chicken',
original_file=File(fd),
)
photo.save()

# don't forget to clean
os.remove(filepath)

# build story objects
self.stories = []

# the following two stories will be selected
# by the configuration
story = Story(
title='An egg & and a chicken',
workflow_state=Story.WORKFLOW_STATE.ONLINE,
content='a'*500,
photo=photo,
updating_date=datetime.now()
)
story.save()
self.stories.append(story)
story = Story(
title='The dear and the fawn ',
workflow_state=Story.WORKFLOW_STATE.ONLINE,
content='a'*500,
photo=photo,
updating_date=datetime.now()
)
story.save()
self.stories.append(story)

# This story is not selected
story = Story(
title='Tazmania devil dog',
workflow_state=Story.WORKFLOW_STATE.OFFLINE,
content='a'*500,
photo=photo,
updating_date=datetime.now() - timedelta(days=99)
)
story.save()

def _test_content(self):
for story in self.stories:
filename = 'example_app_story_%s.xml' % story.pk
filepath = os.path.join(
self.outbox,
filename,
)
with open(filepath) as f:
with open(self.data(filename)) as d:
self.assertEqual(
f.read(),
d.read(),
)

photopath = os.path.join(
self.outbox,
'photos',
'1.jpg',
)
self.assertTrue(os.path.exists(photopath))
with open(photopath) as f:
self.assertEqual(f.read(), 'coucou')

def test_pigeon_push(self):
call_command('pigeon_mass_push', 'flatmassexport')
self._test_content()
127 changes: 127 additions & 0 deletions example_project/example_app/tests/mass/zip_packer.py
@@ -0,0 +1,127 @@
import os
import shutil
import hashlib
import zipfile
from StringIO import StringIO
from datetime import date
from datetime import datetime
from datetime import timedelta

from django.conf import settings
from django.core.files import File
from django.core.management import call_command

from carrier_pigeon.models import ItemToPush

from example_app.models import Photo
from example_app.models import Story
from example_app.tests.base import ImplementationBaseTests


class WeeklyDigestTests(ImplementationBaseTests):
"""Check proper behavior of WeeklyDigest mass zip export configuration"""

tested_configuration_name = 'weeklydigest'

def setUp(self):
super(WeeklyDigestTests, self).setUp()

settings.CARRIER_PIGEON_PUSH_URLS = {
self.tested_configuration_name: (
"dummy://user:pwd@ftp.weeklydigest.tld",
),
}

# create Photo instance

# build a random filename
m = hashlib.sha1()
m.update(os.urandom(24))
filepath = '/tmp/%s.jpeg' % m.hexdigest()
# open and write photo filename with data
fd = open(filepath, 'w')
fd.write('coucou')
fd.close()
fd = open(filepath)
# create Photo object
photo = Photo(
title='Egg',
credits='Chicken',
caption='An egg created by a chicken',
original_file=File(fd),
)
photo.save()

# don't forget to clean
os.remove(filepath)

# build story objects
self.stories = []

# the following two stories will be selected
# by the configuration
story = Story(
title='An egg & and a chicken',
workflow_state=Story.WORKFLOW_STATE.ONLINE,
content='a'*500,
photo=photo,
updating_date=date(2012, 5, 2)
)
story.save()
self.stories.append(story)
story = Story(
title='The dear and the fawn ',
workflow_state=Story.WORKFLOW_STATE.ONLINE,
content='a'*500,
photo=photo,
updating_date=date(2012, 5, 2)
)
story.save()
self.stories.append(story)

# This story is not selected
story = Story(
title='Tazmania devil dog',
workflow_state=Story.WORKFLOW_STATE.OFFLINE,
content='a'*500,
photo=photo,
updating_date=date(2012, 5, 2)
)
story.save()

def _test_content(self):
for story in self.stories:
filename = 'example_app_story_%s.xml' % story.pk
filepath = os.path.join(
self.outbox,
filename,
)
with open(filepath) as f:
with open(self.data(filename)) as d:
self.assertEqual(
f.read(),
d.read(),
)

photopath = os.path.join(
self.outbox,
'photos',
'1.jpg',
)
self.assertTrue(os.path.exists(photopath))
with open(photopath) as f:
self.assertEqual(f.read(), 'coucou')

def test_pigeon_push(self):
call_command('pigeon_mass_push', 'weeklydigest')
listdir = os.listdir(self.outbox)
self.assertEqual(len(listdir), 1)
filename = listdir[0]
self.assertEqual(filename, 'weeklydigest.zip')
path = os.path.join(
self.outbox,
filename,
)
zzz = zipfile.ZipFile(path)
zzz.extractall(self.outbox)
self._test_content()
Empty file.

0 comments on commit 8f914a1

Please sign in to comment.