def test_save_cluster(self): # TO DO -- Cluster saving probably not working yet y = ['Wave'] x = ['q2', 'q3'] v = 'x|frequency||||counts' cluster = Cluster(name="ClusterName") self.assertIsInstance(cluster, Cluster) self.assertEqual([],cluster.keys()) for i in xrange(3): chain = self.stack0.get_chain(name="ChainName{0}".format(i), data_keys="Jan", x=x, y=y, views=[v]) cluster.add_chain(chains=chain) # Create a dictionary with the attribute structure of the cluster cluster_attributes = cluster.__dict__ path_cluster = '{}test.cluster'.format(self.path) cluster.save(path_cluster) loaded_cluster = Cluster.load(path_cluster) # Create a dictionary with the attribute structure of the cluster loaded_cluster_attributes = loaded_cluster.__dict__ # Ensure that we are not comparing the same variable (in memory) self.assertNotEqual(id(cluster), id(loaded_cluster)) # Make sure that this is working by altering the loaded_stack_attributes # and comparing the result. (It should fail) # Change a 'value' in the dict loaded_cluster_attributes['name'] = "SomeOtherName" with self.assertRaises(AssertionError): self.assertEqual(cluster.name, loaded_cluster_attributes['name']) # reset the value loaded_cluster_attributes['name'] = cluster_attributes['name'] self.assertEqual(cluster_attributes['name'], loaded_cluster_attributes['name']) # Change a 'key' in the dict del loaded_cluster_attributes['name'] loaded_cluster_attributes['new_name'] = cluster_attributes['name'] with self.assertRaises(AttributeError): self.assertEqual(cluster.name, loaded_cluster.name) # reset the value del loaded_cluster_attributes['new_name'] loaded_cluster_attributes['name'] = cluster_attributes['name'] self.assertEqual(cluster.name, loaded_cluster.name) # Remove a key/value pair del loaded_cluster_attributes['name'] with self.assertRaises(AttributeError): self.assertEqual(cluster.name, loaded_cluster.name) # Cleanup if os.path.exists(path_cluster): os.remove(path_cluster)