|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import unittest |
| 4 | +import pydocumentdb.document_client as document_client |
| 5 | +import pydocumentdb.documents as documents |
| 6 | +import test.test_config as test_config |
| 7 | + |
| 8 | +class EncodingTest(unittest.TestCase): |
| 9 | + """Test to ensure escaping of non-ascii characters from partition key""" |
| 10 | + |
| 11 | + host = test_config._test_config.host |
| 12 | + masterKey = test_config._test_config.masterKey |
| 13 | + testDbName = 'testDatabase' |
| 14 | + testCollectionName = 'testCollection' |
| 15 | + |
| 16 | + @classmethod |
| 17 | + def cleanUpTestDatabase(cls): |
| 18 | + global client |
| 19 | + client = document_client.DocumentClient(cls.host, |
| 20 | + {'masterKey': cls.masterKey}) |
| 21 | + query_iterable = client.QueryDatabases('SELECT * FROM root r WHERE r.id=\'' + cls.testDbName + '\'') |
| 22 | + it = iter(query_iterable) |
| 23 | + |
| 24 | + test_db = next(it, None) |
| 25 | + if test_db is not None: |
| 26 | + client.DeleteDatabase(test_db['_self']) |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def tearDownClass(cls): |
| 30 | + EncodingTest.cleanUpTestDatabase() |
| 31 | + |
| 32 | + def setUp(self): |
| 33 | + global created_collection |
| 34 | + |
| 35 | + EncodingTest.cleanUpTestDatabase() |
| 36 | + created_db = client.CreateDatabase({ 'id': self.testDbName }) |
| 37 | + |
| 38 | + collection_definition = { 'id': self.testCollectionName, 'partitionKey': {'paths': ['/pk'],'kind': 'Hash'} } |
| 39 | + collection_options = { 'offerThroughput': 10100 } |
| 40 | + created_collection = client.CreateCollection(created_db['_self'], collection_definition, collection_options) |
| 41 | + |
| 42 | + def test_unicode_characters_in_partition_key (self): |
| 43 | + test_string = u'€€ کلید پارتیشن विभाजन कुंजी 123' |
| 44 | + document_definition = {'pk': test_string, 'id':'myid'} |
| 45 | + created_doc = client.CreateDocument(created_collection['_self'], document_definition) |
| 46 | + |
| 47 | + read_options = {'partitionKey': test_string } |
| 48 | + read_doc = client.ReadDocument(created_doc['_self'], read_options) |
| 49 | + self.assertEqual(read_doc['pk'], test_string) |
| 50 | + |
| 51 | + def test_create_document_with_line_separator_para_seperator_next_line_unicodes (self): |
| 52 | + |
| 53 | + test_string = u'Line Separator (
) & Paragraph Separator (
) & Next Line (
) & نیمفاصله' |
| 54 | + document_definition = {'pk': 'pk', 'id':'myid', 'unicode_content':test_string } |
| 55 | + created_doc = client.CreateDocument(created_collection['_self'], document_definition) |
| 56 | + |
| 57 | + read_options = {'partitionKey': 'pk' } |
| 58 | + read_doc = client.ReadDocument(created_doc['_self'], read_options) |
| 59 | + self.assertEqual(read_doc['unicode_content'], test_string) |
| 60 | + |
| 61 | + def test_create_stored_procedure_with_line_separator_para_seperator_next_line_unicodes (self): |
| 62 | + |
| 63 | + test_string = 'Line Separator (
) & Paragraph Separator (
) & Next Line (
) & نیمفاصله' |
| 64 | + |
| 65 | + test_string_unicode = u'Line Separator (
) & Paragraph Separator (
) & Next Line (
) & نیمفاصله' |
| 66 | + |
| 67 | + stored_proc_definition = {'id':'myid', 'body': test_string} |
| 68 | + created_sp = client.CreateStoredProcedure(created_collection['_self'], stored_proc_definition) |
| 69 | + |
| 70 | + read_sp = client.ReadStoredProcedure(created_sp['_self'], dict()) |
| 71 | + self.assertEqual(read_sp['body'], test_string_unicode) |
| 72 | +if __name__ == '__main__': |
| 73 | + unittest.main() |
0 commit comments