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

test: Add tests for CTX manager usage on PEP0249. #448

Closed
wants to merge 1 commit into from

Conversation

pvital
Copy link
Member

@pvital pvital commented Aug 30, 2023

Add the test_connect_ctx_mngr UT to test the span creation when using the psycopg2 connect with context manager, and the test_connect_cursor_ctx_mngr UT when using the psycopg2 connect and cursos context manager.

@pvital pvital requested review from Ferenc- and GSVarsha August 30, 2023 14:07
Copy link
Collaborator

@Ferenc- Ferenc- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a TC where the db is not managed, but the cursor is.
Please cover the rest of the instrumentations where PEP0248 is used, like mysql.

@GSVarsha
Copy link
Contributor

GSVarsha commented Aug 31, 2023

Hi @pvital ,
I've also been adding tests to db modules that conform to PEP-249 in this branch.
Do you think we should somehow merge changes from these 2 branches?
I've also covered pymysql and mysqlclient here.

Add the test_connect_ctx_mngr UT to test the span creation when
using the psycopg2 connect with context manager, and the
test_connect_cursor_ctx_mngr UT when using the psycopg2 connect
and cursos context manager.

Signed-off-by: Paulo Vital <[email protected]>
@pvital pvital force-pushed the pep0249_ctx_mngr_ut branch from f67eabc to ccf7ce0 Compare August 31, 2023 06:56
@pvital pvital requested a review from Ferenc- August 31, 2023 07:12
@pvital
Copy link
Member Author

pvital commented Aug 31, 2023

I am closing this PR once we use #450 to track this fix.

@pvital pvital closed this Aug 31, 2023
self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

result = cursor.execute("""SELECT * from users""")
cursor.fetchone()

assert(result >= 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.

result = cursor.execute("""SELECT * from users""")
cursor.fetchone()

assert(result >= 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.

cursor.fetchone()
self.db.close()

assert(result >= 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.

result = cursor.execute("""SELECT * from users""")
cursor.fetchone()

assert(result >= 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.

cursor.fetchone()
self.db.close()

assert(result >= 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.

result = cursor.execute("""SELECT * from users""")
cursor.fetchone()

assert(result >= 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.

@@ -217,3 +217,93 @@ def test_error_capture(self):
self.assertEqual(db_span.data["mysql"]["stmt"], 'SELECT * from blah')
self.assertEqual(db_span.data["mysql"]["host"], testenv['mysql_host'])
self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

def test_connect_ctx_mngr(self):
result = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context manager does not create a new scope, the result = cursor.execute( statement is enough. Please remove this line, luckily this is not Pascal or ANSI C, where you have to declare the variables way ahead of assigning them.

self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

def test_cursor_ctx_mngr(self):
result = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

def test_connect_cursor_ctx_mngr(self):
result = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

@@ -243,3 +243,93 @@ def test_error_capture(self):
self.assertEqual(db_span.data["mysql"]["stmt"], 'SELECT * from blah')
self.assertEqual(db_span.data["mysql"]["host"], testenv['mysql_host'])
self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

def test_connect_ctx_mngr(self):
result = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

def test_cursor_ctx_mngr(self):
result = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

def test_connect_cursor_ctx_mngr(self):
result = None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

Copy link
Collaborator

@Ferenc- Ferenc- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a preliminary review. With some minor formalities commented for now.

@pvital
Copy link
Member Author

pvital commented Aug 31, 2023

@Ferenc- as mentioned here, this PR is closed. Thanks anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants