Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pytest instead of testtools #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions pytests/common_test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

mtalexan marked this conversation as resolved.
Show resolved Hide resolved
importedOk=True

# Allow for tests that can't don't import this to use this file still
try:
import v1pysdk
except ImportError:
importedOk=False


class PublicTestServerConnection():
username = 'admin'
password = 'admin'
address = 'www14.v1host.com'
instance = 'v1sdktesting'
scheme = 'https'
# must match scheme + "://" + address + "/" + instance
instance_url = 'https://www14.v1host.com/v1sdktesting'
token = '1.VdeWXQVNdY0yVpYexTtznCxcWTQ='

def __init__():
pass

@staticmethod
def getV1Meta():
"""Creates a V1Meta object from the default configuration and returns it
"""
# If we couldn't import the v1pysdk, we can't create the object
if not importedOk:
return None
else:
return v1pysdk.V1Meta(
address = PublicTestServerConnection.address,
instance = PublicTestServerConnection.instance,
scheme = 'https',
username = PublicTestServerConnection.username,
password = PublicTestServerConnection.password,
)
6 changes: 6 additions & 0 deletions pytests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest
import common_test_server

@pytest.fixture
def v1():
return common_test_server.PublicTestServerConnection.getV1Meta()
54 changes: 54 additions & 0 deletions pytests/test_common_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import math

import pytest

import v1pysdk


class TestV1CommonSetup():
def test_initial_create_story(self, v1):
"""Creates a very simple story and returns the object"""
#v1StoryName = self.getUniqueString()
v1StoryName = "tests.query_tests.TestV1Query.test_find_query-1"
defaultEstimate = 1.0
reference = "http://test.com"

scope = v1.Scope.select('Name').page(size=1)
defaultScope = None
if len(scope) > 0:
defaultScope = scope.first()

epic = v1.Epic.select('Name').page(size=1)
defaultSuper = None
if len(epic) > 0:
defaultSuper = epic.first()

# build a filter string that exactly matches what we've set above
baseFilterStr = "Reference='" + reference + "'&DetailEstimate='" + str(defaultEstimate) + "'&"
if defaultScope:
baseFilterStr += "Scope.Name='" + defaultScope.Name + "'&"
if defaultSuper:
baseFilterStr += "Super.Name='" + defaultSuper.Name + "'&"
baseFilterStr += "Name='" + v1StoryName + "'"

newStory = None
try:
newStory = v1.Story.create(
Name = v1StoryName,
Scope = defaultScope,
Super = defaultSuper,
DetailEstimate = defaultEstimate,
Reference = reference,
)
except Exception as e:
pytest.fail("Error creating new story: {0}".format(str(e)))

#Perform a readback using the constructed filter to make sure the item's on the server

createdItems = v1.Story.select('Name').filter(baseFilterStr)
for t in createdItems: # run query, but don't throw an exception if nothing is returned
pass

assert(len(createdItems) > 0, "Created item can't be queried")

return newStory
214 changes: 214 additions & 0 deletions pytests/test_connects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import sys
if sys.version_info >= (3,0):
from urllib.error import HTTPError
else:
from urllib2 import HTTPError

# try the old version, then fallback to the new one
try:
from xml.etree import ElementTree
from xml.etree.ElementTree import parse, fromstring, Element
except ImportError:
from elementtree import ElementTree
from elementtree.ElementTree import parse, fromstring, Element

from v1pysdk.client import *
from v1pysdk import V1Meta
from common_test_server import PublicTestServerConnection

import pytest

class TestV1Connection():
def test_connect(self):
username = PublicTestServerConnection.username
password = PublicTestServerConnection.password
address = PublicTestServerConnection.address
instance = PublicTestServerConnection.instance

server = V1Server(address=address, username=username, password=password,instance=instance)
# The story names, but limit to only the first result so we don't get inundated with results
code, body = server.fetch('/rest-1.v1/Data/Story?sel=Name&page=1,0')

elem = fromstring(body)
assert(elem.tag == 'Assets')

def test_meta_connect_instance_url(self):
v1 = None
try:
v1 = V1Meta(
instance_url = PublicTestServerConnection.instance_url,
username = PublicTestServerConnection.username,
password = PublicTestServerConnection.password,
)
except Exception as e:
pytest.fail("Error trying to create connection: {0}".format(str(e)))

try:
items = v1.Story.select('Name').page(size=1)
items.first() #run the query
except Exception as e:
pytest.fail("Error running query from connection: {0}".format(str(e)))

def test_meta_connect_instance_and_address(self):
v1 = None
try:
v1 = V1Meta(
address = PublicTestServerConnection.address,
instance = PublicTestServerConnection.instance,
username = PublicTestServerConnection.username,
password = PublicTestServerConnection.password,
)
except Exception as e:
pytest.fail("Error trying to create connection: {0}".format(str(e)))
try:
items = v1.Story.select('Name').page(size=1)
items.first() #run the query
except Exception as e:
pytest.fail("Error running query from connection: {0}".format(str(e)))

def test_meta_connect_instance_url_overrides_separate(self):
v1 = None
address = ''
instance = None

try:
v1 = V1Meta(
instance_url = PublicTestServerConnection.instance_url,
address = address,
instance = instance,
username = PublicTestServerConnection.username,
password = PublicTestServerConnection.password,
)
except Exception as e:
pytest.fail("Error trying to create connection: {0}".format(str(e)))

try:
items = v1.Story.select('Name').page(size=1)
items.first() #run the query
except Exception as e:
pytest.fail("Error trying to create connection: {0}".format(str(e)))

def test_meta_connect_oauth(self):
v1 = None
try:
v1 = V1Meta(
instance_url = PublicTestServerConnection.instance_url,
#no username
password = PublicTestServerConnection.token,
use_password_as_token=True,
)
except Exception as e:
pytest.fail("Error trying to create connection: {0}".format(str(e)))

try:
items = v1.Story.select('Name').page(size=1)
items.first() #run the query
except Exception as e:
pytest.fail("Error running query from connection: {0}".format(str(e)))

def test_meta_connect_oauth_ignores_username(self):
v1 = None
username = ''

try:
v1 = V1Meta(
instance_url = PublicTestServerConnection.instance_url,
username = username,
password = PublicTestServerConnection.token,
use_password_as_token=True,
)
except Exception as e:
pytest.fail("Error trying to create connection: {0}".format(str(e)))

try:
items = v1.Story.select('Name').page(size=1)
items.first() #run the query
except Exception as e:
pytest.fail("Error running query from connection: {0}".format(str(e)))

def test_connect_fails_when_invalid(self):
v1bad = None
username = ''
password = ''

try:
v1bad = V1Meta(
instance_url = PublicTestServerConnection.instance_url,
username = username,
password = password,
use_password_as_token=False,
)
# we have to try to use it to get it to connect and fail
items = v1bad.Story.select('Name').page(size=1)
items.first() #run the query
except HTTPError as e:
assert(str(e.code) == '401', "Connection failed for reasons other than authorization")
else:
pytest.fail("Connection succeeded with bad credentials.")

def test_reconnect_succeeds_after_invalid(self):
v1bad = None
username = ''
password = ''

try:
v1bad = V1Meta(
instance_url = PublicTestServerConnection.instance_url,
username = username,
password = password,
use_password_as_token=False,
)
items = v1bad.Story.select('Name').page(size=1)
items.first() #run the query
except HTTPError as e:
assert(str(e.code) == '401', "Connection failed for reasons other than authorization")
else:
pytest.fail("First connection succeeded with bad credentials, cannot continue test")

v1good = None

# Connect correctly first
try:
v1good = V1Meta(
instance_url = PublicTestServerConnection.instance_url,
password = PublicTestServerConnection.token,
use_password_as_token=True,
)
items = v1good.Story.select('Name').page(size=1)
items.first() #run the query
except Exception as e:
pytest.fail("Error running query from good connection: {0}".format(str(e)))

def test_reconnect_fails_when_invalid(self):
v1good = None

# Connect correctly first
try:
v1good = V1Meta(
instance_url = PublicTestServerConnection.instance_url,
password = PublicTestServerConnection.token,
use_password_as_token=True,
)
items = v1good.Story.select('Name').page(size=1)
items.first() #run the query
except Exception as e:
pytest.fail("Error running query from good connection: {0}".format(str(e)))

v1bad = None
username = ''

password = ''

try:
v1bad = V1Meta(
instance_url = PublicTestServerConnection.instance_url,
username = username,
password = password,
use_password_as_token=False,
)
items = v1bad.Story.select('Name').page(size=1)
items.first() #run the query
except HTTPError as e:
assert(str(e.code) == "401", "Connection failed for reasons other than authorization")
else:
assert(str(e.code) == "401", "Second connection failed for reasons other than authorization")
10 changes: 10 additions & 0 deletions pytests/test_creations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import v1pysdk
import common_test_server
from test_common_setup import TestV1CommonSetup

class TestV1Create(TestV1CommonSetup):
def test_create_story(self, v1):
"""Creates a very simple story"""
with common_test_server.PublicTestServerConnection.getV1Meta() as v1:
# common setup already does this and tests the creation, it just needs a V1 instance to work on
self.test_initial_create_story(v1)
52 changes: 52 additions & 0 deletions pytests/test_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
from testtools import TestCase
from testtools.assertions import assert_that
from testtools.content import text_content
from testtools.matchers import Equals
"""

import v1pysdk
from common_test_server import PublicTestServerConnection
import common_test_server
import test_common_setup
import pytest


class TestV1Operations(test_common_setup.TestV1CommonSetup):
def test_quick_close_and_reopen(self):
"""Creates a story, quick closes it, then reopens it, then quick closes again"""
with common_test_server.PublicTestServerConnection.getV1Meta() as v1:
newStory = None
try:
newStory = self.test_initial_create_story(v1)
except Exception as e:
pytest.fail("Unable to setup by creating initial test story: {0}".format(str(e)))

assert(newStory.IsClosed == False, "New story created already closed, cannot test")

try:
newStory.QuickClose()
except Exception as e:
pytest.fail("Error while quick closing story: {0}".format(str(e)))

try:
v1.commit()
except Exception as e:
pytest.fail("Error while syncing commits after close {0}".format(str(e)))

assert(newStory.IsClosed == True, "Story didn't close when QuickClose() was called")

try:
newStory.Reactivate()
except Exception as e:
pytest.fail("Error while reactivating story {0}".format(str(e)))

try:
v1.commit()
except Exception as e:
pytest.fail("Error while syncing commits after reactivation")
assert(newStory.IsClosed == 'false', "Story didn't re-open when Reactivate() was called")

# "cleanup" by closing the story
newStory.QuickClose()
v1.commit()
Loading