Skip to content

Commit

Permalink
Add ArtCollection
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Olivia Brown <oliviabrown@MacBook-Pro-4.localdomain>
Co-authored-by: Olivia Brown <oliviabrown@MacBook-Pro-4.local>
  • Loading branch information
3 people authored Jan 27, 2024
1 parent 2833d5f commit 9bd1404
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 5 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.analysis.extraPaths": ["./src/main/py"]
}
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ test: ruff pylint
--top-level-directory .
coverage report
coverage html

2 changes: 1 addition & 1 deletion devops/deploy/spin-up-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ port=$PSTART
while [ ${COUNT} -le ${NUM_PORTS} ]
do
#Check for Open ports, if open then OK, else continue
if ! netstat -apn | grep -q "$IP:$port"
if ! netstat -apn | grep -q "$IP:$port"
then
# Generating keys
ssh-keygen -t ed25519 -f "keys/node${COUNT}" -N ""
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ skip_empty = true
[tool.pylint]
# I prefer to allow an empty line at the end of a source file.
disable = ["trailing-newlines"]

1 change: 0 additions & 1 deletion set-up-python-venv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,3 @@ else
pip install -r requirements.txt
fi
fi

Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
39 changes: 39 additions & 0 deletions src/main/py/commission/artcollection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
Module to manage art collections for Art Collectors.
"""

from commission.artwork import Artwork


class ArtCollection:
"""
Class to manage art collections for Art Collectors
"""

def __init__(self) -> None:
"""
Initialize the art collection by creating an empty list for the artworks
in the collection
"""
self.artworks = []

def get_artworks(self):
"""
Returns the artworks in the collection
"""

return self.artworks

def add_to_art_collection(self, artwork: Artwork):
"""
Add an artwork to the collection
"""

self.artworks.append(artwork)

def remove_from_art_collection(self, artwork: Artwork):
"""
Remove an artwork from the collection
"""

self.artworks.remove(artwork)
1 change: 0 additions & 1 deletion src/main/py/commission/artwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class Artwork:
"""

# pylint: disable=too-many-arguments, too-many-instance-attributes

def __init__(
self,
width: float,
Expand Down
41 changes: 41 additions & 0 deletions src/main/py/peer/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,47 @@ def merge_canvas(self, width: int, height: int, fragment) -> Image.Image:

canvas.save("canvas.png", "PNG")
return canvas
def add_to_art_collection(self, artwork, collection):
"""
Add artwork to collection
"""

try:
collection.add_to_art_collection(artwork)
logging.info("Artwork successfully added to collection.")
except ValueError as e:
logging.error("Failed to add artwork to collection: %s", e)

def remove_from_art_collection(self, artwork, collection):
"""
Remove artwork from collection
"""

try:
collection.remove_from_art_collection(artwork)
logging.info("Artwork removed from collection successfully.")
except ValueError as e:
logging.error("Failed to remove artwork from collection: %s", e)

def swap_art(self, my_art, their_art, my_art_collection, their_art_collection):
"""
Swap artwork between two collections
"""

try:
if (
my_art in my_art_collection.get_artworks()
and their_art in their_art_collection.get_artworks()
):
my_art_collection.remove_from_art_collection(my_art)
their_art_collection.remove_from_art_collection(their_art)
my_art_collection.add_to_art_collection(their_art)
their_art_collection.add_to_art_collection(my_art)
logging.info("Artwork successfully swapped.")
else:
logging.warning("Artwork not found in collections.")
except ValueError as e:
logging.error("Failed to swap artwork: %s", e)


async def main():
Expand Down
1 change: 1 addition & 0 deletions src/main/py/server/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, data_stored_callback, ksize=20, alpha=3, node_id=None):
"""
self.data_stored_callback = data_stored_callback
# Call the parent class's __init__ with the new protocol

super().__init__(ksize, alpha, node_id=node_id)

def _create_protocol(self):
Expand Down
Empty file.
45 changes: 45 additions & 0 deletions src/test/py/artcollection/art_collection_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Test module for ArtCollection class"""

import unittest
from datetime import timedelta
from commission.artcollection import ArtCollection
from commission.artwork import Artwork


class TestArtCollection(unittest.TestCase):
"""
Test class for ArtCollection class
"""

def setUp(self):
"""
Create an instance of ArtCollection and Artwork
with width=100, height=100, and wait_time=1 day
"""

self.collection = ArtCollection()
self.artwork1 = Artwork(100.0, 100.0, timedelta(days=1))
self.artwork2 = Artwork(200.0, 200.0, timedelta(days=2))

def test_add_to_art_collection(self):
"""
Test the add_to_art_collection method of ArtCollection
This test verifies that the add_to_art_collection method correctly
"""

self.collection.add_to_art_collection(self.artwork1)
self.assertIn(self.artwork1, self.collection.get_artworks())

def test_remove_from_art_collection(self):
"""Test the remove_from_art_collection method of ArtCollection
This test verifies that the remove_from_art_collection method correctly
and removes an artwork from a collection
"""

self.collection.add_to_art_collection(self.artwork1)
self.collection.remove_from_art_collection(self.artwork1)
self.assertNotIn(self.artwork1, self.collection.get_artworks())


if __name__ == "__main__":
unittest.main()
39 changes: 39 additions & 0 deletions src/test/py/peer/peer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import unittest
from unittest.mock import patch, MagicMock, AsyncMock
from peer.peer import Peer
from commission.artcollection import ArtCollection
from commission.artwork import Artwork


class MockNode:
Expand Down Expand Up @@ -49,6 +51,8 @@ class TestPeer(unittest.IsolatedAsyncioTestCase):
Class to test peer functionality.
"""

# pylint: disable=too-many-instance-attributes

test_logger = logging.getLogger("TestPeer")

def setUp(self):
Expand All @@ -66,6 +70,10 @@ def setUp(self):
self.mock_kdm,
)
self.deadline_task = None
self.collection1 = ArtCollection()
self.collection2 = ArtCollection()
self.artwork1 = Artwork(100.0, 100.0, timedelta(days=1))
self.artwork2 = Artwork(200.0, 200.0, timedelta(days=2))

def test_initialization(self):
"""
Expand Down Expand Up @@ -113,6 +121,37 @@ async def test_commission_art_piece(self, mock_input):
await self.deadline_task
self.assertEqual(self.mock_node.set.call_count, 2)

def test_add_to_art_collection(self):
"""
Test case for the add_to_art_collection method of the Peer class.
This test verifies that the add_to_art_collection method correctly
adds an artwork to a collection.
"""
self.peer.add_to_art_collection(self.artwork1, self.collection1)
self.assertIn(self.artwork1, self.collection1.get_artworks())

def test_remove_from_art_collection(self):
"""
Test case for the remove_from_art_collection method of the Peer class.
This test verifies that the remove_from_art_collection method correctly
"""
self.collection1.add_to_art_collection(self.artwork1)
self.peer.remove_from_art_collection(self.artwork1, self.collection1)
self.assertNotIn(self.artwork1, self.collection1.get_artworks())

def test_swap_art(self):
"""
Test case for the swap_art method of the Peer class.
This test verifies that the swap_art method correctly swaps artworks
"""
self.collection1.add_to_art_collection(self.artwork1)
self.collection2.add_to_art_collection(self.artwork2)
self.peer.swap_art(
self.artwork1, self.artwork2, self.collection1, self.collection2
)
self.assertIn(self.artwork1, self.collection2.get_artworks())
self.assertIn(self.artwork2, self.collection1.get_artworks())


if __name__ == "__main__":
# Create an event loop
Expand Down

0 comments on commit 9bd1404

Please sign in to comment.