def getAllEntries(self): ''' Return a tuple of all the records from the database, including all columns. ''' returnValue = None with self.connection, closing(self.connection.cursor()) as cursor: cursor.execute('select * from contacts') returnValue = tuple([PersonRecord(*columns) for columns in cursor]) return returnValue
def getEntry(self, datum): ''' Return one or more entries by providing a selector in terms of a sequence with column entries. An empty string means no selector. ''' datum = datum.asTuple() assert len(datum) == 5 whereClause = '' if datum[0] != '': whereClause += 'where {} = "{}"'.format(columnNames[0], datum[0]) for i in range(1, len(datum)): if datum[i] != '': whereClause += ('where' if len(whereClause) == 0 else 'and') + ' {} = "{}"'.format( columnNames[i], datum[i]) assert whereClause[0:5] == 'where' returnValue = None with self.connection, closing(self.connection.cursor()) as cursor: cursor.execute('select * from contacts ' + whereClause) returnValue = tuple([PersonRecord(*columns) for columns in cursor]) return returnValue
def test_value_inequality_fails_on_empty_records(): assert not (PersonRecord() != PersonRecord())
def test_value_equality_empty_records(): assert PersonRecord() == PersonRecord()
def test_value_inequality_partial_records(): assert PersonRecord('Winder') != PersonRecord('Bloggs')
def test_empty_record_as_tuple(): assert ('', '', '', '', '') == PersonRecord().asTuple()
def test_single_full_record_as_tuple(): assert datum == PersonRecord(*datum).asTuple()
def test_empty_record_repr(): assert 'PersonRecord("","","","","")' == repr(PersonRecord())
def test_single_full_record_repr(): assert 'PersonRecord("{}","{}","{}","{}","{}")'.format(*datum) == repr( PersonRecord(*datum))
def test_value_inequality_fails_on_partial_records(): assert not (PersonRecord('Winder') != PersonRecord('Winder'))
def test_empty_record_repr(self): self.assertEqual('PersonRecord("","","","","")', repr(PersonRecord()))
def test_addOnePersonAndRemoveThemByLastnameAndFirstname(self): self.connection.addEntry(TestContacts.personA) self.connection.deleteEntry(PersonRecord(TestContacts.personA.lastname, TestContacts.personA.firstname)) self.assertEqual((), self.connection.getAllEntries())
def test_addTwoPeopleAndGetByLastnameAndFirstname(self): self.connection.addEntry(TestContacts.personA) self.connection.addEntry(TestContacts.personB) self.assertEqual((TestContacts.personA,), self.connection.getEntry(PersonRecord('Winder', 'Russel')))
class TestContacts(TestCase): ''' This is an integration test for a Connection class. The functions are not easily amenable to unit testing using mocks. The point here is that we want more functionality/requirements-oriented testing rather than implementation-oriented testing. This is more behaviour-driven rather than test-driven. There is an assumption of the existence of a getConnectionInstance function returning a connection instance. ''' personA = PersonRecord('Winder', 'Russel', '41 Buckmaster Road, London SW11 1EN', '020 7585 2200', '07770 465 077') personB = PersonRecord('Winder', 'Geri', '41 Buckmaster Road, London SW11 1EN', '020 7585 2200', '') def setUp(self): # Since each test is self-contained, indeed there must not be any data coupling between tests, we # can use an in-memory SQLite3 database, thereby speeding up the tests. self.connection = ConnectionClass(url) self.connection.initializeDatabase() def tearDown(self): self.connection.close() def test_emptyDatabaseHasNoEntries(self): self.assertEqual((), self.connection.getAllEntries()) def test_addOnePerson(self): self.connection.addEntry(TestContacts.personA) self.assertEqual((TestContacts.personA,), self.connection.getAllEntries()) def test_addTwoPeople(self): self.connection.addEntry(TestContacts.personA) self.connection.addEntry(TestContacts.personB) self.assertEqual((TestContacts.personA, TestContacts.personB), self.connection.getAllEntries()) def test_addOnePersonAndRemoveThemByLastname(self): self.connection.addEntry(TestContacts.personA) self.connection.deleteEntry(PersonRecord(TestContacts.personA.lastname,)) self.assertEqual((), self.connection.getAllEntries()) def test_addOnePersonAndRemoveThemByFirstname(self): self.connection.addEntry(TestContacts.personA) self.connection.deleteEntry(PersonRecord('', TestContacts.personA.firstname)) self.assertEqual((), self.connection.getAllEntries()) def test_addOnePersonAndRemoveThemByLastnameAndFirstname(self): self.connection.addEntry(TestContacts.personA) self.connection.deleteEntry(PersonRecord(TestContacts.personA.lastname, TestContacts.personA.firstname)) self.assertEqual((), self.connection.getAllEntries()) def test_addTwoPeopleAndGetByLastname(self): self.connection.addEntry(TestContacts.personA) self.connection.addEntry(TestContacts.personB) self.assertEqual((TestContacts.personA, TestContacts.personB), self.connection.getEntry(PersonRecord('Winder',))) def test_addTwoPeopleAndGetByFirstname(self): self.connection.addEntry(TestContacts.personA) self.connection.addEntry(TestContacts.personB) self.assertEqual((TestContacts.personA,), self.connection.getEntry(PersonRecord('', 'Russel'))) def test_addTwoPeopleAndGetByLastnameAndFirstname(self): self.connection.addEntry(TestContacts.personA) self.connection.addEntry(TestContacts.personB) self.assertEqual((TestContacts.personA,), self.connection.getEntry(PersonRecord('Winder', 'Russel')))
def test_value_equality_empty_records(self): self.assertEqual(PersonRecord(), PersonRecord())
def test_single_full_record_as_tuple(self): self.assertEqual(datum, PersonRecord(*datum).asTuple())
def test_empty_record_as_tuple(self): self.assertEqual(('', '', '', '', ''), PersonRecord().asTuple())
def test_single_full_record_repr(self): self.assertEqual( 'PersonRecord("{}","{}","{}","{}","{}")'.format(*datum), repr(PersonRecord(*datum)))
def getAllEntries(self): ''' Return one or more entries by providing a selector in terms of a sequence with column entries. An empty string means no selector. ''' return [PersonRecord(*r) for r in self.recordTable.select().execute()]
def addEntry(self, datum): ''' Add a new record into the database. The second parameter is a sequence with entries for each column. ''' self.session.add(PersonRecord(datum))