Commit 39659b1f authored by Alexander's avatar Alexander

renamed node list for clusters

parent e5010b4c
...@@ -4,15 +4,15 @@ from datetime import date, datetime ...@@ -4,15 +4,15 @@ from datetime import date, datetime
class Cluster: class Cluster:
def __init__(self, cluster_label: int = None, clusters: List = None): def __init__(self, cluster_label: int = None, nodes: List = None):
self.cluster_label = cluster_label self.cluster_label = cluster_label
self.clusters = clusters self.nodes = nodes
class LocationCluster(Cluster): class LocationCluster(Cluster):
def __init__(self, cluster_label: int = None, clusters: List = None, def __init__(self, cluster_label: int = None, nodes: List = None,
location_dict: Dict = None, from_db=False): location_dict: Dict = None, from_db=False):
super().__init__(cluster_label, clusters) super().__init__(cluster_label, nodes)
self.id = f'{self.cluster_label}' self.id = f'{self.cluster_label}'
if location_dict is not None: if location_dict is not None:
...@@ -22,14 +22,14 @@ class LocationCluster(Cluster): ...@@ -22,14 +22,14 @@ class LocationCluster(Cluster):
return { return {
"id": self.id, "id": self.id,
"cluster_label": self.cluster_label, "cluster_label": self.cluster_label,
"clusters": json.dumps(self.clusters) if for_db else self.clusters "nodes": json.dumps(self.nodes) if for_db else self.nodes
} }
def from_serializable_dict(self, location_dict: Dict, from_db=False): def from_serializable_dict(self, location_dict: Dict, from_db=False):
self.id = location_dict["id"] self.id = location_dict["id"]
self.cluster_label = location_dict["cluster_label"] self.cluster_label = location_dict["cluster_label"]
self.clusters = json.loads(location_dict["clusters"]) \ self.nodes = json.loads(location_dict["nodes"]) \
if from_db else location_dict["clusters"] if from_db else location_dict["nodes"]
def __repr__(self): def __repr__(self):
return json.dumps(self.to_serializable_dict()) return json.dumps(self.to_serializable_dict())
...@@ -39,9 +39,9 @@ class LocationCluster(Cluster): ...@@ -39,9 +39,9 @@ class LocationCluster(Cluster):
class TimeCluster(Cluster): class TimeCluster(Cluster):
def __init__(self, date: date = None, hour: int = None, cluster_label: int = None, clusters: List = None, def __init__(self, date: date = None, hour: int = None, cluster_label: int = None, nodes: List = None,
time_dict: Dict = None, from_db=False): time_dict: Dict = None, from_db=False):
super().__init__(cluster_label, clusters) super().__init__(cluster_label, nodes)
self.date = date self.date = date
self.hour = hour self.hour = hour
self.id = f'{self.date}-{self.hour}-{self.cluster_label}' self.id = f'{self.date}-{self.hour}-{self.cluster_label}'
...@@ -55,7 +55,7 @@ class TimeCluster(Cluster): ...@@ -55,7 +55,7 @@ class TimeCluster(Cluster):
"date": str(self.date), "date": str(self.date),
"hour": self.hour, "hour": self.hour,
"cluster_label": self.cluster_label, "cluster_label": self.cluster_label,
"clusters": json.dumps(self.clusters) if for_db else self.clusters "nodes": json.dumps(self.nodes) if for_db else self.nodes
} }
def from_serializable_dict(self, time_dict: Dict, from_db=False): def from_serializable_dict(self, time_dict: Dict, from_db=False):
...@@ -63,8 +63,8 @@ class TimeCluster(Cluster): ...@@ -63,8 +63,8 @@ class TimeCluster(Cluster):
self.date = datetime.strptime(time_dict["date"], '%Y-%m-%d').date() self.date = datetime.strptime(time_dict["date"], '%Y-%m-%d').date()
self.hour = time_dict["hour"] self.hour = time_dict["hour"]
self.cluster_label = time_dict["cluster_label"] self.cluster_label = time_dict["cluster_label"]
self.clusters = json.loads(time_dict["clusters"]) \ self.nodes = json.loads(time_dict["nodes"]) \
if from_db else time_dict["clusters"] if from_db else time_dict["nodes"]
def __repr__(self): def __repr__(self):
return json.dumps(self.to_serializable_dict()) return json.dumps(self.to_serializable_dict())
......
...@@ -15,7 +15,7 @@ class TestCluster(unittest.TestCase): ...@@ -15,7 +15,7 @@ class TestCluster(unittest.TestCase):
c = Cluster(1, [1, 2, 3]) c = Cluster(1, [1, 2, 3])
self.assertEqual(1, c.cluster_label) self.assertEqual(1, c.cluster_label)
self.assertEqual([1, 2, 3], c.clusters) self.assertEqual([1, 2, 3], c.nodes)
class TestLocationCluster(unittest.TestCase): class TestLocationCluster(unittest.TestCase):
...@@ -27,30 +27,30 @@ class TestLocationCluster(unittest.TestCase): ...@@ -27,30 +27,30 @@ class TestLocationCluster(unittest.TestCase):
self.assertEqual('1', c.id) self.assertEqual('1', c.id)
self.assertEqual(1, c.cluster_label) self.assertEqual(1, c.cluster_label)
self.assertEqual([1, 2, 3], c.clusters) self.assertEqual([1, 2, 3], c.nodes)
def test_init_dictArgument(self): def test_init_dictArgument(self):
dict_ = {'id': '123', 'cluster_label': 1, 'clusters': [1, 2, 3]} dict_ = {'id': '123', 'cluster_label': 1, 'nodes': [1, 2, 3]}
c = LocationCluster(location_dict=dict_) c = LocationCluster(location_dict=dict_)
self.assertEqual('123', c.id) self.assertEqual('123', c.id)
self.assertEqual(1, c.cluster_label) self.assertEqual(1, c.cluster_label)
self.assertEqual([1, 2, 3], c.clusters) self.assertEqual([1, 2, 3], c.nodes)
def test_init_dictArgument_fromDb(self): def test_init_dictArgument_fromDb(self):
dict_ = {'id': '123', 'cluster_label': 1, 'clusters': '[1, 2, 3]'} dict_ = {'id': '123', 'cluster_label': 1, 'nodes': '[1, 2, 3]'}
c = LocationCluster(location_dict=dict_, from_db=True) c = LocationCluster(location_dict=dict_, from_db=True)
self.assertEqual('123', c.id) self.assertEqual('123', c.id)
self.assertEqual(1, c.cluster_label) self.assertEqual(1, c.cluster_label)
self.assertEqual([1, 2, 3], c.clusters) self.assertEqual([1, 2, 3], c.nodes)
def test_to_serializable_dict_noDb(self): def test_to_serializable_dict_noDb(self):
c_dict = self.c.to_serializable_dict() c_dict = self.c.to_serializable_dict()
self.assertEqual(self.c.id, c_dict['id']) self.assertEqual(self.c.id, c_dict['id'])
self.assertEqual(self.c.cluster_label, c_dict['cluster_label']) self.assertEqual(self.c.cluster_label, c_dict['cluster_label'])
self.assertEqual(self.c.clusters, c_dict['clusters']) self.assertEqual(self.c.nodes, c_dict['nodes'])
def test_from_serializable_dict_noDb(self): def test_from_serializable_dict_noDb(self):
new_c = LocationCluster() new_c = LocationCluster()
...@@ -59,12 +59,12 @@ class TestLocationCluster(unittest.TestCase): ...@@ -59,12 +59,12 @@ class TestLocationCluster(unittest.TestCase):
self.assertEqual(self.c.id, new_c.id) self.assertEqual(self.c.id, new_c.id)
self.assertEqual(str(self.c), str(new_c)) self.assertEqual(str(self.c), str(new_c))
def test_to_serializable_dict_db_jsonClusters(self): def test_to_serializable_dict_db_jsonNodes(self):
c_dict = self.c.to_serializable_dict(for_db=True) c_dict = self.c.to_serializable_dict(for_db=True)
self.assertEqual(self.c.id, c_dict['id']) self.assertEqual(self.c.id, c_dict['id'])
self.assertEqual(self.c.cluster_label, c_dict['cluster_label']) self.assertEqual(self.c.cluster_label, c_dict['cluster_label'])
self.assertEqual(self.c.clusters, json.loads(c_dict['clusters'])) self.assertEqual(self.c.nodes, json.loads(c_dict['nodes']))
def test_from_serializable_dict_fromDb(self): def test_from_serializable_dict_fromDb(self):
new_c = LocationCluster() new_c = LocationCluster()
...@@ -88,10 +88,10 @@ class TestTimeCluster(unittest.TestCase): ...@@ -88,10 +88,10 @@ class TestTimeCluster(unittest.TestCase):
self.assertEqual(self.date_, c.date) self.assertEqual(self.date_, c.date)
self.assertEqual(14, c.hour) self.assertEqual(14, c.hour)
self.assertEqual(1, c.cluster_label) self.assertEqual(1, c.cluster_label)
self.assertEqual([1, 2, 3], c.clusters) self.assertEqual([1, 2, 3], c.nodes)
def test_init_dictArgument(self): def test_init_dictArgument(self):
dict_ = {'id': '123', 'cluster_label': 1, 'clusters': [1, 2, 3], dict_ = {'id': '123', 'cluster_label': 1, 'nodes': [1, 2, 3],
'date': str(self.date_), 'hour': 14} 'date': str(self.date_), 'hour': 14}
c = TimeCluster(time_dict=dict_) c = TimeCluster(time_dict=dict_)
...@@ -99,10 +99,10 @@ class TestTimeCluster(unittest.TestCase): ...@@ -99,10 +99,10 @@ class TestTimeCluster(unittest.TestCase):
self.assertEqual(self.date_, c.date) self.assertEqual(self.date_, c.date)
self.assertEqual(14, c.hour) self.assertEqual(14, c.hour)
self.assertEqual(1, c.cluster_label) self.assertEqual(1, c.cluster_label)
self.assertEqual([1, 2, 3], c.clusters) self.assertEqual([1, 2, 3], c.nodes)
def test_init_dictArgument_fromDb(self): def test_init_dictArgument_fromDb(self):
dict_ = {'id': '123', 'cluster_label': 1, 'clusters': '[1, 2, 3]', dict_ = {'id': '123', 'cluster_label': 1, 'nodes': '[1, 2, 3]',
'date': str(self.date_), 'hour': 14} 'date': str(self.date_), 'hour': 14}
c = TimeCluster(time_dict=dict_, from_db=True) c = TimeCluster(time_dict=dict_, from_db=True)
...@@ -110,14 +110,14 @@ class TestTimeCluster(unittest.TestCase): ...@@ -110,14 +110,14 @@ class TestTimeCluster(unittest.TestCase):
self.assertEqual(self.date_, c.date) self.assertEqual(self.date_, c.date)
self.assertEqual(14, c.hour) self.assertEqual(14, c.hour)
self.assertEqual(1, c.cluster_label) self.assertEqual(1, c.cluster_label)
self.assertEqual([1, 2, 3], c.clusters) self.assertEqual([1, 2, 3], c.nodes)
def test_to_serializable_dict_noDb(self): def test_to_serializable_dict_noDb(self):
c_dict = self.c.to_serializable_dict() c_dict = self.c.to_serializable_dict()
self.assertEqual(self.c.id, c_dict['id']) self.assertEqual(self.c.id, c_dict['id'])
self.assertEqual(self.c.cluster_label, c_dict['cluster_label']) self.assertEqual(self.c.cluster_label, c_dict['cluster_label'])
self.assertEqual(self.c.clusters, c_dict['clusters']) self.assertEqual(self.c.nodes, c_dict['nodes'])
self.assertEqual(self.c.date, datetime.strptime( self.assertEqual(self.c.date, datetime.strptime(
c_dict['date'], '%Y-%m-%d').date()) c_dict['date'], '%Y-%m-%d').date())
self.assertEqual(self.c.hour, c_dict['hour']) self.assertEqual(self.c.hour, c_dict['hour'])
...@@ -129,12 +129,12 @@ class TestTimeCluster(unittest.TestCase): ...@@ -129,12 +129,12 @@ class TestTimeCluster(unittest.TestCase):
self.assertEqual(self.c.id, new_c.id) self.assertEqual(self.c.id, new_c.id)
self.assertEqual(str(self.c), str(new_c)) self.assertEqual(str(self.c), str(new_c))
def test_to_serializable_dict_fromDb_jsonClusters(self): def test_to_serializable_dict_fromDb_jsonNodes(self):
c_dict = self.c.to_serializable_dict(for_db=True) c_dict = self.c.to_serializable_dict(for_db=True)
self.assertEqual(self.c.id, c_dict['id']) self.assertEqual(self.c.id, c_dict['id'])
self.assertEqual(self.c.cluster_label, c_dict['cluster_label']) self.assertEqual(self.c.cluster_label, c_dict['cluster_label'])
self.assertEqual(self.c.clusters, json.loads(c_dict['clusters'])) self.assertEqual(self.c.nodes, json.loads(c_dict['nodes']))
self.assertEqual(self.c.date, datetime.strptime( self.assertEqual(self.c.date, datetime.strptime(
c_dict['date'], '%Y-%m-%d').date()) c_dict['date'], '%Y-%m-%d').date())
self.assertEqual(self.c.hour, c_dict['hour']) self.assertEqual(self.c.hour, c_dict['hour'])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment