Commit 2c5f1f29 authored by Michal Čihař's avatar Michal Čihař

Test saving of files

Saving of file should generate more or less same file as original.

Issue #910
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent bcc342c9
......@@ -23,6 +23,7 @@ File format specific behavior.
import tempfile
from StringIO import StringIO
from unittest import TestCase, SkipTest
from django.test import SimpleTestCase
from weblate.trans.formats import (
AutoFormat, PoFormat, AndroidFormat, PropertiesFormat,
JSONFormat, RESXFormat, PhpFormat, XliffFormat, TSFormat,
......@@ -84,7 +85,7 @@ class AutoLoadTest(TestCase):
self.assertIsInstance(store.store, pofile)
class AutoFormatTest(TestCase):
class AutoFormatTest(SimpleTestCase):
FORMAT = AutoFormat
FILE = TEST_PO
BASE = TEST_POT
......@@ -103,6 +104,41 @@ class AutoFormatTest(TestCase):
self.assertEqual(storage.mimetype, self.MIME)
self.assertEqual(storage.extension, self.EXT)
def test_save(self):
# Read test content
with open(self.FILE, 'r') as handle:
testdata = handle.read()
# Create test file
testfile = tempfile.NamedTemporaryFile(suffix='.{0}'.format(self.EXT))
try:
# Write test data to file
testfile.write(testdata)
testfile.flush()
# Parse test file
storage = self.FORMAT(testfile.name)
# Save test file
storage.save()
# Read new content
with open(testfile.name, 'r') as handle:
newdata = handle.read()
# Check if content matches
self.assert_same(newdata, testdata)
finally:
testfile.close()
def assert_same(self, newdata, testdata):
"""Content aware comparison.
This can be implemented in subclasses to implement content
aware comparing of translation files.
"""
self.assertEqual(newdata.strip(), testdata.strip())
def test_find(self):
storage = self.FORMAT(self.FILE)
unit, add = storage.find_unit('', self.FIND)
......@@ -130,6 +166,11 @@ class AutoFormatTest(TestCase):
)
class XMLMixin(object):
def assert_same(self, newdata, testdata):
self.assertXMLEqual(newdata, testdata)
class PoFormatTest(AutoFormatTest):
FORMAT = PoFormat
......@@ -153,6 +194,12 @@ class PropertiesFormatTest(AutoFormatTest):
FIND_MATCH = 'Ignore'
MATCH = '\n'
def assert_same(self, newdata, testdata):
self.assertEqual(
newdata.strip().splitlines(),
testdata.strip().splitlines(),
)
class JSONFormatTest(AutoFormatTest):
FORMAT = JSONFormat
......@@ -165,6 +212,9 @@ class JSONFormatTest(AutoFormatTest):
MATCH = '{}\n'
BASE = ''
def assert_same(self, newdata, testdata):
self.assertJSONEqual(newdata, testdata)
class PhpFormatTest(AutoFormatTest):
FORMAT = PhpFormat
......@@ -179,7 +229,7 @@ class PhpFormatTest(AutoFormatTest):
FIND_MATCH = 'bar'
class AndroidFormatTest(AutoFormatTest):
class AndroidFormatTest(XMLMixin, AutoFormatTest):
FORMAT = AndroidFormat
FILE = TEST_ANDROID
MIME = 'application/xml'
......@@ -190,7 +240,7 @@ class AndroidFormatTest(AutoFormatTest):
EXPECTED_PATH = 'res/values-cs-rCZ/strings.xml'
class XliffFormatTest(AutoFormatTest):
class XliffFormatTest(XMLMixin, AutoFormatTest):
FORMAT = XliffFormat
FILE = TEST_XLIFF
BASE = TEST_XLIFF
......@@ -203,7 +253,7 @@ class XliffFormatTest(AutoFormatTest):
EXPECTED_PATH = 'loc/cs_CZ/default.xliff'
class RESXFormatTest(AutoFormatTest):
class RESXFormatTest(XMLMixin, AutoFormatTest):
FORMAT = RESXFormat
FILE = TEST_RESX
MIME = 'text/microsoft-resx'
......@@ -221,7 +271,7 @@ class RESXFormatTest(AutoFormatTest):
raise SkipTest('resx not supported!')
class TSFormatTest(AutoFormatTest):
class TSFormatTest(XMLMixin, AutoFormatTest):
FORMAT = TSFormat
FILE = TEST_TS
BASE = TEST_TS
......@@ -232,6 +282,13 @@ class TSFormatTest(AutoFormatTest):
EXPECTED_PATH = 'ts/cs_CZ.ts'
MATCH = '<TS version="2.0" language="cs">'
def assert_same(self, newdata, testdata):
# Comparing of XML with doctype fails...
self.assertXMLEqual(
newdata.replace('<!DOCTYPE TS>', ''),
testdata.replace('<!DOCTYPE TS>', '')
)
class OutputTest(TestCase):
def test_json_default_output(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment