Skip to content

Commit

Permalink
Fixed context manager-related bug. Refactored unit tests to use the t…
Browse files Browse the repository at this point in the history
…opic store as a context manager.
  • Loading branch information
brettkromkamp committed Jun 16, 2017
1 parent a82c82c commit 8d87f03
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 82 deletions.
29 changes: 13 additions & 16 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ TopicDB from StoryTechnologies

TopicDB is a topic map-based graph library (using `PostgreSQL`_ for persistence).

.. image:: http://www.storytechnologies.com/wp-content/uploads/2016/12/topic-db-logo.png
.. image:: http://www.storytechnologies.com/wp-content/uploads/2017/06/topic-db-logo3.png

For a more in-depth introduction to topic maps, I recommend reading the excellent introductory
article on topic maps over at MSDN: `An Introduction to Topic Maps`_. With that being said, although
Expand Down Expand Up @@ -47,25 +47,22 @@ First-Time Use
TOPIC_MAP_IDENTIFIER = 1
# Instantiate and open topic store.
store = TopicStore("localhost", "username", "password")
store.open()
with TopicStore(username, password) as store:
# Create the topic map and bootstrap default topics.
store.set_topic_map(TOPIC_MAP_IDENTIFIER, "Topic Map Test")
# Create the topic map and bootstrap default topics.
store.set_topic_map(TOPIC_MAP_IDENTIFIER, "Topic Map Test")
topic1 = Topic(identifier='test-topic1',
base_name='Tópico de Prueba',
language=Language.SPA)
topic1 = Topic(identifier='test-topic1',
base_name='Tópico de Prueba',
language=Language.SPA)
# Persist topic to store.
if not store.topic_exists(TOPIC_MAP_IDENTIFIER, 'test-topic1'):
store.set_topic(TOPIC_MAP_IDENTIFIER, topic1)
# Persist topic to store.
if not store.topic_exists(TOPIC_MAP_IDENTIFIER, 'test-topic1'):
store.set_topic(TOPIC_MAP_IDENTIFIER, topic1)
# Retrieve topic from store (with the accompanying topic attributes).
topic2 = store.get_topic(TOPIC_MAP_IDENTIFIER, 'test-topic1',
resolve_attributes=RetrievalOption.RESOLVE_ATTRIBUTES)
store.close()
# Retrieve topic from store (with the accompanying topic attributes).
topic2 = store.get_topic(TOPIC_MAP_IDENTIFIER, 'test-topic1',
resolve_attributes=RetrievalOption.RESOLVE_ATTRIBUTES)
Tutorial
--------
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
psycopg2>=2.7.1
python-slugify>=1.2.4
pytest>=3.0.7
pytest>=3.1.2
9 changes: 3 additions & 6 deletions scripts/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
password = config['DATABASE']['Password']

# Instantiate and open topic store.
store = TopicStore(username, password)
store.open()
with TopicStore(username, password) as store:

store.set_topic_map(1, "The Doomsday Weapon", "A soldier has to infiltrate behind enemy lines to steal the plans for a secret doomsday weapon.")
store.set_topic_map(2, "An Unexpected Meeting", "Two people meet ever so briefly. A chance encounter that changes their lives forever.")

store.close()
store.set_topic_map(1, "The Doomsday Weapon", "A soldier has to infiltrate behind enemy lines to steal the plans for a secret doomsday weapon.")
store.set_topic_map(2, "An Unexpected Meeting", "Two people meet ever so briefly. A chance encounter that changes their lives forever.")
99 changes: 41 additions & 58 deletions tests/test_topicstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,15 @@ def test_topic():
language=Language.SPA)

# Instantiate and open topic store.
store = TopicStore(username, password)
store.open()
with TopicStore(username, password) as store:

# Persist topic to store.
if not store.topic_exists(TOPIC_MAP_IDENTIFIER, 'test-topic1'):
store.set_topic(TOPIC_MAP_IDENTIFIER, topic1)
# Persist topic to store.
if not store.topic_exists(TOPIC_MAP_IDENTIFIER, 'test-topic1'):
store.set_topic(TOPIC_MAP_IDENTIFIER, topic1)

# Retrieve topic from store.
topic2 = store.get_topic(TOPIC_MAP_IDENTIFIER, 'test-topic1',
resolve_attributes=RetrievalOption.RESOLVE_ATTRIBUTES)

store.close()
# Retrieve topic from store.
topic2 = store.get_topic(TOPIC_MAP_IDENTIFIER, 'test-topic1',
resolve_attributes=RetrievalOption.RESOLVE_ATTRIBUTES)

assert topic2.identifier == 'test-topic1'
assert topic2.instance_of == 'topic'
Expand All @@ -65,18 +62,15 @@ def test_occurrence():
language=Language.DEU)

# Instantiate and open topic store.
store = TopicStore(username, password)
store.open()

# Persist occurrence to store.
if not store.occurrence_exists(TOPIC_MAP_IDENTIFIER, 'test-occurrence1'):
store.set_occurrence(TOPIC_MAP_IDENTIFIER, occurrence1)
with TopicStore(username, password) as store:

# Retrieve occurrence from store.
occurrence2 = store.get_occurrence(TOPIC_MAP_IDENTIFIER, 'test-occurrence1',
resolve_attributes=RetrievalOption.RESOLVE_ATTRIBUTES)
# Persist occurrence to store.
if not store.occurrence_exists(TOPIC_MAP_IDENTIFIER, 'test-occurrence1'):
store.set_occurrence(TOPIC_MAP_IDENTIFIER, occurrence1)

store.close()
# Retrieve occurrence from store.
occurrence2 = store.get_occurrence(TOPIC_MAP_IDENTIFIER, 'test-occurrence1',
resolve_attributes=RetrievalOption.RESOLVE_ATTRIBUTES)

assert occurrence2.identifier == 'test-occurrence1'
assert occurrence2.topic_identifier == 'test-topic1'
Expand All @@ -90,15 +84,12 @@ def test_occurrence():

def test_topic_occurrences():
# Instantiate and open topic store.
store = TopicStore(username, password)
store.open()

# Retrieve topic from store.
topic2 = store.get_topic(TOPIC_MAP_IDENTIFIER, 'test-topic1',
resolve_attributes=RetrievalOption.RESOLVE_ATTRIBUTES,
resolve_occurrences=RetrievalOption.RESOLVE_OCCURRENCES)
with TopicStore(username, password) as store:

store.close()
# Retrieve topic from store.
topic2 = store.get_topic(TOPIC_MAP_IDENTIFIER, 'test-topic1',
resolve_attributes=RetrievalOption.RESOLVE_ATTRIBUTES,
resolve_occurrences=RetrievalOption.RESOLVE_OCCURRENCES)

assert topic2.identifier == 'test-topic1'
assert topic2.instance_of == 'topic'
Expand All @@ -125,19 +116,16 @@ def test_occurrence_resource_data():
resource_data=resource_data)

# Instantiate and open topic store.
store = TopicStore(username, password)
store.open()
with TopicStore(username, password) as store:

# Persist occurrence to store.
if not store.occurrence_exists(TOPIC_MAP_IDENTIFIER, 'test-occurrence2'):
store.set_occurrence(TOPIC_MAP_IDENTIFIER, occurrence1)
# Persist occurrence to store.
if not store.occurrence_exists(TOPIC_MAP_IDENTIFIER, 'test-occurrence2'):
store.set_occurrence(TOPIC_MAP_IDENTIFIER, occurrence1)

# Retrieve occurrence from store.
occurrence2 = store.get_occurrence(TOPIC_MAP_IDENTIFIER, 'test-occurrence2',
resolve_attributes=RetrievalOption.RESOLVE_ATTRIBUTES,
inline_resource_data=RetrievalOption.INLINE_RESOURCE_DATA)

store.close()
# Retrieve occurrence from store.
occurrence2 = store.get_occurrence(TOPIC_MAP_IDENTIFIER, 'test-occurrence2',
resolve_attributes=RetrievalOption.RESOLVE_ATTRIBUTES,
inline_resource_data=RetrievalOption.INLINE_RESOURCE_DATA)

# Converting the resource data from bytes to string.
assert occurrence2.resource_data.decode("utf-8") == '<p>This is some text with a <a href="https://www.google.com">test</a> link.</p>'
Expand All @@ -157,18 +145,16 @@ def test_association():
dest_topic_ref='test-topic2')

# Instantiate and open topic store.
store = TopicStore(username, password)
store.open()

# Persist association to store.
if not store.topic_exists(TOPIC_MAP_IDENTIFIER, 'test-association1'):
store.set_association(TOPIC_MAP_IDENTIFIER, association1)
with TopicStore(username, password) as store:

# Retrieve occurrence from store.
association2 = store.get_association(TOPIC_MAP_IDENTIFIER, 'test-association1',
resolve_attributes=RetrievalOption.RESOLVE_ATTRIBUTES)
# Associations are topics, as well (in TopicDB). For that reason, to check for the existence of an
# association we can use the *topic_exists* method.
if not store.topic_exists(TOPIC_MAP_IDENTIFIER, 'test-association1'):
store.set_association(TOPIC_MAP_IDENTIFIER, association1) # Persist association to store.

store.close()
# Retrieve occurrence from store.
association2 = store.get_association(TOPIC_MAP_IDENTIFIER, 'test-association1',
resolve_attributes=RetrievalOption.RESOLVE_ATTRIBUTES)

assert association2.identifier == 'test-association1'
assert association2.instance_of == 'association'
Expand All @@ -194,17 +180,14 @@ def test_attribute():
language=Language.FRA)

# Instantiate and open topic store.
store = TopicStore(username, password)
store.open()

# Persist attribute to store.
if not store.attribute_exists(TOPIC_MAP_IDENTIFIER, 'test-entity1', 'name'):
store.set_attribute(TOPIC_MAP_IDENTIFIER, attribute1)
with TopicStore(username, password) as store:

# Retrieve attribute from store.
attribute2 = store.get_attribute(TOPIC_MAP_IDENTIFIER, 'test-attribute1')
# Persist attribute to store.
if not store.attribute_exists(TOPIC_MAP_IDENTIFIER, 'test-entity1', 'name'):
store.set_attribute(TOPIC_MAP_IDENTIFIER, attribute1)

store.close()
# Retrieve attribute from store.
attribute2 = store.get_attribute(TOPIC_MAP_IDENTIFIER, 'test-attribute1')

assert attribute2.identifier == 'test-attribute1'
assert attribute2.name == 'name'
Expand Down
2 changes: 1 addition & 1 deletion topicdb/core/store/topicstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def open(self):
password=self.password,
host=self.host,
port=self.port)
return self.connection
return self

def close(self):
if self.connection:
Expand Down

0 comments on commit 8d87f03

Please sign in to comment.