File: test_3300_soda_database.py

package info (click to toggle)
python-oracledb 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,224 kB
  • sloc: python: 17,637; sql: 1,819; makefile: 41
file content (129 lines) | stat: -rw-r--r-- 5,670 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#------------------------------------------------------------------------------
# Copyright (c) 2020, 2022, Oracle and/or its affiliates.
#
# This software is dual-licensed to you under the Universal Permissive License
# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
# 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
# either license.
#
# If you elect to accept the software under the Apache License, Version 2.0,
# the following applies:
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#------------------------------------------------------------------------------

"""
3300 - Module for testing Simple Oracle Document Access (SODA) Database
"""

import json
import unittest

import oracledb
import test_env

@unittest.skipIf(test_env.skip_soda_tests(),
                 "unsupported client/server combination")
class TestCase(test_env.BaseTestCase):

    def __drop_existing_collections(self, soda_db):
        for name in soda_db.getCollectionNames():
            soda_db.openCollection(name).drop()

    def __verify_doc(self, doc, raw_content, str_content=None, content=None,
                     key=None, media_type='application/json'):
        self.assertEqual(doc.getContentAsBytes(), raw_content)
        if str_content is not None:
            self.assertEqual(doc.getContentAsString(), str_content)
        if content is not None:
            self.assertEqual(doc.getContent(), content)
        self.assertEqual(doc.key, key)
        self.assertEqual(doc.mediaType, media_type)

    def test_3300_create_document_with_json(self):
        "3300 - test creating documents with JSON data"
        soda_db = self.connection.getSodaDatabase()
        val = {"testKey1": "testValue1", "testKey2": "testValue2"}
        str_val = json.dumps(val)
        bytes_val = str_val.encode()
        key = "MyKey"
        media_type = "text/plain"
        doc = soda_db.createDocument(val)
        self.__verify_doc(doc, bytes_val, str_val, val)
        doc = soda_db.createDocument(str_val, key)
        self.__verify_doc(doc, bytes_val, str_val, val, key)
        doc = soda_db.createDocument(bytes_val, key, media_type)
        self.__verify_doc(doc, bytes_val, str_val, val, key, media_type)

    def test_3301_create_document_with_raw(self):
        "3301 - test creating documents with raw data"
        soda_db = self.connection.getSodaDatabase()
        val = b"<html/>"
        key = "MyRawKey"
        media_type = "text/html"
        doc = soda_db.createDocument(val)
        self.__verify_doc(doc, val)
        doc = soda_db.createDocument(val, key)
        self.__verify_doc(doc, val, key=key)
        doc = soda_db.createDocument(val, key, media_type)
        self.__verify_doc(doc, val, key=key, media_type=media_type)

    def test_3302_get_collection_names(self):
        "3302 - test getting collection names from the database"
        soda_db = self.connection.getSodaDatabase()
        self.__drop_existing_collections(soda_db)
        self.assertEqual(soda_db.getCollectionNames(), [])
        names = ["zCol", "dCol", "sCol", "aCol", "gCol"]
        sorted_names = list(sorted(names))
        for name in names:
            soda_db.createCollection(name)
        self.assertEqual(soda_db.getCollectionNames(), sorted_names)
        self.assertEqual(soda_db.getCollectionNames(limit=2), sorted_names[:2])
        self.assertEqual(soda_db.getCollectionNames("a"), sorted_names)
        self.assertEqual(soda_db.getCollectionNames("C"), sorted_names)
        self.assertEqual(soda_db.getCollectionNames("b", limit=3),
                                                    sorted_names[1:4])
        self.assertEqual(soda_db.getCollectionNames("z"), sorted_names[-1:])

    def test_3303_open_collection(self):
        "3303 - test opening a collection"
        soda_db = self.connection.getSodaDatabase()
        self.__drop_existing_collections(soda_db)
        coll = soda_db.openCollection("CollectionThatDoesNotExist")
        self.assertEqual(coll, None)
        created_coll = soda_db.createCollection("TestOpenCollection")
        coll = soda_db.openCollection(created_coll.name)
        self.assertEqual(coll.name, created_coll.name)
        coll.drop()

    def test_3304_repr(self):
        "3304 - test SodaDatabase representation"
        con1 = self.connection
        con2 = test_env.get_connection()
        soda_db1 = self.connection.getSodaDatabase()
        soda_db2 = con1.getSodaDatabase()
        soda_db3 = con2.getSodaDatabase()
        self.assertEqual(str(soda_db1), str(soda_db2))
        self.assertEqual(str(soda_db2), str(soda_db3))

    def test_3305_negative(self):
        "3305 - test negative cases for SODA database methods"
        soda_db = self.connection.getSodaDatabase()
        self.assertRaises(TypeError, soda_db.createCollection)
        self.assertRaises(TypeError, soda_db.createCollection, 1)
        self.assertRaisesRegex(oracledb.DatabaseError, "^ORA-40658:",
                               soda_db.createCollection, None)
        self.assertRaises(TypeError, soda_db.getCollectionNames, 1)

if __name__ == "__main__":
    test_env.run_test_cases()