def testSaveContentIncludesExpectedKeysAndValues(self): """ When a backend saves, its JSON content must include the expected keys and values. """ dbParams = DatabaseParameters(landmarks=[], trigPoints=[], limitPerLandmark=16, maxDistance=17, minDistance=18, distanceBase=19.0) be = Backend() be.configure(dbParams, 'backend', 33) fp = StringIO() be.save(fp) fp.seek(0) DatabaseParameters.restore(fp) SubjectStore.restore(fp) state = loads(fp.readline()[:-1]) # Keys self.assertEqual( set(['checksum', 'd', 'name', '_totalCoveredResidues']), set(state.keys())) # Values self.assertEqual(be.checksum(), state['checksum']) self.assertEqual({}, state['d']) self.assertEqual('backend', state['name']) self.assertEqual(0, state['_totalCoveredResidues'])
def testLengthIsOneAfterAddSubject(self): """ Adding a subject to a new SubjectStore results in SubjectStore with length one. """ ss = SubjectStore() subject = Subject(AARead('id', 'AA')) ss.add(subject) self.assertEqual(1, len(ss))
def testLengthIsOneAfterAddSameSubjectTwice(self): """ Adding a subject to a new SubjectStore twice (with an identical index) results in SubjectStore with length one. """ ss = SubjectStore() subject = Subject(AARead('id', 'AA')) ss.add(subject) ss.add(subject) self.assertEqual(1, len(ss))
def testAddTwoSubjectsWithIndex(self): """ Adding two subjects to a SubjectStore and giving their indices must have the expected result. """ ss = SubjectStore() subject1 = Subject(AARead('id1', 'AA')) subject2 = Subject(AARead('id2', 'AA')) ss.add(subject1, '1') ss.add(subject2, '2') self.assertEqual(2, len(ss))
def testSaveRestoreEmpty(self): """ After a save/restore of an empty subject store, the result must be as expected. """ ss = SubjectStore() fp = StringIO() ss.save(fp) fp.seek(0) ss = SubjectStore.restore(fp) self.assertEqual(0, len(ss))
def testGetSubjects(self): """ getSubjects must return the expected result. """ ss = SubjectStore() subject1 = Subject(AARead('id1', 'AA')) subject2 = Subject(AARead('id2', 'CC')) ss.add(subject1, '1') ss.add(subject2, '2') result = set(ss.getSubjects()) self.assertEqual(set([subject1, subject2]), result)
def testAddSubjectTwiceWithSameIndex(self): """ Adding a subject to a SubjectStore and giving a subject index and then re-adding it with the same index must return the expected result. """ subject = Subject(AARead('id', 'AA')) ss = SubjectStore() ss.add(subject, '3') preExisting, subjectIndex = ss.add(subject, '3') self.assertTrue(preExisting) self.assertEqual('3', subjectIndex)
def testAddSubjectTwiceWithDifferentIndex(self): """ Adding a subject to a SubjectStore and giving a subject index and then re-adding it with a different index must raise a SubjectStoreException. """ subject = Subject(AARead('id', 'AA')) ss = SubjectStore() ss.add(subject, '3') error = ("^Already stored index \(3\) for subject 'id' does not match " "subsequently passed index \(4\) for the subject\.$") six.assertRaisesRegex(self, SubjectStoreException, error, ss.add, subject, '4')
def testAddSubjectWithIndex(self): """ Adding one subject to a SubjectStore and giving a subject index must return the expected result. """ subject = Subject(AARead('id', 'AA')) preExisting, subjectIndex = SubjectStore().add(subject, '3') self.assertFalse(preExisting) self.assertEqual('3', subjectIndex)
def testSaveRestoreNonEmpty(self): """ After a save/restore of a non-empty subject store, the subject store must be as expected. """ ss = SubjectStore() subject = Subject(AARead('id', 'AA'), 6) ss.add(subject, '3') fp = StringIO() ss.save(fp) fp.seek(0) ss = SubjectStore.restore(fp) self.assertEqual(1, len(ss)) self.assertEqual('3', ss.getIndexBySubject(subject)) result = ss.getSubjectByIndex('3') self.assertEqual(subject, result) self.assertEqual(6, result.hashCount)
def __init__(self, dbParams, _id=None, subjectStore=None, checksum=None, disconnectedBackends=None, filePrefix=None): self.dbParams = dbParams # Assign ourselves a random id to make backend name collisions # highly unlikely. I.e., if an unknown backend connects, the name # it supplies should be very unlikely to be accepted. self._id = _id or '%020d' % int(uniform(1, 1e20)) self._subjectStore = subjectStore or SubjectStore() for method in ('getIndexBySubject', 'getSubjectByIndex', 'getSubjects'): setattr(self, method, getattr(self._subjectStore, method)) # Our checksum will be based on our parameters. The checksum should # not be based on our id as that's a random value that changes from # run to run. If we used it in the checksum then if we restarted a # connector and it began to talk to existing backends, it would # have a different checksum and errors would result. self._checksum = checksum or Checksum(dbParams.checksum) # In self._disconnectedBackends and self._backends, keys are # backend names and values are Checksum instances. self._disconnectedBackends = disconnectedBackends or {} self._backends = {} self._filePrefix = filePrefix # _sessionIdToName maps ephemeral WAMP session ids to (connected) # backend names. self._sessionIdToName = {} # The WAMP server component. This will be set by the server # database component using our setComponent method. self._component = None # TODO: Remove. logging.info('Connector started. Disconnected backends: %r' % self._disconnectedBackends)
def testEmptyStoreHasZeroLength(self): """ An empty SubjectStore must have zero length. """ self.assertEqual(0, len(SubjectStore()))