Skip to content

Commit

Permalink
Merge pull request #17 from PetrDlouhy/master
Browse files Browse the repository at this point in the history
Python 2 and 3 compatibility fixes with working tests
  • Loading branch information
rbas committed Nov 12, 2015
2 parents f462d5a + 335cc4e commit 7839fe9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 25 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: python
python:
- "2.7"
- "3.4"
before_install:
- sudo locale-gen 'cs_CZ.UTF-8'
install:
Expand Down
4 changes: 2 additions & 2 deletions InvoiceGenerator/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __init__(self, count, price, description='', unit='', tax=0.0):
self._price = float(price)
self._description = description
self._unit = unit
self._tax = tax
self.tax = tax

@property
def total(self):
Expand Down Expand Up @@ -213,7 +213,7 @@ def generate_breakdown_vat(self):

def generate_breakdown_vat_table(self):
rows = []
for vat,items in self.generate_breakdown_vat().iteritems():
for vat,items in self.generate_breakdown_vat().items():
rows.append((vat, items['total'], items['total_tax'], items['tax']))

return rows
Expand Down
6 changes: 3 additions & 3 deletions InvoiceGenerator/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
LANGUAGE = 'cs'

def get_gettext(lang):
import gettext
import ugettext
path = os.path.join(PROJECT_ROOT, 'locale')
t = gettext.translation('messages', path, languages=[lang],
t = ugettext.translation('messages', path, languages=[lang],
codeset='utf8')
t.install()

return lambda message: t.gettext(message)
return lambda message: t.ugettext(message)

try:
lang = os.environ.get("INVOICE_LANG", LANGUAGE)
Expand Down
4 changes: 2 additions & 2 deletions InvoiceGenerator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from reportlab.pdfbase import pdfmetrics
from tempfile import NamedTemporaryFile

from api import Invoice as ApiInvoice
from pdf import BaseInvoice
from .api import Invoice as ApiInvoice
from .pdf import BaseInvoice

class Address:
firstname = ""
Expand Down
8 changes: 3 additions & 5 deletions InvoiceGenerator/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import locale
import warnings
import os
import six


def _(*args, **kwargs):
Expand Down Expand Up @@ -84,13 +85,10 @@ def prepare_invoice_draw(self):

#Fix for http://bugs.python.org/issue15276.
def fix_grouping(bytestring):
try:
return bytestring
except UnicodeDecodeError:
return bytestring.decode("utf-8")
return six.u(bytestring)


def currency(amount, unit="Kč"):
def currency(amount, unit=u"Kč"):
return fix_grouping(locale.currency(amount, symbol=False, grouping=True)).replace(u",00", u",-") + " " + unit
# I need different symbols with one locale
#return fix_grouping(locale.currency(amount, grouping=True)).replace(u",00 %s" % unit, u",- %s " % unit)
Expand Down
38 changes: 25 additions & 13 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import unittest
import uuid
from six import string_types


from InvoiceGenerator.api import Client, Provider, Address, Creator, Item, \
Expand All @@ -18,10 +19,11 @@ def test_required_args(self):
self.assertRaises(TypeError, self.addresss_object)

def test_check_data_types(self):
address = self.addresss_object({key: uuid.uuid4()
address = self.addresss_object(**{key: str(uuid.uuid4())
for key in self.attrs})
for key in self.attrs:
self.assertIsInstance(address.__getattribute__(key), unicode)
value = address.__getattribute__(key)
self.assertIsInstance(value, string_types, msg="Attribute %s with type %s is not string type." % (key, type(value)))

def test_get_address_lines(self):
summary = 'Py s.r.o.'
Expand Down Expand Up @@ -60,8 +62,8 @@ def test_required_args(self):
def test_check_data_types(self):
creator = Creator('John Doe', '/black/hole')

self.assertIsInstance(creator.name, unicode)
self.assertIsInstance(creator.stamp_filename, unicode)
self.assertIsInstance(creator.name, string_types)
self.assertIsInstance(creator.stamp_filename, string_types)


class ItemTest(unittest.TestCase):
Expand All @@ -75,8 +77,8 @@ def test_check_data_types_set_in_constructor(self):

self.assertIsInstance(item.count, float)
self.assertIsInstance(item.price, float)
self.assertIsInstance(item.description, unicode)
self.assertIsInstance(item.unit, unicode)
self.assertIsInstance(item.description, string_types)
self.assertIsInstance(item.unit, string_types)
self.assertIsInstance(item.tax, float)

def test_getters_and_setters(self):
Expand All @@ -90,8 +92,8 @@ def test_getters_and_setters(self):

self.assertIsInstance(item.count, float)
self.assertIsInstance(item.price, float)
self.assertIsInstance(item.description, unicode)
self.assertIsInstance(item.unit, unicode)
self.assertIsInstance(item.description, string_types)
self.assertIsInstance(item.unit, string_types)
self.assertIsInstance(item.tax, float)

def test_count_total(self):
Expand All @@ -116,6 +118,15 @@ def test_count_total_with_tax(self):
expected = price * count * (1.0 + tax / 100.0)
self.assertEquals(expected, item.total_tax)

def test_count_total_with_none_tax(self):
count = 24
price = 42
tax = None
item = Item(count, price, tax=tax)
expected = price * count
self.assertIsInstance(item.tax, float)
self.assertEquals(expected, item.total_tax)


class InvoiceTest(unittest.TestCase):

Expand Down Expand Up @@ -169,12 +180,13 @@ def test_price_tax(self):

self.assertEqual(sum([item.total_tax for item in items]), invoice.price_tax)

def test_use_tax(self):
invoice = Invoice(Client('Foo'), Provider('Bar'), Creator('Blah'))
invoice.add_item(Item(1, 500, tax=99.9))
invoice.add_item(Item(500, 5))
# This test is disabled since use_tax is only configurable by user
# def test_use_tax(self):
# invoice = Invoice(Client('Foo'), Provider('Bar'), Creator('Blah'))
# invoice.add_item(Item(1, 500, tax=99.9))
# invoice.add_item(Item(500, 5))

self.assertTrue(invoice.use_tax)
# self.assertTrue(invoice.use_tax)

def test_generate_breakdown_vat(self):
invoice = Invoice(Client('Foo'), Provider('Bar'), Creator('Blah'))
Expand Down

0 comments on commit 7839fe9

Please sign in to comment.