Skip to content

Instantly share code, notes, and snippets.

@SRomansky
Created September 27, 2014 06:25
Show Gist options
  • Save SRomansky/341ebb33baaa57ee4a57 to your computer and use it in GitHub Desktop.
Save SRomansky/341ebb33baaa57ee4a57 to your computer and use it in GitHub Desktop.
test_database.py with pytest decorator and fixture.
#!/usr/bin/python
# -*- coding: utf-8 -*-
# freeseer - vga/presentation capture software
#
# Copyright (C) 2013 Free and Open Source Software Learning Centre
# http://fosslc.org
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
# For support, questions, suggestions or any other inquiries, visit:
# http://wiki.github.com/Freeseer/freeseer/
import os
import shutil
import tempfile
import unittest
import pytest
from PyQt4 import QtSql
from freeseer.framework.config.profile import Profile
from freeseer.framework.database import QtDBConnector
from freeseer.framework.plugin import PluginManager
from freeseer.framework.presentation import Presentation
profile_path = ""
@pytest.fixture
def db(scope="session"):
global profile_path
profile_path = tempfile.mkdtemp()
profile = Profile(profile_path, 'testing')
db_file = os.path.join(profile_path, 'presentations.db')
db = QtDBConnector(db_file, PluginManager(profile))
return db
def db_cleanup(db):
db.__close_table()
shutil.rmtree(profile_path)
def test_query_instances(db):
@pytest.mark.parametrize("input,expected", [
# Tests that check if a query is returned
(db.get_talks(), QtSql.QSqlQuery),
(db.get_events(), QtSql.QSqlQuery),
(db.get_talk_ids(), QtSql.QSqlQuery),
(db.get_talks_by_event("SC2011"), QtSql.QSqlQuery),
(db.get_talks_by_room("T105"), QtSql.QSqlQuery),
# Test that a presentation is returned
(db.get_presentation(1), Presentation),
# Tests that check if a model is returned
(db.get_presentations_model(), QtSql.QSqlTableModel),
(db.get_events_model(), QtSql.QSqlQueryModel),
(db.get_rooms_model("SC2011"), QtSql.QSqlQueryModel),
(db.get_talks_model("SC2011", "T105"), QtSql.QSqlQueryModel)
])
def test_isinstance(input, expected):
assert isinstance(input, expected)
def test_add_talks_from_rss(db):
"""Test that talks are retrieved from the RSS feed"""
feed1 = "http://fosslc.org/drupal/presentations_rss/summercamp2010"
feed2 = "http://fosslc.org/drupal/presentations_rss/sc2011"
presentation1 = Presentation("Managing map data in a database", "Andrew Ross")
presentation2 = Presentation("Building NetBSD", "David Maxwell")
db.add_talks_from_rss(feed1)
assert(db.presentation_exists(presentation1))
db.add_talks_from_rss(feed2)
assert(db.presentation_exists(presentation2))
def test_add_talks_from_csv(db):
"""Test that talks are retrieved from the CSV file"""
dirname = os.path.dirname(__file__)
fname = os.path.join(dirname, 'sample_talks.csv')
presentation = Presentation("Building NetBSD", "David Maxwell")
db.add_talks_from_csv(fname)
assert(db.presentation_exists(presentation))
# TODO: Do we still need this cleanup function?
# def tearDown(self):
# '''
# Generic unittest.TestCase.tearDown()
# '''
# shutil.rmtree(self.profile_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment