def test_to_dict(self): """Test output of to_dict() method""" uid = UniqueIdentity(uuid='John Smith') self.session.add(uid) org = Organization(name='Example') self.session.add(org) rol = Enrollment(uidentity=uid, organization=org, start=datetime.datetime(1999, 1, 1, 0, 0, 0), end=datetime.datetime(2001, 1, 1, 0, 0, 0)) self.session.add(rol) self.session.commit() # Tests d = rol.to_dict() self.assertIsInstance(d, dict) self.assertEqual(d['uuid'], 'John Smith') self.assertEqual(d['organization'], 'Example') self.assertEqual(d['start'], datetime.datetime(1999, 1, 1, 0, 0, 0)) self.assertEqual(d['end'], datetime.datetime(2001, 1, 1, 0, 0, 0))
def test_not_null_relationships(self): """Check whether every enrollment is assigned organizations and unique identities""" if sys.version_info[0] >= 3: # Python 3 expected = IntegrityError else: # Python 2 expected = OperationalError with self.assertRaisesRegexp(expected, NULL_CHECK_ERROR): rol1 = Enrollment() self.session.add(rol1) self.session.commit() self.session.rollback() with self.assertRaisesRegexp(expected, NULL_CHECK_ERROR): uid = UniqueIdentity(uuid='John Smith') self.session.add(uid) rol2 = Enrollment(uidentity=uid) self.session.add(rol2) self.session.commit() self.session.rollback() with self.assertRaisesRegexp(expected, NULL_CHECK_ERROR): org = Organization(name='Example') self.session.add(org) rol3 = Enrollment(organization=org) self.session.add(rol3) self.session.commit() self.session.rollback()
def test_not_null_relationships(self): """Check whether every enrollment is assigned organizations and unique identities""" with self.assertRaisesRegex(IntegrityError, NULL_CHECK_ERROR): rol1 = Enrollment() self.session.add(rol1) self.session.commit() self.session.rollback() with self.assertRaisesRegex(IntegrityError, NULL_CHECK_ERROR): uid = UniqueIdentity(uuid='John Smith') self.session.add(uid) rol2 = Enrollment(uidentity=uid) self.session.add(rol2) self.session.commit() self.session.rollback() with self.assertRaisesRegex(IntegrityError, NULL_CHECK_ERROR): org = Organization(name='Example') self.session.add(org) rol3 = Enrollment(organization=org) self.session.add(rol3) self.session.commit() self.session.rollback()
def test_default_enrollment_period(self): """Check whether the default period is set when initializing the class""" uid = UniqueIdentity(uuid='John Smith') self.session.add(uid) org = Organization(name='Example') self.session.add(org) rol1 = Enrollment(uidentity=uid, organization=org) self.session.add(rol1) self.session.commit() self.assertEqual(rol1.start, datetime.datetime(1900, 1, 1, 0, 0, 0)) self.assertEqual(rol1.end, datetime.datetime(2100, 1, 1, 0, 0, 0)) # Setting start and end dates to None produce the same result rol2 = Enrollment(uidentity=uid, organization=org, start=None, end=datetime.datetime(2222, 1, 1, 0, 0, 0)) self.session.add(rol2) self.session.commit() self.assertEqual(rol2.start, datetime.datetime(1900, 1, 1, 0, 0, 0)) self.assertEqual(rol2.end, datetime.datetime(2222, 1, 1, 0, 0, 0)) rol3 = Enrollment(uidentity=uid, organization=org, start=datetime.datetime(1999, 1, 1, 0, 0, 0), end=None) self.session.add(rol3) self.session.commit() self.assertEqual(rol3.start, datetime.datetime(1999, 1, 1, 0, 0, 0)) self.assertEqual(rol3.end, datetime.datetime(2100, 1, 1, 0, 0, 0))
def test_unique_enrollments(self): """Check if there is only one tuple with the same values""" with self.assertRaisesRegexp(IntegrityError, DUP_CHECK_ERROR): uid = UniqueIdentity(uuid='John Smith') self.session.add(uid) org = Organization(name='Example') self.session.add(org) rol1 = Enrollment(uidentity=uid, organization=org) rol2 = Enrollment(uidentity=uid, organization=org) self.session.add(rol1) self.session.add(rol2) self.session.commit()