def _ignored__should_update_existing_data_records(self):
        mocked_form_model = Mock(spec=FormModel)
        mocked_form_model.is_registration_form.return_value = False
        mocked_form_model.entity_type = "clinic"
        form_submission = FormSubmissionFactory().get_form_submission(mocked_form_model, OrderedDict(), None)

        form_submission._values.insert(0,(u'What are symptoms?', ['High Fever']))

        mock_dbm = Mock(DatabaseManager)
        data_record = DataRecord(mock_dbm)
        data_record._doc = DataRecordDocument(entity_doc=EntityDocument())
        data_record._doc.entity['data'] = {u'What are symptoms?': {'value': ['Dry cough', 'Memory loss']}}

        data_record_id = 'abcdefgh1234jklmnop'
        submission = DocumentBase(document_type=(Mock(Document)))
        submission._data['data_record_id'] = data_record_id

        with patch('mangrove.datastore.database.DatabaseManager._load_document') as load_document:
            with patch('mangrove.datastore.database.DataObject.get') as get_data_record:
                load_document.return_value = data_record_id
                get_data_record.return_value = data_record

                form_submission.update_existing_data_records(mock_dbm,'8765gjngtbhjmk4567hloub')

        data_record.save().assert_called_once()
    def test_survey_response_is_edited_and_new_submission_and_datarecord_is_created(
            self):
        test_data = TestData(self.manager)
        survey_response_service = SurveyResponseService(self.manager)

        values = {
            'ID': test_data.entity1.short_code,
            'Q1': 'name',
            'Q2': '80',
            'Q3': 'a'
        }
        transport_info = TransportInfo('web', 'src', 'dest')
        request = Request(values, transport_info)

        saved_response = survey_response_service.save_survey(
            'CL1', values, [], transport_info, "rep2")
        self.assertDictEqual(
            OrderedDict([('Q1', 'name'), ('Q3', ['RED']), ('Q2', 80),
                         ('ID', u'1')]), saved_response.processed_data)

        new_values = {
            'ID': test_data.entity1.short_code,
            'Q1': 'new_name',
            'Q2': '430',
            'Q3': 'b'
        }
        survey_response_to_edit = SurveyResponse.get(
            self.manager, saved_response.survey_response_id)
        edited_response = survey_response_service.edit_survey(
            'CL1', new_values, [], survey_response_to_edit)

        self.assertTrue(edited_response.success)
        self.assertEqual(0, edited_response.errors.__len__())
        self.assertIsNotNone(edited_response.datarecord_id)
        self.assertIsNotNone(edited_response.survey_response_id)
        self.assertEqual(test_data.entity_type, edited_response.entity_type)
        self.assertEqual('CL1', edited_response.form_code)
        self.assertDictEqual(
            OrderedDict([('Q1', 'new_name'), ('Q3', ['YELLOW']), ('Q2', 430),
                         ('ID', u'1')]), edited_response.processed_data)

        self.assertNotEquals(saved_response.datarecord_id,
                             edited_response.datarecord_id)

        old_data_record = DataRecord.get(self.manager,
                                         saved_response.datarecord_id)
        self.assertTrue(old_data_record.voided)
        new_data_record = DataRecord.get(self.manager,
                                         edited_response.datarecord_id)
        self.assertFalse(new_data_record.voided)

        edited_survey_response = SurveyResponse.get(
            self.manager, edited_response.survey_response_id)
        self.assertEquals(1,
                          len(edited_survey_response._doc.data_record_history))
        self.assertEquals(old_data_record.id,
                          edited_survey_response._doc.data_record_history[0])
    def handle(self, *args, **options):
        crs_org = Organization.objects.get(org_id=settings.CRS_ORG_ID)
        document_store = OrganizationSetting.objects.get(
            organization=crs_org).document_store
        crs_database_manager = get_db_manager(server=settings.COUCH_DB_SERVER,
                                              database=document_store)

        crs_questionnaires = [
            settings.WAYBILL_SENT_QUESTIONNAIRE_CODE,
            settings.WAYBILL_SENT_BY_SITE, settings.WAYBILL_RECEIVED_BY_SITE,
            settings.SFE_DISTRIBUTION_CODE, settings.SFM_DISTRIBUTION_CODE,
            settings.WAYBILL_RECEIVED_BY_WH,
            settings.WAYBILL_RECEIVED_QUESTIONNAIRE_CODE,
            settings.PACKING_LIST_QUESTIONNAIRE_CODE,
            settings.BREAK_BULK_RECEIVED_PORT_QUESTIONNAIRE_CODE,
            settings.BILL_OF_LADING_QUESTIONNAIRE_CODE,
            settings.PHYSICAL_INVENTORY_QUESTIONNAIRE_CODE,
            settings.FFA_DISTRIBUTION_CODE, settings.BAV_CPS_CODE,
            settings.BAV_SF_CODE, settings.BAV_FFA_CODE,
            settings.CONTAINER_RECEIVED_PORT_QUESTIONNAIRE_CODE,
            settings.CONTAINER_SENT_QUESTIONNAIRE_CODE,
            settings.CPS_DISTRIBUTION_CODE, settings.NO_OF_RECIPIENT_CPS_CODE,
            settings.NO_OF_RECIPIENT_FFA_CODE,
            settings.NO_OF_RECIPIENT_SFE_CODE,
            settings.NO_OF_RECIPIENT_SFM_CODE,
            settings.BREAK_BULK_SENT_QUESTIONNAIRE_CODE
        ]
        try:
            form_code = list(args)[0]
            if form_code in crs_questionnaires:
                crs_questionnaires = [form_code]
        except Exception:
            pass

        try:
            datarecords_id = list(args)[1:]
            if not len(datarecords_id):
                datarecords_id = None
        except KeyError:
            datarecords_id = None

        handler = CRSCustomReportHandler()
        for questionnaire_code in crs_questionnaires:
            print 'getting the questionnaire'
            questionnaire = get_form_model_by_code(crs_database_manager,
                                                   questionnaire_code)
            print 'migrations for %s' % questionnaire_code
            print 'adding to sql'
            if datarecords_id:
                for data_record in [
                        DataRecord.get(crs_database_manager, data_record_id)
                        for data_record_id in datarecords_id
                ]:
                    formatted_submission = self._get_formatted_submission(
                        data_record, questionnaire)
                    try:
                        db.start_transaction()
                        handler.handle(questionnaire_code,
                                       formatted_submission, data_record.id)
                        db.commit_transaction()
                    except Exception as e:
                        db.rollback_transaction()
                        print formatted_submission
                        print data_record.id
                        print e.message
                print 'finished adding to sql for questionnaire %s' % questionnaire_code
Example #4
0
 def data_record(self):
     return DataRecord.get(self._dbm, self._doc.data_record_id) if self._doc.data_record_id is not None else None
Example #5
0
 def _void_existing_data_record(self, void=True):
     data_record_id = self._doc.data_record_id
     if data_record_id is not None:
         data_record = DataRecord.get(self._dbm, data_record_id)
         data_record.void(void)
         self._doc.data_record_history.append(data_record_id)
Example #6
0
from datetime import datetime
from pytz import UTC
from mangrove.datastore.entity import DataRecord
from mangrove.datastore.tests.test_data import TestData
from mangrove.utils.test_utils.mangrove_test_case import MangroveTestCase


class TestDataRecords(MangroveTestCase):
    def test_add_data_record_to_entity(self):
        test_data = TestData(self.manager)
        data_record = [('meds', 20), ('doc', "aroj"), ('facility', 'clinic'),
                       ('opened_on', datetime(2011, 01, 02, tzinfo=UTC))]
        data_record_id = test_data.entity1.add_data(
            data=data_record,
            event_time=datetime(2011, 01, 02, tzinfo=UTC),
            submission=dict(submission_id="123456"))
        self.assertTrue(data_record_id is not None)

        # Assert the saved document structure is as expected
        saved = DataRecord.get(self.manager, data_record_id)
        for (label, value) in data_record:
            self.assertTrue(label in saved.data)
            self.assertTrue('value' in saved.data[label])
            self.assertTrue(value == saved.data[label]['value'])
        self.assertEqual(saved.event_time, datetime(2011, 01, 02, tzinfo=UTC))
        self.assertEqual(saved.submission['submission_id'], "123456")