Skip to content

Commit

Permalink
add service class and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
socraticDevBlog committed Jun 15, 2023
1 parent 2bccf93 commit db3af7a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 11 deletions.
38 changes: 33 additions & 5 deletions apis/pastebin/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,29 @@


class Paste:
"""
Paste
a Paste is the model used to capture all relevant informations about
stuff being posted to the Pastebin
id: six(6) first characters of the Pasted content hashed with MD5
paste: the content being saved to the Pastebin
badge(optionnal): some kind of user submitted identifier used to regroup
pastes together. in order to avoid a badge getting usurped by another user,
the value is hashed with same function as the 'id' field
insert_timestamp: Unix timestamp
"""

def __init__(self, paste, badge: str = ""):
self.id = self._hash(paste)
self.paste = paste
self.badge = badge
self.badge = self._hash(badge)
self.insert_timestamp = time.mktime(datetime.datetime.now().timetuple())

def _hash(self, val):
Expand All @@ -23,7 +42,18 @@ def _hash(self, val):


class DB:
def __init__(self, db_path: str = "default.db") -> None:
"""
DB
Data Access Layer:
- create Database and tables
- execute SQL requests to database
"""

def __init__(self, db_path: str = "pastebin.db") -> None:
self._database = db_path
self._conn = self._create_connection(db_path)
self._create_pastebin_table()
Expand All @@ -36,7 +66,6 @@ def insert_paste(self, paste: Paste) -> str:
sql = """ INSERT INTO pastebin(id,paste,insert_timestamp,badge)
VALUES(?,?,?,?) """
try:
self._conn.cursor()
self._conn.cursor().execute(
sql, (paste.id, paste.paste, paste.insert_timestamp, paste.badge)
)
Expand All @@ -63,7 +92,6 @@ def _create_pastebin_table(self) -> None:
logging.error("Error creating sqlite3 pastebin table", exc_info=True)

def _create_connection(self, db_file) -> Connection:
"""returns a database connection to a SQLite database"""
conn = None
try:
conn = sqlite3.connect(db_file)
Expand All @@ -78,6 +106,6 @@ def _create_connection(self, db_file) -> Connection:
#
# if __name__ == "__main__":
# db = DB(r"pastebin.db")
# p = Paste(paste="zoom zoom", badge="mon ami")
# p = Paste(paste="dadabababa", badge="mon ami")
# r = db.insert_paste(paste=p)
# print(r)
14 changes: 9 additions & 5 deletions apis/pastebin/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ curl localhost:8000

**Server:** Python, FastApi


## License

[MIT](https://choosealicense.com/licenses/mit/)
Expand All @@ -62,7 +61,14 @@ Please adhere to this project's `code of conduct`.
If you have any feedback, please reach out to us at IRC Rizon network:
#dailybuild

-------------
## run unit tests locally

1. make sure you have installed all dependencies running `pip install -r requirements.txt`
2. run command: `pytest`
3. expect some green in your terminal; all test should pass

---

everything below this line is templated stuff

## API Reference (tbd)
Expand Down Expand Up @@ -91,8 +97,6 @@ everything below this line is templated stuff

Takes two numbers and returns the sum.



## FAQ

#### Question 1
Expand All @@ -109,4 +113,4 @@ To run this project, you will need to add the following environment variables to

`API_KEY`

`ANOTHER_API_KEY`
`ANOTHER_API_KEY`
3 changes: 2 additions & 1 deletion apis/pastebin/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
black
fastapi
uvicorn[standard]
db-sqlite3
db-sqlite3
pytest
26 changes: 26 additions & 0 deletions apis/pastebin/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from db import DB, Paste
from typing import Any


class Service:
"""
Service
'business layer' of the Pastebin API
Will interact with a requester (API routes) and the Database
unit test coverage should be around 100%
"""

def __init__(self, db: DB = None):
self._db = db if db is not None else DB()

def save(self, content: Any, badge: str = "") -> str:
p = Paste(badge=badge, paste=content)
return self._db.insert_paste(paste=p)

def retrieve_by_id(self, id: str):
...
18 changes: 18 additions & 0 deletions apis/pastebin/test_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest
from service import Service
from db import Paste

PASTE_CONTENT = "a sample paste"
HASHED_CONTENT_FIRST_SIX_CHARS = "b054cd"


class Mock_db:
def insert_paste(self, paste: Paste) -> str:
return HASHED_CONTENT_FIRST_SIX_CHARS


def test_simple_insert():
svc = Service(db=Mock_db())

r = svc.save(content=PASTE_CONTENT)
assert r == HASHED_CONTENT_FIRST_SIX_CHARS

0 comments on commit db3af7a

Please sign in to comment.