def test_build_from_key(self): entity_key = EntityKeyFactory().build( kind="Project", name=1, ) project_key = ProjectKey.build_from_key(key=entity_key) assert entity_key != project_key assert entity_key.key_literal() == project_key.key_literal()
def _to_domain_entity(self, doc: DatastoreEntity) -> Project: model = ProjectDataModel.from_datastore_entity(doc=doc) return Project( key=ProjectKey.build_from_key(key=self.to_entity_key( datastore_key=model.key)), name=ProjectName(model.name), created_at=model.created_at, )
def test_project_key_property(self): task = Task( key=TaskKey.build_by_id(task_id=1), name=TaskName("Task Name"), project_key=None, finished_at=None, created_at=datetime.datetime(2019, 12, 1, 0, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2019, 12, 1, 0, 0, 0, tzinfo=datetime.timezone.utc), ) assert not task.project_key project = Project( key=ProjectKey.build_by_id(project_id=1), name=ProjectName("Project Name"), created_at=datetime.datetime(2019, 12, 1, 0, 0, 0, tzinfo=datetime.timezone.utc), ) finished_task = Task( key=TaskKey.build_by_id(task_id=1), name=TaskName("Task Name"), project_key=project.key, finished_at=None, created_at=datetime.datetime(2019, 12, 1, 0, 0, 0, tzinfo=datetime.timezone.utc), updated_at=datetime.datetime(2019, 12, 1, 0, 0, 0, tzinfo=datetime.timezone.utc), ) assert finished_task.key
def _to_domain_entity(self, doc: datastore.Entity) -> Task: return Task( key=TaskKey.build_from_key(key=doc.key), name=TaskName(doc["name"]), project_key=ProjectKey.build_from_key(key=doc["project_key"]) if doc["project_key"] is not None else doc["project_key"], finished_at=doc["finished_at"], created_at=doc["created_at"], updated_at=doc["update_at"], )
def execute(self, project_name: str) -> Project: now = datetime.datetime.utcnow().astimezone(tz=datetime.timezone.utc) project = Project( key=ProjectKey.build_for_new(), name=ProjectName(project_name), created_at=now, ) self._project_repository.save(project=project) return project
def _to_domain_entity(self, doc: DatastoreEntity) -> Task: model = TaskDataModel.from_datastore_entity(doc=doc) return Task( key=TaskKey.build_from_key(key=self.to_entity_key(datastore_key=model.key)), name=TaskName(model.name), project_key=ProjectKey.build_from_key(key=self.to_entity_key(datastore_key=model.project_key)) if model.project_key is not None else model.project_key, finished_at=model.finished_at, created_at=model.created_at, updated_at=model.update_at, )
def test_build_failure(self): with pytest.raises(expected_exception=TypeValidationError): Project( key=ProjectKey.build_by_id(project_id=1), name='project name', created_at=datetime.datetime(2019, 12, 1, 0, 0, 0, tzinfo=datetime.timezone.utc), )
def test_build_successful(self): project = Project( key=ProjectKey.build_by_id(project_id=1), name=ProjectName("Project Name"), created_at=datetime.datetime(2019, 12, 1, 0, 0, 0, tzinfo=datetime.timezone.utc), ) assert isinstance(project, Project)
def execute(self, key: TaskKey, project_id: str) -> "Task": task = self._task_repository.fetch(key=key) if project_id != "None": project_key = ProjectKey.build_by_id(int(project_id)) else: project_key = None modified_task = task.with_project_key(project_key=project_key) self._task_repository.save(task=modified_task) return modified_task
def test_save(self): query = datastore_client.query(kind=self.KIND) query.keys_only() datastore_client.delete_multi( keys=[entity.key for entity in query.fetch()]) assert len(list(query.fetch())) == 0 project = Project( key=ProjectKey.build_by_id(project_id=123), name=ProjectName("Project Name"), created_at=datetime.datetime(2019, 12, 1, tzinfo=datetime.timezone.utc), ) self.repository.save(project=project) assert len(list(query.fetch())) == 1
def test_equal(self): key1 = ProjectKey.build_by_id(project_id=1) key2 = ProjectKey.build_by_id(project_id=1) assert key1 == key2 assert key1.project_id == 1
def _to_domain_entity(self, doc: datastore.Entity) -> Project: return Project( key=ProjectKey.build_from_key(key=doc.key), name=ProjectName(doc["name"]), created_at=doc["created_at"], )