Last active
October 29, 2019 18:54
-
-
Save invalidusrname/9a5ba7c883bd5ed336fd34d5392ed755 to your computer and use it in GitHub Desktop.
Entity object
This file contains hidden or 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
# . | |
# ├── lib | |
# │ ├── __init__.py | |
# │ ├── entity | |
# │ │ ├── __init__.py | |
# │ │ ├── entity.py | |
# │ │ └── job_post_entity.py | |
# │ └── repository | |
# │ ├── __init__.py | |
# │ ├── job_post_repository.py | |
# │ └── repository.py | |
# ├── test.py | |
# └── tests | |
# ├── __init__.py | |
# └── lib | |
# ├── __init__.py | |
# └── entity | |
# └── test_job_post_entity.py | |
# ---------------------------- # | |
# lib/entity/entity.py | |
# ---------------------------- # | |
# from .repository import Repository | |
# from .entity import Entity | |
# from ..entity.job_post_entity import JobPostEntity | |
class Entity(object): | |
FIELDS = [] | |
def __init__(self, data={}): | |
self.attributes = {} | |
for key in self.FIELDS: | |
self.attributes[key] = None | |
for key in data: | |
if key in self.FIELDS: | |
self.attributes[key] = data[key] | |
def __getattr__(self, name): | |
if name in self.attributes: | |
return self.attributes[name] | |
else: | |
raise AttributeError(name) | |
# ---------------------------- # | |
# lib/entity/job_post_entity.py | |
# ---------------------------- # | |
class JobPostEntity(Entity): | |
FIELDS = [ | |
'description', | |
'job_type', | |
] | |
# ---------------------------- # | |
# lib/repository/repository.py | |
# ---------------------------- # | |
class Repository: | |
def __init__(self, database): | |
self.database = database | |
def add(self, entity): | |
raise NotImplementedError | |
def remove(self, entity): | |
raise NotImplementedError | |
def get(self, id): | |
raise NotImplementedError | |
def find(self): | |
raise NotImplementedError | |
def exists(self, id): | |
raise NotImplementedError | |
# ---------------------------- # | |
# lib/repository/job_post_repository.py | |
# ---------------------------- # | |
class JobPostRepository(Repository): | |
def get(self, id): | |
sql = "SELECT * FROM job_posts WHERE id = %s" | |
self.database.execute(sql, (id, )) | |
data = self.database.fetch_as_dict() | |
if data: | |
attributes = {} | |
attributes['description'] = data['description'] | |
attributes['job_type'] = data['job_type'] | |
return JobPostEntity(attributes) | |
def update_job_post(self, job_post): | |
pass | |
def insert_job_post(self, job_post): | |
pass | |
def add(self, job_post): | |
if self.exists(job_post.id): | |
self.update_job_post(job_post) | |
else: | |
self.insert_job_post(job_post) | |
# ---------------------------- # | |
# tests/lib/entity/test_jobpost_entity.py | |
# ---------------------------- # | |
from unittest import TestCase | |
class TestJobPostEntity(TestCase): | |
def test_description(self): | |
entity = JobPostEntity(data = {"description": "Something Descriptive"}) | |
self.assertEqual("Something Descriptive", entity.description) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment