Example #1
0
 def setUp(self):
     self.con = psycopg2.connect(user=AMGUT_CONFIG.user,
                                 password=AMGUT_CONFIG.password,
                                 database=AMGUT_CONFIG.database,
                                 host=AMGUT_CONFIG.host,
                                 port=AMGUT_CONFIG.port)
     self.data_access = AGDataAccess(self.con)
     self.data_access.ag_update_kit_password('test',
                                             AMGUT_CONFIG.badpassword)
     self.con.commit()
Example #2
0
    def __getattr__(self, name):
        if not self._ag_data_access:
            from amgut.lib.data_access.ag_data_access import AGDataAccess

            print 'Connecting to postgres for amgut.connections.ag_data'

            self._ag_data_access = AGDataAccess()

        return getattr(self._ag_data_access, name)
Example #3
0
 def setUp(self):
     self.ag_data = AGDataAccess()
Example #4
0
class TestUtil(TestCase):
    def setUp(self):
        self.ag_data = AGDataAccess()

    def tearDown(self):
        del self.ag_data

    def test_survey_fermented(self):
        obs = survey_fermented('survey_id', {'participant_name': 'test'})
        exp = ('<h3 style="text-align: center"><a href="/authed/'
               'secondary_survey/?type=fermented&participant_name=test" '
               'target="_blank">Fermented Foods Survey</a></h3>As part of our '
               'onging research into what drive changes in the human gut '
               'microbiome, we are looking at fermented foods and the '
               'microbiomes of fermented food eaters. Please click the link '
               'above if you would like to participate in this survey.')
        self.assertEqual(obs, exp)

    def test_survey_surf(self):
        obs = survey_surf('survey_id', {'participant_name': 'test'})
        exp = ('<h3 style="text-align: center"><a href="/authed/'
               'secondary_survey/?type=surf&participant_name=test" target='
               '"_blank">Surfing Survey</a></h3>As part of our study, we are '
               'interested in the effects of frequent and prolonged exposure '
               'to salt water and the ocean, as it pertains to surfing and '
               'surfers. If you are interested in participating, you can click'
               ' the link above and take the survey.')
        self.assertEqual(obs, exp)

    def test_survey_asd(self):
        obs = survey_asd('survey_id', {'participant_name': 'test'})
        exp = ('<h3 style="text-align: center"><a href="https://docs.google.'
               'com/forms/d/1ZlaQzENj7NA7TcdfFhXfW0jshrToTywAarV0fjTZQxc/'
               'viewform?entry.1089722816=survey_id&entry.1116725993&entry.'
               '1983725631&entry.2036966278&entry.1785627282&entry.1461731626'
               '&entry.1203990558&entry.843049551&entry.476318397&entry.'
               '383297943&entry.228366248&entry.1651855735&entry.1234457826&'
               'entry.1079752165" target="_blank">ASD-Cohort survey</a></h3>'
               '<a href="http://www.anl.gov/contributors/jack-gilbert">Dr. '
               'Jack Gilbert</a> is exploring the relationship between gut '
               'dysbiosis and Autism Spectrum Disorders, and in conjunction '
               'with the American Gut Project, we started an ASD-Cohort '
               'study. This additional survey contains questions specific to '
               'that cohort, but it is open to any participant to take if '
               'they so choose.')
        self.assertEqual(obs, exp)

    def test_survey_vioscreen(self):
        obs = survey_vioscreen('survey_id', {'participant_name': 'test'})
        # Validate using in because key changes every time due to encription
        self.assertIn(
            'This is a validated FFQ, and is the one used '
            'by the Mayo Clinic.', obs)
        self.assertIn(
            '<h3 style="text-align: center"><a href="'
            'https://vioscreen.com/remotelogin.aspx?Key=', obs)

    def test_rolback(self):
        # fetching an arbitrary kit ID from DB that is not yet verified
        kit = self.ag_data.ut_get_arbitrary_supplied_kit_id_unverified()

        @rollback
        def tf(kit):
            self.ag_data.verifyKit(kit)

        obs = self.ag_data.getAGKitDetails(kit)
        self.assertEqual(obs['kit_verified'], 'n')

        tf(kit)
        obs = self.ag_data.getAGKitDetails(kit)
        self.assertEqual(obs['kit_verified'], 'n')
Example #5
0
 def setUp(self):
     self.ag_data = AGDataAccess()
Example #6
0
class TestUtil(TestCase):
    def setUp(self):
        self.ag_data = AGDataAccess()

    def tearDown(self):
        del self.ag_data

    def test_survey_fermented(self):
        obs = survey_fermented('survey_id', {'participant_name': 'test'})
        exp = ('<h3 style="text-align: center"><a href="/authed/'
               'secondary_survey/?type=fermented&participant_name=test" '
               'target="_blank">Fermented Foods Survey</a></h3>As part of our '
               'onging research into what drive changes in the human gut '
               'microbiome, we are looking at fermented foods and the '
               'microbiomes of fermented food eaters. Please click the link '
               'above if you would like to participate in this survey.')
        self.assertEqual(obs, exp)

    def test_survey_surf(self):
        obs = survey_surf('survey_id', {'participant_name': 'test'})
        exp = ('<h3 style="text-align: center"><a href="/authed/'
               'secondary_survey/?type=surf&participant_name=test" target='
               '"_blank">Surfing Survey</a></h3>As part of our study, we are '
               'interested in the effects of frequent and prolonged exposure '
               'to salt water and the ocean, as it pertains to surfing and '
               'surfers. If you are interested in participating, you can click'
               ' the link above and take the survey.')
        self.assertEqual(obs, exp)

    def test_survey_asd(self):
        obs = survey_asd('survey_id', {'participant_name': 'test'})
        exp = ('<h3 style="text-align: center"><a href="https://docs.google.'
               'com/forms/d/1ZlaQzENj7NA7TcdfFhXfW0jshrToTywAarV0fjTZQxc/'
               'viewform?entry.1089722816=survey_id&entry.1116725993&entry.'
               '1983725631&entry.2036966278&entry.1785627282&entry.1461731626'
               '&entry.1203990558&entry.843049551&entry.476318397&entry.'
               '383297943&entry.228366248&entry.1651855735&entry.1234457826&'
               'entry.1079752165" target="_blank">ASD-Cohort survey</a></h3>'
               '<a href="http://www.anl.gov/contributors/jack-gilbert">Dr. '
               'Jack Gilbert</a> is exploring the relationship between gut '
               'dysbiosis and Autism Spectrum Disorders, and in conjunction '
               'with the American Gut Project, we started an ASD-Cohort '
               'study. This additional survey contains questions specific to '
               'that cohort, but it is open to any participant to take if '
               'they so choose.')
        self.assertEqual(obs, exp)

    def test_survey_vioscreen(self):
        obs = survey_vioscreen('survey_id', {'participant_name': 'test'})
        # Validate using in because key changes every time due to encription
        self.assertIn('This is a validated FFQ, and is the one used '
                      'by the Mayo Clinic.', obs)
        self.assertIn('<h3 style="text-align: center"><a href="'
                      'https://vioscreen.com/remotelogin.aspx?Key=', obs)

    def test_rolback(self):
        # fetching an arbitrary kit ID from DB that is not yet verified
        kit = self.ag_data.ut_get_arbitrary_supplied_kit_id_unverified()

        @rollback
        def tf(kit):
            self.ag_data.verifyKit(kit)

        obs = self.ag_data.getAGKitDetails(kit)
        self.assertEqual(obs['kit_verified'], 'n')

        tf(kit)
        obs = self.ag_data.getAGKitDetails(kit)
        self.assertEqual(obs['kit_verified'], 'n')
Example #7
0
class TestAGDataAccess(TestCase):
    def setUp(self):
        self.con = psycopg2.connect(user=AMGUT_CONFIG.user,
                                    password=AMGUT_CONFIG.password,
                                    database=AMGUT_CONFIG.database,
                                    host=AMGUT_CONFIG.host,
                                    port=AMGUT_CONFIG.port)
        self.data_access = AGDataAccess(self.con)
        self.data_access.ag_update_kit_password('test',
                                                AMGUT_CONFIG.badpassword)
        self.con.commit()

    def tearDown(self):
        self.data_access.ag_update_kit_password('test',
                                                AMGUT_CONFIG.goodpassword)
        self.con.close()

    def test_testDatabase(self):
        self.assertTrue(self.data_access.testDatabase())

    def test_authenticateWebAppUser(self):
        self.assertFalse(self.data_access.authenticateWebAppUser('bad',
                                                                 'wrong'))
        data = self.data_access.authenticateWebAppUser(
            'test', AMGUT_CONFIG.badpassword)
        self.assertEqual(data['email'], '*****@*****.**')

    def test_addAGLogin(self):
        self.data_access.addAGLogin('*****@*****.**', 'test', 'test',
                                    'test', 'CO', '80303', 'USA')
        cur = self.con.cursor()
        cur.execute(
            'select * from ag_login where email = %s', ('*****@*****.**',))
        rec = cur.fetchall()
        self.assertEqual(len(rec), 1)
        cur.execute('delete from ag_login where email = %s',
                    ('*****@*****.**',))
        self.con.commit()
        cur.execute(
            'select * from ag_login where email = %s', ('*****@*****.**',))
        rec = cur.fetchall()
        self.assertEqual(len(rec), 0)

    def test_updateAGLogin(self):
        self.data_access.updateAGLogin('d8592c74-7da1-2135-e040-8a80115d6401',
                                       '*****@*****.**', '', 'add',
                                       'city', 'state', 'zip', 'USA')
        cur = self.con.cursor()
        cur.execute('select * from ag_login where ag_login_id = %s',
                    ('d8592c74-7da1-2135-e040-8a80115d6401',))
        rec = cur.fetchone()
        self.assertEqual(rec[1], '*****@*****.**')
        self.data_access.updateAGLogin('d8592c74-7da1-2135-e040-8a80115d6401',
                                       '*****@*****.**', 'Test', 'Test',
                                       'Boulder', 'CO', '80303',
                                       'United States')
        cur.execute('select * from ag_login where ag_login_id = %s',
                    ('d8592c74-7da1-2135-e040-8a80115d6401',))
        rec = cur.fetchone()
        self.assertEqual(rec[1], '*****@*****.**')

    def test_getAGSurveyDetails(self):
        ag_login_id = 'd8592c74-7da1-2135-e040-8a80115d6401'
        participant_name = 'foo'
        data = self.data_access.getAGSurveyDetails(ag_login_id,
                                                   participant_name)
        self.assertEqual(data['consent'], 'Yes')

    def test_getAGLogins(self):
        data = self.data_access.getAGLogins()
        self.assertTrue({'ag_login_id': 'd8592c74-7da1-2135-e040-8a80115d6401',
                         'email': '*****@*****.**', 'name': 'Test'} in data)

    def test_getAGKitsByLogin(self):
        data = self.data_access.getAGKitsByLogin()
        self.assertTrue({'email': '*****@*****.**',
                         'supplied_kit_id': 'test',
                         'ag_kit_id': 'd8592c74-7da2-2135-e040-8a80115d6401'}
                         in data)

    def test_getAGBarcodes(self):
        data = self.data_access.getAGBarcodes()
        self.assertEqual(data[0], '000000001')
        self.assertEqual(data[-1], '999999999')

    def test_getAGBarcodesByLogin(self):
        data = self.data_access.getAGBarcodesByLogin(
            'd8592c74-7da1-2135-e040-8a80115d6401')
        expected = {
            '000017221', '000017223', '000017222', '000017225', '000017224',
            '000017227', '000017226', '000010860', '000010859', '000006616',
            '000000001'}
        observed = {row['barcode'] for row in data}
        self.assertEqual(observed, expected)

    def test_getAGBarcodeDetails(self):
        data = self.data_access.getAGBarcodeDetails('000000001')
        self.assertEqual(data['participant_name'], 'foo')
        self.assertEqual(data['site_sampled'], 'Stool')
        self.assertEqual(data['status'], 'Received')

    def test_getAGKitDetails(self):
        data = self.data_access.getAGKitDetails('test')
        self.assertEqual(data['kit_verification_code'], 'test')

    def test_getNextAGBarcode(self):
        barcode, barcode_text = self.data_access.getNextAGBarcode()
        data = self.data_access.getAGBarcodes()
        self.assertTrue(barcode_text not in data)

    def test_reassignAGBarcode(self):
        test = "d8592c74-7da2-2135-e040-8a80115d6401"
        oneone = "dbd466b5-651b-bfb2-e040-8a80115d6775"
        self.data_access.reassignAGBarcode(oneone, '000010860')
        data = self.data_access.getBarcodesByKit('1111')
        self.assertEqual(len(data), 1)
        self.data_access.reassignAGBarcode(test, '000010860')
        data = self.data_access.getBarcodesByKit('1111')
        self.assertEqual(len(data), 0)

    def test_addAGKit(self):
        result = self.data_access.addAGKit('d8592c747da12135e0408a80115d6401',
                                           'somekit', 'pass', 2, 'ver', 'n')
        self.assertEqual(result, 1)
        cur = self.con.cursor()
        cur.execute('select * from ag_kit where ag_login_id = %s',
                    ('d8592c747da12135e0408a80115d6401',))
        rec = cur.fetchall()
        self.assertEqual(len(rec), 3)
        cur.execute(
            'delete from ag_kit where ag_login_id = %s '
            'and supplied_kit_id = %s', ('d8592c747da12135e0408a80115d6401',
                                         'somekit',))
        self.con.commit()
        cur.execute('select * from ag_kit where ag_login_id = %s',
                    ('d8592c747da12135e0408a80115d6401',))
        rec = cur.fetchall()
        self.assertEqual(len(rec), 2)
        result = self.data_access.addAGKit('d8592c747da12135e0408a80115d6401',
                                           'test', 'pass', 2, 'ver', 'n')
        self.assertEqual(result, -1)

    def test_updateAGKit(self):
        self.data_access.updateAGKit('d8592c74-7da2-2135-e040-8a80115d6401',
                                     'test22', 'newpass', 24, 'ver')
        cur = self.con.cursor()
        cur.execute('select * from ag_kit where ag_kit_id = %s',
                    ('d8592c74-7da2-2135-e040-8a80115d6401',))
        rec = cur.fetchone()
        self.assertEqual(rec[2], 'test22')
        self.data_access.updateAGKit('d8592c74-7da2-2135-e040-8a80115d6401',
                                     'test', 'oldpass', 1, 'test')
        cur.execute('select * from ag_kit where ag_kit_id = %s',
                    ('d8592c74-7da2-2135-e040-8a80115d6401',))
        rec = cur.fetchone()
        self.assertEqual(rec[2], 'test')

    def test_addAGBarcode(self):
        self.data_access.addAGBarcode('d8592c74-7da2-2135-e040-8a80115d6401',
                                      '991299')
        cur = self.con.cursor()
        cur.execute('select * from ag_kit_barcodes where barcode = %s',
                    ('991299',))
        rec = cur.fetchone()
        self.assertEqual(rec[1],  'd8592c74-7da2-2135-e040-8a80115d6401')
        cur.execute('select * from project_barcode where barcode = %s',
                    ('991299',))
        rec = cur.fetchone()
        self.assertEqual(rec[0], 1)
        cur.execute('delete from ag_kit_barcodes where barcode = %s',
                    ('991299',))
        cur.execute('delete from project_barcode where barcode = %s',
                    ('991299',))
        cur.execute('delete from barcode where barcode = %s', ('991299', ))
        self.con.commit()

    def test_updateAGBarcode(self):
        self.data_access.updateAGBarcode(
            '000010860', 'd8592c74-7da2-2135-e040-8a80115d6401', 'Stool', '',
            '07/30/2014', '9:30 AM', 'test', 'notes', 'n', 'n')
        cur = self.con.cursor()
        cur.execute('select * from ag_kit_barcodes where barcode = %s',
                    ('000010860',))
        rec = cur.fetchone()
        self.assertEqual(rec[5], 'Stool')
        self.assertEqual(rec[6], '07/30/2014')
        self.data_access.updateAGBarcode(
            '000010860', 'd8592c74-7da2-2135-e040-8a80115d6401', '', '', '',
            '', '', '', '', '')
        cur.execute('select * from ag_kit_barcodes where barcode = %s',
                    ('000010860',))
        rec = cur.fetchone()
        self.assertEqual(rec[5], '')
        self.assertEqual(rec[6], '')

    def test_addAGHumanParticipant(self):
        self.data_access.addAGHumanParticipant(
            'd8592c747da12135e0408a80115d6401', 'sp_test')
        data = self.data_access.getHumanParticipants(
            'd8592c74-7da1-2135-e040-8a80115d6401')
        self.assertTrue('sp_test' in data)
        cur = self.con.cursor()
        cur.execute('delete from ag_human_survey where ag_login_id =   %s and '
                    'participant_name = %s',
                    ('d8592c747da12135e0408a80115d6401', 'sp_test',))
        self.con.commit()

    def test_addAGAnimalParticipant(self):
        self.data_access.addAGAnimalParticipant(
            'd8592c747da12135e0408a80115d6401', 'fuzzy2')
        data = self.data_access.getAnimalParticipants(
            'd8592c747da12135e0408a80115d6401')
        self.assertTrue('fuzzy2' in data)
        cur = self.con.cursor()
        cur.execute('delete from ag_animal_survey where ag_login_id = %s and '
                    'participant_name = %s',
                    ('d8592c747da12135e0408a80115d6401', 'fuzzy2',))
        self.con.commit()

    def test_addAGSingle(self):
        self.data_access.addAGSingle('d8592c747da12135e0408a80115d6401',
                                     'Emily', 'softener', 'haha',
                                     'ag_human_survey')
        cur = self.con.cursor()
        cur.execute('select softener from ag_human_survey where ag_login_id = '
                    '%s and participant_name = %s',
                    ('d8592c747da12135e0408a80115d6401', 'Emily',))
        rec = cur.fetchone()[0]
        self.assertEqual(rec, 'haha')
        self.data_access.addAGSingle('d8592c747da12135e0408a80115d6401',
                                     'Emily', 'softener', 'no',
                                     'ag_human_survey')
        cur.execute('select softener from ag_human_survey where ag_login_id = '
                    '%s and participant_name = %s',
                    ('d8592c747da12135e0408a80115d6401', 'Emily',))
        rec = cur.fetchone()[0]
        self.assertEqual(rec, 'no')

    def test_deleteAGParticipant(self):
        cur = self.con.cursor()
        cur.execute('insert into ag_human_survey (ag_login_id, '
                    'participant_name) values (%s, %s)',
                    ('d8592c747da12135e0408a80115d6401', 'sp_test',))
        self.con.commit()
        self.data_access.deleteAGParticipant(
            'd8592c747da12135e0408a80115d6401', 'sp_test')
        data = self.data_access.getHumanParticipants(
            'd8592c747da12135e0408a80115d6401')
        self.assertEqual(len(data), 2)

    def test_addAGGeneralValue(self):
        self.data_access.addAGGeneralValue('d8592c747da12135e0408a80115d6401',
                                           'test', 'badquest', 'badans')
        cur = self.con.cursor()
        cur.execute('select * from ag_survey_answer where question = %s',
                    ('badquest',))
        rec = cur.fetchall()
        self.assertEqual(len(rec), 1)
        cur.execute('delete from ag_survey_answer where ag_login_id = %s and '
                    'participant_name = %s and question =  %s',
                    ('d8592c747da12135e0408a80115d6401', 'test', 'badquest',))
        self.con.commit()

    def test_deleteAGGeneralValues(self):
        cur = self.con.cursor()
        cur.execute('insert into ag_survey_answer(ag_login_id, '
                    'participant_name, question, answer) values (%s, %s, '
                    '%s, %s), (%s,%s, %s, %s)',
                    ('d8592c747da12135e0408a80115d6401', 'Emily2', 'race',
                        'Caucasian', 'd8592c747da12135e0408a80115d6401',
                        'Emily2', 'cat', 'yes',))
        self.data_access.deleteAGGeneralValues(
            'd8592c747da12135e0408a80115d6401', 'Emily2')
        cur.execute('select * from ag_survey_answer where ag_login_id = %s and'
                    ' participant_name = %s',
                    ('d8592c747da12135e0408a80115d6401', 'Emily2',))
        rec = cur.fetchall()
        self.assertEqual(len(rec), 0)

    def test_logParticipantSample(self):
        self.data_access.logParticipantSample('000010860', 'Stool', '',
                                              '07/29/2014', '09:30 AM',
                                              'Emily', 'no notes')
        data = self.data_access.getAGBarcodeDetails('000010860')
        self.assertEqual(data['participant_name'], 'Emily')
        self.assertEqual(data['site_sampled'], 'Stool')
        self.assertEqual(data['sample_date'], '07/29/2014')
        self.data_access.deleteSample('000010860',
                                      'd8592c747da12135e0408a80115d6401')
        data = self.data_access.getAGBarcodeDetails('000010860')
        self.assertEqual(data['participant_name'], None)

    def test_deleteSample(self):
        cur = self.con.cursor()
        cur.execute('update ag_kit_barcodes set site_sampled = %s,'
                    'sample_date = %s, participant_name = %s, '
                    'sample_time = %s where barcode = %s', ('Stool',
                                                            '07/30/2014',
                                                            'test', '9:30 AM',
                                                            '000010860'))
        self.con.commit()
        data = self.data_access.getAGBarcodeDetails('000010860')
        self.assertEqual(data['site_sampled'], 'Stool')
        self.data_access.deleteSample('000010860',
                                      'd8592c747da12135e0408a80115d6401')
        data = self.data_access.getAGBarcodeDetails('000010860')
        self.assertEqual(data['participant_name'], None)

    def test_getHumanParticipants(self):
        data = self.data_access.getHumanParticipants(
            'd8592c74-7da1-2135-e040-8a80115d6401')
        self.assertEqual(set(data), {'Emily', 'foo'})

    def test_AGGetBarcodeMetadata(self):
        data = self.data_access.AGGetBarcodeMetadata('000000001')
        self.assertEqual(data[0]['foodallergies_peanuts'], 'unknown')
        self.assertEqual(data[0]['chickenpox'], 'unknown')

    def test_AGGetBarcodeMetadataAnimal(self):
        data = self.data_access.AGGetBarcodeMetadataAnimal('000010860')
        # this test needs to be changed when the test database is updated
        self.assertEqual(len(data), 0)

    def test_getAnimalParticipants(self):
        data = self.data_access.getAnimalParticipants(
            'd8592c74-7da1-2135-e040-8a80115d6401')
        # this test needs updated when the test database is updated
        self.assertEqual(len(data), 9)

    def test_getParticipantExceptions(self):
        data = self.data_access.getParticipantExceptions(
            'd8592c74-7da1-2135-e040-8a80115d6401')
        self.assertEqual(len(data), 1)

    def test_getParticipantSamples(self):
        data = self.data_access.getParticipantSamples(
            'd8592c74-7da1-2135-e040-8a80115d6401', 'foo')
        self.assertEqual(data[0]['status'], 'Received')
        self.assertEqual(data[0]['barcode'],  '000000001')

    def test_getEnvironmentalSamples(self):
        data = self.data_access.getEnvironmentalSamples(
            'd8592c74-7da1-2135-e040-8a80115d6401')
        barcodes = {x['barcode'] for x in data}
        # TODO: This is broken -- there are no environmental samples associated
        # with the test user. We need to set something up like we have in qiita
        # where a test DB is set up and torn down for each individual test
        self.assertEqual(barcodes, set())

    def test_getAvailableBarcodes(self):
        data = self.data_access.getAvailableBarcodes(
            'd8592c74-7da1-2135-e040-8a80115d6401')
        # this test will change when test database is updated
        self.assertEqual(len(data), 9)

    def test_verifyKit(self):
        cur = self.con.cursor()
        cur.execute(' update ag_kit set kit_verified = %s where '
                    'supplied_kit_id = %s', ('n', 'test',))
        self.con.commit()
        self.data_access.verifyKit('test')
        cur.execute('select kit_verified from ag_kit where supplied_kit_id = '
                    '%s', ('test',))
        rec = cur.fetchone()
        self.assertEqual(rec[0], 'y')

    def test_getMapMarkers(self):
        data = self.data_access.getMapMarkers()
        self.assertNotEqual(len(data), 0)

    def test_addParticipantException(self):
        self.data_access.addParticipantException(
            'd8592c747da12135e0408a80115d6401', 'random kid')
        data = self.data_access.getParticipantExceptions(
            'd8592c747da12135e0408a80115d6401')
        self.assertTrue('random kid' in data)
        cur = self.con.cursor()
        cur.execute('delete from ag_participant_exceptions where '
                    'ag_login_id = %s and participant_name = %s',
                    ('d8592c747da12135e0408a80115d6401', 'random kid',))
        self.con.commit()

    def test_handoutCheck(self):
        is_handout = self.data_access.handoutCheck('test', 'wrongpass')
        self.assertEqual(is_handout, 'n')

    def test_checkBarcode(self):
        data = self.data_access.checkBarcode('000000001')
        self.assertEqual(data['site_sampled'], 'Stool')
        self.assertEqual(data['name'], 'Test')

    def test_updateAGSurvey(self):
        self.data_access.updateAGSurvey('d8592c747da12135e0408a80115d6401',
                                        'Emily', 'softener', 'haha')
        cur = self.con.cursor()
        cur.execute('select softener from ag_human_survey where ag_login_id = '
                    '%s and participant_name = %s',
                    ('d8592c747da12135e0408a80115d6401', 'Emily',))
        rec = cur.fetchone()[0]
        self.assertEqual(rec, 'haha')
        self.data_access.updateAGSurvey('d8592c747da12135e0408a80115d6401',
                                        'Emily', 'softener', 'no')
        cur.execute('select softener from ag_human_survey where ag_login_id = '
                    '%s and participant_name = %s',
                    ('d8592c747da12135e0408a80115d6401', 'Emily',))
        rec = cur.fetchone()[0]
        self.assertEqual(rec, 'no')

    def test_getAGStats(self):
        data = self.data_access.getAGStats()
        self.assertEqual(len(data), 23)

    def test_updateAKB(self):
        self.data_access.updateAKB('000010860', 'n', 'n', 'y',
                                   'some other text', '07/30/2014')
        cur = self.con.cursor()
        cur.execute('select * from ag_kit_barcodes where barcode = %s',
                    ('000010860',))
        rec = cur.fetchone()
        self.assertEqual([rec[11], rec[12], rec[13]], ['n', 'n', 'y'])
        self.data_access.updateAKB('000010860', None, None, None, None, None)
        cur.execute('select * from ag_kit_barcodes where barcode = %s',
                    ('000010860',))
        rec = cur.fetchone()
        self.assertEqual([rec[11], rec[12], rec[13]], [None, None, None])

    def test_getAGKitIDsByEmail(self):
        data = self.data_access.getAGKitIDsByEmail('*****@*****.**')
        self.assertEqual(set(data), {'test', '1111'})

    def test_ag_set_pass_change_code(self):
        self.data_access.ag_set_pass_change_code('*****@*****.**', 'test',
                                                 '123456789')
        cur = self.con.cursor()
        cur.execute('select pass_reset_code from ag_kit where '
                    'supplied_kit_id = %s', ('test',))
        rec = cur.fetchone()
        self.assertEqual(rec[0], '123456789')
        cur.execute('update ag_kit set pass_reset_code = %s, '
                    'pass_reset_time = %s where supplied_kit_id = %s',
                    ('', None, 'test',))
        self.con.commit()

    def test_ag_update_kit_password(self):
        self.data_access.ag_update_kit_password('test', 'newpass')
        cur = self.con.cursor()
        cur.execute('select kit_password from ag_kit where supplied_kit_id = '
                    '%s', ('test',))
        rec = cur.fetchone()
        self.assertEqual(rec[0], 'newpass')

    def test_ag_verify_kit_password_change_code(self):
        self.data_access.ag_set_pass_change_code('*****@*****.**', 'test',
                                                 '123456789')
        result = self.data_access.ag_verify_kit_password_change_code(
            '*****@*****.**', 'test', '123456789')
        self.assertEqual(result, 1)
        cur = self.con.cursor()
        cur.execute('update ag_kit set pass_reset_code = %s, '
                    'pass_reset_time = %s where supplied_kit_id = %s',
                    ('', None, 'test',))
        self.con.commit()

    def test_getBarcodesByKit(self):
        observed = self.data_access.getBarcodesByKit('test')
        expected = {'000010860', '000010859', '000006616', '000000001',
                    '000017221', '000017223', '000017222', '000017225',
                    '000017224', '000017227', '000017226'}

        self.assertEqual(set(observed), expected)

    def test_checkPrintResults(self):
        data = self.data_access.checkPrintResults('test')
        self.assertTrue(data is None)

    def test_get_user_for_kit(self):
        data = self.data_access.get_user_for_kit('test')
        self.assertEqual(data, 'd8592c74-7da1-2135-e040-8a80115d6401')

    def test_menu_items(self):
        data = self.data_access.get_menu_items('test')
        self.assertEqual(data[0]['foo'][0]['barcode'], '000000001')

    def test_get_verification_code(self):
        data = self.data_access.get_verification_code('test')
        self.assertEqual(data, 'test')

    def test_get_user_info(self):
        data = self.data_access.get_user_info('test')
        self.assertEqual(data['email'], '*****@*****.**')

    def test_get_barcode_results(self):
        data = self.data_access.get_barcode_results('test')
        self.assertEqual(len(data), 1)
        data = self.data_access.get_barcode_results('1111')
        self.assertEqual(len(data), 0)

    def test_get_barcodes_from_handout_kit(self):
        data = self.data_access.get_barcodes_from_handout_kit('PGP_cRzJo')
        self.assertEqual(len(data), 5)
class TestAGDataAccess(TestCase):
    def setUp(self):
        self.ag_data = AGDataAccess()

    def tearDown(self):
        del self.ag_data

    def test_authenticateWebAppUser(self):
        # Test right pass but non-existant kit ID
        obs = self.ag_data.authenticateWebAppUser('randomkitID', 'test')
        self.assertEqual(obs, False)

        kit_id = self.ag_data.ut_get_supplied_kit_id(
            'ded5101d-cafb-f6b3-e040-8a80115d6f03')
        # Test wrong password
        obs = self.ag_data.authenticateWebAppUser(kit_id, 'wrongPass')
        self.assertEqual(obs, False)

        # Test corect password
        obs = self.ag_data.authenticateWebAppUser(kit_id, 'test')
        self.assertTrue(isinstance(obs, dict))
        self.assertEqual(obs['ag_login_id'],
                         'ded5101d-cafb-f6b3-e040-8a80115d6f03')

    def test_check_login_exists(self):
        email = '*****@*****.**'
        obs = self.ag_data.check_login_exists(email)
        self.assertEqual(obs, None)

        email = self.ag_data.ut_get_arbitrary_email()
        obs = self.ag_data.check_login_exists(email)
        as_uuid = UUID(obs, version=4)
        self.assertTrue(as_uuid.version, 4)

    @rollback
    def test_addAGLogin(self):
        # test new user
        exists = 'EXISTS'
        while exists is not None:
            email = ''.join([choice(ascii_letters)
                             for i in range(randint(5, 10))])
            domain = ''.join([choice(ascii_letters)
                             for i in range(randint(5, 10))])
            new_email = '@'.join([email, domain]) + '.com'
            exists = self.ag_data.check_login_exists(new_email)

        # make sure the ag_login_id is a UUID4 string
        ag_login_id = self.ag_data.addAGLogin(
            new_email, 'TESTDUDE', '123 fake test street', 'testcity',
            'teststate', '1L2 2G3', 'United Kingdom')
        as_uuid = UUID(ag_login_id)
        self.assertTrue(as_uuid.version, 4)

        # test existing user
        ag_login_id = self.ag_data.addAGLogin(
            '*****@*****.**', 'TESTOTHER', '123 fake test street', 'testcity',
            'teststate', '1L2 2G3', 'United Kingdom')

        obs = self.ag_data.addAGLogin(
            '*****@*****.**', 'TESTDUDE', '123 fake test street', 'testcity',
            'teststate', '1L2 2G3', 'United Kingdom')
        self.assertEqual(ag_login_id, obs)

    def test_getAGBarcodeDetails_bad_barcode(self):
        # test non-existant barcode
        with self.assertRaises(ValueError):
            self.ag_data.getAGBarcodeDetails('99')

        # test existing barcode but not in AG
        with self.assertRaises(ValueError):
            self.ag_data.getAGBarcodeDetails('000006232')

    def test_get_nonconsented_scanned_barcodes(self):
        supplied_kit_id, barcode =\
            self.ag_data.ut_get_arbitrary_supplied_kit_id_scanned_unconsented()
        obs = self.ag_data.get_nonconsented_scanned_barcodes(supplied_kit_id)
        self.assertIn(barcode, obs)

    def test_getAGBarcodeDetails(self):
        # test existing AG barcode
        obs = self.ag_data.getAGBarcodeDetails('000001047')
        exp = {
            'barcode': '000001047',
            'status': 'Received',
            'ag_kit_id': 'd8592c74-7e35-2135-e040-8a80115d6401',
            'site_sampled': 'Stool',
            'environment_sampled': None,
            'sample_date': datetime.date(2013, 3, 28),
            'sample_time': datetime.time(23, 25),
            'overloaded': None,
            'withdrawn': None,
            'other': None,
            'moldy': None,
            'refunded': None,
            'ag_kit_barcode_id': 'd8592c74-7e36-2135-e040-8a80115d6401',
            'date_of_last_email': None,
        }
        # only look at those fields, that are not subject to scrubbing
        self.assertEqual({k: obs[k] for k in exp}, exp)

    def test_getAGKitDetails(self):
        # test non-existant kit
        with self.assertRaises(ValueError):
            self.ag_data.getAGKitDetails('IDONTEXI5T')

        # test existing AG kit
        obs = self.ag_data.getAGKitDetails(
            self.ag_data.ut_get_supplied_kit_id(
                'd8592c74-84ba-2135-e040-8a80115d6401'))
        # deleted password, since it is not stable along DB versions
        exp = {'ag_kit_id': 'd8592c74-84bb-2135-e040-8a80115d6401',
               'supplied_kit_id':
               self.ag_data.ut_get_supplied_kit_id(
                'd8592c74-84ba-2135-e040-8a80115d6401'),
               'swabs_per_kit': 1L,
               'kit_password':
               '******',
               'verification_email_sent': 'n',
               'kit_verified': 'y'}
        # only look at those fields, that are not subject to scrubbing
        self.assertEqual({k: obs[k] for k in exp}, exp)

    def test_get_all_handout_kits(self):
        obs = self.ag_data.get_all_handout_kits()
        self.assertTrue(isinstance(obs, list))
        self.assertTrue(len(obs) > 0)

        for kit_id in obs:
            self.assertRegexpMatches(kit_id, '[a-zA-Z_]*')

    def test_registerHandoutKit_bad_data(self):
        # run on bad data
        with self.assertRaises(ValueError):
            self.ag_data.registerHandoutKit('BAD', 'DATA')

    def test_registerHandoutKit_bad_idz(self):
        # run on non-existant login id
        ag_login_id = '877bb1b5-7352-48bf-a7b1-1248c689b819'
        kit = self.ag_data.get_all_handout_kits()[0]
        obs = self.ag_data.registerHandoutKit(ag_login_id, kit)
        self.assertFalse(obs)

        # run on non-existant kit_id
        ag_login_id = 'dc3172b2-792c-4087-8a20-714297821c6a'
        kit = 'NoTR3AL'
        obs = self.ag_data.registerHandoutKit(ag_login_id, kit)
        self.assertFalse(obs)

    @rollback
    def test_registerHandoutKit(self):
        # run on real data
        ag_login_id = 'dc3172b2-792c-4087-8a20-714297821c6a'
        kit = self.ag_data.get_all_handout_kits()[0]
        obs = self.ag_data.registerHandoutKit(ag_login_id, kit)
        self.assertTrue(obs)
        # make sure kit removed from ag_handout_kits and inserted in ag_kit
        kits = self.ag_data.get_all_handout_kits()
        self.assertNotIn(kit, kits)
        obs = self.ag_data.getAGKitDetails(kit)
        self.assertEqual(obs['supplied_kit_id'], kit)

    @rollback
    def test_deleteAGParticipantSurvey(self):
        ag_login_id = '000fc4cd-8fa4-db8b-e050-8a800c5d02b5'

        with TRN:
            sql = """SELECT survey_id
                     FROM ag.ag_login_surveys
                     WHERE ag_login_id = %s"""
            TRN.add(sql, [ag_login_id])
            old_survey_ids = [x[0] for x in TRN.execute_fetchindex()]

            sql = """SELECT barcode
                     FROM ag.source_barcodes_surveys
                     WHERE survey_id IN %s"""
            TRN.add(sql, [tuple(old_survey_ids)])
            old_barcodes = [x[0] for x in TRN.execute_fetchindex()]

        # make sure we can get the corresponding survey by ID
        self.ag_data.getConsent('8b2b45bb3390b585')

        names = self.ag_data.ut_get_participant_names_from_ag_login_id(
            ag_login_id)
        self.ag_data.deleteAGParticipantSurvey(ag_login_id, names[0])

        with self.assertRaises(ValueError):
            self.ag_data.getConsent('8b2b45bb3390b585')

        res = self.ag_data.get_withdrawn()
        today = datetime.datetime.now().date()
        self.assertIn(ag_login_id, [r[0] for r in res])
        # we cannot check name and email since they get randomly scrubbed
        for r in res:
            if r[0] == ag_login_id:
                self.assertEqual(r[3], today)

        with TRN:
            # check that barcode are really deleted from
            # source_barcodes_surveys
            sql = """SELECT COUNT(*)
                     FROM ag.source_barcodes_surveys
                     WHERE barcode IN %s"""
            TRN.add(sql, [tuple(old_barcodes)])
            self.assertEqual(TRN.execute_fetchindex()[0][0], 0)

            # check that survey_ids are really deleted from
            # source_barcodes_surveys
            sql = """SELECT COUNT(*)
                     FROM ag.source_barcodes_surveys
                     WHERE survey_id IN %s"""
            TRN.add(sql, [tuple(old_survey_ids)])
            self.assertEqual(TRN.execute_fetchindex()[0][0], 0)

            # check that barcode are really deleted from
            # ag_kit_barcodes
            sql = """SELECT COUNT(*)
                     FROM ag.ag_kit_barcodes
                     WHERE barcode IN %s"""
            TRN.add(sql, [tuple(old_barcodes)])
            self.assertEqual(TRN.execute_fetchindex()[0][0], 0)

    @rollback
    def test_deleteAGParticipantSurvey_with_sample_bug(self):
        ag_login_id = 'd8592c74-9694-2135-e040-8a80115d6401'
        names = self.ag_data.ut_get_participant_names_from_ag_login_id(
            ag_login_id)

        # issue 616
        self.ag_data.deleteAGParticipantSurvey(ag_login_id, names[0])
        with self.assertRaises(ValueError):
            self.ag_data.getConsent('be8516e8c5d4ff4d')

    def test_getConsent(self):
        obs = self.ag_data.getConsent("8b2b45bb3390b585")
        exp = {'date_signed': None,
               'age_range': None,
               'ag_login_id': '000fc4cd-8fa4-db8b-e050-8a800c5d02b5',
               'deceased_parent': 'false',
               'survey_id': '8b2b45bb3390b585',
               'is_juvenile': False}
        # only look at those fields, that are not subject to scrubbing
        self.assertEqual({k: obs[k] for k in exp}, exp)

    def test_getConsentNotPresent(self):
        with self.assertRaises(ValueError):
            self.ag_data.getConsent("42")

    def test_logParticipantSample_badinfo(self):
        # bad ag_login_id
        with self.assertRaises(ValueError):
            self.ag_data.logParticipantSample(
                '11111111-1111-1111-1111-714297821c6a', '000001047',
                'stool', None, datetime.date(2015, 9, 27),
                datetime.time(15, 54), 'BADNAME', '')

    @rollback
    def test_logParticipantSample_tomultiplesurveys(self):
        ag_login_id = '5a10ea3e-9c7f-4ec3-9e96-3dc42e896668'
        participant_name = "Name - )?Åú*IüKb+"
        barcode = "000027913"

        # check that there are no barcode <-> survey assignments, prior to
        # logging
        with TRN:
            sql = """SELECT COUNT(*) FROM ag.source_barcodes_surveys
                     WHERE survey_id IN (SELECT survey_id
                                         FROM ag.ag_login_surveys
                                         WHERE ag_login_id = %s
                                         AND participant_name = %s)"""
            TRN.add(sql, [ag_login_id, participant_name])
            self.assertEqual(TRN.execute_fetchindex()[0][0], 0)

        self.ag_data.logParticipantSample(
            ag_login_id, barcode, 'Stool', None, datetime.date(2015, 9, 27),
            datetime.time(15, 54), participant_name, '')

        # check that single barcode gets assigned to BOTH surveys
        with TRN:
            TRN.add(sql, [ag_login_id, participant_name])
            self.assertEqual(TRN.execute_fetchindex()[0][0], 2)

    @rollback
    def test_deleteSample_removeallsurveys(self):
        ag_login_id = '5a10ea3e-9c7f-4ec3-9e96-3dc42e896668'
        participant_name = "Name - )?Åú*IüKb+"
        barcode = "000027913"
        self.ag_data.logParticipantSample(
            ag_login_id, barcode, 'Stool', None, datetime.date(2015, 9, 27),
            datetime.time(15, 54), participant_name, '')

        sql = """SELECT COUNT(*)
                 FROM ag.source_barcodes_surveys
                 WHERE barcode = %s"""
        # check that barcodes are assigned to surveys
        with TRN:
            TRN.add(sql, [barcode])
            self.assertEqual(TRN.execute_fetchindex()[0][0], 2)

        self.ag_data.deleteSample(barcode, ag_login_id)

        # ensure barcode to survey assignment is deleted
        with TRN:
            TRN.add(sql, [barcode])
            self.assertEqual(TRN.execute_fetchindex()[0][0], 0)

    @rollback
    def test_logParticipantSample(self):
        # regular sample
        ag_login_id = '7732aafe-c4e1-4ae4-8337-6f22704c1064'
        barcode = '000027376'
        names = self.ag_data.ut_get_participant_names_from_ag_login_id(
            ag_login_id)

        self.ag_data.logParticipantSample(
            ag_login_id, barcode, 'Stool', None, datetime.date(2015, 9, 27),
            datetime.time(15, 54), names[0], '')
        obs = self.ag_data.getAGBarcodeDetails(barcode)
        exp = {'status': None,
               'ag_kit_id': '5bfa9526-8dbb-492f-937c-bceb6b5a56fe',
               'ag_kit_barcode_id': '793dab39-d9bf-4a0f-8d67-f21796e3faae',
               'barcode': '000027376',
               'site_sampled': 'Stool',
               'environment_sampled': None,
               'sample_date': datetime.date(2015, 9, 27),
               'sample_time': datetime.time(15, 54),
               'notes': '', 'overloaded': None,
               'withdrawn': None,
               'other': None,
               'moldy': None,
               'refunded': None,
               'date_of_last_email': None,
               }
        self.ag_data.deleteSample(barcode, ag_login_id)
        # only look at those fields, that are not subject to scrubbing
        self.assertEqual({k: obs[k] for k in exp}, exp)

        # env sample
        self.ag_data.logParticipantSample(
            ag_login_id, barcode, None, 'animal_habitat',
            datetime.date(2015, 9, 26), datetime.time(15, 00), 'REMOVED', '')
        obs = self.ag_data.getAGBarcodeDetails(barcode)
        exp = {'status': None,
               'ag_kit_id': '5bfa9526-8dbb-492f-937c-bceb6b5a56fe',
               'ag_kit_barcode_id': '793dab39-d9bf-4a0f-8d67-f21796e3faae',
               'barcode': '000027376',
               'site_sampled': None,
               'environment_sampled': 'animal_habitat',
               'sample_date': datetime.date(2015, 9, 26),
               'sample_time': datetime.time(15, 00),
               'notes': '', 'overloaded': None,
               'withdrawn': None,
               'other': None,
               'moldy': None,
               'refunded': None,
               'date_of_last_email': None,
               }
        self.ag_data.deleteSample(barcode, ag_login_id)
        # only look at those fields, that are not subject to scrubbing
        self.assertEqual({k: obs[k] for k in exp}, exp)

    def test_getHumanParticipants(self):
        i = "d8592c74-9694-2135-e040-8a80115d6401"
        res = self.ag_data.getHumanParticipants(i)
        exp = self.ag_data.ut_get_participant_names_from_ag_login_id(i)
        # check if results are subset of all names for this ag_login_id
        for name in res:
            self.assertIn(name, exp)

    def test_getHumanParticipantsNotPresent(self):
        i = '00000000-0000-0000-0000-000000000000'
        res = self.ag_data.getHumanParticipants(i)
        self.assertEqual(res, [])

    @rollback
    def test_vioscreen_status(self):
        survey_id = 'eba20dea4f54b997'
        self.ag_data.updateVioscreenStatus(survey_id, 3)
        obs = self.ag_data.get_vioscreen_status(survey_id)
        self.assertEqual(obs, 3)

        self.ag_data.updateVioscreenStatus(survey_id, None)
        obs = self.ag_data.get_vioscreen_status(survey_id)
        self.assertEqual(obs, None)

    def test_get_vioscreen_status_unknown_survey(self):
        with self.assertRaises(ValueError):
            self.ag_data.get_vioscreen_status('SomeRandomSurveyID')

    def test_getAnimalParticipants(self):
        i = "ed5ab96f-fe3b-ead5-e040-8a80115d1c4b"
        res = self.ag_data.getAnimalParticipants(i)
        exp = self.ag_data.ut_get_participant_names_from_ag_login_id(i)
        # check if results are subset of all names for this ag_login_id
        for name in res:
            self.assertIn(name, exp)

    def test_getAnimalParticipantsNotPresent(self):
        i = "00711b0a-67d6-0fed-e050-8a800c5d7570"
        res = self.ag_data.getAnimalParticipants(i)
        self.assertEqual(res, [])

    def test_getParticipantSamples(self):
        i = "d6b0f287-b9d9-40d4-82fd-a8fd3db6c476"
        names = self.ag_data.ut_get_participant_names_from_ag_login_id(i)
        # collect results for ALL names
        res = [self.ag_data.getParticipantSamples(i, name)
               for name in names]
        exp = {'sample_time': datetime.time(11, 55),
               'barcode': '000028432',
               'sample_date': datetime.date(2015, 6, 7),
               'site_sampled': 'Stool'}
        # transform results
        res = [dict((k, r[0][k]) for k in exp.keys()) for r in res if r != []]
        # only look at those fields, that are not subject to scrubbing
        self.assertIn(exp, res)

        i = "d8592c74-9694-2135-e040-8a80115d6401"
        # collect all results for ALL names for this ag_login_id and remove
        # field "notes" since it gets scrubbed.
        names = set(self.ag_data.ut_get_participant_names_from_ag_login_id(i))
        obs = []
        for name in names:
            res = self.ag_data.getParticipantSamples(i, name)
            for r in res:
                del r['notes']
                obs.append(r)
        exp = [{'status': 'Received',
                'sample_time': datetime.time(7, 40),
                'barcode': '000016704',
                'sample_date': datetime.date(2014, 6, 5),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(11, 30),
                'barcode': '000016705',
                'sample_date': datetime.date(2014, 6, 1),
                'site_sampled': 'Stool'},
               {'status': 'Received', 'sample_time': datetime.time(9, 20),
                'barcode': '000016706',
                'sample_date': datetime.date(2014, 6, 8),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(9, 20),
                'barcode': '000016707',
                'sample_date': datetime.date(2014, 6, 1),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(22, 0),
                'barcode': '000016708',
                'sample_date': datetime.date(2014, 5, 28),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(11, 0),
                'barcode': '000016709',
                'sample_date': datetime.date(2014, 5, 29),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(22, 20),
                'barcode': '000016710',
                'sample_date': datetime.date(2014, 5, 27),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(8, 0),
                'barcode': '000016711',
                'sample_date': datetime.date(2014, 6, 11),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(8, 15),
                'barcode': '000016712',
                'sample_date': datetime.date(2014, 6, 2),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(12, 0),
                'barcode': '000016713',
                'sample_date': datetime.date(2014, 5, 30),
                'site_sampled': 'Stool'},
               {'status': None,
                'sample_time': datetime.time(19, 30),
                'barcode': '000016496',
                'sample_date': datetime.date(2014, 4, 29),
                'site_sampled': 'Stool'},
               {'status': None,
                'sample_time': datetime.time(19, 30),
                'barcode': '000016497',
                'sample_date': datetime.date(2014, 4, 29),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(10, 20),
                'barcode': '000004213',
                'sample_date': datetime.date(2013, 10, 16),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(9, 50),
                'barcode': '000004214',
                'sample_date': datetime.date(2013, 10, 14),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(12, 0),
                'barcode': '000004215',
                'sample_date': datetime.date(2013, 10, 13),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(9, 30),
                'barcode': '000004216',
                'sample_date': datetime.date(2013, 10, 15),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(14, 25),
                'barcode': '000004218',
                'sample_date': datetime.date(2013, 10, 12),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(10, 15),
                'barcode': '000004219',
                'sample_date': datetime.date(2013, 10, 17),
                'site_sampled': 'Stool'}]
        self.assertItemsEqual(obs, exp)

    def test_getParticipantSamplesNotPresent(self):
        i = '00000000-0000-0000-0000-000000000000'
        res = self.ag_data.getParticipantSamples(i, "REMOVED")
        self.assertEqual(res, [])

    def test_getEnvironmentalSamples(self):
        fields = ['sample_time', 'barcode', 'sample_date', 'site_sampled']

        i = "df62647f-c7e1-9de7-e040-8a80115d5c07"
        obs = []
        for bc in self.ag_data.ut_get_barcode_from_ag_login_id(i):
            obs.append(dict((k, bc[k]) for k in fields))

        for bc in self.ag_data.getEnvironmentalSamples(i):
            self.assertIn(dict((k, bc[k]) for k in fields), obs)

    def test_getEnvironmentalSamplesNotPresent(self):
        i = '00000000-0000-0000-0000-000000000000'
        res = self.ag_data.getEnvironmentalSamples(i)
        self.assertEqual(res, [])

    def test_getAvailableBarcodes(self):
        i = "d8592c74-9694-2135-e040-8a80115d6401"
        res = self.ag_data.getAvailableBarcodes(i)
        exp = ['000005628', '000005627', '000005624',
               '000005625', '000005626', '000004217']
        self.assertItemsEqual(res, exp)

        i = "d6b0f287-b9d9-40d4-82fd-a8fd3db6c476"
        res = self.ag_data.getAvailableBarcodes(i)
        exp = [x['barcode'] for x in
               self.ag_data.ut_get_barcode_from_ag_login_id(i)
               if x['kit_verified'] == 'y' and x['sample_date'] is None]
        self.assertItemsEqual(res, exp)

    def test_getAvailableBarcodesNotPresent(self):
        i = '00000000-0000-0000-0000-000000000000'
        res = self.ag_data.getAvailableBarcodes(i)
        self.assertEqual(res, [])

    @rollback
    def test_verifyKit(self):
        # Test verifying works
        kit = self.ag_data._get_unverified_kits()[0]
        self.ag_data.verifyKit(kit)
        obs = self.ag_data.getAGKitDetails(kit)
        self.assertEqual(obs['kit_verified'], 'y')

        # Test verifying a non-existant kit
        with self.assertRaises(ValueError):
            self.ag_data.getAGKitDetails('NOTAREALKITID')

    def test__get_unverified_kits(self):
        obs = self.ag_data._get_unverified_kits()
        self.assertTrue(isinstance(obs, list))
        self.assertTrue(len(obs) > 0)

        for kit_id in obs:
            self.assertRegexpMatches(kit_id, '[a-zA-Z_]*')
            obs = self.ag_data.getAGKitDetails(kit_id)
            self.assertEqual(obs['kit_verified'], 'n')

    @rollback
    def test_handoutCheck(self):
        # Test proper password for handout
        # All tests use assertEqual to make sure bool object returned
        kit = self.ag_data.get_all_handout_kits()[0]
        obs = self.ag_data.handoutCheck(kit, 'test')
        self.assertEqual(obs, True)

        # Test wrong password
        obs = self.ag_data.handoutCheck(kit, 'badPass')
        self.assertEqual(obs, False)

        # Test non-handout kit
        obs = self.ag_data.handoutCheck(
            self.ag_data.ut_get_supplied_kit_id(
                'd8592c74-84ba-2135-e040-8a80115d6401'),
            'test')
        self.assertEqual(obs, False)
        obs = self.ag_data.handoutCheck('randomKitID', 'test')
        self.assertEqual(obs, False)

    def test_check_access(self):
        # Has access
        obs = self.ag_data.check_access(
            self.ag_data.ut_get_supplied_kit_id(
                'd8592c74-7e34-2135-e040-8a80115d6401'),
            '000001047')
        self.assertEqual(obs, True)

        # No access
        obs = self.ag_data.check_access(
            self.ag_data.ut_get_supplied_kit_id(
                'd8592c74-7e34-2135-e040-8a80115d6401'),
            '000001111')
        self.assertEqual(obs, False)

    @rollback
    def test_ag_set_pass_change_code(self):
        ag_login_id = 'd8592c74-8416-2135-e040-8a80115d6401'

        # Generate new random code and assign it
        testcode = ''.join(choice(ascii_letters) for i in range(10))
        email = self.ag_data.ut_get_email_from_ag_login_id(ag_login_id)
        self.ag_data.ag_set_pass_change_code(
            email,
            self.ag_data.ut_get_supplied_kit_id(ag_login_id),
            testcode)

        # Actually test the code change
        obs = self.ag_data.ag_verify_kit_password_change_code(
            email,
            self.ag_data.ut_get_supplied_kit_id(ag_login_id),
            'SOMELONGTHINGTHATWILLFAIL')
        self.assertEqual(obs, False)
        obs = self.ag_data.ag_verify_kit_password_change_code(
            email,
            self.ag_data.ut_get_supplied_kit_id(ag_login_id),
            testcode)
        # Using equal to make sure boolean True is returned, not something that
        # equates to True
        self.assertEqual(obs, True)

        # Test giving nonsense email
        # TODO: make this raise error and test
        self.ag_data.ag_set_pass_change_code(
            '*****@*****.**',
            self.ag_data.ut_get_supplied_kit_id(ag_login_id),
            testcode)

        # Test giving bad skid
        # TODO: make this raise error and test
        self.ag_data.ag_set_pass_change_code('REMOVED', 'NOTINTHEDB', testcode)

    @rollback
    def test_ag_update_kit_password(self):
        # Generate new pass and make sure is different from current pass
        newpass = ''.join(choice(ascii_letters) for i in range(randint(8, 15)))
        auth = self.ag_data.authenticateWebAppUser(
            self.ag_data.ut_get_supplied_kit_id(
                'd8592c74-8416-2135-e040-8a80115d6401'),
            newpass)
        self.assertFalse(
            auth, msg="Randomly generated password matches existing")

        # Actually test password change
        self.ag_data.ag_update_kit_password(
            self.ag_data.ut_get_supplied_kit_id(
                'd8592c74-8416-2135-e040-8a80115d6401'),
            newpass)
        auth = self.ag_data.authenticateWebAppUser(
            self.ag_data.ut_get_supplied_kit_id(
                'd8592c74-8416-2135-e040-8a80115d6401'),
            newpass)
        self.assertTrue(isinstance(auth, dict))
        self.assertEqual(auth['ag_login_id'],
                         'd8592c74-8416-2135-e040-8a80115d6401')

        # Test giving bad skid
        # TODO: make this raise error and test
        self.ag_data.ag_update_kit_password('NOTINTHEDB', newpass)

    @rollback
    def test_ag_verify_kit_password_change_code(self):
        ag_login_id = '6165453f-e8bc-4edc-b00e-50e72fe550c9'
        email = self.ag_data.ut_get_email_from_ag_login_id(ag_login_id)

        # Test actual functionality
        obs = self.ag_data.ag_verify_kit_password_change_code(
            email,
            self.ag_data.ut_get_supplied_kit_id(ag_login_id),
            'FAIL')
        # Using assertEqual to make sure boolean False is returned, not
        # something that equates to False. Same for rest of assertEquals below
        self.assertEqual(obs, False)
        # Outside reset time, should fail
        obs = self.ag_data.ag_verify_kit_password_change_code(
            email,
            self.ag_data.ut_get_supplied_kit_id(
                ag_login_id),
            'Mw1eY4wWVXpE0cQlvQwS')
        self.assertEqual(obs, False)

        # Reset code and make sure it works
        testcode = ''.join(choice(ascii_letters) for i in range(10))
        self.ag_data.ag_set_pass_change_code(
            email,
            self.ag_data.ut_get_supplied_kit_id(ag_login_id),
            testcode)
        obs = self.ag_data.ag_verify_kit_password_change_code(
            email,
            self.ag_data.ut_get_supplied_kit_id(ag_login_id),
            testcode)
        self.assertEqual(obs, True)

        # Test with incorrect kit id
        obs = self.ag_data.ag_verify_kit_password_change_code(
            'REMOVED', 'NOTAREALKITID', 'FAIL')
        self.assertEqual(obs, False)

        # Test with incorrect email
        ag_login_id = 'd8592c74-8416-2135-e040-8a80115d6401'
        email = self.ag_data.ut_get_email_from_ag_login_id(ag_login_id)
        obs = self.ag_data.ag_verify_kit_password_change_code(
            email,
            self.ag_data.ut_get_supplied_kit_id(ag_login_id),
            testcode)
        self.assertEqual(obs, False)

    def test_getBarcodesByKit(self):
        res = self.ag_data.getBarcodesByKit(
            self.ag_data.ut_get_supplied_kit_id(
                'd8592c74-7e7f-2135-e040-8a80115d6401'))
        exp = ['000001322']
        self.assertItemsEqual(res, exp)

    def test_getBarcodesByKitNotPresent(self):
        res = self.ag_data.getBarcodesByKit('42')
        self.assertEqual(res, [])

    def test_checkPrintResults(self):
        obs = self.ag_data.checkPrintResults(
            self.ag_data.ut_get_supplied_kit_id(
                'dc3172b2-792c-4087-8a20-714297821c6a'))
        self.assertFalse(obs)

        kit_id = self.ag_data\
            .ut_get_arbitrary_handout_printed_min6_supplied_kit_id()
        obs = self.ag_data.checkPrintResults(kit_id)
        self.assertTrue(obs)

    def test_checkPrintResults_invalid_ids(self):
        obs = self.ag_data.checkPrintResults('xxx00112333123---123222')
        self.assertFalse(obs)

        obs = self.ag_data.checkPrintResults(':Lfoo:Lbar:Lbaz:Ospam:Leggs')
        self.assertFalse(obs)

    def test_get_user_for_kit(self):
        obs = self.ag_data.get_user_for_kit(
            self.ag_data.ut_get_supplied_kit_id(
                'ded5101d-c8e3-f6b3-e040-8a80115d6f03'))
        self.assertEqual('ded5101d-c8e3-f6b3-e040-8a80115d6f03', obs)

        obs = self.ag_data.get_user_for_kit(
            self.ag_data.ut_get_supplied_kit_id(
                'd8592c74-8421-2135-e040-8a80115d6401'))
        self.assertEqual('d8592c74-8421-2135-e040-8a80115d6401', obs)

    def test_get_user_for_kit_errors(self):
        with self.assertRaises(ValueError):
            self.ag_data.get_user_for_kit('the_fooster')

        with self.assertRaises(ValueError):
            self.ag_data.get_user_for_kit('NOT_IN_DB')

    def test_get_menu_items(self):
        obs = self.ag_data.get_menu_items(
            self.ag_data.ut_get_supplied_kit_id(
                'd8592c74-844b-2135-e040-8a80115d6401'))
        self.assertEqual(({}, {}, [], True), obs)

        ag_login_id = 'd8592c74-84c9-2135-e040-8a80115d6401'
        obs = self.ag_data.get_menu_items(
            self.ag_data.ut_get_supplied_kit_id(ag_login_id))
        names = self.ag_data.ut_get_participant_names_from_ag_login_id(
            ag_login_id)
        self.assertEqual((dict((name, []) for name in names),
                         {}, [], True), obs)

    def test_get_menu_items_errors(self):
        with self.assertRaises(ValueError):
            self.ag_data.get_menu_items('NOT_IN_DB')

    def test_check_if_consent_exists(self):
        ag_login_id = '00711b0a-67d6-0fed-e050-8a800c5d7570'
        names = self.ag_data.ut_get_participant_names_from_ag_login_id(
            ag_login_id)
        obs = self.ag_data.check_if_consent_exists(
            ag_login_id, names[0])
        self.assertTrue(obs)

    def test_check_if_consent_exists_non_existent_user(self):
        ag_login_id = '00711b0a-67d6-0fed-e050-8a800c5d7570'
        obs = self.ag_data.check_if_consent_exists(ag_login_id, 'REMOVED-111')
        self.assertFalse(obs)

    def test_get_user_info(self):
        ag_login_id = 'd8592c74-84a5-2135-e040-8a80115d6401'
        obs = self.ag_data.get_user_info(
            self.ag_data.ut_get_supplied_kit_id(ag_login_id))
        # unfortunatly, most fields are scrubbed in the database, thus we
        # cannot compare them over DB versions
        exp = {'ag_login_id': ag_login_id,
               'email': self.ag_data.ut_get_email_from_ag_login_id(
                ag_login_id)}
        self.assertEqual(exp, dict((k, obs[k]) for k in exp.keys()))
        exp = ['address', 'ag_login_id', 'city', 'country', 'email', 'name',
               'state', 'zip']
        self.assertItemsEqual(obs.keys(), exp)

    def test_get_user_info_non_existent(self):
        with self.assertRaises(ValueError):
            self.ag_data.get_user_info('NOT_IN_DB')

    def test_get_barcode_results(self):
        ag_login_id = 'd8592c74-9694-2135-e040-8a80115d6401'
        obs = self.ag_data.get_barcode_results(
            self.ag_data.ut_get_supplied_kit_id(ag_login_id))
        # remove participant_names from results
        for o in obs:
            del o['participant_name']
        # we cannot compare participant_names, since they are scrubbed
        exp = [{'barcode': '000016704'},
               {'barcode': '000016705'},
               {'barcode': '000016706'},
               {'barcode': '000016707'},
               {'barcode': '000016708'},
               {'barcode': '000016709'},
               {'barcode': '000016710'},
               {'barcode': '000016711'},
               {'barcode': '000016712'},
               {'barcode': '000016713'},
               {'barcode': '000004213'},
               {'barcode': '000004214'},
               {'barcode': '000004215'},
               {'barcode': '000004216'},
               {'barcode': '000004218'},
               {'barcode': '000004219'}]
        self.assertItemsEqual(obs, exp)

    def test_get_barcode_results_non_existant_id(self):
        with self.assertRaises(ValueError):
            self.ag_data.get_barcode_results("something that doesn't exist")

    def test_get_login_info(self):
        id_ = 'fecebeae-4244-2d78-e040-8a800c5d4f50'
        exp = {'ag_login_id': id_,
               'email': self.ag_data.ut_get_email_from_ag_login_id(id_)}
        obs = self.ag_data.get_login_info(id_)
        self.assertEqual(dict((k, obs[0][k]) for k in exp.keys()), exp)

    def test_get_login_info_non_existant_id(self):
        id_ = '00000000-0000-0000-0000-000000000000'
        with self.assertRaises(ValueError):
            self.ag_data.get_login_info(id_)

    def test_get_survey_ids(self):
        id_ = '8ca47059-000a-469f-aa64-ff1afbd6fcb1'
        names = self.ag_data.ut_get_participant_names_from_ag_login_id(id_)
        obs = [self.ag_data.get_survey_ids(id_, name) for name in names]
        self.assertIn({1: 'd08758a1510256f0'}, obs)

    def test_get_survey_ids_non_existant_id(self):
        id_ = '00000000-0000-0000-0000-000000000000'
        with self.assertRaises(ValueError):
            self.ag_data.get_survey_ids(id_, 'REMOVED')

    def test_get_countries(self):
        obs = self.ag_data.get_countries()
        # Make sure is a list with proper length
        self.assertTrue(isinstance(obs, list))
        self.assertEqual(len(obs), 244)

        # Spot check a few countries
        self.assertIn('United States', obs)
        self.assertIn('United Kingdom', obs)

    def test_is_deposited_ebi(self):
        barcode = self.ag_data.ut_get_arbitrary_barcode(deposited=False)
        obs = self.ag_data.is_deposited_ebi(barcode)
        self.assertFalse(obs)

    def test_is_deposited_ebi_bad_barcode(self):
        with self.assertRaises(ValueError):
            self.ag_data.is_deposited_ebi('NOTABARCODE')
Example #9
0
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The American Gut Project Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

from amgut.lib.data_access.ag_data_access import AGDataAccess

__all__ = ['AG_DATA_ACCESS']

PORTAL_TYPE = 'americangut'

# Data Access
AG_DATA_ACCESS = AGDataAccess()
class TestAGDataAccess(TestCase):
    def setUp(self):
        self.ag_data = AGDataAccess()

    def tearDown(self):
        del self.ag_data

    def test_authenticateWebAppUser(self):
        # Test right pass but non-existant kit ID
        obs = self.ag_data.authenticateWebAppUser('randomkitID', 'test')
        self.assertEqual(obs, False)

        kit_id = 'tst_xfphP'
        # Test wrong password
        obs = self.ag_data.authenticateWebAppUser(kit_id, 'wrongPass')
        self.assertEqual(obs, False)

        # Test corect password
        obs = self.ag_data.authenticateWebAppUser(kit_id, 'test')
        self.assertTrue(isinstance(obs, dict))
        self.assertEqual(obs['ag_login_id'],
                         'ded5101d-cafb-f6b3-e040-8a80115d6f03')

    def test_check_login_exists(self):
        email = '*****@*****.**'
        obs = self.ag_data.check_login_exists(email)
        self.assertEqual(obs, None)

        email = 'REMOVED'
        obs = self.ag_data.check_login_exists(email)
        as_uuid = UUID(obs)
        self.assertTrue(as_uuid.version, 4)

    def test_addAGLogin(self):
        # test new user
        exists = 'EXISTS'
        while exists is not None:
            email = ''.join([choice(ascii_letters)
                             for i in range(randint(5, 10))])
            domain = ''.join([choice(ascii_letters)
                             for i in range(randint(5, 10))])
            new_email = '@'.join([email, domain]) + '.com'
            exists = self.ag_data.check_login_exists(new_email)

        # make sure the ag_login_id is a UUID4 string
        ag_login_id = self.ag_data.addAGLogin(
            new_email, 'TESTDUDE', '123 fake test street', 'testcity',
            'teststate', '1L2 2G3', 'United Kingdom')
        as_uuid = UUID(ag_login_id)
        self.assertTrue(as_uuid.version, 4)

        # test existing user
        ag_login_id = self.ag_data.addAGLogin(
            '*****@*****.**', 'TESTOTHER', '123 fake test street', 'testcity',
            'teststate', '1L2 2G3', 'United Kingdom')

        obs = self.ag_data.addAGLogin(
            '*****@*****.**', 'TESTDUDE', '123 fake test street', 'testcity',
            'teststate', '1L2 2G3', 'United Kingdom')
        self.assertEqual(ag_login_id, obs)

    def test_getAGBarcodeDetails_bad_barcode(self):
        # test non-existant barcode
        with self.assertRaises(ValueError):
            self.ag_data.getAGBarcodeDetails('99')

        # test existing barcode but not in AG
        with self.assertRaises(ValueError):
            self.ag_data.getAGBarcodeDetails('000006232')

    def test_get_nonconsented_scanned_barcodes(self):
        obs = self.ag_data.get_nonconsented_scanned_barcodes('tst_KWfyv')
        exp = ['000027262']
        self.assertEqual(obs, exp)

    def test_getAGBarcodeDetails(self):
        # test existing AG barcode
        obs = self.ag_data.getAGBarcodeDetails('000001047')
        exp = {
            'barcode': '000001047',
            'status': 'Received',
            'ag_kit_id': 'd8592c74-7e35-2135-e040-8a80115d6401',
            'name': 'REMOVED',
            'participant_name': 'REMOVED-0',
            'email': 'REMOVED',
            'site_sampled': 'Stool',
            'environment_sampled': None,
            'sample_date': datetime.date(2013, 3, 28),
            'sample_time': datetime.time(23, 25),
            'notes': 'REMOVED',
            'overloaded': None,
            'withdrawn': None,
            'other': None,
            'moldy': None,
            'refunded': None,
            'ag_kit_barcode_id': 'd8592c74-7e36-2135-e040-8a80115d6401',
            'date_of_last_email': None,
            'other_text': 'REMOVED'
        }
        self.assertEqual(obs, exp)

    def test_getAGKitDetails(self):
        # test non-existant kit
        with self.assertRaises(ValueError):
            self.ag_data.getAGKitDetails('IDONTEXI5T')

        # test existing AG kit
        obs = self.ag_data.getAGKitDetails('tst_ODmhG')
        exp = {'ag_kit_id': 'd8592c74-84bb-2135-e040-8a80115d6401',
               'supplied_kit_id': 'tst_ODmhG',
               'swabs_per_kit': 1,
               'verification_email_sent': 'n',
               'kit_verification_code': 'f4UjhV4B',
               'kit_password': '******'
                               'ePcmKHWWAePrQ.',
               'kit_verified': 'y'}
        self.assertEqual(obs, exp)

    def test_get_all_handout_kits(self):
        obs = self.ag_data.get_all_handout_kits()
        self.assertTrue(isinstance(obs, list))
        self.assertTrue(len(obs) > 0)

        for kit_id in obs:
            self.assertRegexpMatches(kit_id, 'tst_[a-zA-Z]{5}')

    def test_registerHandoutKit_bad_data(self):
        # run on bad data
        with self.assertRaises(ValueError):
            self.ag_data.registerHandoutKit('BAD', 'DATA')

    def test_registerHandoutKit_bad_idz(self):
        # run on non-existant login id
        ag_login_id = '877bb1b5-7352-48bf-a7b1-1248c689b819'
        kit = self.ag_data.get_all_handout_kits()[0]
        obs = self.ag_data.registerHandoutKit(ag_login_id, kit)
        self.assertFalse(obs)

        # run on non-existant kit_id
        ag_login_id = 'dc3172b2-792c-4087-8a20-714297821c6a'
        kit = 'NoTR3AL'
        obs = self.ag_data.registerHandoutKit(ag_login_id, kit)
        self.assertFalse(obs)

    def test_registerHandoutKit(self):
        # run on real data
        ag_login_id = 'dc3172b2-792c-4087-8a20-714297821c6a'
        kit = self.ag_data.get_all_handout_kits()[0]
        obs = self.ag_data.registerHandoutKit(ag_login_id, kit)
        self.assertTrue(obs)
        # make sure kit removed from ag_handout_kits and inserted in ag_kit
        kits = self.ag_data.get_all_handout_kits()
        self.assertNotIn(kit, kits)
        obs = self.ag_data.getAGKitDetails(kit)
        self.assertEqual(obs['supplied_kit_id'], kit)

    @rollback
    def test_deleteAGParticipantSurvey(self):
        self.ag_data.deleteAGParticipantSurvey(
            '000fc4cd-8fa4-db8b-e050-8a800c5d02b5', 'REMOVED-0')
        with self.assertRaises(ValueError):
            self.ag_data.getConsent('8b2b45bb3390b585')

        res = self.ag_data.get_withdrawn()
        today = datetime.datetime.now().date()
        exp = [['000fc4cd-8fa4-db8b-e050-8a800c5d02b5', 'REMOVED-0',
                'REMOVED', today]]
        self.assertItemsEqual(res, exp)

    def test_getConsent(self):
        res = self.ag_data.getConsent("8b2b45bb3390b585")
        exp = {'date_signed': None,
               'assent_obtainer': None,
               'age_range': None,
               'parent_1_name': 'REMOVED',
               'participant_email': 'REMOVED',
               'parent_2_name': 'REMOVED',
               'ag_login_id': '000fc4cd-8fa4-db8b-e050-8a800c5d02b5',
               'deceased_parent': 'false',
               'participant_name': 'REMOVED-0',
               'survey_id': '8b2b45bb3390b585',
               'is_juvenile': False}
        self.assertEquals(res, exp)

    def test_getConsentNotPresent(self):
        with self.assertRaises(ValueError):
            self.ag_data.getConsent("42")

    def test_logParticipantSample_badinfo(self):
        # bad ag_login_id
        with self.assertRaises(ValueError):
            self.ag_data.logParticipantSample(
                '11111111-1111-1111-1111-714297821c6a', '000001047',
                'stool', None, datetime.date(2015, 9, 27),
                datetime.time(15, 54), 'BADNAME', '')

    def test_logParticipantSample(self):
        # regular sample
        ag_login_id = '7732aafe-c4e1-4ae4-8337-6f22704c1064'
        barcode = '000027376'

        self.ag_data.logParticipantSample(
            ag_login_id, barcode, 'Stool', None, datetime.date(2015, 9, 27),
            datetime.time(15, 54), 'REMOVED-0', '')
        obs = self.ag_data.getAGBarcodeDetails(barcode)
        exp = {'status': None,
               'ag_kit_id': '5bfa9526-8dbb-492f-937c-bceb6b5a56fe',
               'ag_kit_barcode_id': '793dab39-d9bf-4a0f-8d67-f21796e3faae',
               'barcode': '000027376',
               'site_sampled': 'Stool',
               'environment_sampled': None,
               'name': 'REMOVED',
               'sample_date': datetime.date(2015, 9, 27),
               'sample_time': datetime.time(15, 54),
               'notes': '', 'overloaded': None,
               'withdrawn': None,
               'email': 'REMOVED',
               'other': None,
               'moldy': None,
               'participant_name': 'REMOVED-0',
               'refunded': None,
               'date_of_last_email': None,
               'other_text': 'REMOVED'
               }
        self.ag_data.deleteSample(barcode, ag_login_id)
        self.assertEqual(obs, exp)

        # env sample
        self.ag_data.logParticipantSample(
            ag_login_id, barcode, None, 'animal_habitat',
            datetime.date(2015, 9, 26), datetime.time(15, 00), 'REMOVED', '')
        obs = self.ag_data.getAGBarcodeDetails(barcode)
        exp = {'status': None,
               'ag_kit_id': '5bfa9526-8dbb-492f-937c-bceb6b5a56fe',
               'ag_kit_barcode_id': '793dab39-d9bf-4a0f-8d67-f21796e3faae',
               'barcode': '000027376',
               'site_sampled': None,
               'environment_sampled': 'animal_habitat',
               'name': 'REMOVED',
               'sample_date': datetime.date(2015, 9, 26),
               'sample_time': datetime.time(15, 00),
               'notes': '', 'overloaded': None,
               'withdrawn': None,
               'email': 'REMOVED',
               'other': None,
               'moldy': None,
               'participant_name': None,
               'refunded': None,
               'date_of_last_email': None,
               'other_text': 'REMOVED'
               }
        self.ag_data.deleteSample(barcode, ag_login_id)
        self.assertEqual(obs, exp)

    def test_getHumanParticipants(self):
        i = "d8592c74-9694-2135-e040-8a80115d6401"
        res = self.ag_data.getHumanParticipants(i)
        exp = ['REMOVED-2', 'REMOVED-0', 'REMOVED-3', 'REMOVED-1']
        self.assertItemsEqual(res, exp)

    def test_getHumanParticipantsNotPresent(self):
        i = '00000000-0000-0000-0000-000000000000'
        res = self.ag_data.getHumanParticipants(i)
        self.assertEqual(res, [])

    def test_vioscreen_status(self):
        survey_id = 'eba20dea4f54b997'
        self.ag_data.updateVioscreenStatus(survey_id, 3)
        obs = self.ag_data.get_vioscreen_status(survey_id)
        self.assertEqual(obs, 3)

        self.ag_data.updateVioscreenStatus(survey_id, None)
        obs = self.ag_data.get_vioscreen_status(survey_id)
        self.assertEqual(obs, None)

    def test_get_vioscreen_status_unknown_survey(self):
        with self.assertRaises(ValueError):
            self.ag_data.get_vioscreen_status('SomeRandomSurveyID')

    def test_getAnimalParticipants(self):
        i = "ed5ab96f-fe3b-ead5-e040-8a80115d1c4b"
        res = self.ag_data.getAnimalParticipants(i)
        exp = ['REMOVED-0']
        self.assertItemsEqual(res, exp)

    def test_getAnimalParticipantsNotPresent(self):
        i = "00711b0a-67d6-0fed-e050-8a800c5d7570"
        res = self.ag_data.getAnimalParticipants(i)
        self.assertEqual(res, [])

    def test_getParticipantSamples(self):
        i = "d6b0f287-b9d9-40d4-82fd-a8fd3db6c476"
        res = self.ag_data.getParticipantSamples(i, "REMOVED-0")
        exp = [{'status': None,
                'sample_time': datetime.time(11, 55),
                'notes': 'REMOVED',
                'barcode': '000028432',
                'sample_date': datetime.date(2015, 6, 7),
                'site_sampled': 'Stool'}]
        self.assertEqual(res, exp)

        i = "d8592c74-9694-2135-e040-8a80115d6401"
        res = self.ag_data.getParticipantSamples(i, "REMOVED-0")
        exp = [{'status': 'Received',
                'sample_time': datetime.time(7, 40),
                'notes': 'REMOVED',
                'barcode': '000016704',
                'sample_date': datetime.date(2014, 6, 5),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(11, 30),
                'notes': 'REMOVED',
                'barcode': '000016705',
                'sample_date': datetime.date(2014, 6, 1),
                'site_sampled': 'Stool'},
               {'status': 'Received', 'sample_time': datetime.time(9, 20),
                'notes': 'REMOVED',
                'barcode': '000016706',
                'sample_date': datetime.date(2014, 6, 8),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(9, 20),
                'notes': 'REMOVED',
                'barcode': '000016707',
                'sample_date': datetime.date(2014, 6, 1),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(22, 0),
                'notes': 'REMOVED',
                'barcode': '000016708',
                'sample_date': datetime.date(2014, 5, 28),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(11, 0),
                'notes': 'REMOVED',
                'barcode': '000016709',
                'sample_date': datetime.date(2014, 5, 29),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(22, 20),
                'notes': 'REMOVED',
                'barcode': '000016710',
                'sample_date': datetime.date(2014, 5, 27),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(8, 0),
                'notes': 'REMOVED',
                'barcode': '000016711',
                'sample_date': datetime.date(2014, 6, 11),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(8, 15),
                'notes': 'REMOVED',
                'barcode': '000016712',
                'sample_date': datetime.date(2014, 6, 2),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(12, 0),
                'notes': 'REMOVED',
                'barcode': '000016713',
                'sample_date': datetime.date(2014, 5, 30),
                'site_sampled': 'Stool'},
               {'status': None,
                'sample_time': datetime.time(19, 30),
                'notes': 'REMOVED',
                'barcode': '000016496',
                'sample_date': datetime.date(2014, 4, 29),
                'site_sampled': 'Stool'},
               {'status': None,
                'sample_time': datetime.time(19, 30),
                'notes': 'REMOVED',
                'barcode': '000016497',
                'sample_date': datetime.date(2014, 4, 29),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(10, 20),
                'notes': 'REMOVED',
                'barcode': '000004213',
                'sample_date': datetime.date(2013, 10, 16),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(9, 50),
                'notes': 'REMOVED',
                'barcode': '000004214',
                'sample_date': datetime.date(2013, 10, 14),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(12, 0),
                'notes': 'REMOVED',
                'barcode': '000004215',
                'sample_date': datetime.date(2013, 10, 13),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(9, 30),
                'notes': 'REMOVED',
                'barcode': '000004216',
                'sample_date': datetime.date(2013, 10, 15),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(14, 25),
                'notes': 'REMOVED',
                'barcode': '000004218',
                'sample_date': datetime.date(2013, 10, 12),
                'site_sampled': 'Stool'},
               {'status': 'Received',
                'sample_time': datetime.time(10, 15),
                'notes': 'REMOVED',
                'barcode': '000004219',
                'sample_date': datetime.date(2013, 10, 17),
                'site_sampled': 'Stool'}]
        self.assertItemsEqual(res, exp)

    def test_getParticipantSamplesNotPresent(self):
        i = '00000000-0000-0000-0000-000000000000'
        res = self.ag_data.getParticipantSamples(i, "REMOVED")
        self.assertEqual(res, [])

    def test_getEnvironmentalSamples(self):
        i = "d6b0f287-b9d9-40d4-82fd-a8fd3db6c476"
        res = self.ag_data.getEnvironmentalSamples(i)
        exp = [{'status': None, 'sample_time': datetime.time(21, 45),
                'notes': 'REMOVED', 'barcode': '000028433',
                'sample_date': datetime.date(2015, 6, 7),
                'site_sampled': None}]
        self.assertItemsEqual(res, exp)

    def test_getEnvironmentalSamplesNotPresent(self):
        i = '00000000-0000-0000-0000-000000000000'
        res = self.ag_data.getEnvironmentalSamples(i)
        self.assertEqual(res, [])

    def test_getAvailableBarcodes(self):
        i = "d8592c74-9694-2135-e040-8a80115d6401"
        res = self.ag_data.getAvailableBarcodes(i)
        exp = ['000005628', '000005627', '000005624',
               '000005625', '000005626', '000004217']
        self.assertItemsEqual(res, exp)

        i = "d6b0f287-b9d9-40d4-82fd-a8fd3db6c476"
        res = self.ag_data.getAvailableBarcodes(i)
        exp = ['000028434']
        self.assertItemsEqual(res, exp)

    def test_getAvailableBarcodesNotPresent(self):
        i = '00000000-0000-0000-0000-000000000000'
        res = self.ag_data.getAvailableBarcodes(i)
        self.assertEqual(res, [])

    def test_verifyKit(self):
        # Test verifying works
        kit = self.ag_data._get_unverified_kits()[0]
        self.ag_data.verifyKit(kit)
        obs = self.ag_data.getAGKitDetails(kit)
        self.assertEqual(obs['kit_verified'], 'y')

        # Test verifying a non-existant kit
        with self.assertRaises(ValueError):
            self.ag_data.getAGKitDetails('NOTAREALKITID')

    def test__get_unverified_kits(self):
        obs = self.ag_data._get_unverified_kits()
        self.assertTrue(isinstance(obs, list))
        self.assertTrue(len(obs) > 0)

        for kit_id in obs:
            self.assertRegexpMatches(kit_id, 'tst_[a-zA-Z]{5}')
            obs = self.ag_data.getAGKitDetails(kit_id)
            self.assertEqual(obs['kit_verified'], 'n')

    def test_handoutCheck(self):
        # Test proper password for handout
        # All tests use assertEqual to make sure bool object returned
        kit = self.ag_data.get_all_handout_kits()[0]
        obs = self.ag_data.handoutCheck(kit, 'test')
        self.assertEqual(obs, True)

        # Test wrong password
        obs = self.ag_data.handoutCheck(kit, 'badPass')
        self.assertEqual(obs, False)

        # Test non-handout kit
        obs = self.ag_data.handoutCheck('tst_ODmhG', 'test')
        self.assertEqual(obs, False)
        obs = self.ag_data.handoutCheck('randomKitID', 'test')
        self.assertEqual(obs, False)

    def test_check_access(self):
        # Has access
        obs = self.ag_data.check_access('tst_BudVu', '000001047')
        self.assertEqual(obs, True)

        # No access
        obs = self.ag_data.check_access('tst_BudVu', '000001111')
        self.assertEqual(obs, False)

    def test_ag_set_pass_change_code(self):
        # Generate new random code and assign it
        testcode = ''.join(choice(ascii_letters) for i in range(10))
        self.ag_data.ag_set_pass_change_code('REMOVED', 'tst_ULGcr', testcode)

        # Actually test the code change
        obs = self.ag_data.ag_verify_kit_password_change_code(
            'REMOVED', 'tst_ULGcr', 'SOMELONGTHINGTHATWILLFAIL')
        self.assertEqual(obs, False)
        obs = self.ag_data.ag_verify_kit_password_change_code(
            'REMOVED', 'tst_ULGcr', testcode)
        # Using equal to make sure boolean True is returned, not something that
        # equates to True
        self.assertEqual(obs, True)

        # Test giving nonsense email
        # TODO: make this raise error and test
        self.ag_data.ag_set_pass_change_code('*****@*****.**',
                                             'tst_ULGcr', testcode)

        # Test giving bad skid
        # TODO: make this raise error and test
        self.ag_data.ag_set_pass_change_code('REMOVED', 'NOTINTHEDB', testcode)

    def test_ag_update_kit_password(self):
        # Generate new pass and make sure is different from current pass
        newpass = ''.join(choice(ascii_letters) for i in range(randint(8, 15)))
        auth = self.ag_data.authenticateWebAppUser('tst_ULGcr', newpass)
        self.assertFalse(
            auth, msg="Randomly generated password matches existing")

        # Actually test password change
        self.ag_data.ag_update_kit_password('tst_ULGcr', newpass)
        auth = self.ag_data.authenticateWebAppUser('tst_ULGcr', newpass)
        self.assertTrue(isinstance(auth, dict))
        self.assertEqual(auth['ag_login_id'],
                         'd8592c74-8416-2135-e040-8a80115d6401')

        # Test giving bad skid
        # TODO: make this raise error and test
        self.ag_data.ag_update_kit_password('NOTINTHEDB', newpass)

    def test_ag_verify_kit_password_change_code(self):
        # Test actual functionality
        obs = self.ag_data.ag_verify_kit_password_change_code(
            'REMOVED', 'tst_omubN', 'FAIL')
        # Using assertEqual to make sure boolean False is returned, not
        # something that equates to False. Same for rest of assertEquals below
        self.assertEqual(obs, False)
        # Outside reset time, should fail
        obs = self.ag_data.ag_verify_kit_password_change_code(
            'REMOVED', 'tst_omubN', 'Mw1eY4wWVXpE0cQlvQwS')
        self.assertEqual(obs, False)

        # Reset code and make sure it works
        testcode = ''.join(choice(ascii_letters) for i in range(10))
        self.ag_data.ag_set_pass_change_code('REMOVED', 'tst_ULGcr', testcode)
        obs = self.ag_data.ag_verify_kit_password_change_code(
            'REMOVED', 'tst_ULGcr', testcode)
        self.assertEqual(obs, True)

        # Test with incorrect kit id
        obs = self.ag_data.ag_verify_kit_password_change_code(
            'REMOVED', 'NOTAREALKITID', 'FAIL')
        self.assertEqual(obs, False)

        # Test with incorrect email
        obs = self.ag_data.ag_verify_kit_password_change_code(
            '*****@*****.**', 'tst_ULGcr', testcode)
        self.assertEqual(obs, False)

    def test_getBarcodesByKit(self):
        res = self.ag_data.getBarcodesByKit('tst_qmhLX')
        exp = ['000001322']
        self.assertItemsEqual(res, exp)

    def test_getBarcodesByKitNotPresent(self):
        res = self.ag_data.getBarcodesByKit('42')
        self.assertEqual(res, [])

    def test_checkPrintResults(self):
        obs = self.ag_data.checkPrintResults('tst_oasoR')
        self.assertFalse(obs)

        obs = self.ag_data.checkPrintResults('tst_TMYwD')
        self.assertTrue(obs)

    def test_checkPrintResults_invalid_ids(self):
        obs = self.ag_data.checkPrintResults('xxx00112333123---123222')
        self.assertFalse(obs)

        obs = self.ag_data.checkPrintResults(':Lfoo:Lbar:Lbaz:Ospam:Leggs')
        self.assertFalse(obs)

    def test_get_user_for_kit(self):
        obs = self.ag_data.get_user_for_kit('tst_IueFX')
        self.assertEqual('ded5101d-c8e3-f6b3-e040-8a80115d6f03', obs)

        obs = self.ag_data.get_user_for_kit('tst_esABz')
        self.assertEqual('d8592c74-8421-2135-e040-8a80115d6401', obs)

    def test_get_user_for_kit_errors(self):
        with self.assertRaises(ValueError):
            self.ag_data.get_user_for_kit('the_fooster')

        with self.assertRaises(ValueError):
            self.ag_data.get_user_for_kit('tst_esXXX')

    def test_get_menu_items(self):
        obs = self.ag_data.get_menu_items('tst_pDWcB')
        self.assertEqual(({}, {}, [], True), obs)

        obs = self.ag_data.get_menu_items('tst_VpQsT')
        self.assertEqual(({'REMOVED-0': []}, {}, [], True), obs)

    def test_get_menu_items_errors(self):
        with self.assertRaises(ValueError):
            self.ag_data.get_menu_items('tst_esXXX')

    def test_check_if_consent_exists(self):
        obs = self.ag_data.check_if_consent_exists(
            '00711b0a-67d6-0fed-e050-8a800c5d7570', 'REMOVED-42')
        self.assertTrue(obs)

    def test_check_if_consent_exists_non_existent_user(self):
        obs = self.ag_data.check_if_consent_exists(
            '00711b0a-67d6-0fed-e050-8a800c5d7570', 'REMOVED-111')
        self.assertFalse(obs)

    def test_get_user_info(self):
        obs = self.ag_data.get_user_info('tst_wAhSB')
        exp = {'address': 'REMOVED', 'ag_login_id':
               'd8592c74-84a5-2135-e040-8a80115d6401', 'city': 'REMOVED',
               'country': 'REMOVED', 'email': 'REMOVED', 'name': 'REMOVED',
               'state': 'REMOVED', 'zip': 'REMOVED'}
        self.assertEqual(exp, obs)

    def test_get_user_info_non_existent(self):
        with self.assertRaises(ValueError):
            self.ag_data.get_user_info('tst_XX1123')

    def test_get_barcode_results(self):
        obs = self.ag_data.get_barcode_results('tst_yCzro')
        exp = [{'barcode': '000016704', 'participant_name': 'REMOVED-0'},
               {'barcode': '000016705', 'participant_name': 'REMOVED-0'},
               {'barcode': '000016706', 'participant_name': 'REMOVED-0'},
               {'barcode': '000016707', 'participant_name': 'REMOVED-0'},
               {'barcode': '000016708', 'participant_name': 'REMOVED-0'},
               {'barcode': '000016709', 'participant_name': 'REMOVED-0'},
               {'barcode': '000016710', 'participant_name': 'REMOVED-0'},
               {'barcode': '000016711', 'participant_name': 'REMOVED-0'},
               {'barcode': '000016712', 'participant_name': 'REMOVED-0'},
               {'barcode': '000016713', 'participant_name': 'REMOVED-0'},
               {'barcode': '000004213', 'participant_name': 'REMOVED-0'},
               {'barcode': '000004214', 'participant_name': 'REMOVED-0'},
               {'barcode': '000004215', 'participant_name': 'REMOVED-0'},
               {'barcode': '000004216', 'participant_name': 'REMOVED-0'},
               {'barcode': '000004218', 'participant_name': 'REMOVED-0'},
               {'barcode': '000004219', 'participant_name': 'REMOVED-0'}]
        self.assertItemsEqual(obs, exp)

    def test_get_barcode_results_non_existant_id(self):
        with self.assertRaises(ValueError):
            self.ag_data.get_barcode_results("something that doesn't exist")

    def test_get_login_info(self):
        id_ = 'fecebeae-4244-2d78-e040-8a800c5d4f50'
        exp = [{'ag_login_id': id_,
                'email': 'REMOVED',
                'name': 'REMOVED',
                'address': 'REMOVED',
                'city': 'REMOVED',
                'state': 'REMOVED',
                'zip': 'REMOVED',
                'country': 'REMOVED'}]
        obs = self.ag_data.get_login_info(id_)
        self.assertEqual(obs, exp)

    def test_get_login_info_non_existant_id(self):
        id_ = '00000000-0000-0000-0000-000000000000'
        with self.assertRaises(ValueError):
            self.ag_data.get_login_info(id_)

    def test_get_survey_id(self):
        id_ = '8ca47059-000a-469f-aa64-ff1afbd6fcb1'
        obs = self.ag_data.get_survey_id(id_, 'REMOVED-0')
        self.assertEquals(obs, 'd08758a1510256f0')

    def test_get_survey_id_non_existant_id(self):
        id_ = '00000000-0000-0000-0000-000000000000'
        with self.assertRaises(ValueError):
            self.ag_data.get_survey_id(id_, 'REMOVED')

    def test_get_countries(self):
        obs = self.ag_data.get_countries()
        # Make sure is a list with proper length
        self.assertTrue(isinstance(obs, list))
        self.assertEqual(len(obs), 244)

        # Spot check a few countries
        self.assertIn('United States', obs)
        self.assertIn('United Kingdom', obs)

    def test_is_deposited_ebi(self):
        obs = self.ag_data.is_deposited_ebi('000027262')
        self.assertFalse(obs)

    def test_is_deposited_ebi_bad_barcode(self):
        with self.assertRaises(ValueError):
            self.ag_data.is_deposited_ebi('NOTABARCODE')
class TestAGDataAccess(TestCase):
    def setUp(self):
        self.con = psycopg2.connect(user=AMGUT_CONFIG.user,
                                    password=AMGUT_CONFIG.password,
                                    database=AMGUT_CONFIG.database,
                                    host=AMGUT_CONFIG.host,
                                    port=AMGUT_CONFIG.port)
        self.data_access = AGDataAccess(self.con)
        self.data_access.ag_update_kit_password('test',
                                                AMGUT_CONFIG.badpassword)
        self.con.commit()

    def tearDown(self):
        self.data_access.ag_update_kit_password('test',
                                                AMGUT_CONFIG.goodpassword)
        self.con.close()

    def test_authenticateWebAppUser(self):
        self.assertFalse(self.data_access.authenticateWebAppUser('bad',
                                                                 'wrong'))
        data = self.data_access.authenticateWebAppUser(
            'test', AMGUT_CONFIG.badpassword)
        self.assertEqual(data['email'], '*****@*****.**')

    def test_addAGLogin(self):
        self.data_access.addAGLogin('*****@*****.**', 'test', 'test',
                                    'test', 'CO', '80303', 'USA')
        cur = self.con.cursor()
        cur.execute(
            'select * from ag_login where email = %s', ('*****@*****.**',))
        rec = cur.fetchall()
        self.assertEqual(len(rec), 1)
        cur.execute('delete from ag_login where email = %s',
                    ('*****@*****.**',))
        self.con.commit()
        cur.execute(
            'select * from ag_login where email = %s', ('*****@*****.**',))
        rec = cur.fetchall()
        self.assertEqual(len(rec), 0)

    def test_updateAGLogin(self):
        self.data_access.updateAGLogin('d8592c74-7da1-2135-e040-8a80115d6401',
                                       '*****@*****.**', '', 'add',
                                       'city', 'state', 'zip', 'USA')
        cur = self.con.cursor()
        cur.execute('select * from ag_login where ag_login_id = %s',
                    ('d8592c74-7da1-2135-e040-8a80115d6401',))
        rec = cur.fetchone()
        self.assertEqual(rec[1], '*****@*****.**')
        self.data_access.updateAGLogin('d8592c74-7da1-2135-e040-8a80115d6401',
                                       '*****@*****.**', 'Test', 'Test',
                                       'Boulder', 'CO', '80303',
                                       'United States')
        cur.execute('select * from ag_login where ag_login_id = %s',
                    ('d8592c74-7da1-2135-e040-8a80115d6401',))
        rec = cur.fetchone()
        self.assertEqual(rec[1], '*****@*****.**')

    def test_getAGKitsByLogin(self):
        data = self.data_access.getAGKitsByLogin()
        self.assertTrue({'email': '*****@*****.**',
                         'supplied_kit_id': 'test',
                         'ag_kit_id': 'd8592c74-7da2-2135-e040-8a80115d6401'}
                        in data)

    def test_getAGBarcodeDetails(self):
        data = self.data_access.getAGBarcodeDetails('000000001')
        self.assertEqual(data['participant_name'], 'foo')
        self.assertEqual(data['site_sampled'], 'Stool')
        self.assertEqual(data['status'], 'Received')

    def test_getAGKitDetails(self):
        data = self.data_access.getAGKitDetails('test')
        self.assertEqual(data['kit_verification_code'], 'test')

    def test_getNextAGBarcode(self):
        barcode, barcode_text = self.data_access.getNextAGBarcode()
        data = self.data_access.getAGBarcodes()
        self.assertTrue(barcode_text not in data)

    def test_updateAGKit(self):
        self.data_access.updateAGKit('d8592c74-7da2-2135-e040-8a80115d6401',
                                     'test22', 'newpass', 24, 'ver')
        cur = self.con.cursor()
        cur.execute('select * from ag_kit where ag_kit_id = %s',
                    ('d8592c74-7da2-2135-e040-8a80115d6401',))
        rec = cur.fetchone()
        self.assertEqual(rec[2], 'test22')
        self.data_access.updateAGKit('d8592c74-7da2-2135-e040-8a80115d6401',
                                     'test', 'oldpass', 1, 'test')
        cur.execute('select * from ag_kit where ag_kit_id = %s',
                    ('d8592c74-7da2-2135-e040-8a80115d6401',))
        rec = cur.fetchone()
        self.assertEqual(rec[2], 'test')

    def test_updateAGBarcode(self):
        self.data_access.updateAGBarcode(
            '000010860', 'd8592c74-7da2-2135-e040-8a80115d6401', 'Stool', '',
            '07/30/2014', '9:30 AM', 'test', 'notes', 'n', 'n')
        cur = self.con.cursor()
        cur.execute('select * from ag_kit_barcodes where barcode = %s',
                    ('000010860',))
        rec = cur.fetchone()
        self.assertEqual(rec[6], 'Stool')
        self.assertEqual(rec[7], '07/30/2014')
        self.data_access.updateAGBarcode(
            '000010860', 'd8592c74-7da2-2135-e040-8a80115d6401', '', '', '',
            '', '', '', '', '')
        cur.execute('select * from ag_kit_barcodes where barcode = %s',
                    ('000010860',))
        rec = cur.fetchone()
        self.assertEqual(rec[6], '')
        self.assertEqual(rec[7], '')

    def test_registerHandoutKit(self):
        ag_login_id = 'd8592c74-7da1-2135-e040-8a80115d6401'
        self.data_access.registerHandoutKit(
            ag_login_id, 'test_ha')
        cur = self.con.cursor()
        # make sure handout kit removed
        cur.execute("SELECT * FROM ag_handout_kits")
        obs = cur.fetchall()
        self.assertEqual(obs, [])

        # make sure handout kit registered as regular kit
        cur.execute("SELECT * FROM ag_kit WHERE supplied_kit_id = 'test_ha'",
                    [ag_login_id])
        obs = cur.fetchall()
        exp = [('a70a398c-a29e-4367-8ae2-f291d5217b29',
                'd8592c74-7da1-2135-e040-8a80115d6401',
                'test_ha', '1234', 3, '5678', 'n', 'n', None, None, 'n', None)]
        self.assertEqual(obs, exp)
        kit_id = obs[0][0]

        # make sure barcodes registered
        cur.execute("SELECT * FROM barcode JOIN ag_kit_barcodes USING "
                    "(barcode) WHERE ag_kit_id = %s",
                    [kit_id])
        obs = cur.fetchall()
        exp = [
            ('000000004', datetime(2015, 4, 29, 9, 25, 51, 842222), None, None,
             None, None, None, None, 'f3033ee2-391c-4f24-b0eb-f9ed1d26444a',
             'a70a398c-a29e-4367-8ae2-f291d5217b29', None, '000000004.jpg',
             None, None, None, None, None, None, None, None, None, None, None,
             None, None, None, None),
            ('000000003', datetime(2015, 4, 29, 9, 25, 51, 842222), None, None,
             None, None, None, None, '8e3b037e-fc79-4523-9816-dc7e9d250ebb',
             'a70a398c-a29e-4367-8ae2-f291d5217b29', None, '000000003.jpg',
             None, None, None, None, None, None, None, None, None, None, None,
             None, None, None, None),
            ('000000002', datetime(2015, 4, 29, 9, 25, 51, 842222), None, None,
             None, None, None, None, 'f98bf005-d308-4994-a1fd-9826ab3dbb9d',
             'a70a398c-a29e-4367-8ae2-f291d5217b29', None, '000000002.jpg',
             None, None, None, None, None, None, None, None, None, None, None,
             None, None, None, None)]
        self.assertItemsEqual(obs, exp)

    def test_deleteAGParticipantSurvey(self):
        cur = self.con.cursor()
        cur.execute('insert into ag_consent (ag_login_id, '
                    'participant_name, participant_email) values (%s, %s, %s)',
                    ('d8592c747da12135e0408a80115d6401', 'sp_test',
                        '*****@*****.**'))
        self.con.commit()
        cur.execute('insert into ag_login_surveys (ag_login_id, survey_id,'
                    ' participant_name) values (%s, %s, %s)',
                    ('d8592c74-7da1-2135-e040-8a80115d6401', '1235',
                        'sp_test'))
        self.con.commit()
        self.data_access.deleteAGParticipantSurvey(
            'd8592c747da12135e0408a80115d6401', 'sp_test')
        data = self.data_access.getHumanParticipants(
            'd8592c747da12135e0408a80115d6401')
        self.assertEqual(len(data), 1)

    def test_logParticipantSample(self):
        self.data_access.logParticipantSample(
            'd8592c74-7da1-2135-e040-8a80115d6401', '000010860', 'Stool', '',
            '07/29/2014', '09:30 AM', 'foo', 'no notes')
        data = self.data_access.getAGBarcodeDetails('000010860')
        self.assertEqual(data['participant_name'], 'foo')
        self.assertEqual(data['site_sampled'], 'Stool')
        self.assertEqual(data['sample_date'], '07/29/2014')
        self.data_access.deleteSample('000010860',
                                      'd8592c747da12135e0408a80115d6401')
        data = self.data_access.getAGBarcodeDetails('000010860')
        self.assertEqual(data['participant_name'], None)

    def test_deleteSample(self):
        cur = self.con.cursor()
        cur.execute('update ag_kit_barcodes set site_sampled = %s,'
                    'sample_date = %s, participant_name = %s, '
                    'sample_time = %s where barcode = %s', ('Stool',
                                                            '07/30/2014',
                                                            'test', '9:30 AM',
                                                            '000010860'))
        self.con.commit()
        data = self.data_access.getAGBarcodeDetails('000010860')
        self.assertEqual(data['site_sampled'], 'Stool')
        self.data_access.deleteSample('000010860',
                                      'd8592c747da12135e0408a80115d6401')
        data = self.data_access.getAGBarcodeDetails('000010860')
        self.assertEqual(data['participant_name'], None)

    def test_getHumanParticipants(self):
        data = self.data_access.getHumanParticipants(
            'd8592c74-7da1-2135-e040-8a80115d6401')
        self.assertEqual(set(data), {'foo'})

    def test_getAnimalParticipants(self):
        data = self.data_access.getAnimalParticipants(
            'd8592c74-7da1-2135-e040-8a80115d6401')
        # this test needs updated when the test database is updated
        self.assertEqual(len(data), 0)

    def test_getParticipantSamples(self):
        data = self.data_access.getParticipantSamples(
            'd8592c74-7da1-2135-e040-8a80115d6401', 'foo')
        self.assertEqual(data[0]['status'], 'Received')
        self.assertEqual(data[0]['barcode'],  '000000001')

    def test_getEnvironmentalSamples(self):
        data = self.data_access.getEnvironmentalSamples(
            'd8592c74-7da1-2135-e040-8a80115d6401')
        barcodes = {x['barcode'] for x in data}
        # TODO: This is broken -- there are no environmental samples associated
        # with the test user. We need to set something up like we have in qiita
        # where a test DB is set up and torn down for each individual test
        self.assertEqual(barcodes, set())

    def test_getAvailableBarcodes(self):
        data = self.data_access.getAvailableBarcodes(
            'd8592c74-7da1-2135-e040-8a80115d6401')
        self.assertEqual(len(data), 1)

    def test_verifyKit(self):
        cur = self.con.cursor()
        cur.execute(' update ag_kit set kit_verified = %s where '
                    'supplied_kit_id = %s', ('n', 'test',))
        self.con.commit()
        self.data_access.verifyKit('test')
        cur.execute('select kit_verified from ag_kit where supplied_kit_id = '
                    '%s', ('test',))
        rec = cur.fetchone()
        self.assertEqual(rec[0], 'y')

    def test_getMapMarkers(self):
        data = self.data_access.getMapMarkers()
        self.assertNotEqual(len(data), 0)

    def test_handoutCheck(self):
        is_handout = self.data_access.handoutCheck('test', 'wrongpass')
        self.assertFalse(is_handout)

    def test_getAGStats(self):
        data = self.data_access.getAGStats()
        self.assertEqual(len(data), 23)

    def test_updateAKB(self):
        self.data_access.updateAKB('000010860', 'n', 'n', 'y',
                                   'some other text', '07/30/2014')
        cur = self.con.cursor()
        cur.execute('select * from ag_kit_barcodes where barcode = %s',
                    ('000010860',))
        rec = cur.fetchone()
        self.assertEqual([rec[12], rec[13], rec[14]], ['n', 'n', 'y'])
        self.data_access.updateAKB('000010860', None, None, None, None, None)
        cur.execute('select * from ag_kit_barcodes where barcode = %s',
                    ('000010860',))
        rec = cur.fetchone()
        self.assertEqual([rec[12], rec[13], rec[14]], [None, None, None])

    def test_getAGKitIDsByEmail(self):
        data = self.data_access.getAGKitIDsByEmail('*****@*****.**')
        self.assertEqual(set(data), {'test', '1111'})

    def test_ag_set_pass_change_code(self):
        self.data_access.ag_set_pass_change_code('*****@*****.**', 'test',
                                                 '123456789')
        cur = self.con.cursor()
        cur.execute('select pass_reset_code from ag_kit where '
                    'supplied_kit_id = %s', ('test',))
        rec = cur.fetchone()
        self.assertEqual(rec[0], '123456789')
        cur.execute('update ag_kit set pass_reset_code = %s, '
                    'pass_reset_time = %s where supplied_kit_id = %s',
                    ('', None, 'test',))
        self.con.commit()

    def test_ag_update_kit_password(self):
        self.data_access.ag_update_kit_password('test', 'newpass')
        cur = self.con.cursor()
        cur.execute('select kit_password from ag_kit where supplied_kit_id = '
                    '%s', ('test',))
        rec = cur.fetchone()
        self.assertTrue(bcrypt.verify('newpass', rec[0]))

    def test_ag_verify_kit_password_change_code(self):
        self.data_access.ag_set_pass_change_code('*****@*****.**', 'test',
                                                 '123456789')
        result = self.data_access.ag_verify_kit_password_change_code(
            '*****@*****.**', 'test', '123456789')
        self.assertEqual(result, 1)
        cur = self.con.cursor()
        cur.execute('update ag_kit set pass_reset_code = %s, '
                    'pass_reset_time = %s where supplied_kit_id = %s',
                    ('', None, 'test',))
        self.con.commit()

    def test_getBarcodesByKit(self):
        observed = self.data_access.getBarcodesByKit('test')
        expected = {'000010860', '000006616', '000000001'}

        self.assertEqual(set(observed), expected)

    def test_checkPrintResults(self):
        data = self.data_access.checkPrintResults('test')
        self.assertTrue(data is None)

    def test_get_user_for_kit(self):
        data = self.data_access.get_user_for_kit('test')
        self.assertEqual(data, 'd8592c74-7da1-2135-e040-8a80115d6401')

    def test_menu_items(self):
        data = self.data_access.get_menu_items('test')
        self.assertEqual(data[0]['foo'][0]['barcode'], '000000001')

    def test_get_user_info(self):
        data = self.data_access.get_user_info('test')
        self.assertEqual(data['email'], '*****@*****.**')

    def test_get_barcode_results(self):
        data = self.data_access.get_barcode_results('test')
        self.assertEqual(len(data), 1)
        data = self.data_access.get_barcode_results('1111')
        self.assertEqual(len(data), 0)