Skip to content

Commit

Permalink
Merge pull request #1 from phillip-hopper/feature/TravisSetup
Browse files Browse the repository at this point in the history
Add unit tests and travis-ci
  • Loading branch information
Phil Hopper committed Aug 29, 2016
2 parents b3244c7 + a1d785a commit 330ec13
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[run]
source =
obs

omit =
*/resources/*
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: python
python:
- '2.7'
- '3.5'

install:
- pip install -r requirements.txt
- pip install coveralls

script: coverage run test-setup.py test

after_success:
- coveralls
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Build Status](https://travis-ci.org/unfoldingWord-dev/obs_tools.svg)](https://travis-ci.org/unfoldingWord-dev/obs_tools) [![Coverage Status](https://coveralls.io/repos/github/unfoldingWord-dev/obs_tools/badge.svg)](https://coveralls.io/github/unfoldingWord-dev/obs_tools)

# unfoldingWord OBS Tools

A collection of Python scripts that have proven useful and have been reused.
Expand Down
2 changes: 1 addition & 1 deletion obs/obs_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from datetime import datetime
import os
from json import JSONEncoder
import chapters_and_frames
from obs import chapters_and_frames
from general_tools.file_utils import load_json_object
from general_tools.languages import Language

Expand Down
15 changes: 15 additions & 0 deletions test-setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from setuptools import setup

setup(
name="obs_tools",
version="0.0.1",
author="unfoldingWord",
author_email="unfoldingword.org",
description="Unit test setup file.",
keywords="",
url="https://github.org/unfoldingWord-dev/obs_tools",
packages=['obs'],
long_description='Unit test setup file',
classifiers=[],
test_suite='tests'
)
Empty file added tests/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions tests/test_chapters_and_frames.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from __future__ import print_function, unicode_literals
from unittest import TestCase
from obs.chapters_and_frames import frame_counts


class TestChaptersAndFrames(TestCase):

def test_chapters_and_frames(self):

# there should be 50 chapters
self.assertEqual(50, len(frame_counts))

# the first chapter should have 16 frames
self.assertEqual(16, frame_counts[0])

# the last chapter should have 17 frames
self.assertEqual(17, frame_counts[49])
98 changes: 98 additions & 0 deletions tests/test_obs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from __future__ import print_function, unicode_literals

import codecs
import glob
import json
import os
import datetime
from unittest import TestCase
from general_tools.file_utils import load_json_object
from obs.obs_classes import OBS, OBSChapter, OBSEncoder


class TestOBS(TestCase):

def test_ts_obs(self):
"""
Tests all the functions of the OBS class, as used when importing from a tS repository
"""
today = ''.join(str(datetime.date.today()).rsplit('-')[0:3])

# load and process the manifest
content_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'resources', 'ts')
file_name = os.path.join(content_dir, 'manifest.json')
manifest = load_json_object(file_name)

lang = manifest['target_language']['id']
obs_obj = OBS()
obs_obj.date_modified = today
obs_obj.direction = manifest['target_language']['direction']
obs_obj.language = lang

self.assertEqual('fr', obs_obj.language)
self.assertEqual('ltr', obs_obj.direction)

# load the frames for each chapter
obs_obj.chapters = TestOBS.load_obs_chapters(content_dir)
obs_obj.chapters.sort(key=lambda c: c['number'])

self.assertEqual(50, len(obs_obj.chapters))

# verify the data
verified = obs_obj.verify_all()
self.assertTrue(verified)

# load language data
lang_dict = OBS.load_lang_strings()
self.assertGreater(len(lang_dict), 7000)

# dump the OBS to a json string that could be written to a file
obs_json_str = json.dumps(obs_obj, sort_keys=True, cls=OBSEncoder)
self.assertTrue('"app_words":' in obs_json_str)
self.assertTrue('"chapters":' in obs_json_str)
self.assertTrue('"date_modified":' in obs_json_str)
self.assertTrue('"language":' in obs_json_str)
self.assertTrue('"direction":' in obs_json_str)

@staticmethod
def load_obs_chapters(content_dir):
print('Reading OBS pages...', end=' ')
chapters = []
img_url = 'https://cdn.door43.org/obs/jpg/360px/obs-en-{0}.jpg'
for story_num in range(1, 51):
chapter_num = str(story_num).zfill(2)
story_dir = os.path.join(content_dir, chapter_num)
obs_chapter = OBSChapter()
obs_chapter.number = chapter_num

# get the translated chapter ref
with codecs.open(os.path.join(story_dir, 'reference.txt'), 'r', encoding='utf-8-sig') as in_file:
obs_chapter.ref = in_file.read()

# get the translated chapter title
with codecs.open(os.path.join(story_dir, 'title.txt'), 'r', encoding='utf-8-sig') as in_file:
obs_chapter.title = in_file.read()

# loop through the frames for this chapter
frame_list = glob.glob('{0}/[0-9][0-9].txt'.format(story_dir))
for frame_file in frame_list:
with codecs.open(frame_file, 'r', encoding='utf-8-sig') as in_file:
frame_text = in_file.read()

frame_id = chapter_num + '-' + os.path.splitext(os.path.basename(frame_file))[0]

frame = {'id': frame_id,
'img': img_url.format(frame_id),
'text': frame_text
}

obs_chapter.frames.append(frame)

# sort the frames by id
obs_chapter.frames.sort(key=lambda f: f['id'])

# add this chapter to the OBS object
chapters.append(obs_chapter)

print('finished.')
return chapters
20 changes: 20 additions & 0 deletions tests/test_obs_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from __future__ import print_function, unicode_literals
import os
from unittest import TestCase
from obs.obs_classes import OBSStatus


class TestOBSStatus(TestCase):

def test_default_constructor(self):
status_obj = OBSStatus()
self.assertEqual('1', status_obj.checking_level)
self.assertEqual('en', status_obj.source_text)
self.assertEqual('', status_obj.checking_entity)

def test_file_name_constructor(self):
file_name = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'resources', 'status.json')
status_obj = OBSStatus(file_name)
self.assertEqual('3', status_obj.checking_level)
self.assertEqual('en', status_obj.source_text)
self.assertEqual('Unit testing team', status_obj.checking_entity)

0 comments on commit 330ec13

Please sign in to comment.