Exemplo n.º 1
0
 def test_wrapperTable_setattr_setitem(self):
     cif_wrapper = CIFWrapper(self.raw_dictionary["TEST_BLOCK_2"],
                              "NEW_ID",
                              preserve_token_order=True)
     #        # dot notation access
     cif_wrapper._test_category_1["test_value_2"] = ["a", "b", "c", "d"]
     self.assertEqual(
         cif_wrapper._test_category_1["test_value_2"],
         ["a", "b", "c", "d"],
         "Conventional attribute setter failed or gave inconsistent results",
     )
     cif_wrapper._test_category_1["test_value_2"] = "FOO"
     self.assertEqual(
         cif_wrapper._test_category_1["test_value_2"],
         [
             "FOO",
         ],
         "Attribute setter failed to overwrite or gave inconsistent results",
     )
     cif_wrapper._test_category_1.test_value_3 = "Bundy"
     self.assertEqual(
         cif_wrapper._test_category_1["test_value_3"],
         [
             "Bundy",
         ],
         "Dot-notation attribute setting failed to overwrite or gave inconsistent results",
     )
Exemplo n.º 2
0
 def test_write_ciffile_after_cifwrapper_dictionary_import(self):
     # Test write CifFile initialized by CIFWrapper and dictionary import
     unit_test_file = "io_testcase_2.cif"
     cfw = CifFileWriter(
         file_path=os.path.join(self.FILE_ROOT, unit_test_file))
     cif_obj = dict((k, CIFWrapper(v, preserve_token_order=True))
                    for k, v in list(self.raw_dictionary.items()))
     cif_wrapper = CIFWrapper(
         {"TEST_BLOCK_1": self.raw_dictionary["TEST_BLOCK_1"]},
         preserve_token_order=True,
     )
     cfw.write(cif_wrapper)
     cif_wrapper = CIFWrapper(
         {"TEST_BLOCK_2": self.raw_dictionary["TEST_BLOCK_2"]},
         preserve_token_order=True,
     )
     cfw.write(cif_wrapper)
     del cfw
     cfr = CifFileReader(input="data", preserve_order=True)
     test_file = cfr.read(os.path.join(self.FILE_ROOT, unit_test_file),
                          output="cif_wrapper")
     data_block_ids = list(test_file.keys())
     data_block_ids.sort()
     self.assertEqual(
         data_block_ids,
         ["TEST_BLOCK_1", "TEST_BLOCK_2"],
         "Datablock(s) were not written correctly",
     )
     self.assertEqual(
         test_file["TEST_BLOCK_1"]._test_category_2.test_value_1,
         ["1", "2", "3", "4"],
         "mmCIF data  was not written correctly",
     )
Exemplo n.º 3
0
 def test_listContents(self):
     cif_wrapper = CIFWrapper(self.raw_dictionary["TEST_BLOCK_1"],
                              preserve_token_order=True)
     categories = cif_wrapper.contents()
     categories.sort()
     assert_equal(
         categories,
         ["_test_category_1", "_test_category_2"],
         "Item list for the category is incorrect",
     )
Exemplo n.º 4
0
    def test_wrapperTableContains(self):
        cif_wrapper = CIFWrapper(self.raw_dictionary["TEST_BLOCK_2"],
                                 "NEW_ID",
                                 preserve_token_order=True)
        self.assertIsInstance(
            cif_wrapper,
            CIFWrapper,
            "Failed to instantiate CIFWrapper from 2-level mmCIF-like dictionary",
        )
        self.assertEqual(cif_wrapper.data_id, "NEW_ID",
                         "'NEW_ID' not set correctly as datablock ID")
        self.assertFalse("bogus" in cif_wrapper,
                         "CIFWrapper.__contains__ not returning boolean")
        self.assertTrue(
            "_test_category_1" in cif_wrapper,
            "CIFWrapper.__contains__ not returning boolean",
        )
        test_category_1 = cif_wrapper._test_category_1

        self.assertFalse(
            "bogus" in test_category_1,
            "CIFWrapperTable.__contains__ not returning boolean",
        )
        self.assertTrue(
            "test_value_1" in test_category_1,
            "CIFWrapperTable.__contains__ not returning boolean",
        )
Exemplo n.º 5
0
 def test_iter(self):
     cif_wrapper = CIFWrapper(self.raw_dictionary["TEST_BLOCK_1"],
                              preserve_token_order=True)
     rows_in = [
         {
             "test_value_3": "A ->\nLINE = A",
             "test_value_2": "Sleepy",
             "test_value_1": 1,
         },
         {
             "test_value_3": "B ->\nLINE = B",
             "test_value_2": "Dopey",
             "test_value_1": 2,
         },
         {
             "test_value_3": "C ->\nLINE = C",
             "test_value_2": "Bashful",
             "test_value_1": 3,
         },
         {
             "test_value_3": "D ->\nLINE = D",
             "test_value_2": "Grumpy",
             "test_value_1": 4,
         },
     ]
     rows_out = [row for row in cif_wrapper._test_category_2]
     self.assertEqual(rows_out, rows_in,
                      "Row iteration failed or gave inconsistent results")
Exemplo n.º 6
0
 def test_init_2_level_dictionary(self):
     cif_wrapper = CIFWrapper(self.raw_dictionary["TEST_BLOCK_1"],
                              preserve_token_order=True)
     self.assertIsInstance(
         cif_wrapper,
         CIFWrapper,
         "Failed to instantiate CIFWrapper from 2-level mmCIF-like dictionary",
     )
     assert_equal(cif_wrapper.data_id, "", "datablock ID should be ''")
Exemplo n.º 7
0
 def test_listContents(self):
     cif_wrapper = CIFWrapper(self.raw_dictionary["TEST_BLOCK_1"],
                              preserve_token_order=True)
     items = cif_wrapper._test_category_1.contents()
     items.sort()
     self.assertEqual(
         items,
         ["test_value_1", "test_value_2", "test_value_3"],
         "Item list for the category is incorrect",
     )
Exemplo n.º 8
0
 def test_init_2_level_dictionary_new_id(self):
     cif_wrapper = CIFWrapper(self.raw_dictionary["TEST_BLOCK_2"],
                              "NEW_ID",
                              preserve_token_order=True)
     self.assertIsInstance(
         cif_wrapper,
         CIFWrapper,
         "Failed to instantiate CIFWrapper from 2-level mmCIF-like dictionary",
     )
     assert_equal(cif_wrapper.data_id, "NEW_ID",
                  "'NEW_ID' not set correctly as datablock ID")
Exemplo n.º 9
0
 def test_search(self):
     cif_wrapper = CIFWrapper(self.raw_dictionary["TEST_BLOCK_1"],
                              preserve_token_order=True)
     check_row = {
         "test_value_3": "C ->\nLINE = C",
         "test_value_2": "Bashful",
         "test_value_1": 3,
     }
     result = cif_wrapper._test_category_2.search("test_value_1", 3)
     self.assertEqual(
         result[2], check_row,
         "Category search failed or gave inconsistent results")
Exemplo n.º 10
0
 def test_unwrap(self):
     cif_wrapper = CIFWrapper(self.raw_dictionary["TEST_BLOCK_2"],
                              "NEW_ID",
                              preserve_token_order=True)
     assert_equal(
         cif_wrapper.unwrap(),
         {"NEW_ID": {
             "_test_category_1": {
                 "test_value_1": [1, 2, 3, 4]
             }
         }},
         "CIFWrapper to dictionary conversion failed",
     )
     cif_wrapper = CIFWrapper(self.raw_dictionary["TEST_BLOCK_2"],
                              preserve_token_order=True)
     for unique_id, data in list(cif_wrapper.unwrap().items()):
         self.assertNotEqual(unique_id, "",
                             "No unique datablock id was assigned")
         assert_equal(
             data,
             {"_test_category_1": {
                 "test_value_1": [1, 2, 3, 4]
             }},
             "CIFWrapper to dictionary conversion failed",
         )
Exemplo n.º 11
0
 def test_wrapper_getattr(self):
     cif_wrapper = CIFWrapper(self.raw_dictionary["TEST_BLOCK_2"],
                              "NEW_ID",
                              preserve_token_order=True)
     self.assertIsNone(
         cif_wrapper._bogus,
         "__getattr__ not returning None for uninitialized attribute",
     )
     self.assertIsInstance(
         cif_wrapper._test_category_1,
         CIFWrapperTable,
         "__getattr__ not returning CIFWrapperTable",
     )
Exemplo n.º 12
0
 def test_delitem(self):
     cif_wrapper = CIFWrapper(self.raw_dictionary["TEST_BLOCK_1"],
                              preserve_token_order=True)
     del cif_wrapper._test_category_2["test_value_2"]
     self.assertFalse(
         "test_value_2" in cif_wrapper._test_category_2,
         "Failed to delete item from category",
     )
     columns = list([k for k in cif_wrapper._test_category_2][0].keys())
     columns.sort()
     self.assertEqual(
         columns,
         ["test_value_1", "test_value_3"],
         "item deletion failed or gave inconsistent results",
     )
Exemplo n.º 13
0
 def test_init_3_level_dictionary(self):
     """ preserve_token_order=True ensures deterministic unittests otherwise CIF data is random access """
     cif_wrapper = CIFWrapper(self.raw_dictionary,
                              preserve_token_order=True)
     self.assertIsInstance(
         cif_wrapper,
         CIFWrapper,
         "Failed to instantiate CIFWrapper from 3-level mmCIF-like dictionary",
     )
     sys.stdout.write(cif_wrapper.data_id)
     self.assertEqual(
         cif_wrapper.data_id,
         "TEST_BLOCK_1",
         "'TEST_BLOCK_1' not set correctly as datablock ID",
     )
Exemplo n.º 14
0
 def test_wrapperTable_getattr(self):
     cif_wrapper = CIFWrapper(self.raw_dictionary["TEST_BLOCK_2"],
                              "NEW_ID",
                              preserve_token_order=True)
     #        # dot notation access
     cif_wrapper._test_category_1.test_value_3 = "Bundy"
     self.assertEqual(
         cif_wrapper._test_category_1["test_value_3"],
         [
             "Bundy",
         ],
         "Dot-notation attribute setting failed to overwrite or gave inconsistent results",
     )
     self.assertEqual(
         cif_wrapper._test_category_1.test_value_3,
         [
             "Bundy",
         ],
         "Dot-notation attribute setting failed to overwrite or gave inconsistent results",
     )
Exemplo n.º 15
0
 def test_wrapper_delItem(self):
     cif_wrapper = CIFWrapper(self.raw_dictionary["TEST_BLOCK_2"],
                              "NEW_ID",
                              preserve_token_order=True)
     self.assertIsNone(
         cif_wrapper._bogus,
         "__getattr__ not returning None for uninitialized attribute",
     )
     self.assertIsInstance(
         cif_wrapper._test_category_1,
         CIFWrapperTable,
         "__getattr__ not returning CIFWrapperTable",
     )
     del cif_wrapper["_test_category_1"]
     self.assertIsNone(cif_wrapper._test_category_1,
                       "__delitem__ failed (dot notation)")
     self.assertIsNone(cif_wrapper["_test_category_1"],
                       "__delitem__ failed (dict notation)")
     self.assertIsNone(
         cif_wrapper._DATA.get("_test_category_1", None),
         "__delitem__ failed (direct access)",
     )