1. from unittest import skipIf
    
  2. 
    
  3. from django.test import SimpleTestCase
    
  4. 
    
  5. from ..utils import setup
    
  6. 
    
  7. try:
    
  8.     import numpy
    
  9. except ImportError:
    
  10.     numpy = False
    
  11. 
    
  12. 
    
  13. @skipIf(numpy is False, "Numpy must be installed to run these tests.")
    
  14. class NumpyTests(SimpleTestCase):
    
  15.     @setup({"numpy-array-index01": "{{ var.1 }}"})
    
  16.     def test_numpy_array_index01(self):
    
  17.         """
    
  18.         Numpy's array-index syntax allows a template to access a certain
    
  19.         item of a subscriptable object.
    
  20.         """
    
  21.         output = self.engine.render_to_string(
    
  22.             "numpy-array-index01",
    
  23.             {"var": numpy.array(["first item", "second item"])},
    
  24.         )
    
  25.         self.assertEqual(output, "second item")
    
  26. 
    
  27.     @setup({"numpy-array-index02": "{{ var.5 }}"})
    
  28.     def test_numpy_array_index02(self):
    
  29.         """
    
  30.         Fail silently when the array index is out of range.
    
  31.         """
    
  32.         output = self.engine.render_to_string(
    
  33.             "numpy-array-index02",
    
  34.             {"var": numpy.array(["first item", "second item"])},
    
  35.         )
    
  36.         if self.engine.string_if_invalid:
    
  37.             self.assertEqual(output, "INVALID")
    
  38.         else:
    
  39.             self.assertEqual(output, "")