def test_auto_create_site(self, db_session): """ It should create an unregisterd site when generating an OUR number """ from occams_roster import generate, models, OUR_PATTERN res = generate(db_session, u'AEH') assert OUR_PATTERN.match(res) assert 1 == db_session.query(models.Site).count()
def edit_json(context, request): check_csrf_token(request) db_session = request.db_session is_new = isinstance(context, models.PatientFactory) form = PatientSchema(context, request).from_json(request.json_body) if not form.validate(): raise HTTPBadRequest(json={'errors': wtferrors(form)}) if is_new: # if any errors occurr after this, this PID is essentially wasted patient = models.Patient( pid=six.text_type(generate(db_session, form.site.data.name))) db_session.add(patient) else: patient = context patient.site = form.site.data if form.references.data: inputs = dict( ((r['reference_type'].id, r['reference_number']), r) for r in form.references.data) for r in patient.references: try: # Remove already-existing values from the inputs del inputs[(r.reference_type.id, r.reference_number)] except KeyError: # References not in the inputs indicate they have been removed db_session.delete(r) for r in six.itervalues(inputs): db_session.add(models.PatientReference( patient=patient, reference_type=r['reference_type'], reference_number=r['reference_number'])) # Add the patient forms if is_new: schemata_query = ( db_session.query(datastore.Schema) .join(models.patient_schema_table)) pending_entry = ( db_session.query(datastore.State) .filter_by(name=u'pending-entry') .one()) for schema in schemata_query: patient.entities.add(datastore.Entity( schema=schema, state=pending_entry )) db_session.flush() db_session.refresh(patient) return view_json(patient, request)
def test_issues_only_valid_our_numbers(self, db_session): """ It should only generate valid OUR numbers """ from occams_roster import generate, models, OUR_PATTERN # eventually it will create an invalid one and mark is as invactive for i in range(100): generate(db_session, u'AEH') invalids_query = (db_session.query( models.Identifier).filter_by(is_active=False)) valids_query = (db_session.query( models.Identifier).filter_by(is_active=True)) assert invalids_query.count() >= 1 for record in invalids_query: res = record.our_number assert not OUR_PATTERN.match(res) for record in valids_query: res = record.our_number assert OUR_PATTERN.match(res)
def test_existing_site(self, db_session): """ It should re-use previously registered sites """ from occams_roster import generate, models, OUR_PATTERN db_session.add(models.Site(title=u'AEH')) db_session.flush() res = generate(db_session, u'AEH') assert OUR_PATTERN.match(res) assert 1 == db_session.query(models.Site).count()
def edit_json(context, request): check_csrf_token(request) db_session = request.db_session is_new = isinstance(context, models.PatientFactory) form = PatientSchema(context, request).from_json(request.json_body) if not form.validate(): raise HTTPBadRequest(json={'errors': wtferrors(form)}) if is_new: # if any errors occurr after this, this PID is essentially wasted patient = models.Patient( pid=six.text_type(generate(db_session, form.site.data.name))) db_session.add(patient) else: patient = context patient.site = form.site.data if form.references.data: inputs = dict(((r['reference_type'].id, r['reference_number']), r) for r in form.references.data) for r in patient.references: try: # Remove already-existing values from the inputs del inputs[(r.reference_type.id, r.reference_number)] except KeyError: # References not in the inputs indicate they have been removed db_session.delete(r) for r in six.itervalues(inputs): db_session.add( models.PatientReference( patient=patient, reference_type=r['reference_type'], reference_number=r['reference_number'])) # Add the patient forms if is_new: schemata_query = (db_session.query(datastore.Schema).join( models.patient_schema_table)) pending_entry = (db_session.query( datastore.State).filter_by(name=u'pending-entry').one()) for schema in schemata_query: patient.entities.add( datastore.Entity(schema=schema, state=pending_entry)) db_session.flush() db_session.refresh(patient) return view_json(patient, request)