class TestAddHouseHoldMemberLogic(unittest.TestCase): def test___init__(self): add_house_hold_member_logic = AddHouseHoldMemberLogic(10, 10) def test_age(self): add_house_hold_member_logic = AddHouseHoldMemberLogic(10, 10) # make the tests work on different dates. AddHouseHoldMemberLogic.thisYear = lambda x: 2014 self.assertEqual("4", add_house_hold_member_logic.age("2010")) def test_saveMember(self): self.helper = DatabaseHelper() self.config = self.helper.getConfig() self.helper.start_tests() self.helper.setup_clean_db() self.helper.execute_instruction(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test', '2012-06-04', '2013-07-03', 'a simple test', 'GBP')""") self.helper.execute_instruction(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test 2', '2012-06-04', '2013-07-03', 'another simple test', 'GBP')""") with session_scope() as session: self.assertEqual(session.query(Household).count(), 0) house1 = Household(hhid=40, householdname='Test', pid=2, dateofcollection=datetime.date(2012,6,4)) session.add(house1) add_house_hold_member_logic = AddHouseHoldMemberLogic(40, 2) assert add_house_hold_member_logic.saveMember('Male', "10", "2004", False, "", "", "") r = AddHouseHoldMemberLogic(40, 2, 'm10') m = r.getExistingMember() self.assertEqual(m.headofhousehold, 'No') self.assertEqual(m.sex, 'Male') assert r.saveMember('Female', "12", "2002", False, "", "", "") m2 = r.getExistingMember() self.assertEqual(m2.personid, 'f12') self.assertEqual(m2.headofhousehold, 'No') self.helper.end_tests() def test_yearOfBirth(self): add_house_hold_member_logic = AddHouseHoldMemberLogic(10, 10) AddHouseHoldMemberLogic.thisYear = lambda x: 2014 self.assertEqual("2010", add_house_hold_member_logic.yearOfBirth("4"))
class TestSQLAlchemy(unittest.TestCase): """ In order to use these tests you need to have a config file named test_openihm.cfg in the directory you are running the tests from (i.e. the src directory). [database] superuser = root superuser_password = s00pers3cur3 database = test_openihm user = openihm password = ihm2012 This should contain database credentials for a database that exists in mysql for testing. This database will be toyed around with a lot. Obviously avoid pointing it at a database you care about... """ def setUp(self): self.helper = DatabaseHelper() self.config = self.helper.getConfig() self.helper.start_tests() def tearDown(self): # drop the database self.helper.end_tests() def test_connection_string(self): self.helper.setup_clean_db() cs = self.helper.real_config.sqlalchemy_connection_string() engine = create_engine(cs, echo=True) Session = sessionmaker(bind=engine) session = Session() self.assertEqual(session.query(SetupFoodsCrops).count(), 0) crop = SetupFoodsCrops('test', 'category', 10, 'kg') session.add(crop) self.assertEqual(session.query(SetupFoodsCrops).count(), 1) session.commit() def test_alchemy_wrapper(self): self.helper.setup_clean_db() with session_scope() as session: self.assertEqual(session.query(SetupFoodsCrops).count(), 0)
class TestRemoveMembers(unittest.TestCase): def setUp(self): self.helper = DatabaseHelper() self.config = self.helper.getConfig() self.helper.start_tests() self.helper.setup_clean_db() self.helper.execute_instruction(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test', '2012-06-04', '2013-07-03', 'a simple test', 'GBP')""") self.helper.execute_instruction(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test 2', '2012-06-04', '2013-07-03', 'another simple test', 'GBP')""") with session_scope() as session: self.assertEqual(session.query(Household).count(), 0) q = household.search(session, 1, '', '') self.assertEqual(q.count(), 0) house1 = Household(hhid=40, householdname='Test', pid=2, dateofcollection=datetime.date(2012,6,4)) house2 = Household(hhid=55, householdname='A Test 2', pid=2, dateofcollection=datetime.date(2012,6,4)) house3 = Household(hhid=42, householdname='Test 3', pid=3, dateofcollection=datetime.date(2012,6,4)) session.add(house1) session.add(house2) session.add(house3) household.addMember(session, house2, 'm4', '2010', 'No', 'Male', '', '', '', '') c = Householdcharacteristic(characteristic='Test', charvalue='a', hhid=42, pid=3) session.add(c) def tearDown(self): # drop the database self.helper.end_tests() def test_remove_members(self): with session_scope() as session: household_members.remove_members(session, 40, 2, ['m4'])
class TestDatabase(unittest.TestCase): """ In order to use these tests you need to have a config file named test_openihm.cfg in the directory you are running the tests from (i.e. the src directory). [database] superuser = root superuser_password = s00pers3cur3 database = test_openihm user = openihm password = ihm2012 This should contain database credentials for a database that exists in mysql for testing. This database will be toyed around with a lot. Obviously avoid pointing it at a database you care about... """ def setUp(self): self.helper = DatabaseHelper() self.config = self.helper.getConfig() self.helper.start_tests() def tearDown(self): # drop the database self.helper.end_tests() def test___init__(self): database = Database() self.assertEqual(self.config.dbinfo(), database.config) def test_close(self): database = Database() database.open() database.close() @unittest.expectedFailure def test_databaseExists(self): database = Database() assert database.databaseExists() @unittest.expectedFailure def test_databaseServerRunning(self): database = Database() assert database.databaseServerRunning() def test_execDefinitionQuery(self): self.helper.setup_clean_db() database = Database() database.open() database.execDefinitionQuery('create table simples (test int)') database.close() # and just to prove it's there to put something into. database.open() database.execUpdateQuery('insert into simples values (3)') database.close() def test_execSelectQuery(self): self.helper.setup_clean_db() self.helper.execute_instruction(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test', 2012-06-04, 2013-07-03, 'a simple test', 'GBP')""") database = Database() query = 'select * from projects' database.open() self.assertEqual([(2, u'test', None, None, u'a simple test', u'GBP')], database.execSelectQuery(query)) database.close() def test_execUpdateQuery(self): self.helper.setup_clean_db() database = Database() database.open() database.execUpdateQuery(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test', 2012-06-04, 2013-07-03, 'a simple test', 'GBP')""") query = 'select * from projects' # FIXME: the None's look hinky. self.assertEqual([(2, u'test', None, None, u'a simple test', u'GBP')], database.execSelectQuery(query)) database.close() def test_open(self): database = Database() database.open()
class TestMySQLMixin(unittest.TestCase): """ In order to use these tests you need to have a config file named test_openihm.cfg in the directory you are running the tests from (i.e. the src directory). [database] superuser = root superuser_password = s00pers3cur3 database = test_openihm user = openihm password = ihm2012 This should contain database credentials for a database that exists in mysql for testing. This database will be toyed around with a lot. Obviously avoid pointing it at a database you care about... """ def setUp(self): self.helper = DatabaseHelper() self.helper.start_tests() def tearDown(self): # drop the database self.helper.end_tests() def test_executeMultipleResultsQueries(self): self.helper.setup_clean_db() mixin = MySQLMixin() queries = [ """ insert into projects (projectname, description, currency) values ('test', 'a simple test', 'GBP')""", """ insert into projects (projectname, description, currency) values ('test2', 'simple test', 'AUS')""", ] mixin.executeMultipleUpdateQueries(queries) exp = [ [(u'test', None, None, u'a simple test', u'GBP')], [(u'test2', None, None, u'simple test', u'AUS')] ] s_queries = [ """ select projectname, startdate, enddate, description, currency from projects limit 1 """, """ select projectname, startdate, enddate, description, currency from projects limit 1 offset 1 """, ] self.assertEqual(exp, mixin.executeMultipleResultsQueries(s_queries)) def test_executeMultipleUpdateQueries(self): self.helper.setup_clean_db() my_sql_mixin = MySQLMixin() queries = [ """ insert into projects (projectname, description, currency) values ('test', 'a simple test', 'GBP')""", """ insert into projects (projectname, description, currency) values ('test2', 'simple test', 'AUS')""", ] my_sql_mixin.executeMultipleUpdateQueries(queries) expected = [ (u'test', None, None, u'a simple test', u'GBP'), (u'test2', None, None, u'simple test', u'AUS') ] query = """ select projectname, startdate, enddate, description, currency from projects """ self.assertEqual(expected, my_sql_mixin.executeResultsQuery(query)) def test_executeResultsQuery(self): self.helper.setup_clean_db() my_sql_mixin = MySQLMixin() my_sql_mixin.executeUpdateQuery(""" insert into projects (projectname, description, currency) values ('test', 'a simple test', 'GBP')""") expected = [(2, u'test', None, None, u'a simple test', u'GBP')] query = 'select * from projects' self.assertEqual(expected, my_sql_mixin.executeResultsQuery(query)) def test_executeUpdateQueryWithParams(self): self.helper.setup_clean_db() my_sql_mixin = MySQLMixin() p = ['test3', '2012-06-04', 'description', 'LIR'] my_sql_mixin.executeUpdateQuery(""" insert into projects (projectname, startdate, enddate, description, currency) values (%s, %s, '2013-07-03', %s, %s)""", p) expected = [(2, u'test3', datetime.date(2012, 6, 4), datetime.date(2013, 7, 3), u'description', u'LIR')] query = 'select * from projects' self.assertEqual(expected, my_sql_mixin.executeResultsQuery(query)) def test_executeUpdateQuery(self): self.helper.setup_clean_db() my_sql_mixin = MySQLMixin() my_sql_mixin.executeUpdateQuery(""" insert into projects (projectname, description, currency) values ('test', 'a simple test', 'GBP')""") expected = [(2, u'test', None, None, u'a simple test', u'GBP')] query = 'select * from projects' self.assertEqual(expected, my_sql_mixin.executeResultsQuery(query))
class TestDatabaseInitialiser(unittest.TestCase): """ In order to use these tests you need to have a config file named test_openihm.cfg in the directory you are running the tests from (i.e. the src directory). [database] superuser = root superuser_password = s00pers3cur3 database = test_openihm user = openihm password = ihm2012 This should contain database credentials for a database that exists in mysql for testing. This database will be toyed around with a lot. Obviously avoid pointing it at a database you care about... """ def setUp(self): self.helper = DatabaseHelper() self.config = self.helper.getConfig() self.helper.start_tests() def tearDown(self): # drop the database self.helper.end_tests() def test___init__(self): database_initialiser = DatabaseInitialiser(self.config) # FIXME: this test is probably bogus # we're really just checking we manage to create the # object okay. expected = "latest update on 2012-05-16" self.assertEqual(expected, database_initialiser.latestupdatestring) def test_createDatabase(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_db_file('openihmdb_mysql.sql') assert database_initialiser.createDatabase() @unittest.expectedFailure def test_cropsExist(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() self.helper.setup_db_file('openihmdb_mysql_fix59.sql') # FIXME: push some data into the relevant table. #self._execute_instruction('insert into setup_foods_crops values (%s)') assert database_initialiser.cropsExist() def test_databaseUpToDate(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() assert database_initialiser.databaseUpToDate() def test_databaseUpToDate_needupdate(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() self.make_db_outofdate() assert not database_initialiser.databaseUpToDate() def test_databaseUpToDate_fresh_db(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() self.helper.execute_instruction('delete from dbupdate') assert database_initialiser.databaseUpToDate() def make_db_outofdate(self): updatestr = "latest update on %s" % (date.min.isoformat()) self.helper.execute_instruction( "update dbupdate set lastupdate = %s", [updatestr]) def test_initialiseDB(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() self.assertEqual({ 'mysqlstarted': True, 'dbinstalled': True, 'dbuptodate': True}, database_initialiser.initialiseDB()) def test_insertStartupCrops(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() self.helper.setup_db_file('openihmdb_mysql_fix59.sql') assert database_initialiser.insertStartupCrops() def test_setupStartupCrops(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() self.helper.setup_db_file('openihmdb_mysql_fix59.sql') assert database_initialiser.setupStartupCrops() def test_updateDatabase(self): # NOTE: it might be worth adding a test that compares the new # database to the updated database to check they match. database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() # this isn't much of a check... # our coverage isn't very good. assert database_initialiser.updateDatabase() def test_updateDatabase_needs_update(self): database_initialiser = DatabaseInitialiser(self.config) self.helper.setup_clean_db() self.make_db_outofdate() self.helper.setup_db_file('openihmdb_mysql_update.sql') assert database_initialiser.updateDatabase()
class TestModelHouseHold(unittest.TestCase): """ In order to use these tests you need to have a config file named test_openihm.cfg in the directory you are running the tests from (i.e. the src directory). [database] superuser = root superuser_password = s00pers3cur3 database = test_openihm user = openihm password = ihm2012 This should contain database credentials for a database that exists in mysql for testing. This database will be toyed around with a lot. Obviously avoid pointing it at a database you care about... """ def setUp(self): self.helper = DatabaseHelper() self.config = self.helper.getConfig() self.helper.start_tests() self.helper.setup_clean_db() self.helper.execute_instruction(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test', '2012-06-04', '2013-07-03', 'a simple test', 'GBP')""") self.helper.execute_instruction(""" insert into projects (projectname, startdate, enddate, description, currency) values ('test 2', '2012-06-04', '2013-07-03', 'another simple test', 'GBP')""") with session_scope() as session: self.assertEqual(session.query(Household).count(), 0) q = household.search(session, 1, '', '') self.assertEqual(q.count(), 0) house1 = Household(hhid=40, householdname='Test', pid=2, dateofcollection=datetime.date(2012,6,4)) house2 = Household(hhid=55, householdname='A Test 2', pid=2, dateofcollection=datetime.date(2012,6,4)) house3 = Household(hhid=42, householdname='Test 3', pid=3, dateofcollection=datetime.date(2012,6,4)) session.add(house1) session.add(house2) session.add(house3) c = Householdcharacteristic(characteristic='Test', charvalue='a', hhid=42, pid=3) session.add(c) def tearDown(self): # drop the database self.helper.end_tests() def test_household_search(self): # a) no effective paramaters with session_scope() as session: q = household.search(session, 1, '', '') self.assertEqual(q.count(), 0) q = household.search(session, 2, '', '') self.assertEqual(q.count(), 2) q = household.search(session, 3, '', '') self.assertEqual(q.count(), 1) # g) ensure different projects don't clash - gets tested implicitly def test_number(self): # b) just number with session_scope() as session: q = household.search(session, 2, name='', number='55') self.assertEqual(q.count(), 1) q = household.search(session, 2, '', '33') self.assertEqual(q.count(), 0) def test_name(self): # c) just name with session_scope() as session: q = household.search(session, 3, 'Test 3', '') self.assertEqual(q.count(), 1) def test_both(self): # d) both with session_scope() as session: q = household.search(session, 2, 'Test', '40') self.assertEqual(q.count(), 2) q2 = household.search(session, 2, 'A Test', '55') self.assertEqual(q2.count(), 1) q2 = household.search(session, 2, 'A Test 2', '40') self.assertEqual(q2.count(), 2) q2 = household.search(session, 2, 'Not', '55') self.assertEqual(q2.count(), 1) def test_like(self): # e) like works with session_scope() as session: q = household.search(session, 2, 'Test', '') self.assertEqual(q.count(), 2) q2 = household.search(session, 2, 'A test', '') self.assertEqual(q2.count(), 1) q2 = household.search(session, 2, 'A Test 2', '') self.assertEqual(q2.count(), 1) q2 = household.search(session, 2, 'Not', '') self.assertEqual(q2.count(), 0) def test_results(self): # f) count and results work as expected. with session_scope() as session: q = household.search(session, 2, '', '') self.assertEqual(q.count(), 2) l = [ (h.hhid, h.householdname) for h in q ] self.assertEqual(l, [(40, 'Test'), (55, 'A Test 2')]) def test_remove(self): with session_scope() as session: household.remove_house(session, 2, [55]) q = household.search(session, 2, 'Test', '') self.assertEqual(q.count(), 1) def test_remove_project_scope(self): # ensure we don't accidentally delete houses from # different projects. with session_scope() as session: household.remove_house(session, 2, [55, 40, 42]) q = household.search(session, 2, '', '') self.assertEqual(q.count(), 0) q = household.search(session, 3, '', '') self.assertEqual(q.count(), 1) def test_add_member(self): with session_scope() as session: house = Household(hhid=40, pid=2) household.addMember(session, house, 'm4', '2010', 'No', 'Male', '', '', '', '') # ensure that it deals with twins okay. household.addMember(session, house, 'm4', '2010', 'No', 'Male', '', '', '', '') # FIXME: ought to deal with removing and adding members okay. def test_eager_loading(self): with session_scope() as session: p = session.query(Project).options(joinedload('houses')).filter(Project.projectname == 'test').one() self.assertEqual(len(p.houses), 2)