def test_get_by_id(self): delivery_request = DeliveryRequestGetter.get_by_id( u'DLpVc0QmbOHzfDo24Hpp') expected = DeliveryRequest( uid='DLpVc0QmbOHzfDo24Hpp', item='Xbox controller', description='I AM USED FOR TESTS. DO NOT REMOVE', origin=Location(name="Odenvägen 1, SE-194 63 Odenslunda, Sweden", latitude=59.51224, longitude=17.93536).to_dict(), destination=Location(name="Rolsmo 1, SE-360 24 Linneryd, Sweden", latitude=56.64989, longitude=15.16624).to_dict(), reward=123, weight=0, fragile=False, status=Status.AVAILABLE, money_lock=23, owner=MinifiedUser( mail="*****@*****.**", name= 'Travis CI Account - DON\'T DELETE OR YOULL BREAK THE ENTIRE CI WORKFLOW/UNITTESTS REEEEEEEEEEEEEEEEEEEEEEEEE', phonenumber='0', uid="xUQeyplJshTzco4vyHHVoytT3FD2").to_dict(), assistant=dict(), image_path='') self.assertDictEqual.__self__.maxDiff = None self.assertDictEqual(delivery_request.to_dict(), expected.to_dict())
def _update_content(self, collection_snapshot): """Fetch all deliveries the current owner has accepted""" delivery_requests = [] for doc in collection_snapshot: data = doc.to_dict() data['uid'] = doc.id data['status'] = Status(data['status']) delivery_requests.append(DeliveryRequest(**data)) # Fill delivery list self.ids.my_deliveries.clear_widgets() no_content = True for req in delivery_requests: if req.status == Status.DELIVERED: continue no_content = False self.ids.my_deliveries.add_widget( ListItem(req, self._transition_to_detail_view)) if self.no_content_label is not None: self.ids.content.remove_widget(self.no_content_label) # Add no content label if no content is shown if no_content: self.no_content_label = MDLabel( id="no_content_label", text="""You currently do not have any packages to deliver.\n Accept deliveries by searching for them. """, size_hint_y=9, halign="center", font_style='Subtitle1') self.ids.content.add_widget(self.no_content_label) self.content = self.ids.content
def test_locking_money(self): user = UserGetter.get_by_id('xUQeyplJshTzco4vyHHVoytT3FD2') request = DeliveryRequest( "id", "item", "description\ntext", origin=Location(name="Odenvägen 1, SE-194 63 Odenslunda, Sweden", latitude=59.51224, longitude=17.93536).to_dict(), destination=Location(name="Rolsmo 1, SE-360 24 Linneryd, Sweden", latitude=56.64989, longitude=15.16624).to_dict(), reward=100, weight=2, fragile=True, status=Status.AVAILABLE, money_lock=0, owner=user.to_minified().to_dict(), assistant=MinifiedUser("", "", "", "").to_dict(), image_path="") # ensure enough capital user.deposit(100) user_balance = user.balance self.assertEqual(user.balance, user_balance) user.lock_delivery_amount(request) self.assertEqual(user.balance, user_balance - 100)
def _update_content(self, collection_snapshot): """Fetch my posted deliveries""" self.content = self.ids.content delivery_requests = [] for doc in collection_snapshot: data = doc.to_dict() data['uid'] = doc.id data['status'] = Status(data['status']) if data['status'] == Status.CANCELLED_BY_OWNER: continue delivery_requests.append(DeliveryRequest(**data)) # Fill delivery list self.ids.my_requests.clear_widgets() no_content = True for req in delivery_requests: no_content = False self.ids.my_requests.add_widget( MyPostedRequest(req, self._transition_to_detail_view)) if self.no_content_label is not None: self.ids.content.remove_widget(self.no_content_label) # Add no content label if no content is shown if no_content: self.no_content_label = MDLabel( id="no_content_label", text="""You currently do not have any posted packages.\n Request deliveries with the package button down below. """, size_hint_y=9, halign="center", font_style='Subtitle1') self.ids.content.add_widget(self.no_content_label)
def get_by_id(delivery_request_id: Text) -> DeliveryRequest: """ Get a specific DeliveryRequest from Firestore :param delivery_request_id: The id of the delivery request. """ data = Firestore.get_raw('packages').document( delivery_request_id).get().to_dict() data['uid'] = delivery_request_id data['status'] = Status(data['status']) return DeliveryRequest(**data)
def upload(request: DeliveryRequest): """ Upload a delivery request to Firebase. :param request: The delivery request to upload. :type request: DeliveryRequest """ request_dict = request.to_dict() with Firestore.batch("packages") as batch: batch.create_with_random_id(request_dict)
def test_query(self): user = UserGetter.get_by_id('xUQeyplJshTzco4vyHHVoytT3FD2') delivery_requests = DeliveryRequestGetter.query( u'owner.uid', u'==', u'xUQeyplJshTzco4vyHHVoytT3FD2') self.assertGreaterEqual(len(delivery_requests), 1, msg="Expected at least 1 match.") # Clean up DRs for dr in delivery_requests: if not dr.uid == 'DLpVc0QmbOHzfDo24Hpp': with Firestore.batch('packages') as batch: batch.delete(dr.uid) delivery_requests = DeliveryRequestGetter.query( u'owner.uid', u'==', u'xUQeyplJshTzco4vyHHVoytT3FD2') self.assertEqual(len(delivery_requests), 1, msg="Expected 1 match.") expected = DeliveryRequest( uid='DLpVc0QmbOHzfDo24Hpp', item='Xbox controller', description='I AM USED FOR TESTS. DO NOT REMOVE', origin=Location("Odenvägen 1, SE-194 63 Odenslunda, Sweden", latitude=59.51224, longitude=17.93536).to_dict(), destination=Location("Rolsmo 1, SE-360 24 Linneryd, Sweden", latitude=56.64989, longitude=15.16624).to_dict(), reward=123, weight=0, fragile=False, status=Status.AVAILABLE, money_lock=23, owner=user.to_minified().to_dict(), assistant=dict(), image_path='') self.assertDictEqual.__self__.maxDiff = None self.assertDictEqual(delivery_requests[0].to_dict(), expected.to_dict())
def _on_snapshot_user_deliveries(collection_snapshot, _, __): """ The callback for when the users deliveries are updated in Firebase :param collection_snapshot: The deliveries for the user """ if UserMeGetter.user is None: return delivery_requests = [] for doc in collection_snapshot: data = doc.to_dict() data['uid'] = doc.id data['status'] = Status(data['status']) delivery_requests.append(DeliveryRequest(**data)) UserMeGetter.user.update(deliveries=delivery_requests)
def _submit_request(self): payment_amount: int = int(self.ids.payment_amount.text) user: User = UserMeGetter.user if not self._verify_entries(): return origin = self.from_suggester.currently_used_suggestion destination = self.to_suggester.currently_used_suggestion # Both origin and destination must be well-defined if not origin or not destination: return if self.photo_path: firestore_image_path = Bucket.upload(self.photo_path) else: firestore_image_path = "" request = DeliveryRequest( item=self.ids.package_name.text, description=self.ids.description_text.text, origin=origin.to_dict(), destination=destination.to_dict(), reward=payment_amount, weight=self.weight, fragile=self.ids.fragile_bool.active, status=Status.AVAILABLE, money_lock=int(self.ids.money_lock_amount.text), owner=UserMeGetter.user.to_minified().to_dict(), assistant={}, uid='', image_path=firestore_image_path) print(request) user.lock_delivery_amount(request) DeliveryRequestUploader.upload(request) # Hide the sliding popup. We are placed in a SlidingPopupContent widget, # whose parent is the SlidingPopup we need. self.parent.parent.hide() self._clear_form()
def query(field_path: Text, op_str: Text, value: Any) -> List[DeliveryRequest]: """ Get a list of DeliveryRequest objects by querying Firestore :param field_path: The path being filtered on. :param op_str: The comparison operation being made :param value: The value which each entry is being compared to. """ docs = Firestore.get_raw('packages').where(field_path, op_str, value).stream() delivery_requests = [] for doc in docs: data = doc.to_dict() data['uid'] = doc.id data['status'] = Status(data['status']) delivery_requests.append(DeliveryRequest(**data)) return delivery_requests
def create_delivery_request(): origin = Location(name="Odenvägen 1, SE-194 63 Odenslunda, Sweden", latitude=59.51224, longitude=17.93536).to_dict() destination = Location(name="Rolsmo 1, SE-360 24 Linneryd, Sweden", latitude=56.64989, longitude=15.16624).to_dict() return DeliveryRequest( "TEST", "item", "This a test, feel free to remove.", origin, destination, reward=10, weight=2, fragile=True, status=Status.AVAILABLE, money_lock=0, owner=MinifiedUser("", "", "", "xUQeyplJshTzco4vyHHVoytT3FD2").to_dict(), assistant=MinifiedUser("", "", "", "").to_dict(), image_path="")