Skip to content

Commit

Permalink
add node provider table crud functions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
louisevelayo committed Nov 7, 2023
1 parent 40fbe7b commit 77a0edf
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
64 changes: 64 additions & 0 deletions node_monitor/node_provider_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ class NodeProviderDB:
"""
table_node_label_lookup_cols = ['node_id', 'node_label']

# TABLE node_provider_lookup
# This table will keep up to date with the node providers registered
# on the ic api.
create_table_node_provider_lookup = """
CREATE TABLE IF NOT EXISTS node_provider_lookup (
node_provider_id TEXT PRIMARY KEY,
node_provider_name TEXT
);
"""
table_node_provider_lookup_cols = ['node_provider_id', 'node_provider_name']



##############################################
Expand Down Expand Up @@ -131,6 +142,7 @@ def _create_tables(self) -> None:
cur.execute(self.create_table_email_lookup)
cur.execute(self.create_table_channel_lookup)
cur.execute(self.create_table_node_label_lookup)
cur.execute(self.create_table_node_provider_lookup)
self.disconnect()


Expand Down Expand Up @@ -424,3 +436,55 @@ def get_node_labels_as_dict(self) -> Dict[Principal, str]:
node_labels = {row[0]: row[1] for row in labels}
return node_labels


##############################################
## CRUD :: TABLE node_provider_lookup

def _insert_node_provider(
self, node_provider_id: Principal, node_provider_name: str) -> None:
"""Inserts a record into the node_provider_lookup table. Overwrites if
record already exists."""
query = """
INSERT INTO node_provider_lookup (
node_provider_id,
node_provider_name
) VALUES (%s, %s)
ON CONFLICT (node_provider_id) DO UPDATE SET
node_provider_name = EXCLUDED.node_provider_name
"""
values = (node_provider_id, node_provider_name)
self.connect()
assert self.conn is not None
with self.conn.cursor() as cur:
cur.execute(query, values)
self.disconnect()

def _delete_node_provider(self, node_provider_id: Principal) -> None:
"""Deletes a record from the node_provider_lookup table based
on node provider principal."""
query = """
DELETE FROM node_provider_lookup
WHERE node_provider_id = %s
"""
self.connect()
assert self.conn is not None
with self.conn.cursor() as cur:
cur.execute(query, (node_provider_id,))
self.disconnect()

def get_node_providers(self) -> List[Tuple[Any, ...]]:
"""Returns the table of all records in node_provider_lookup."""
query = "SELECT * FROM node_provider_lookup"
self.connect()
assert self.conn is not None
with self.conn.cursor() as cur:
cur.execute(query)
rows = cur.fetchall()
self.disconnect()
return rows

def get_node_providers_as_dict(self) -> Dict[int, Dict[str, str]]:
"""Returns the table of all records in node_provider_lookup as a dictionary."""
node_providers = self.get_node_providers()
node_providers_dict = {row[0]: row[1] for row in node_providers}
return node_providers_dict
45 changes: 44 additions & 1 deletion tests/test_node_provider_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def test_init():
def test_get_public_schema_tables():
all_public_tables = set(node_provider_db.get_public_schema_tables())
necessary_tables = {'subscribers', 'email_lookup',
'channel_lookup', 'node_label_lookup'}
'channel_lookup', 'node_label_lookup',
'node_provider_lookup'}
assert necessary_tables.issubset(all_public_tables)

@pytest.mark.db
Expand All @@ -34,6 +35,7 @@ def test_validate_column_names():
node_provider_db._validate_col_names('email_lookup', NodeProviderDB.table_email_lookup_cols)
node_provider_db._validate_col_names('channel_lookup', NodeProviderDB.table_channel_lookup_cols)
node_provider_db._validate_col_names('node_label_lookup', NodeProviderDB.table_node_label_lookup_cols)
node_provider_db._validate_col_names('node_provider_lookup', NodeProviderDB.table_node_provider_lookup_cols)


##############################################
Expand Down Expand Up @@ -217,3 +219,44 @@ def test_node_label_lookup_crud():
node_labels = node_provider_db.get_node_labels_as_dict()
assert 'test-dummy-node-id-1' not in node_labels
assert 'test-dummy-node-id-2' not in node_labels


##############################################
# ## TEST CRUD :: TABLE node_provider_lookup

@pytest.mark.db
def test_node_provider_lookup_crud():
# Insert new records / overwrite existing record
node_provider_db._insert_node_provider('test-dummy-principal-1', 'Node Provider A')
node_provider_db._insert_node_provider('test-dummy-principal-2', 'Node Provider B')

# Get and check node provider lookup records
node_providers = node_provider_db.get_node_providers()
assert ('test-dummy-principal-1', 'Node Provider A') in node_providers
assert ('test-dummy-principal-2', 'Node Provider B') in node_providers

# Overwrite a node label
node_provider_db._insert_node_provider('test-dummy-principal-1', 'Node Provider C')

# Get the new node labels, make sure the value was overwritten
node_providers = node_provider_db.get_node_providers()
assert ('test-dummy-principal-1', 'Node Provider C') in node_providers

# Get the node labels as dict, make sure they were inserted correctly
node_providers = node_provider_db.get_node_providers_as_dict()
assert node_providers['test-dummy-principal-1'] == 'Node Provider C'
assert node_providers['test-dummy-principal-2'] == 'Node Provider B'

# Delete node labels
node_provider_db._delete_node_provider('test-dummy-principal-1')
node_provider_db._delete_node_provider('test-dummy-principal-2')

# Get and check node provider lookup records
node_providers = node_provider_db.get_node_providers()
assert ('test-dummy-principal-1', 'Node Provider C') not in node_providers
assert ('test-dummy-principal-2', 'Node Provider B') not in node_providers

# # Get and check node provider lookup records as dict
# lookup_dict = node_provider_db.get_node_providers_as_dict()
# assert 1 not in lookup_dict
# assert 2 not in lookup_dict

0 comments on commit 77a0edf

Please sign in to comment.