# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-
"""
hpsspy.test.test_data
~~~~~~~~~~~~~~~~~~~~~

Check the associated data files.
"""
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
#
import unittest
import json
from pkg_resources import resource_exists, resource_stream


class TestData(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_sdss(self):
        self.assertTrue(resource_exists('hpsspy', 'data/sdss.json'),
                        "Could not find sdss.json!")
        t = resource_stream('hpsspy', 'data/sdss.json')
        hpss_map = json.load(t)
        for release in ['dr{0:d}'.format(k) for k in range(8, 13)]:
            self.assertIn(release, hpss_map,
                          "Release {0} is not in sdss.json!".format(release))
        t.close()

    def test_desi(self):
        self.assertTrue(resource_exists('hpsspy', 'data/desi.json'),
                        "Could not find desi.json!")
        t = resource_stream('hpsspy', 'data/desi.json')
        hpss_map = json.load(t)
        for release in ('datachallenge', 'imaging', 'mocks', 'release',
                        'spectro', 'target'):
            self.assertIn(release, hpss_map,
                          "Release {0} is not in sdss.json!".format(release))
        t.close()


if __name__ == '__main__':
    unittest.main()

@@ -488,24 +488,146 @@
 def 
-test_sdss(self):
+check_json(self, filename, sections):%0A        %22%22%22Check a generic JSON file.%0A        %22%22%22%0A        res_filename = 'data/%7B0%7D'.format(filename)
 %0A   
@@ -673,32 +673,28 @@
 psspy', 
-'data/sdss.json'
+res_filename
 ),%0A     
@@ -728,27 +728,38 @@
 ot find 
-sdss.json!%22
+%7B0%7D!%22.format(filename)
 )%0A      
@@ -794,124 +794,199 @@
 y', 
-'data/sdss.json')%0A        hpss_map = json.load(t)%0A        for release in %5B'dr%7B0:d%7D'.format(k)
+res_filename)%0A        try:%0A            hpss_map = json.load(t)%0A        except TypeError:%0A            hpss_map = json.loads(t.read().decode('utf8'))%0A        t.close()%0A       
  for 
-k
+s
  in 
-range(8, 13)%5D
+sections
 :%0A  
@@ -1001,39 +1001,33 @@
   self.assertIn(
-release
+s
 , hpss_map,%0A    
@@ -1067,33 +1067,27 @@
 %7D is not in 
-sdss.json
+%7B1%7D
 !%22.format(re
@@ -1088,34 +1088,20 @@
 mat(
-release))%0A        t.close(
+s, filename)
 )%0A%0A 
@@ -1112,20 +1112,20 @@
 ef test_
-desi
+sdss
 (self):%0A
@@ -1136,82 +1136,40 @@
     
-self.assertTrue(resource_exists('hpsspy', 'data/desi.json'),%0A     
+%22%22%22Test SDSS data file.%0A
         
     
@@ -1160,32 +1160,36 @@
 le.%0A        
+%22%22%22%0A
         
    %22Could no
@@ -1180,147 +1180,195 @@
     
-   %22Could not find desi.json!%22)%0A        t = resource_stream('hpsspy', 'data/desi.json')%0A        hpss_map = json.load(t)%0A        for
+releases = %5B'dr%7B0:d%7D'.format(k) for k in range(8, 13)%5D%0A        self.check_json('sdss.json', releases)%0A%0A    def test_desi(self):%0A        %22%22%22Test DESI data file.%0A        %22%22%22%0A       
  release
  in 
@@ -1363,19 +1363,19 @@
  release
- in
+s =
  ('datac
@@ -1436,20 +1436,16 @@
         
-    
 'spectro
@@ -1470,139 +1470,45 @@
     
-    
 self.
-assertIn(release, hpss_map,%0A                          %22Release %7B0%7D is not in sdss.json!%22.format(release))%0A        t.close(
+check_json('desi.json', releases
 )%0A%0A%0A

fix tests for Python 3