Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
move files to make it testable
Browse files Browse the repository at this point in the history
  • Loading branch information
philipsabri committed Oct 24, 2023
1 parent c7c682e commit 575b7cc
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Empty file added __init__.py
Empty file.
8 changes: 4 additions & 4 deletions src/main.py → main.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/repositories/apartment_firestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions src/usecase/apartment.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
84 changes: 84 additions & 0 deletions tests/entities/test_apartment.py
Original file line number Diff line number Diff line change
@@ -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()
53 changes: 53 additions & 0 deletions tests/repositories/test_apartment_firestore.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 575b7cc

Please sign in to comment.