def test_set_creation_date(self):
     engine = temp_db.get_temp_db()
     tr_1 = self._get_test_transaction()
     t_db = TransactionDB(engine)
     t_id = t_db.create_transaction(tr_1)
     tr_2 = t_db.get_transaction(t_id)
     self.assertTrue(tr_2.creation_date)
 def test_add_user_already_exists(self):
     """test that we can't add duplicate users by user name"""
     engine = temp_db.get_temp_db()
     t_db = TransactionDB(engine)
     user_id = t_db.add_user('Pere', 'pwd')
     self.assertTrue(user_id >= 0)
     t_db.add_user('Pere', 'pwd')
    def test_create_transaction_lm(self):
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()
        tr_1.last_message = json.dumps({
            't_id': None,
            'data': {
                'dicom_info': {
                    't1': {
                        'header': {
                            'SeriesDescription': 'series_t1_1'
                        }
                    },
                    't2': {
                        'header': {
                            'SeriesDescription': 'series_t2_1'
                        }
                    }
                }
            }
        })
        t_db = TransactionDB(engine)
        t_id = t_db.create_transaction(tr_1)
        tr_2 = t_db.get_transaction(t_id)

        self.assertEqual(t_id, json.loads(tr_2.last_message)['t_id'])
 def test_add_user_ok(self):
     """test that we can add User entity"""
     engine = temp_db.get_temp_db()
     t_db = TransactionDB(engine)
     user_id = t_db.add_user('Pere', 'pwd')
     user = t_db.session.query(User).get(user_id)
     self.assertEqual('Pere', user.name)
     self.assertTrue(user.hashed_password)
    def test_user_preferences_invalid_keys(self):
        """test that upon providing an invalid preference key for the
        preferences system, a TransactionDBException is thrown"""
        engine = temp_db.get_temp_db()
        t_db = TransactionDB(engine)
        user_id_1 = t_db.add_user('Pere1', 'pwd')

        t_db.set_user_preferences(user_id_1, {'foo': 'bar'})
 def test_add_role_ok(self):
     """test that we can add a Role entity"""
     engine = temp_db.get_temp_db()
     t_db = TransactionDB(engine)
     t_db.add_role('radiologist', 'whatever', 128)
     role = t_db.session.query(Role).get('radiologist')
     self.assertEqual('whatever', role.description)
     self.assertEqual(128, role.permissions)
    def test_user_preferences_invalid_user(self):
        """test that user_id is a foreign key in the relational model
        of the user preferences, and as such will cause the db to fail
        if we provide whatever as user_id"""
        engine = temp_db.get_temp_db()
        t_db = TransactionDB(engine)

        t_db.set_user_preferences(100, {'report_language': 'en'})
 def test_remove_user_ok(self):
     """test that we can remove an existing user from the database"""
     engine = temp_db.get_temp_db()
     t_db = TransactionDB(engine)
     user_id = t_db.add_user('Pere', 'pwd')
     user = t_db.session.query(User).get(user_id)
     self.assertEqual('Pere', user.name)
     t_db.remove_user(user_id)
     user = t_db.session.query(User).get(user_id)
     self.assertFalse(user)
    def test_retry_logic_2(self):
        """test that our database retry logic works.
        Raise exception randomly and perform the given task randomly,
        such that retrying should eventually work"""
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()

        t_db = TransactionDB(engine)
        t_id = t_db.create_transaction(tr_1)
        t = t_db.get_transaction(t_id)
        self.assertEqual(0, t.patient_consent)

        orig_f = t_db._get_transaction_or_raise_exception

        should_fail_once = True

        def mocked_f(t_id):
            nonlocal should_fail_once
            if should_fail_once:
                should_fail_once = False
                # Raising this exception means it should be retried
                raise Sqlite3OperationalError
            return orig_f(t_id)

        t_db._get_transaction_or_raise_exception = mocked_f

        try:
            t_db.set_patient_consent(t_id)
        except Exception:
            pass

        t = t_db.get_transaction(t_id)
        self.assertEqual(1, t.patient_consent)
 def test_read_transaction_from_dict(self):
     d = {
         'transaction_id': 1,
         'name': 'John Doe',
         'birth_date': '01/02/2020'
     }
     t = Transaction().read_dict(d)
     engine = temp_db.get_temp_db()
     t_db = TransactionDB(engine)
     t_id = t_db.create_transaction(t)
     t_from_db = t_db.get_transaction(t_id)
     self.assertEqual(d['transaction_id'], t_from_db.transaction_id)
     self.assertEqual(d['name'], t_from_db.name)
     self.assertEqual(date(2020, 2, 1), t_from_db.birth_date)
    def test_migrations(self):
        temp_folder = tempfile.mkdtemp(
            suffix='_test_migrations_transaction_db_')
        temp_db_path = os.path.join(temp_folder, 't_v1.db')
        shutil.copy('tests/fixtures/t_v1.db', temp_db_path)
        engine = create_engine('sqlite:///' + temp_db_path)
        # should execute all migrations code
        t_db = TransactionDB(engine, create_db=True, db_file_path=temp_db_path)

        self.assertTrue(os.path.exists(temp_db_path + '.v_1.bkp'))
        # add a new transaction with the current model
        t = Transaction()
        t_db.create_transaction(t)
        shutil.rmtree(temp_folder)
Exemple #12
0
    def test_migrate_institution(self):
        engine = self._get_temp_db(2)
        t_db = TransactionDB(engine)
        last_message = {
            'data': {
                'dicom_info': {
                    't1': {
                        'header': {
                            'InstitutionName': 'MockInstitution'
                        }
                    }
                }
            }
        }
        tr_1 = Transaction()
        tr_1.last_message = json.dumps(last_message)
        t_id = t_db.create_transaction(tr_1)

        # remove institution field
        session = t_db.session
        tr_2 = t_db.get_transaction(t_id)
        tr_2.institution = ''
        session.commit()
        self.assertEqual('', t_db.get_transaction(t_id).institution)

        # execute migrate python script
        model = get_transaction_model(engine)
        migrations.migrate_institution(session, model)
        session.commit()
        tr_2 = t_db.get_transaction(t_id)
        self.assertEqual('MockInstitution', tr_2.institution)

        t_db.close()
    def test_revoke_user_role_ok(self):
        """test that we can revoke a role from a user"""
        engine = temp_db.get_temp_db()
        t_db = TransactionDB(engine)
        user_id = t_db.add_user('Pere', 'pwd')
        t_db.add_role('radiologist', 'whatever', 128)

        t_db.add_user_role(user_id, 'radiologist')
        self.assertTrue(self.__user_has_role(t_db, user_id, 'radiologist'))
        t_db.revoke_user_role(user_id, 'radiologist')
        self.assertFalse(self.__user_has_role(t_db, user_id, 'radiologist'))
    def test_transaction_archived(self):
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()

        t_db = TransactionDB(engine)
        t_id = t_db.create_transaction(tr_1)
        t = t_db.get_transaction(t_id)
        self.assertEqual(t.archived, 0)
        t_db.set_archived(t_id)
        t = t_db.get_transaction(t_id)
        self.assertEqual(t.archived, 1)
        t_db.close()
    def test_transaction_failed(self):
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()

        t_db = TransactionDB(engine)
        t_id = t_db.create_transaction(tr_1)
        t_db.set_processing(t_id, '', '')
        # to be called when a transaction fails
        t_db.set_failed(t_id, 'because it failed')
        t = t_db.get_transaction(t_id)

        self.assertEqual(t.task_state, TaskState.failed)
        self.assertTrue(t.end_date > t.start_date)
        self.assertEqual(t.error, 'because it failed')

        t_db.close()
    def test_transaction_completed(self):
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()

        t_db = TransactionDB(engine)
        t_id = t_db.create_transaction(tr_1)
        t_db.set_processing(t_id, '', '')
        # to be called when a transaction completes
        t_db.set_completed(t_id)
        t = t_db.get_transaction(t_id)

        self.assertEqual(t.task_state, TaskState.completed)
        self.assertEqual(t.status, 'unseen')
        self.assertTrue(t.end_date > t.start_date)

        t_db.close()
    def test_set_qa_score(self):
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()

        t_db = TransactionDB(engine)
        t_id = t_db.create_transaction(tr_1, qa_score='good')
        t = t_db.get_transaction(t_id)
        self.assertEqual('good', t.qa_score)

        t_db.set_qa_score(t_id, 'rejected')
        t = t_db.get_transaction(t_id)
        self.assertEqual('rejected', t.qa_score)
        t_db.close()
    def test_set_billable(self):
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()

        t_db = TransactionDB(engine)
        t_id = t_db.create_transaction(tr_1)
        t = t_db.get_transaction(t_id)
        self.assertEqual(None, t.billable)
        # set billable
        t_db.set_billable(t_id, 'bill')
        t = t_db.get_transaction(t_id)
        self.assertEqual('bill', t.billable)
        t_db.close()
Exemple #19
0
    def test_migrate_report_qa_2(self):
        engine = self._get_temp_db(5)
        t_db = TransactionDB(engine)
        last_message = {
            'data': {
                'report_qa_score_outcomes': {
                    'mdbrain_nd': 'good',
                    'mdbrain_ms': 'acceptable'
                }
            }
        }
        tr_1 = Transaction()
        tr_1.last_message = json.dumps(last_message)
        t_id = t_db.create_transaction(tr_1)
        tr_1 = t_db.get_transaction(t_id)
        # by default TransactionsDB doesn't set this field
        self.assertEqual(None, tr_1.qa_score)

        # execute migrate python script
        model = get_transaction_model(engine)
        migrations.migrate_qa_scores(t_db.session, model)
        t_db.session.commit()

        tr_2 = t_db.get_transaction(t_id)
        self.assertTrue('mdbrain_ms:acceptable' in tr_2.qa_score)
        self.assertTrue('mdbrain_nd:good' in tr_2.qa_score)
        t_db.close()
Exemple #20
0
    def test_migrate_analysis_type_2(self):
        engine = self._get_temp_db(5)
        t_db = TransactionDB(engine)
        last_message = {
            'data': {
                'report_pdf_paths': {
                    'mdbrain_nd': 'path1',
                    'mdbrain_ms': 'path2'
                }
            }
        }
        tr_1 = Transaction()
        tr_1.last_message = json.dumps(last_message)
        t_id = t_db.create_transaction(tr_1)
        tr_1 = t_db.get_transaction(t_id)
        # by default TransactionsDB doesn't set this field
        self.assertEqual(None, tr_1.analysis_type)

        # execute migrate python script
        model = get_transaction_model(engine)
        migrations.migrate_analysis_types(t_db.session, model)
        t_db.session.commit()

        tr_2 = t_db.get_transaction(t_id)
        self.assertTrue('mdbrain_ms' in tr_2.analysis_type)
        self.assertTrue('mdbrain_nd' in tr_2.analysis_type)
        t_db.close()
Exemple #21
0
    def test_migrate_study_date(self):
        engine = self._get_temp_db(4)
        t_db = TransactionDB(engine)
        last_message = {
            'data': {
                'dicom_info': {
                    't1': {
                        'header': {
                            'StudyDate': '20190101'
                        }
                    }
                }
            }
        }
        tr_1 = Transaction()
        tr_1.last_message = json.dumps(last_message)
        t_id = t_db.create_transaction(tr_1)
        tr_1 = t_db.get_transaction(t_id)
        # by default TransactionsDB doesn't set this field
        self.assertEqual(None, tr_1.study_date)

        # execute migrate python script
        model = get_transaction_model(engine)
        migrations.migrate_study_date(t_db.session, model)
        t_db.session.commit()

        tr_2 = t_db.get_transaction(t_id)
        self.assertEqual('20190101', tr_2.study_date)

        t_db.close()
    def test_set_priority(self):
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()

        t_db = TransactionDB(engine)
        t_id = t_db.create_transaction(tr_1)
        t = t_db.get_transaction(t_id)
        self.assertEqual(0, t.priority)

        t_db.set_priority(t_id, 2)
        t = t_db.get_transaction(t_id)
        self.assertEqual(2, t.priority)

        t_db.close()
    def test_read_dict_dates(self):
        # test that date and datetimes are parsed correctly
        engine = temp_db.get_temp_db()
        t_db = TransactionDB(engine)
        datetime_vars = ['start_date', 'end_date', 'data_uploaded']
        date_vars = ['birth_date']
        t = Transaction()
        for key in datetime_vars:
            setattr(t, key, datetime(2020, 2, 1, 18, 30, 4))
        for key in date_vars:
            setattr(t, key, datetime(2020, 2, 1))

        t_r = Transaction().read_dict(t.to_dict())
        t_id = t_db.create_transaction(t_r)
        t_r_from_db = t_db.get_transaction(t_id)
        self.assertEqual(datetime(2020, 2, 1, 18, 30, 4),
                         t_r_from_db.start_date)
        self.assertEqual(datetime(2020, 2, 1, 18, 30, 4), t_r_from_db.end_date)
        self.assertEqual(date(2020, 2, 1), t_r_from_db.birth_date)
    def test_revoke_user_role_didnt_exist(self):
        """test that an Exception is thrown if we want to revoke an
        already revoked role from a user"""
        engine = temp_db.get_temp_db()
        t_db = TransactionDB(engine)
        user_id = t_db.add_user('Pere', 'pwd')
        t_db.add_role('radiologist', 'whatever')

        t_db.revoke_user_role(user_id, 'radiologist')
    def test_add_user_role_already_exists(self):
        """test that we can't assign twice the same role to a user"""
        engine = temp_db.get_temp_db()
        t_db = TransactionDB(engine)
        user_id = t_db.add_user('Pere', 'pwd')
        t_db.add_role('radiologist', 'whatever')

        t_db.add_user_role(user_id, 'radiologist')
        t_db.add_user_role(user_id, 'radiologist')
    def test_set_status(self):
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()

        t_db = TransactionDB(engine)
        t_id = t_db.create_transaction(tr_1)

        t_db.set_status(t_id, 'reviewed')
        t = t_db.get_transaction(t_id)

        self.assertEqual(t.status, 'reviewed')

        t_db.close()
    def test_change_last_message(self):
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()

        t_db = TransactionDB(engine)
        t_id = t_db.create_transaction(tr_1)

        # update last_message field
        t_db.set_last_message(t_id, 'last_message')
        t = t_db.get_transaction(t_id)

        self.assertEqual(t.last_message, 'last_message')

        t_db.close()
    def test_user_preferences(self):
        """test that the preferences saving and retrieving system works"""
        engine = temp_db.get_temp_db()
        t_db = TransactionDB(engine)
        user_id_1 = t_db.add_user('Pere1', 'pwd')
        user_id_2 = t_db.add_user('Pere2', 'pwd')

        self.assertTrue(t_db.get_user_preferences(user_id_1) is None)
        self.assertTrue(t_db.get_user_preferences(user_id_2) is None)

        preferences = {'report_language': 'en'}
        t_db.set_user_preferences(user_id_1, preferences)
        preferences = {'report_language': 'de'}
        t_db.set_user_preferences(user_id_2, preferences)

        prefs = t_db.get_user_preferences(user_id_1)
        self.assertEqual(user_id_1, prefs['user_id'])
        self.assertEqual('en', prefs['report_language'])

        prefs = t_db.get_user_preferences(user_id_2)
        self.assertEqual(user_id_2, prefs['user_id'])
        self.assertEqual('de', prefs['report_language'])
    def test_change_processing_state(self):
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()

        t_db = TransactionDB(engine)
        t_id = t_db.create_transaction(tr_1)
        # called when a transaction changes its processing state
        t_db.set_processing(t_id, 'spm_volumetry', '{}', 10)
        t = t_db.get_transaction(t_id)

        self.assertEqual(t.processing_state, 'spm_volumetry')
        self.assertEqual(t.task_state, TaskState.processing)
        self.assertEqual(t.task_progress, 10)

        t_db.close()
    def test_transaction_cancelled(self):
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()

        t_db = TransactionDB(engine)
        t_id = t_db.create_transaction(tr_1)

        # to be called when a transaction is skipped
        t_db.set_cancelled(t_id, 'because it is cancelled')
        t = t_db.get_transaction(t_id)

        self.assertEqual(t.task_cancelled, 1)
        self.assertEqual(t.error, 'because it is cancelled')

        t_db.close()