async def from_doc(doc: Document) -> "Product": return Product( id=doc.doc_id, title=doc.get("title"), price=doc.get("price"), inventory_count=doc.get("inventory_count"), )
def __init__(self, db): self.db = db self.table = db.table('metadata') # initializes metadata table in database if it hasn't been created yet if not self.table.contains(doc_id=1): self.table.insert(Document({'last_status_parsed': 1}, doc_id=1))
def add(self, status): # ensures tweet hasn't been added before if self.tweets.contains(doc_id=status.id): print(f'Status #{status.id} already in database') return parent_id = status.in_reply_to_status_id if parent_id: parent_id = str(parent_id) # recording data to store in database tweet = { 'text': status.full_text, # text of the tweet 'user_full_name': status.user.name, # actual name of poster 'user_screen_name': status.user.screen_name, # user name of poster 'user_id': str(status.user.id), # user id of poster 'created': str(status.created_at), # date and time created 'parent_id': parent_id, # id of tweet replying 'child_ids': [], 'parsed': False, # whether tweet has been parsed 'thread_id': 0 # corresponding id in threads table } # inserts data into database under the tweet's id self.tweets.insert( Document( tweet, doc_id=status.id ) )
def __init__(self, db): self.db = db self.tweets = db.table('tweets') self.threads = db.table('threads') if not self.threads.contains(doc_id=0): self.threads.insert(Document( { 'num_threads': 0 }, doc_id=0))
def __outputRow__(self, doc: Document): """ returns a dict of the id and all the keys within the document """ if doc is None: raise DatabaseException('No row has been found') if isinstance(doc, Document) is False: raise DatabaseException( 'the object passed must be a `tinydb.database.Document`.') rDict = {'id': doc.doc_id} for key in doc.keys(): rDict[key] = doc[key] return rDict
def _new_document(self, key, val): # Don't convert the key to a number here! return Document(val, key)
async def from_doc(doc: Document) -> "User": user = User(id=doc.doc_id, username=doc.get("username")) user.password_hash = doc.get("password_hash") return user
async def from_doc(doc: Document) -> "CartItem": return CartItem( product=await Product.from_doc(doc.get("product")), amount=doc.get("amount") )
def _new_document(self, key, val): return Document(val, key)
def _new_document(self, key: str, value: dict) -> Document: return Document(value, key)
def test_get_flat(self): self.db_mock.return_value.get.return_value = Document(asdict(self.flat), 'foo') actual = Flats()._get_flat(self.flat.id) self.db_mock.return_value.get.assert_called_once_with(Query().id == self.flat.id) self.assertEqual(self.flat, actual)