diff --git a/Dockerfile b/Dockerfile index 7497efa..aadfda6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,6 +7,6 @@ COPY requirements.txt requirements.txt RUN pip3 install -r requirements.txt -COPY src src +COPY . . -CMD [ "python3", "src/main.py" ] +CMD [ "python3", "main.py" ] diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/main.py b/main.py similarity index 85% rename from src/main.py rename to main.py index ad64102..6b2ec39 100644 --- a/src/main.py +++ b/main.py @@ -1,7 +1,7 @@ -from repositories.apartment_firestore import ApartmentFirestoreRepository -from usecase.apartment import ApartmentUseCase -from infrastructure.discord import DiscordInfrastructure -from repositories.scraper import ScraperRepository +from src.repositories.apartment_firestore import ApartmentFirestoreRepository +from src.usecase.apartment import ApartmentUseCase +from src.infrastructure.discord import DiscordInfrastructure +from src.repositories.scraper import ScraperRepository from firebase_admin import credentials import os diff --git a/src/repositories/apartment_firestore.py b/src/repositories/apartment_firestore.py index fde45fa..7a2459f 100644 --- a/src/repositories/apartment_firestore.py +++ b/src/repositories/apartment_firestore.py @@ -4,7 +4,7 @@ from firebase_admin import firestore from google.api_core import exceptions -from entities.apartment import Apartment +from src.entities.apartment import Apartment class ApartmentFirestoreRepository: def __init__(self, credentials): diff --git a/src/usecase/apartment.py b/src/usecase/apartment.py index d957fc1..7704f38 100644 --- a/src/usecase/apartment.py +++ b/src/usecase/apartment.py @@ -1,7 +1,7 @@ -from entities.apartment import Apartment -from repositories.scraper import ScraperRepository -from repositories.apartment_firestore import ApartmentFirestoreRepository -from infrastructure.discord import DiscordInfrastructure +from src.entities.apartment import Apartment +from src.repositories.scraper import ScraperRepository +from src.repositories.apartment_firestore import ApartmentFirestoreRepository +from src.infrastructure.discord import DiscordInfrastructure import re class ApartmentUseCase: diff --git a/tests/entities/test_apartment.py b/tests/entities/test_apartment.py new file mode 100644 index 0000000..6040bca --- /dev/null +++ b/tests/entities/test_apartment.py @@ -0,0 +1,84 @@ +from src.entities.apartment import Apartment +import unittest + +class TestApartment(unittest.TestCase): + + def test_initialization(self): + # Test the initialization of the Apartment instance + apartment = Apartment( + id=1, + address="123 Main St", + location="City", + last_register_date="2023-01-01", + level=2, + rent=1000, + rooms=3, + square_meter=75, + temporary=True, + age=20, + youth=True + ) + + self.assertEqual(apartment.id, 1) + self.assertEqual(apartment.address, "123 Main St") + self.assertEqual(apartment.location, "City") + self.assertEqual(apartment.last_register_date, "2023-01-01") + self.assertEqual(apartment.level, 2) + self.assertEqual(apartment.rent, 1000) + self.assertEqual(apartment.rooms, 3) + self.assertEqual(apartment.square_meter, 75) + self.assertTrue(apartment.temporary) + self.assertEqual(apartment.age, 20) + self.assertTrue(apartment.youth) + + def test_string_representation(self): + # Test the __str__ method of the Apartment class + apartment = Apartment( + id=1, + address="123 Main St", + location="City", + last_register_date="2023-01-01", + level=2, + rent=1000, + rooms=3, + square_meter=75, + temporary=True, + age=20, + youth=True + ) + + expected_str = "Address: 123 Main St\nLast Register Date: 2023-01-01\nLevel: 2\nLink: https://bostad.stockholm.se/bostad/1\nLocation: City\nRent: 1000\nRooms: 3\nSquare Meter: 75\nTemporary: True\nAge: 20\nYouth: True\n" + self.assertEqual(str(apartment), expected_str) + + def test_youth_set_but_not_age(self): + # Test that an exception is raised when youth and age are not set correctly + with self.assertRaises(ValueError): + Apartment( + id=1, + address="123 Main St", + location="City", + last_register_date="2023-01-01", + level=2, + rent=1000, + rooms=3, + square_meter=75, + youth=True # Age is missing + ) + + def test_age_set_but_not_youth(self): + # Test that an exception is raised when youth and age are not set correctly + with self.assertRaises(ValueError): + Apartment( + id=1, + address="123 Main St", + location="City", + last_register_date="2023-01-01", + level=2, + rent=1000, + rooms=3, + square_meter=75, + age=23 # Youth is missing + ) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/repositories/test_apartment_firestore.py b/tests/repositories/test_apartment_firestore.py new file mode 100644 index 0000000..383b78e --- /dev/null +++ b/tests/repositories/test_apartment_firestore.py @@ -0,0 +1,53 @@ +import unittest +from unittest.mock import Mock, patch +from src.repositories.apartment_firestore import ApartmentFirestoreRepository +from src.entities.apartment import Apartment +from google.api_core.exceptions import AlreadyExists + +class TestApartmentFirestoreRepository(unittest.TestCase): + + def setUp(self): + # Mock Firebase Admin initialize_app method + self.mock_firebase_admin = Mock() + self.mock_firestore = Mock() + self.mock_collection = Mock() + self.mock_document = Mock() + self.mock_firestore.client.return_value = self.mock_collection + self.mock_collection.document.return_value = self.mock_document + self.patcher = patch('src.repositories.apartment_firestore.firebase_admin', self.mock_firebase_admin) + self.patcher.start() + + # Initialize the repository with a dummy credentials path + self.repo = ApartmentFirestoreRepository('dummy_credentials.json') + + def tearDown(self): + self.patcher.stop() + + def test_post_apartment_listing_successful(self): + # Create a sample Apartment object + apartment = Apartment("1", "Sample Apartment", 1000) + + # Mock Firestore create method to simulate successful creation + self.mock_document.create.return_value = None + + # Attempt to post the apartment listing + result = self.repo.post_apartment_listing(apartment) + + # Verify that the method returned True, indicating success + self.assertTrue(result) + + def test_post_apartment_listing_already_exists(self): + # Create a sample Apartment object + apartment = Apartment("1", "Sample Apartment", 1000) + + # Mock Firestore create method to simulate AlreadyExists exception + self.mock_document.create.side_effect = AlreadyExists("Document already exists") + + # Attempt to post the apartment listing + result = self.repo.post_apartment_listing(apartment) + + # Verify that the method returned False, indicating that the document already exists + self.assertFalse(result) + +if __name__ == '__main__': + unittest.main()