Commit ea806694 authored by Sam Thursfield's avatar Sam Thursfield

Merge branch 'sam/autopep8' into 'master'

functional-tests: Reformat in line with Python standards

See merge request !67
parents 76dbeeef fda451ef
Pipeline #64477 passed with stage
in 2 minutes and 17 seconds
This diff is collapsed.
......@@ -29,11 +29,11 @@ from common.utils.storetest import CommonTrackerStoreTest as CommonTrackerStoreT
class TrackerStoreSparqlBugsTests (CommonTrackerStoreTest):
def test_01_NB217566_union_exists_filter (self):
"""
NB217566: Use of UNION in EXISTS in a FILTER breaks filtering
"""
content = """
def test_01_NB217566_union_exists_filter(self):
"""
NB217566: Use of UNION in EXISTS in a FILTER breaks filtering
"""
content = """
INSERT {
<contact:affiliation> a nco:Affiliation ;
nco:hasPhoneNumber
......@@ -42,10 +42,10 @@ class TrackerStoreSparqlBugsTests (CommonTrackerStoreTest):
nco:hasAffiliation <contact:affiliation> .
}
"""
self.tracker.update (content)
self.tracker.update(content)
""" Check that these 3 queries return the same results """
query1 = """
""" Check that these 3 queries return the same results """
query1 = """
SELECT ?_contact ?n WHERE {
?_contact a nco:PersonContact .
{
......@@ -72,7 +72,7 @@ class TrackerStoreSparqlBugsTests (CommonTrackerStoreTest):
}
"""
query2 = """
query2 = """
SELECT ?_contact ?n WHERE {
?_contact a nco:PersonContact .
{
......@@ -87,7 +87,7 @@ class TrackerStoreSparqlBugsTests (CommonTrackerStoreTest):
}
"""
query3 = """
query3 = """
SELECT ?_contact ?n WHERE {
?_contact a nco:PersonContact .
{
......@@ -109,142 +109,137 @@ class TrackerStoreSparqlBugsTests (CommonTrackerStoreTest):
}
"""
results1 = self.tracker.query (query1)
print("1", results1)
self.assertEqual (len (results1), 1)
self.assertEqual (len (results1[0]), 2)
self.assertEqual (results1[0][0], "contact:test")
self.assertEqual (results1[0][1], "98653")
results2 = self.tracker.query (query2)
print("2", results2)
self.assertEqual (len (results2), 1)
self.assertEqual (len (results2[0]), 2)
self.assertEqual (results2[0][0], "contact:test")
self.assertEqual (results2[0][1], "98653")
results3 = self.tracker.query (query3)
print("3", results3)
self.assertEqual (len (results3), 1)
self.assertEqual (len (results3[0]), 2)
self.assertEqual (results3[0][0], "contact:test")
self.assertEqual (results3[0][1], "98653")
""" Clean the DB """
delete = """
results1 = self.tracker.query(query1)
print("1", results1)
self.assertEqual(len(results1), 1)
self.assertEqual(len(results1[0]), 2)
self.assertEqual(results1[0][0], "contact:test")
self.assertEqual(results1[0][1], "98653")
results2 = self.tracker.query(query2)
print("2", results2)
self.assertEqual(len(results2), 1)
self.assertEqual(len(results2[0]), 2)
self.assertEqual(results2[0][0], "contact:test")
self.assertEqual(results2[0][1], "98653")
results3 = self.tracker.query(query3)
print("3", results3)
self.assertEqual(len(results3), 1)
self.assertEqual(len(results3[0]), 2)
self.assertEqual(results3[0][0], "contact:test")
self.assertEqual(results3[0][1], "98653")
""" Clean the DB """
delete = """
DELETE { <contact:affiliation> a rdfs:Resource .
<contact:test> a rdfs:Resource .
}
"""
def test_02_NB217636_delete_statements (self):
"""
Bug 217636 - Not able to delete contact using
DELETE {<contact:556> ?p ?v} WHERE {<contact:556> ?p ?v}.
"""
data = """ INSERT {
def test_02_NB217636_delete_statements(self):
"""
Bug 217636 - Not able to delete contact using
DELETE {<contact:556> ?p ?v} WHERE {<contact:556> ?p ?v}.
"""
data = """ INSERT {
<contact:test-nb217636> a nco:PersonContact ;
nco:fullname 'Testing bug 217636'
}
"""
self.tracker.update (data)
self.tracker.update(data)
results = self.tracker.query ("""
results = self.tracker.query ("""
SELECT ?u WHERE {
?u a nco:PersonContact ;
nco:fullname 'Testing bug 217636' .
}
""")
self.assertEqual (len (results), 1)
self.assertEqual (len (results[0]), 1)
self.assertEqual (results[0][0], "contact:test-nb217636")
self.assertEqual(len(results), 1)
self.assertEqual(len(results[0]), 1)
self.assertEqual(results[0][0], "contact:test-nb217636")
problematic_delete = """
problematic_delete = """
DELETE { <contact:test-nb217636> ?p ?v }
WHERE { <contact:test-nb217636> ?p ?v }
"""
self.tracker.update (problematic_delete)
self.tracker.update(problematic_delete)
results_after = self.tracker.query ("""
results_after = self.tracker.query ("""
SELECT ?u WHERE {
?u a nco:PersonContact ;
nco:fullname 'Testing bug 217636' .
}
""")
self.assertEqual (len (results_after), 0)
self.assertEqual(len(results_after), 0)
# Safe deletion
delete = """
# Safe deletion
delete = """
DELETE { <contact:test-nb217636> a rdfs:Resource. }
"""
self.tracker.update (delete)
def test_03_NB222645_non_existing_class_resource (self):
"""
NB222645 - Inserting a resource using an non-existing class, doesn't rollback completely
"""
query = "SELECT tracker:modified (?u) ?u WHERE { ?u a nco:Contact }"
original_data = self.tracker.query (query)
wrong_insert = "INSERT { <test://nb222645-wrong-class-contact> a nco:IMContact. } "
self.assertRaises (GLib.Error,
self.tracker.update,
wrong_insert)
new_data = self.tracker.query (query)
self.assertEqual (len (original_data), len (new_data))
# We could be more picky, but checking there are the same number of results
# is enough to verify the problem described in the bug.
def test_04_NB224760_too_long_filter (self):
"""
NB#224760 - 'too many sql variables' when filter ?sth in (long list)
"""
query = "SELECT tracker:id (?m) ?m WHERE { ?m a rdfs:Resource. FILTER (tracker:id (?m) in (%s)) }"
numbers = ",".join ([str (i) for i in range (1000, 2000)])
results = self.tracker.query (query % (numbers))
# The query will raise an exception is the bug is there
# If we are here, everything is fine.
self.assertIsNotNone (results)
def test_05_NB281201_insert_replace_and_superproperties (self):
"""
Bug 281201 - INSERT OR REPLACE does not delete previous values for superproperties
"""
content = """INSERT { <test:resource:nb281201> a nie:InformationElement;
self.tracker.update(delete)
def test_03_NB222645_non_existing_class_resource(self):
"""
NB222645 - Inserting a resource using an non-existing class, doesn't rollback completely
"""
query = "SELECT tracker:modified (?u) ?u WHERE { ?u a nco:Contact }"
original_data = self.tracker.query(query)
wrong_insert = "INSERT { <test://nb222645-wrong-class-contact> a nco:IMContact. } "
self.assertRaises(GLib.Error,
self.tracker.update,
wrong_insert)
new_data = self.tracker.query(query)
self.assertEqual(len(original_data), len(new_data))
# We could be more picky, but checking there are the same number of results
# is enough to verify the problem described in the bug.
def test_04_NB224760_too_long_filter(self):
"""
NB#224760 - 'too many sql variables' when filter ?sth in (long list)
"""
query = "SELECT tracker:id (?m) ?m WHERE { ?m a rdfs:Resource. FILTER (tracker:id (?m) in (%s)) }"
numbers = ",".join([str(i) for i in range(1000, 2000)])
results = self.tracker.query(query % (numbers))
# The query will raise an exception is the bug is there
# If we are here, everything is fine.
self.assertIsNotNone(results)
def test_05_NB281201_insert_replace_and_superproperties(self):
"""
Bug 281201 - INSERT OR REPLACE does not delete previous values for superproperties
"""
content = """INSERT { <test:resource:nb281201> a nie:InformationElement;
nie:contentLastModified '2011-09-27T11:11:11Z'. }"""
self.tracker.update (content)
self.tracker.update(content)
query = """SELECT ?contentLM ?nieIEDate ?dcDate {
query = """SELECT ?contentLM ?nieIEDate ?dcDate {
<test:resource:nb281201> dc:date ?dcDate ;
nie:informationElementDate ?nieIEDate ;
nie:contentLastModified ?contentLM .
}"""
result = self.tracker.query (query)
# Only one row of results, and the 3 colums have the same value
self.assertEqual (len (result), 1)
self.assertEqual (result[0][0], result[0][1])
self.assertEqual (result[0][1], result[0][2])
result = self.tracker.query(query)
# Only one row of results, and the 3 colums have the same value
self.assertEqual(len(result), 1)
self.assertEqual(result[0][0], result[0][1])
self.assertEqual(result[0][1], result[0][2])
problematic = """INSERT OR REPLACE {
problematic = """INSERT OR REPLACE {
<test:resource:nb281201> nie:contentLastModified '2012-10-28T12:12:12'
}"""
self.tracker.update (problematic)
result = self.tracker.query (query)
# Only one row of results, and the 3 colums have the same value
self.assertEqual (len (result), 1)
self.assertEqual (result[0][0], result[0][1])
self.assertEqual (result[0][1], result[0][2])
self.tracker.update(problematic)
result = self.tracker.query(query)
# Only one row of results, and the 3 colums have the same value
self.assertEqual(len(result), 1)
self.assertEqual(result[0][0], result[0][1])
self.assertEqual(result[0][1], result[0][2])
if __name__ == "__main__":
ut.main()
ut.main()
......@@ -25,12 +25,13 @@ and run sparql with fts functions to check the results.
import unittest as ut
from common.utils.storetest import CommonTrackerStoreTest as CommonTrackerStoreTest
class TestFTSFunctions (CommonTrackerStoreTest):
"""
Insert data with text and check the fts:xxxx functions are returning the expected results
"""
@ut.skip("Test currently fails.")
def test_fts_rank (self):
def test_fts_rank(self):
"""
1. Insert a Contact1 with 'abcdefxyz' as fullname and nickname
2. Insert a Contact2 with 'abcdefxyz' as fullname
......@@ -53,7 +54,7 @@ class TestFTSFunctions (CommonTrackerStoreTest):
nco:nickname 'abcdefxyz abcdefxyz' .
}
"""
self.tracker.update (insert_sparql)
self.tracker.update(insert_sparql)
query = """
SELECT ?contact WHERE {
......@@ -61,12 +62,12 @@ class TestFTSFunctions (CommonTrackerStoreTest):
fts:match 'abcdefxyz' .
} ORDER BY DESC (fts:rank(?contact))
"""
results = self.tracker.query (query)
results = self.tracker.query(query)
self.assertEqual (len(results), 3)
self.assertEqual (results[0][0], "contact://test/fts-function/rank/1")
self.assertEqual (results[1][0], "contact://test/fts-function/rank/2")
self.assertEqual (results[2][0], "contact://test/fts-function/rank/3")
self.assertEqual(len(results), 3)
self.assertEqual(results[0][0], "contact://test/fts-function/rank/1")
self.assertEqual(results[1][0], "contact://test/fts-function/rank/2")
self.assertEqual(results[2][0], "contact://test/fts-function/rank/3")
delete_sparql = """
DELETE {
......@@ -75,10 +76,9 @@ class TestFTSFunctions (CommonTrackerStoreTest):
<contact://test/fts-function/rank/3> a rdfs:Resource .
}
"""
self.tracker.update (delete_sparql)
self.tracker.update(delete_sparql)
def test_fts_offsets (self):
def test_fts_offsets(self):
"""
1. Insert a Contact1 with 'abcdefxyz' as fullname and nickname
2. Insert a Contact2 with 'abcdefxyz' as fullname
......@@ -101,7 +101,7 @@ class TestFTSFunctions (CommonTrackerStoreTest):
nco:nickname 'abcdefxyz abcdefxyz' .
}
"""
self.tracker.update (insert_sparql)
self.tracker.update(insert_sparql)
query = """
SELECT fts:offsets (?contact) WHERE {
......@@ -109,12 +109,13 @@ class TestFTSFunctions (CommonTrackerStoreTest):
fts:match 'abcdefxyz' .
}
"""
results = self.tracker.query (query)
results = self.tracker.query(query)
self.assertEqual (len(results), 3)
self.assertEqual (results[0][0], 'nco:fullname,0,nco:nickname,0')
self.assertEqual (results[1][0], 'nco:fullname,0')
self.assertEqual (results[2][0], 'nco:fullname,0,nco:nickname,0,nco:nickname,10')
self.assertEqual(len(results), 3)
self.assertEqual(results[0][0], 'nco:fullname,0,nco:nickname,0')
self.assertEqual(results[1][0], 'nco:fullname,0')
self.assertEqual(
results[2][0], 'nco:fullname,0,nco:nickname,0,nco:nickname,10')
delete_sparql = """
DELETE {
......@@ -123,7 +124,7 @@ class TestFTSFunctions (CommonTrackerStoreTest):
<contact://test/fts-function/offset/3> a rdfs:Resource .
}
"""
self.tracker.update (delete_sparql)
self.tracker.update(delete_sparql)
if __name__ == '__main__':
......
......@@ -23,20 +23,22 @@ Test the GROUP_CONCAT function in Sparql. Only requires the store.
import unittest as ut
from common.utils.storetest import CommonTrackerStoreTest as CommonTrackerStoreTest
class TestGroupConcat (CommonTrackerStoreTest):
"""
Insert a multivalued property and request the results in GROUP_CONCAT
"""
def test_group_concat (self):
def test_group_concat(self):
"""
1. Insert 3 capabilities for a test contact
2. Retrieve contact/capabilites without group_contact (3 results)
2. TEST: contact with group_concat capabilities (1 result)
3. Remove the test contact inserted
"""
uri = "contact://test_group_concat"
insert = """
INSERT { <%s> a nco:IMAddress;
nco:imID \"test_group_concat\";
......@@ -45,7 +47,7 @@ class TestGroupConcat (CommonTrackerStoreTest):
nco:imCapability nco:im-capability-file-transfers .
}
""" % (uri)
self.tracker.update (insert)
self.tracker.update(insert)
query = """
SELECT ?c ?capability WHERE {
......@@ -53,23 +55,23 @@ class TestGroupConcat (CommonTrackerStoreTest):
nco:imID \"test_group_concat\";
nco:imCapability ?capability .
}
"""
results = self.tracker.query (query)
"""
results = self.tracker.query(query)
assert len (results) == 3
assert len(results) == 3
group_concat_query = """
SELECT ?c GROUP_CONCAT (?capability, '|') AS ?cap WHERE {
?c a nco:IMAddress ;
nco:imID \"test_group_concat\";
nco:imCapability ?capability .
} GROUP BY (?c)
"""
results = self.tracker.query (group_concat_query)
assert len (results) == 1
instances = results[0][1].split ('|')
assert len (instances) == 3
"""
results = self.tracker.query(group_concat_query)
assert len(results) == 1
instances = results[0][1].split('|')
assert len(instances) == 3
TEXT_CHAT = "http://www.semanticdesktop.org/ontologies/2007/03/22/nco#im-capability-text-chat"
MEDIA_CALLS = "http://www.semanticdesktop.org/ontologies/2007/03/22/nco#im-capability-media-calls"
FILE_TRANSFERS = "http://www.semanticdesktop.org/ontologies/2007/03/22/nco#im-capability-file-transfers"
......@@ -77,14 +79,13 @@ class TestGroupConcat (CommonTrackerStoreTest):
assert MEDIA_CALLS in instances
assert FILE_TRANSFERS in instances
#self.assertEquals (str(results[0][0]), "test_insertion_1")
delete = """
DELETE { <%s> a rdfs:Resource. }
""" % (uri)
self.tracker.update (delete)
self.tracker.update(delete)
if __name__ == '__main__':
ut.main()
......@@ -23,13 +23,14 @@ Test tracker:coalesce function in Sparql. Only uses the Store
import unittest as ut
from common.utils.storetest import CommonTrackerStoreTest as CommonTrackerStoreTest
class TestCoalesce (CommonTrackerStoreTest):
"""
Insert and instance with some values, and tracker coalesce of some of them
with different combinations (first NULL, none NULL, all NULL...)
"""
def setUp (self):
def setUp(self):
self.resource_uri = "contact://test_group_concat"
#
......@@ -41,17 +42,15 @@ class TestCoalesce (CommonTrackerStoreTest):
nco:nameFamily \"family name\" .
}
""" % (self.resource_uri)
self.tracker.update (insert)
self.tracker.update(insert)
def tearDown (self):
def tearDown(self):
delete = """
DELETE { <%s> a rdfs:Resource. }
""" % (self.resource_uri)
self.tracker.update (delete)
self.tracker.update(delete)
def test_coalesce_first_fine (self):
def test_coalesce_first_fine(self):
"""
setUp: Insert a contact with only some text properties set
1. TEST: run a query with coalesce with the valid value in first position
......@@ -67,13 +66,12 @@ class TestCoalesce (CommonTrackerStoreTest):
OPTIONAL { ?c nco:note ?note }
FILTER (?c != nco:default-contact-me && ?c != nco:default-contact-emergency)
}
"""
results = self.tracker.query (query)
assert len (results) == 1
"""
results = self.tracker.query(query)
assert len(results) == 1
assert results[0][0] == "full name"
def test_coalesce_second_fine (self):
def test_coalesce_second_fine(self):
"""
setUp: Insert a contact with only some text properties set
1. TEST: run a query with coalesce. First property NULL, second fine
......@@ -89,13 +87,12 @@ class TestCoalesce (CommonTrackerStoreTest):
OPTIONAL { ?c nco:note ?note }
FILTER (?c != nco:default-contact-me && ?c != nco:default-contact-emergency)
}
"""
results = self.tracker.query (query)
assert len (results) == 1
"""
results = self.tracker.query(query)
assert len(results) == 1
assert results[0][0] == "family name"
def test_coalesce_none_fine_default (self):
def test_coalesce_none_fine_default(self):
"""
setUp: Insert a contact with only some text properties set
1. TEST: run a query with coalesce. all variables NULL, return default value
......@@ -111,11 +108,11 @@ class TestCoalesce (CommonTrackerStoreTest):
OPTIONAL { ?c nco:note ?note }
FILTER (?c != nco:default-contact-me && ?c != nco:default-contact-emergency)
}
"""
results = self.tracker.query (query)
assert len (results) == 1
"""
results = self.tracker.query(query)
assert len(results) == 1
assert results[0][0] == "test_coalesce"
if __name__ == '__main__':
ut.main()
......@@ -25,13 +25,15 @@ from common.utils.storetest import CommonTrackerStoreTest as CommonTrackerStoreT
POINT_COORDS = [
(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)
]
]
class TestDistanceFunctions (CommonTrackerStoreTest):
"""
Insert some points and get the distance between them.
"""
def setUp (self):
def setUp(self):
self.counter = 0
for lat, log in POINT_COORDS:
insert = """
......@@ -41,25 +43,24 @@ class TestDistanceFunctions (CommonTrackerStoreTest):
mlo:latitude %d .
}
""" % ("point://test/point/" + str(self.counter), log, lat)
self.tracker.update (insert)
self.tracker.update(insert)
self.counter += 1
def tearDown (self):
for i in range (0, self.counter):
def tearDown(self):
for i in range(0, self.counter):
delete = """
DELETE {
<%s> a rdfs:Resource.
}
""" % ("point://test/point/" + str (i))
self.tracker.update (delete)
self.tracker.update(delete)
def get_distance_between_points (self, sum_func, id1, id2):
def get_distance_between_points(self, sum_func, id1, id2):
assert 0 <= id1 <= len (POINT_COORDS)
assert 0 <= id2 <= len (POINT_COORDS)
assert 0 <= id1 <= len(POINT_COORDS)
assert 0 <= id2 <= len(POINT_COORDS)
assert sum_func == "cartesian" or sum_func == "haversine"
query_1_to_2 = """
SELECT xsd:integer(tracker:%s-distance(?lat1,?lat2,?lon1,?lon2))
WHERE {
......@@ -72,58 +73,56 @@ class TestDistanceFunctions (CommonTrackerStoreTest):
mlo:longitude ?lon2 .
}
""" % (sum_func, id1, id2)
result = self.tracker.query (query_1_to_2)
return int (result[0][0])
def test_distance_cartesian_symmetry (self):
result = self.tracker.query(query_1_to_2)
return int(result[0][0])
def test_distance_cartesian_symmetry(self):
"""
setUp: Insert 5 points in the pre-defined coordinates
1. TEST: Check cartesian distance from point A to B, and from B to A
(should be the same :P)
tearDown: Remove the test points inserted before
"""
a_to_b = self.get_distance_between_points ("cartesian", 1, 2)
a_to_b = self.get_distance_between_points("cartesian", 1, 2)
assert a_to_b == 204601
b_to_a = self.get_distance_between_points ("cartesian", 2, 1)
b_to_a = self.get_distance_between_points("cartesian", 2, 1)
assert b_to_a == 204601
assert a_to_b == b_to_a
assert a_to_b == b_to_a
def test_distance_haversine_symmetry (self):
def test_distance_haversine_symmetry(self):
"""
setUp: Insert 5 points in the pre-defined coordinates
1. TEST: Check cartesian distance from point A to B, and from B to A
(should be the same :P)
tearDown: Remove the test points inserted before
"""
a_to_b = self.get_distance_between_points ("haversine", 1, 2)
a_to_b = self.get_distance_between_points("haversine", 1, 2)
assert a_to_b == 157225
b_to_a = self.get_distance_between_points ("haversine", 2, 1)
b_to_a = self.get_distance_between_points("haversine", 2, 1)
assert b_to_a == 157225
assert a_to_b == b_to_a
def test_distance_cartesian_proportion (self):
d_1_to_2 = self.get_distance_between_points ("cartesian", 1, 2)
d_2_to_3 = self.get_distance_between_points ("cartesian", 2, 3)
d_3_to_4 = self.get_distance_between_points ("cartesian", 3, 4)
def test_distance_cartesian_proportion(self):
d_1_to_2 = self.get_distance_between_points("cartesian", 1, 2)
d_2_to_3 = self.get_distance_between_points("cartesian", 2, 3)
d_3_to_4 = self.get_distance_between_points("cartesian", 3, 4)
assert d_1_to_2 > d_2_to_3 > d_3_to_4
def test_distance_haversine_proportion (self):
d_1_to_2 = self.get_distance_between_points ("haversine", 1, 2)
d_2_to_3 = self.get_distance_between_points ("haversine", 2, 3)
d_3_to_4 = self.get_distance_between_points ("haversine", 3, 4)
def test_distance_haversine_proportion(self):
d_1_to_2 = self.get_distance_between_points("haversine", 1, 2)
d_2_to_3 = self.get_distance_between_points("haversine", 2, 3)
d_3_to_4 = self.get_distance_between_points("haversine", 3, 4)
assert d_1_to_2 > d_2_to_3 > d_3_to_4
def test_distance_different (self):
d_2_to_3h = self.get_distance_between_points ("haversine", 2, 3)
d_2_to_3c = self.get_distance_between_points ("cartesian", 2, 3)
def test_distance_different(self):
d_2_to_3h = self.get_distance_between_points("haversine", 2, 3)
d_2_to_3c = self.get_distance_between_points("cartesian", 2, 3)
assert d_2_to_3h < d_2_to_3c
if __name__ == '__main__':
ut.main()
......@@ -23,13 +23,14 @@ Tests graphs in Sparql. Only requires the store.
import unittest as ut
from common.utils.storetest import CommonTrackerStoreTest as CommonTrackerStoreTest
class TestGraphs (CommonTrackerStoreTest):
"""
Insert triplets in different graphs and check the query results asking in
one specific graph, in all of them and so on.
"""
def test_graph_filter (self):
def test_graph_filter(self):
"""
1. Insert a contact with different phone numbers from different sources
2. Query phone numbers of a single graph
......@@ -56,7 +57,7 @@ class TestGraphs (CommonTrackerStoreTest):
}
}
"""
self.tracker.update (insert_sparql)
self.tracker.update(insert_sparql)