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)
Ejemplo n.º 2
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()
Ejemplo n.º 3
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()
Ejemplo n.º 4
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()
Ejemplo n.º 5
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()
    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_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()
    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_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_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_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_set_patient_consent(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.patient_consent)
        # set patient consent
        t_db.set_patient_consent(t_id)
        t = t_db.get_transaction(t_id)
        self.assertEqual(1, t.patient_consent)
        # unset patient consent
        t_db.unset_patient_consent(t_id)
        t = t_db.get_transaction(t_id)
        self.assertEqual(0, t.patient_consent)
        t_db.close()
    def test_transaction_with_product_id(self):
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()
        tr_2 = self._get_test_transaction()

        t_db = TransactionDB(engine)
        t_id = t_db.create_transaction(tr_1, product_id=1)
        t_id_2 = t_db.create_transaction(tr_2, product_id=2)

        t = t_db.get_transaction(t_id)
        self.assertNotEqual(None, t)
        self.assertEqual(1, t.product_id)

        t2 = t_db.get_transaction(t_id_2)
        self.assertNotEqual(None, t2)
        self.assertEqual(2, t2.product_id)

        t_db.close()
Ejemplo n.º 14
0
    def test_migrate_version(self):
        engine = self._get_temp_db(5)
        t_db = TransactionDB(engine)
        last_message = {'data': {'version': '2.2.1'}}
        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.version)

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

        tr_2 = t_db.get_transaction(t_id)
        self.assertEqual('2.2.1', tr_2.version)
        t_db.close()
    def test_multi_session(self):
        engine = temp_db.get_temp_db()
        # no db created yet
        t = Transaction()
        t.name = 'Pere'

        t_db_1 = TransactionDB(engine, False)
        _ = TransactionDB(engine)
        t_db_1.create_transaction(t)
        t_1 = t_db_1.get_transaction(1)
        self.assertEqual('Pere', t_1.name)
    def test_set_start_date(self):
        # set start date at first processing
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()

        t_db = TransactionDB(engine)
        t_id = t_db.create_transaction(tr_1)
        # the engine returns the ID of the newly created transaction
        tr_2 = t_db.get_transaction(t_id)

        self.assertFalse(tr_2.start_date)

        t_db.set_processing(t_id, '', '')
        tr_3 = t_db.get_transaction(t_id)
        self.assertTrue(tr_3.start_date)

        t_db.set_processing(t_id, '', '')
        tr_4 = t_db.get_transaction(t_id)
        self.assertEqual(tr_3.start_date, tr_4.start_date)

        t_db.close()
    def test_transaction_completed_end_date_immutable(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)
        # check that end date is None on creation
        self.assertFalse(t.end_date)
        # complete transaction
        t_db.set_completed(t_id)
        t = t_db.get_transaction(t_id)

        # reset to processing and then to complete again
        end_date_1 = t.end_date
        t_db.set_processing(t_id, '', '')
        t_db.set_completed(t_id)
        t = t_db.get_transaction(t_id)
        self.assertEqual(end_date_1, t.end_date)

        t_db.close()
    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()
Ejemplo n.º 19
0
    def test_migrate_sequences(self):
        engine = self._get_temp_db(3)
        t_db = TransactionDB(engine)
        last_message = {
            'data': {
                'dicom_info': {
                    't1': {
                        'header': {
                            'SeriesDescription': 'T1_sequence'
                        }
                    },
                    't2': {
                        'header': {
                            'SeriesDescription': 'T2_sequence'
                        }
                    }
                }
            }
        }
        tr_1 = Transaction()
        tr_1.last_message = json.dumps(last_message)
        t_id = t_db.create_transaction(tr_1)

        # remove sequences 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_sequences(session, model)
        session.commit()
        tr_2 = t_db.get_transaction(t_id)
        self.assertEqual('T1_sequence;T2_sequence', tr_2.sequences)

        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_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_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()
    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_get_transaction(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)
        # the engine returns the ID of the newly created transaction
        tr_2 = t_db.get_transaction(t_id)

        self.assertEqual(tr_1.name, tr_2.name)
        self.assertEqual(tr_1.patient_id, tr_2.patient_id)
        self.assertEqual(tr_1.study_id, tr_2.study_id)
        self.assertFalse(tr_2.start_date)
        self.assertEqual(t_id, tr_2.transaction_id)
        self.assertEqual(tr_2.task_state, TaskState.queued)

        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_transaction_with_user_id(self):
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()

        t_db = TransactionDB(engine)
        user_id = t_db.add_user('Pere', 'pwd')
        t_id = t_db.create_transaction(tr_1, user_id)

        t = t_db.get_transaction(t_id)
        self.assertNotEqual(None, t)

        ut = t_db.session.query(UserTransaction) \
            .filter_by(transaction_id=t.transaction_id) \
            .filter_by(user_id=user_id).first()

        self.assertEqual(ut.user_id, user_id)
        self.assertEqual(t.transaction_id, ut.transaction_id)

        t_db.close()
    def test_create_transaction_index_sequences(self):
        engine = temp_db.get_temp_db()
        tr_1 = self._get_test_transaction()
        tr_1.last_message = json.dumps({
            '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('series_t1_1;series_t2_1', tr_2.sequences)
 def test_fail_on_get_non_existing_transaction(self):
     engine = temp_db.get_temp_db()
     t_db = TransactionDB(engine)
     t_db.get_transaction(1)