Skip to content
Snippets Groups Projects
Commit 9c13a9d1 authored by Valtteri Virta's avatar Valtteri Virta
Browse files

added tests

parent 0be5cc4f
No related branches found
No related tags found
No related merge requests found
{
"python.testing.unittestArgs": [
"-v",
"-s",
"./tests",
"-p",
"test_*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
}
\ No newline at end of file
AI.txt 0 → 100644
https://chatgpt.com/share/680913c3-f54c-8003-b80f-1e8e53c6b710
\ No newline at end of file
No preview for this file type
from typing import Dict, List
from node import Node #node.py is in same directory
from node import Node
class Graph:
"""
......
File added
File added
import os
import sys
import unittest
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from graph import Graph
from node import Node
class TestGraphMethods(unittest.TestCase):
def setUp(self):
self.g = Graph()
self.a = Node(1)
self.b = Node(2)
self.c = Node(3)
def test_add_node_new(self):
# Adding a new node returns the same instance
result = self.g.add_node(self.a)
self.assertIs(result, self.a)
# The node dictionary now contains that node under its id
self.assertIn(1, self.g.nodes)
self.assertIs(self.g.nodes[1], self.a)
def test_add_node_duplicate(self):
# Add the same node twice
first = self.g.add_node(self.a)
# Create a different Node instance with the same id
dup = Node(1)
second = self.g.add_node(dup)
# It should ignore the new instance and return the original one
self.assertIs(second, first)
# The internal storage must still be the original
self.assertIs(self.g.nodes[1], first)
def test_get_nodes(self):
# Initially empty
self.assertEqual(self.g.get_nodes(), [])
# Add several nodes
self.g.add_node(self.a)
self.g.add_node(self.b)
self.g.add_node(self.c)
# get_nodes should return all three (order not guaranteed)
nodes = self.g.get_nodes()
self.assertCountEqual(nodes, [self.a, self.b, self.c])
def test_add_edge_success(self):
# Add nodes first
self.g.add_node(self.a)
self.g.add_node(self.b)
# Add an edge of weight 5
self.g.add_edge(self.a, self.b, weight=5)
# Check both directions
neighs_a = dict(self.a.get_neighbors())
neighs_b = dict(self.b.get_neighbors())
self.assertIn(self.b, neighs_a)
self.assertEqual(neighs_a[self.b], 5)
self.assertIn(self.a, neighs_b)
self.assertEqual(neighs_b[self.a], 5)
def test_add_edge_missing_node(self):
# Only add one node
self.g.add_node(self.a)
# Attempting to connect to a node not in the graph must raise KeyError
with self.assertRaises(KeyError):
self.g.add_edge(self.a, self.b, weight=1)
with self.assertRaises(KeyError):
self.g.add_edge(self.b, self.a, weight=1)
if __name__ == "__main__":
unittest.main()
import os
import sys
import unittest
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from node import Node
class TestNodeMethods(unittest.TestCase):
def setUp(self):
# Create two nodes for testing
self.a = Node(1)
self.b = Node(2)
def test_add_neighbor(self):
# Before adding, no neighbors
self.assertEqual(list(self.a.get_neighbors()), [])
# Add neighbor with weight 5
self.a.add_neighbor(self.b, 5)
# Now b should be in neighbors with weight 5
neighs = dict(self.a.get_neighbors())
self.assertIn(self.b, neighs)
self.assertEqual(neighs[self.b], 5)
def test_remove_neighbor_present(self):
# Add then remove
self.a.add_neighbor(self.b, 3)
self.a.remove_neighbor(self.b)
# Should no longer be in neighbors
self.assertNotIn(self.b, dict(self.a.get_neighbors()))
def test_remove_neighbor_absent(self):
# Removing a non‐existent neighbor should not raise
try:
self.a.remove_neighbor(self.b)
except Exception as e:
self.fail(f"remove_neighbor raised {e} on absent neighbor")
def test_get_neighbors_iterator(self):
# Add two neighbors
c = Node(3)
self.a.add_neighbor(self.b, 7)
self.a.add_neighbor(c, 9)
# get_neighbors should be an iterator over exactly those pairs
items = list(self.a.get_neighbors())
self.assertCountEqual(items, [(self.b, 7), (c, 9)])
# Ensure it's a fresh iterator each time
items2 = list(self.a.get_neighbors())
self.assertCountEqual(items2, items)
if __name__ == "__main__":
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment