Skip to content

Instantly share code, notes, and snippets.

View klement97's full-sized avatar
🌍
Open for opportunities

Klement Omeri klement97

🌍
Open for opportunities
View GitHub Profile
@klement97
klement97 / install
Created August 22, 2021 16:17
Installation script for Venue Management
#!/usr/bin/env bash
set -e
REPO_URL="https://github.com/klement97/venue"
clone_repo() {
git clone $REPO_URL --depth 1 ~/venue_management
}
setup() {
@klement97
klement97 / test_utils.py
Last active August 7, 2021 21:07
A function to get the week number and a tester function for it.
import calendar
import datetime
from math import ceil
def get_week_number(date: datetime.date) -> int:
"""
Credits goes to:https://stackoverflow.com/a/66176965/9309611
"""
return (date.day - 1 + (date.weekday() - date.day + 1) % 7) // 7 + 1
def create_order() -> Order:
order = baker.make('order.Order')
baker.make('order.PizzaOrder', order=order)
order.save()
return order
@pytest.mark.django_db
def test_order_save(monkeypatch):
def mock_post(*args, **kwargs):
return MockResponse()
monkeypatch.setattr(requests, 'post', mock_post)
order = create_order()
assert order.price is not None
class MockResponse:
# mock json() method always returns a specific testing dictionary
@staticmethod
def json():
return {"mock_key": "mock_response"}
@pytest.mark.django_db
def test_order_save(monkeypatch):
def mock_post(*args, **kwargs):
return None
monkeypatch.setattr(requests, 'post', mock_post)
order = create_order()
assert order.price is not None
def print_receipt(self):
requests.post(
url=settings.RECEIPT_MICROSERVICE,
data=prepare_receipt_data(order=self)
)
def save(self, *args, **kwargs):
self.price = self.total_price
self.print_receipt()
super().save(*args, **kwargs)
@pytest.mark.django_db
@pytest.mark.parametrize('number_of_toppings', list(range(0, 10)))
def test_pizza_price(number_of_toppings):
# Preparation phase
pizza = create_pizza(number_of_toppings=number_of_toppings)
# Calculating results
actual_price = pizza.total_price
expected_price = Decimal(sum([t.price for t in pizza.toppings.all()]))
@pytest.mark.django_db
def test_pizza_price_with_toppings():
# Preparation phase
pizza = create_pizza(number_of_toppings=2)
# Calculating results
actual_price = pizza.total_price
expected_price = sum([t.price for t in pizza.toppings.all()])
# Assertion
def create_pizza(number_of_toppings: int, **kwargs) -> Pizza:
pizza = baker.make('order.Pizza', **kwargs)
for _ in range(number_of_toppings):
pizza.toppings.add(baker.make('order.Topping'))
return pizza