text: import unittest
  
  from portals.spreadsheets.common import permitted_columns
  from portals.spreadsheets.common import SheetColumn
  
  
  class TestSheetColumns(unittest.TestCase):
      def test_fields(self):
          """Initiialization values should be stored in fields"""
          name = "test"
          mandatory = True
          comment = "comment"
          authorization = "authorization"
          column = SheetColumn(name, mandatory, comment, authorization)
          self.assertEqual(column.name, name)
          self.assertEqual(column.mandatory, mandatory)
          self.assertEqual(column.comment, comment)
          self.assertEqual(column.authorization, authorization)
  
      def test_no_default_name(self):
          """An SheetColumn expects at least one value: name"""
          self.assertRaises(TypeError, SheetColumn)
  
      def test_defaults(self):
          """An SheetColumn allows only one value: name
  
          The other 3 fields should default to "empty"
          """
          column = SheetColumn("test")
          self.assertEqual(column.name, "test")
          self.assertEqual(column.mandatory, False)
          self.assertEqual(column.comment, "")
          self.assertEqual(column.authorization, "")
  
      def test_access_as_dict(self):
          """SheetColumns should allow access to fields as if it were a dict"""
          column = SheetColumn("Header 1", False, "hello world")
          self.assertEqual(column["name"], "Header 1")
          self.assertEqual(column["comment"], "hello world")
          self.assertEqual(column["name"], column.name)
          self.assertEqual(column["mandatory"], column.mandatory)
          self.assertEqual(column["comment"], column.comment)
          self.assertEqual(column["authorization"], column.authorization)
  
      def test_bad_access_as_dict(self):
          """SheetColumns should raise a KeyError for bad name used as dict
  
          If the attribute is missing then do not raise an AttributeError
              because it will look like a key
          """
          column = SheetColumn("Header 1", False, "hello world")
          try:
              column["fred"]
              self.fail("fred is not a field: should raise an error")
          except KeyError:
              pass
          except AttributeError:
              self.fail("Access like a dict should raise KeyError")
  
  
  test_columns = [
      SheetColumn("One", True, "Comment 1", "one"),
      SheetColumn("Two", True, "Comment 2", "two"),
      SheetColumn("Three", False, "Comment 3", "three"),
  ]
  
  
  class TestPermittedColumns(unittest.TestCase):
      def test_permitted_column_names(self):
          """Only columns with authorization in permissions may be permitted"""
          permissions = [
              "one",
              "two",
          ]
          expected = ["One", "Two"]
          actual = [_.name for _ in permitted_columns(test_columns, permissions)]
          self.assertEqual(actual, expected)
  
      def test_empty_permissions_allow_all_columns(self):
          """If permissions is empty, all columns are permitted"""
          permissions = []
          expected = ["One", "Two", "Three"]
          actual = [_.name for _ in permitted_columns(test_columns, permissions)]
          self.assertEqual(actual, expected)
  
      def test_column_without_permission_always_allowed(self):
          """Any column with empty authorization is permitted"""
          columns = test_columns + [SheetColumn("Four")]
          permissions = [
              "one",
              "two",
          ]
          expected = ["One", "Two", "Four"]
          actual = [_.name for _ in permitted_columns(columns, permissions)]
          self.assertEqual(actual, expected)
  