def test_post(self):
        self.mock_login_admin()
        response = self.post('/ag_pulldown/download/',
                             {'barcodes': ['000001448', '000001447',
                                           '100001449', '000001445',
                                           '000015296', '000015297',
                                           '000015298', '000015299',
                                           '000016180', '000016280',
                                           '000016281', '000016283',
                                           '000016284'],
                              'blanks': ['BLANK000001449', 'BLANK000001453',
                                         'BLANK100001453'],
                              'external': db.list_external_surveys()[:1],
                              'selected_ag_surveys': [-1, -2, -3, -4, -5],
                              'merged': 'True'})
        self.assertEqual(response.headers['Content-Disposition'],
                         'attachment; filename=metadata.zip')
        self.assertIn('failures.txt', response.body)
        self.assertIn('survey_Personal_Information_md.txt', response.body)

        # store the resulting zip archive to disc ...
        tmpfile = NamedTemporaryFile(mode='w', delete=False,
                                     prefix='metadata_pulldown_single_',
                                     suffix='.zip')
        tmpfile.write(response.body)
        tmpfile.close()
        # ... and read the content as dict of strings
        result = extract_zip(tmpfile.name)
        os.remove(tmpfile.name)

        # read in the true content from data dir for comparison
        truth = extract_zip(join(dirname(realpath(__file__)), 'data',
                                 'results_barcodes.zip'))
        self.maxDiff = None
        self.assertEqual(sneak_files(result), sneak_files(truth))
    def test_post_multiple_surverys(self):
        self.mock_login_admin()
        response = self.post('/ag_pulldown/download/',
                             {'barcodes': ['000001000', '000001001',
                                           '000001002', '000037583',
                                           '000066526', '000031568',
                                           '000037555', '000065893',
                                           '000067690', '000049932',
                                           '000063380', '000063381',
                                           '000006616', '000030821',
                                           '000030822', '000030823',
                                           '000069020', '000069021'
                                           '000069022'],
                              'blanks': [],
                              'external': [],
                              'selected_ag_surveys': [-1, -2, -3, -4, -5],
                              'merged': 'True'})
        self.assertEqual(response.headers['Content-Disposition'],
                         'attachment; filename=metadata.zip')

        # store the resulting zip archive to disc ...
        tmpfile = NamedTemporaryFile(mode='w', delete=False,
                                     prefix='metadata_pulldown_multiple_',
                                     suffix='.zip')
        tmpfile.write(response.body)
        tmpfile.close()
        # ... and read the content as dict of strings
        result = extract_zip(tmpfile.name)
        os.remove(tmpfile.name)

        # read in the true content from data dir for comparison
        truth = extract_zip(join(dirname(realpath(__file__)), 'data',
                                 'results_multiplesurvey_barcodes.zip'))

        self.assertEqual(sneak_files(result), sneak_files(truth))
    def test_post_select_surveys_htmlencoding(self):
        # the interface html page provides arguments as one concatenated string
        # which is not a list of strings. Here, I test if this other behaviour
        # also works.
        self.mock_login_admin()

        response = self.post('/ag_pulldown/download/',
                             {'barcodes': ('000037555,000065893,000067690,'
                                           '000037583,000066526,000031568'),
                              'blanks': '',
                              'external': '',
                              'selected_ag_surveys': '-2,-3,-8',
                              'merged': 'False'})
        # store the resulting zip archive to disc ...
        tmpfile = NamedTemporaryFile(mode='w', delete=False,
                                     prefix='metadata_pulldown_multiple_sel_',
                                     suffix='.zip')
        tmpfile.write(response.body)
        tmpfile.close()
        # ... and read the content as dict of strings
        result = extract_zip(tmpfile.name)
        self.assertItemsEqual(result.keys(),
                              ['failures.txt',
                               'survey_Fermented_Foods_md.txt',
                               'survey_Pet_Information_md.txt'])
        os.remove(tmpfile.name)

        response = self.post('/ag_pulldown/download/',
                             {'barcodes': ('000037555,000065893,000067690,'
                                           '000037583,000066526,000031568'),
                              'blanks': '',
                              'external': '',
                              'selected_ag_surveys': '-3',
                              'merged': 'False'})
        # store the resulting zip archive to disc ...
        tmpfile = NamedTemporaryFile(mode='w', delete=False,
                                     prefix='metadata_pulldown_multiple_sel_',
                                     suffix='.zip')
        tmpfile.write(response.body)
        tmpfile.close()
        # ... and read the content as dict of strings
        result = extract_zip(tmpfile.name)
        self.assertItemsEqual(result.keys(),
                              ['failures.txt',
                               'survey_Fermented_Foods_md.txt'])
        os.remove(tmpfile.name)
Esempio n. 4
0
 def test_sneak_files(self):
     fp_zip = join(dirname(realpath(__file__)), '..', '..', 'tests', 'data',
                   'results_multiplesurvey_barcodes.zip')
     exp = [{'survey_Personal_Microbiome_md.txt': 'sample_name\tPM_AGE\tP'},
            {'survey_Pet_Information_md.txt': 'sample_name\tALTITUDE'},
            {'failures.txt': 'The following barcod'},
            {'survey_Fermented_Foods_md.txt': 'sample_name\tFERMENTE'},
            {'survey_Surfers_md.txt': 'sample_name\tSURF_BOA'},
            {'survey_Personal_Information_md.txt': 'sample_name\tACNE_MED'},
            {'surveys_merged_md.txt': 'sample_name\tACNE_MED'}]
     obs = sneak_files(extract_zip(fp_zip), 20)
     self.assertEqual(exp, obs)
Esempio n. 5
0
 def test_extract_zip(self):
     fp_zip = join(dirname(realpath(__file__)), '..', '..', 'tests', 'data',
                   'results_multiplesurvey_barcodes.zip')
     obs = extract_zip(fp_zip)
     exp_filenames = ['failures.txt', 'survey_Fermented_Foods_md.txt',
                      'survey_Personal_Information_md.txt',
                      'survey_Personal_Microbiome_md.txt',
                      'survey_Pet_Information_md.txt',
                      'surveys_merged_md.txt', 'survey_Surfers_md.txt']
     # check filenames
     self.assertEqual(sorted(obs.keys()), sorted(exp_filenames))
     # check file contents very briefly
     self.assertIn('SURF_BOARD_TYPE', obs['survey_Surfers_md.txt'])
    def test_post_select_surveys(self):
        self.mock_login_admin()
        response = self.post('/ag_pulldown/download/',
                             {'barcodes': ['000037555', '000065893',
                                           '000067690', '000037583',
                                           '000066526', '000031568'],
                              'blanks': [],
                              'external': [],
                              'selected_ag_surveys': [-2, -3, -8],
                              'merged': 'False'})
        # store the resulting zip archive to disc ...
        tmpfile = NamedTemporaryFile(mode='w', delete=False,
                                     prefix='metadata_pulldown_multiple_sel_',
                                     suffix='.zip')
        tmpfile.write(response.body)
        tmpfile.close()
        # ... and read the content as dict of strings
        result = extract_zip(tmpfile.name)
        self.assertItemsEqual(result.keys(),
                              ['failures.txt',
                               'survey_Fermented_Foods_md.txt',
                               'survey_Pet_Information_md.txt'])
        os.remove(tmpfile.name)

        # no blanks
        response = self.post('/ag_pulldown/download/',
                             {'barcodes': ('000001445', '000001446',
                                           '000001447', '000001448',
                                           '100001449', '000016180',
                                           '000015296', '000015297',
                                           '000015298', '000015299',
                                           '000015300', '000016280',
                                           '000016281', '000016282',
                                           '000016283', '000016284'),
                              'blanks': [],
                              'external': db.list_external_surveys()[:1]})
        self.assertEqual(response.code, 200)
        self.assertEqual(response.headers['Content-Disposition'],
                         'attachment; filename=metadata.zip')

        # no externals
        response = self.post('/ag_pulldown/download/',
                             {'barcodes': ['000001445',
                                           '000001446',
                                           '000001447',
                                           '000001448',
                                           '100001449',
                                           '000016180',
                                           '000015296',
                                           '000015297',
                                           '000015298',
                                           '000015299',
                                           '000015300',
                                           '000016280',
                                           '000016281',
                                           '000016282',
                                           '000016283',
                                           '000016284'],
                              'blanks': ['BLANK000001449',
                                         'BLANK000001453',
                                         'BLANK100001453'],
                              'external': []})
        self.assertEqual(response.code, 200)
        self.assertEqual(response.headers['Content-Disposition'],
                         'attachment; filename=metadata.zip')