1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# ~*~ coding: utf-8 ~*~ |
3
|
|
|
#- |
4
|
|
|
# OSMAlchemy - OpenStreetMap to SQLAlchemy bridge |
5
|
|
|
# Copyright (c) 2016 Dominik George <[email protected]> |
6
|
|
|
# |
7
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
# in the Software without restriction, including without limitation the rights |
10
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
# furnished to do so, subject to the following conditions: |
13
|
|
|
# |
14
|
|
|
# The above copyright notice and this permission notice shall be included in all |
15
|
|
|
# copies or substantial portions of the Software. |
16
|
|
|
# |
17
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
# SOFTWARE. |
24
|
|
|
# |
25
|
|
|
# Alternatively, you are free to use OSMAlchemy under Simplified BSD, The |
26
|
|
|
# MirOS Licence, GPL-2+, LGPL-2.1+, AGPL-3+ or the same terms as Python |
27
|
|
|
# itself. |
28
|
|
|
|
29
|
|
|
""" Tests concerning the OSMAlchemy object. """ |
30
|
|
|
|
31
|
|
|
# Standard unit testing framework |
32
|
|
|
import unittest |
33
|
|
|
|
34
|
|
|
# Module to be tested |
35
|
|
|
from osmalchemy import OSMAlchemy |
36
|
|
|
|
37
|
|
|
# SQLAlchemy for working with model and data |
38
|
|
|
from sqlalchemy import create_engine |
39
|
|
|
from sqlalchemy.ext.declarative import declarative_base |
40
|
|
|
from sqlalchemy.orm import sessionmaker, scoped_session |
41
|
|
|
|
42
|
|
|
# Flask for testing integration with Flask |
43
|
|
|
from flask import Flask |
44
|
|
|
from flask_sqlalchemy import SQLAlchemy as FlaskSQLAlchemy |
45
|
|
|
|
46
|
|
|
class OSMAlchemyAPITests(unittest.TestCase): |
47
|
|
|
def test_instantiate_with_engine(self): |
48
|
|
|
engine = create_engine("sqlite:///:memory:") |
49
|
|
|
osmalchemy = OSMAlchemy(engine) |
50
|
|
|
self.assertIsInstance(osmalchemy, OSMAlchemy) |
51
|
|
|
self.assertIs(osmalchemy.engine, engine) |
52
|
|
|
engine.dispose() |
53
|
|
|
|
54
|
|
|
def test_instantiate_with_engine_and_base(self): |
55
|
|
|
engine = create_engine("sqlite:///:memory:") |
56
|
|
|
base = declarative_base(bind=engine) |
57
|
|
|
osmalchemy = OSMAlchemy((engine, base)) |
58
|
|
|
self.assertIsInstance(osmalchemy, OSMAlchemy) |
59
|
|
|
self.assertIs(osmalchemy.engine, engine) |
60
|
|
|
self.assertIs(osmalchemy.base, base) |
61
|
|
|
engine.dispose() |
62
|
|
|
|
63
|
|
|
def test_instantiate_with_engine_and_base_and_session(self): |
64
|
|
|
engine = create_engine("sqlite:///:memory:") |
65
|
|
|
base = declarative_base(bind=engine) |
66
|
|
|
session = scoped_session(sessionmaker(bind=engine)) |
67
|
|
|
osmalchemy = OSMAlchemy((engine, base, session)) |
68
|
|
|
self.assertIsInstance(osmalchemy, OSMAlchemy) |
69
|
|
|
self.assertIs(osmalchemy.engine, engine) |
70
|
|
|
self.assertIs(osmalchemy.base, base) |
71
|
|
|
self.assertIs(osmalchemy.session, session) |
72
|
|
|
engine.dispose() |
73
|
|
|
|
74
|
|
|
def test_instantiate_with_flask_sqlalchemy(self): |
75
|
|
|
app = Flask("test") |
76
|
|
|
db = FlaskSQLAlchemy(app) |
77
|
|
|
osmalchemy = OSMAlchemy(db) |
78
|
|
|
self.assertIsInstance(osmalchemy, OSMAlchemy) |
79
|
|
|
self.assertIs(osmalchemy.engine, db.engine) |
80
|
|
|
self.assertIs(osmalchemy.base, db.Model) |
81
|
|
|
self.assertIs(osmalchemy.session, db.session) |
82
|
|
|
|
83
|
|
|
# Make runnable as standalone script |
84
|
|
|
if __name__ == "__main__": |
85
|
|
|
unittest.main() |
86
|
|
|
|