openlp/tests/functional/openlp_core_lib/test_projectordb.py

188 lines
7.0 KiB
Python
Raw Normal View History

2014-10-06 19:10:03 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2015-12-31 22:46:06 +00:00
# Copyright (c) 2008-2016 OpenLP Developers #
2014-10-06 19:10:03 +00:00
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
Package to test the openlp.core.ui.projectordb find, edit, delete
record functions.
PREREQUISITE: add_record() and get_all() functions validated.
"""
import os
2014-10-06 19:10:03 +00:00
from unittest import TestCase
from openlp.core.lib.projector.db import Projector, ProjectorDB, ProjectorSource
2014-10-06 19:10:03 +00:00
from tests.functional import MagicMock, patch
2016-01-07 21:07:55 +00:00
from tests.resources.projector.data import TEST_DB, TEST1_DATA, TEST2_DATA, TEST3_DATA
2014-10-06 19:10:03 +00:00
def compare_data(one, two):
"""
Verify two Projector() instances contain the same data
"""
return one is not None and \
two is not None and \
one.ip == two.ip and \
one.port == two.port and \
one.name == two.name and \
one.location == two.location and \
one.notes == two.notes
def compare_source(one, two):
"""
Verify two ProjectorSource instances contain the same data
"""
return one is not None and \
two is not None and \
one.projector_id == two.projector_id and \
one.code == two.code and \
one.text == two.text
def add_records(projector_db, test):
2014-10-06 19:10:03 +00:00
"""
Add record if not in database
"""
record_list = projector_db.get_projector_all()
2014-10-06 19:10:03 +00:00
if len(record_list) < 1:
added = False
for record in test:
added = projector_db.add_projector(record) or added
2014-10-06 19:10:03 +00:00
return added
for new_record in test:
added = None
for record in record_list:
if compare_data(record, new_record):
break
added = projector_db.add_projector(new_record)
2014-10-06 19:10:03 +00:00
return added
class TestProjectorDB(TestCase):
"""
Test case for ProjectorDB
"""
def setUp(self):
"""
Set up anything necessary for all tests
"""
with patch('openlp.core.lib.projector.db.init_url') as mocked_init_url:
2016-01-07 21:07:55 +00:00
if os.path.exists(TEST_DB):
os.unlink(TEST_DB)
mocked_init_url.return_value = 'sqlite:///%s' % TEST_DB
self.projector = ProjectorDB()
2014-10-06 19:10:03 +00:00
2015-11-26 20:44:19 +00:00
def tearDown(self):
"""
Clean up
"""
self.projector = None
2014-10-06 19:10:03 +00:00
def find_record_by_ip_test(self):
"""
Test find record by IP
"""
# GIVEN: Record entries in database
2016-01-07 21:07:55 +00:00
add_records(self.projector, [Projector(**TEST1_DATA), Projector(**TEST2_DATA)])
2014-10-06 19:10:03 +00:00
# WHEN: Search for record using IP
2016-01-07 21:07:55 +00:00
record = self.projector.get_projector_by_ip(TEST2_DATA['ip'])
2014-10-06 19:10:03 +00:00
# THEN: Verify proper record returned
2016-01-07 21:07:55 +00:00
self.assertTrue(compare_data(Projector(**TEST2_DATA), record),
2014-10-06 19:10:03 +00:00
'Record found should have been test_2 data')
def find_record_by_name_test(self):
"""
Test find record by name
"""
# GIVEN: Record entries in database
2016-01-07 21:07:55 +00:00
add_records(self.projector, [Projector(**TEST1_DATA), Projector(**TEST2_DATA)])
2014-10-06 19:10:03 +00:00
# WHEN: Search for record using name
2016-01-07 21:07:55 +00:00
record = self.projector.get_projector_by_name(TEST2_DATA['name'])
2014-10-06 19:10:03 +00:00
# THEN: Verify proper record returned
2016-01-07 21:07:55 +00:00
self.assertTrue(compare_data(Projector(**TEST2_DATA), record),
2014-10-06 19:10:03 +00:00
'Record found should have been test_2 data')
def record_delete_test(self):
"""
Test record can be deleted
"""
# GIVEN: Record in database
2016-01-07 21:07:55 +00:00
add_records(self.projector, [Projector(**TEST3_DATA), ])
record = self.projector.get_projector_by_ip(TEST3_DATA['ip'])
2014-10-06 19:10:03 +00:00
# WHEN: Record deleted
self.projector.delete_projector(record)
# THEN: Verify record not retrievable
2016-01-07 21:07:55 +00:00
found = self.projector.get_projector_by_ip(TEST3_DATA['ip'])
2014-10-06 19:10:03 +00:00
self.assertFalse(found, 'test_3 record should have been deleted')
def record_edit_test(self):
"""
Test edited record returns the same record ID with different data
"""
# GIVEN: Record entries in database
2016-01-07 21:07:55 +00:00
add_records(self.projector, [Projector(**TEST1_DATA), Projector(**TEST2_DATA)])
2014-10-06 19:10:03 +00:00
# WHEN: We retrieve a specific record
2016-01-07 21:07:55 +00:00
record = self.projector.get_projector_by_ip(TEST1_DATA['ip'])
2014-10-06 19:10:03 +00:00
record_id = record.id
# WHEN: Data is changed
2016-01-07 21:07:55 +00:00
record.ip = TEST3_DATA['ip']
record.port = TEST3_DATA['port']
record.pin = TEST3_DATA['pin']
record.name = TEST3_DATA['name']
record.location = TEST3_DATA['location']
record.notes = TEST3_DATA['notes']
2014-10-06 19:10:03 +00:00
updated = self.projector.update_projector(record)
self.assertTrue(updated, 'Save updated record should have returned True')
2016-01-07 21:07:55 +00:00
record = self.projector.get_projector_by_ip(TEST3_DATA['ip'])
2014-10-06 19:10:03 +00:00
# THEN: Record ID should remain the same, but data should be changed
self.assertEqual(record_id, record.id, 'Edited record should have the same ID')
2016-01-07 21:07:55 +00:00
self.assertTrue(compare_data(Projector(**TEST3_DATA), record), 'Edited record should have new data')
def source_add_test(self):
"""
2015-01-20 15:44:55 +00:00
Test source entry for projector item
"""
# GIVEN: Record entries in database
2016-01-07 21:07:55 +00:00
projector1 = Projector(**TEST1_DATA)
self.projector.add_projector(projector1)
item = self.projector.get_projector_by_id(projector1.id)
item_id = item.id
2015-01-20 15:44:55 +00:00
# WHEN: A source entry is saved for item
source = ProjectorSource(projector_id=item_id, code='11', text='First RGB source')
self.projector.add_source(source)
# THEN: Projector should have the same source entry
2015-01-20 15:44:55 +00:00
item = self.projector.get_projector_by_id(item_id)
self.assertTrue(compare_source(item.source_list[0], source))
2016-01-07 21:07:55 +00:00