Last active
August 19, 2022 21:01
-
-
Save Z4HRA-S/a5b472ba3414021b3677a63213c30e9d to your computer and use it in GitHub Desktop.
test_example1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from models import SaleRecords | |
from sqlalchemy import func | |
def sold_product_percent(session) -> dict: | |
sold_product = session.query(func.sum(SaleRecords.count), SaleRecords.product_name | |
).group_by(SaleRecords.product_name).all() | |
sum_of_products = sum(count for count, name in sold_product) | |
percents = {item[1]: round((item[0] / sum_of_products) * 100, 2) | |
for item in sold_product} | |
return percents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from models import Base | |
from sqlalchemy import create_engine | |
from sqlalchemy.orm import sessionmaker | |
from main import sold_product_percent | |
class TestMain(unittest.TestCase): | |
def test_sold_product_percent(self): | |
# Arrange | |
db_url = "postgresql://user_name:password@127.0.0.1:5432/test" | |
engine = create_engine(db_url, echo=False) | |
Base.metadata.create_all(engine) | |
engine.connect() | |
session_local = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
session = session_local() | |
# We must have used the mocks here | |
# Act | |
output = sold_product_percent(session) | |
# Assert | |
assert type(output) == dict | |
assert 99 < sum(output.values()) < 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment