def test_create(self): ss = blockstore.StreamStore(bs=self.bs, ls=self.ls, entity_set=self.cdef['Streams']) s1 = ss.new_stream("text/plain") self.assertTrue(isinstance(s1, edm.Entity)) self.assertTrue(s1.exists) try: self.assertTrue(s1.key() is not None) except KeyError: self.fail("stream entity has not key") self.assertTrue(s1['mimetype'].value == "text/plain") s2 = ss.new_stream( params.MediaType('text', 'plain', {'charset': ('charset', 'utf-8')})) self.assertTrue(isinstance(s2, edm.Entity)) self.assertTrue(s2['mimetype'].value == "text/plain; charset=utf-8") skey1 = s1.key() skey2 = s2.key() s1 = ss.get_stream(skey1) self.assertTrue(isinstance(s1, edm.Entity)) for i in range3(10): try: ss.get_stream(i) self.assertTrue(i == skey1 or i == skey2) except KeyError: self.assertFalse(i == skey1 or i == skey2)
def new_stream(self, mimetype=params.MediaType('application', 'octet-stream'), created=None): """Creates a new stream in the store. mimetype A :py:class:`~pyslet.http.params.MediaType` object Returns a stream entity which is an :py:class:`~pyslet.odata2.csdl.Entity` instance. The stream is identified by the stream entity's key which you can store elsewhere as a reference and pass to :py:meth:`get_stream` to retrieve the stream again later.""" with self.stream_set.OpenCollection() as streams: stream = streams.new_entity() if not isinstance(mimetype, params.MediaType): mimetype = params.MediaType.from_str(mimetype) stream['mimetype'].set_from_value(str(mimetype)) now = TimePoint.from_now_utc() stream['size'].set_from_value(0) if created is None: stream['created'].set_from_value(now) stream['modified'].set_from_value(now) else: created = created.shift_zone(0) stream['created'].set_from_value(created) stream['modified'].set_from_value(created) stream['md5'].set_from_value(hashlib.md5().digest()) streams.insert_entity(stream) return stream
def test_content_type(self): req = Request() mtype = params.MediaType('application', 'octet-stream', {'charset': ['Charset', 'utf8']}) req.set_content_type(mtype) self.assertTrue( req.get_header('Content-type') == 'application/octet-stream; Charset=utf8') self.assertTrue(isinstance(req.get_content_type(), params.MediaType))