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: Added test for created_at #6728

Merged
merged 41 commits into from
Feb 2, 2020
Merged

test: Added test for created_at #6728

merged 41 commits into from
Feb 2, 2020

Conversation

me-diru
Copy link
Contributor

@me-diru me-diru commented Jan 7, 2020

Fixes #6722

Short description of what this resolves:

Checks that created_at is set to current time in all models

Changes proposed in this pull request:

  • Added test for created_at if set to the current time.

Checklist

  • I have read the Contribution & Best practices Guide and my PR follows them.
  • My branch is up-to-date with the Upstream development branch.
  • The unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • All the functions created/modified in this PR contain relevant docstrings.

tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
#all the models having created_at are stored in a set
created_at_models=[Session, User, Event , AccessCode, Order, Session, TicketHolder, RoleInvite, DiscountCode, UserTokenBlackListTime, EventInvoice]
for model in created_at_models:
#looping through each model in a subtest to check if created_at time is set to current time

Choose a reason for hiding this comment

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

block comment should start with '# '

tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
from app.models.user_token_blacklist import UserTokenBlackListTime
from app.models.event_invoice import EventInvoice

class TestCreatedatValidation(TestCase):

Choose a reason for hiding this comment

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

expected 2 blank lines, found 1

from app.api import routes

from unittest import TestCase
#importing all models containing createdat variable

Choose a reason for hiding this comment

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

block comment should start with '# '

tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
@@ -0,0 +1,35 @@
import unittest
#improting datetime

Choose a reason for hiding this comment

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

Black would make changes.
block comment should start with '# '

for model in created_at_models:
#looping through each model in a subtest to check if created_at time is set to current time
with self.subTest(model=model):
self.assertEqual(model.created_at,datetime.now(),'\nTests created_at in model {}'.format(model))

Choose a reason for hiding this comment

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

missing whitespace after ','

'''
#all the models having created_at are stored in a set
created_at_models=[Session, User, Event , AccessCode, Order, Session, TicketHolder,
RoleInvite, DiscountCode, UserTokenBlackListTime, EventInvoice]

Choose a reason for hiding this comment

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

continuation line over-indented for visual indent

''' createdat validate time : Tests created_at variable in all models
'''
#all the models having created_at are stored in a set
created_at_models=[Session, User, Event , AccessCode, Order, Session, TicketHolder,

Choose a reason for hiding this comment

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

missing whitespace around operator
trailing whitespace
whitespace before ','

class TestCreatedatValidation(TestCase):

def test_createdat_all_models(self):
''' createdat validate time : Tests created_at variable in all models

Choose a reason for hiding this comment

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

trailing whitespace

for model in created_at_models:
#looping through each model in a subtest to check if created_at time is set to current time
with self.subTest(model=model):
self.assertEqual(model.created_at,datetime.now(),'\nTests created_at in model {}'.format(model))
Copy link
Member

Choose a reason for hiding this comment

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

Are you trying to test for the default attribute of created_at in each model here? If so,
you'll need to instantiate the Alchemy object and then assert its attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am testing already instantiated attribute of created_at in each model to check if it is set to the current timezone. Yeah, instantiating objects and asserting its attribute can also be done if preferred.

Copy link
Member

@iamareebjamal iamareebjamal Jan 8, 2020

Choose a reason for hiding this comment

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

It's not about preference. This'll never work as you can see in the build failure. It's like saying Session.created_at == date. Session is a class, not an object/instance of session so it has no created_at attribute at all

Recommend running tests locally to verify that they pass and what you're doing is correct before sending in PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we need to retrieve the value from db and check or if there is any other way.
After creating an object is there any method returning the object with loaded attributes.
As direct instantiation of object created_at value is stored as None type, as the default is func.now() in class definition of model classes.

Copy link
Member

Choose a reason for hiding this comment

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

Do we need to retrieve the value from db and check

Yes

@me-diru
Copy link
Contributor Author

me-diru commented Jan 8, 2020

@iamareebjamal
Can you please look into this in your free time?
Thank you

@iamareebjamal
Copy link
Member

@mrsaicharan1 Already pointed out the issue. And as you can see, the build is failing. Try to run the tests locally to first see that they are actually passing. Will be happy to review once they do

@iamareebjamal
Copy link
Member

Also, all hound violations should be fixed as well

@me-diru
Copy link
Contributor Author

me-diru commented Jan 23, 2020

image
I retrieved the value from DB successfully, but there's a small time lag in between while checking with datetime.now()
Can I please get the date format used and is millisecond difference ok while comparing.

@iamareebjamal
Copy link
Member

Yes, ms difference is OK

for model in created_at_models:
#looping through each model in a subtest to check if created_at time is set to current time
with self.subTest(model=model):
self.assertEqual(model.created_at,datetime.now(),'\nTests created_at in model {}'.format(model))
Copy link
Member

Choose a reason for hiding this comment

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

Because of the millisecond difference, try testing if the difference between the two is not greater than certain time difference (maybe 50ms or 100ms)

@me-diru
Copy link
Contributor Author

me-diru commented Jan 23, 2020

Not all models created_at have a default value set to test, should I check models which have it set as I am getting None value if not specified?

@iamareebjamal
Copy link
Member

If you're getting None, that means you've caught an issue with the model. Fix the model so it returns date

@me-diru
Copy link
Contributor Author

me-diru commented Jan 24, 2020

image

Some models have a fixedOffset datetime which shouldn't be the case I suppose as all datetime aware objects are set by default to UTC standard

@iamareebjamal
Copy link
Member

You should not test greater than or equal to. That's wrong. You should test that difference between now and stored date is less than a threshold value

@me-diru
Copy link
Contributor Author

me-diru commented Jan 26, 2020

You should not test greater than or equal to. That's wrong. You should test that difference between now and stored date is less than a threshold value

The datetime data retrieved from db is coming with some offset which is not compatible with UTC standards. There is no datetime function to get db version of datetime i.e +5:30 value as I got.

image

@iamareebjamal
Copy link
Member

iamareebjamal commented Jan 26, 2020

There is no datetime function to get db version of datetime i.e +5:30 value as I got.

I cannot understand what you mean by that

The datetime data retrieved from db is coming with some offset which is not compatible with UTC standards.

Which UTC standard does it not follow? Can you show me the clause in the standard which it violates?

@me-diru
Copy link
Contributor Author

me-diru commented Jan 26, 2020

The datetime data retrieved from db is coming with some offset which is not compatible with UTC standards.

Which UTC standard does it not follow? Can you show me the clause in the standard which it violates?

It didn't violate any standard perse, there was a difference of 5:30 gap between the both as you can see. Fixed it by using "datetime.utcnow().astimezone()" for getting time with local offset in accordance with the db.

tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Jan 26, 2020

Codecov Report

Merging #6728 into development will decrease coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@               Coverage Diff               @@
##           development    #6728      +/-   ##
===============================================
- Coverage         65.5%   65.49%   -0.01%     
===============================================
  Files              306      306              
  Lines            15346    15346              
===============================================
- Hits             10052    10051       -1     
- Misses            5294     5295       +1
Impacted Files Coverage Δ
app/api/schema/events.py 91.47% <ø> (ø) ⬆️
app/models/event.py 78.09% <0%> (-0.36%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 91e0de8...dfd2b3e. Read the comment docs.

tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
app/models/role_invite.py Outdated Show resolved Hide resolved
app/models/discount_code.py Outdated Show resolved Hide resolved
time_diff = current_time - db_created_at
allowed_time_lag = timedelta(milliseconds = 50)
self.assertLessEqual(time_diff, allowed_time_lag,
"EventInvoice model created_at not set to current time")

Choose a reason for hiding this comment

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

continuation line under-indented for visual indent

current_time = datetime.utcnow().astimezone()
time_diff = current_time - db_created_at
allowed_time_lag = timedelta(milliseconds = 50)
self.assertLessEqual(time_diff, allowed_time_lag,

Choose a reason for hiding this comment

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

trailing whitespace

db_created_at = db.session.query(EventInvoice).get(event_invoice_id).created_at
current_time = datetime.utcnow().astimezone()
time_diff = current_time - db_created_at
allowed_time_lag = timedelta(milliseconds = 50)

Choose a reason for hiding this comment

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

unexpected spaces around keyword / parameter equals

test_event_invoice = EventInvoice()
save_to_db(test_event_invoice)
event_invoice_id = test_event_invoice.id
db_created_at = db.session.query(EventInvoice).get(event_invoice_id).created_at

Choose a reason for hiding this comment

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

line too long (91 > 90 characters)

tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
@me-diru
Copy link
Contributor Author

me-diru commented Jan 27, 2020

Hello @iamareebjamal ,
I have successfully made proper working tests for each model. Can you please go through them and give your thoughts on it.

@iamareebjamal
Copy link
Member

I think this PR just broke the record of violations/lines changed

@iamareebjamal
Copy link
Member

Your PR has 86 file changes. Rebase with development and push. I can only review when opening this page https://github.com/fossasia/open-event-server/pull/6728/files does not require 400% of my CPU. I don't want to brick my laptop

tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
tests/all/unit/api/validation/test_createdat.py Outdated Show resolved Hide resolved
@iamareebjamal
Copy link
Member

Why haven't you run pre-commit install?

@iamareebjamal
Copy link
Member

There's no use of marking issues resolved. I can still see them and you will still need to resolve them

@me-diru
Copy link
Contributor Author

me-diru commented Feb 1, 2020

Sir, actually no changes are coming when I run black, and also I have run pre-commit as you said and it didn't show any changes.
image

@@ -11,6 +11,7 @@ class AccessCodeFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = AccessCode
sqlalchemy_session = db.session
sqlalchemy_session_persistence = 'commit'
Copy link
Member

Choose a reason for hiding this comment

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

Why add this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sir, as created_at can't be assigned any value in a factory. we need to commit it in db to retrieve actually stored value in db so that is used to commit.

Copy link
Member

Choose a reason for hiding this comment

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

Then commit manually. No need of this

valid_from = common.date_
valid_till = common.dateEnd_
event_id = 1
Copy link
Member

Choose a reason for hiding this comment

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

Revert these changes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can revert in access code without any error. But if I do the same in Session factory the following error occurs.
image

Copy link
Member

Choose a reason for hiding this comment

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

See how it is done in hook files

from app.models.ticket_holder import TicketHolder, db


class TicketHolderFactory(factory.alchemy.SQLAlchemyModelFactory):
Copy link
Member

Choose a reason for hiding this comment

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

AttendeeFactory already exists

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will remove thanks!

@me-diru
Copy link
Contributor Author

me-diru commented Feb 2, 2020

test_export_sessions_with_details_csv and test_delete_ticket_holders_with_no_order_id are failing in travis build.
The first is depending on created_at = common.date_ in SessionFactory and getting some import error on the other when checked locally.
Can you please take a look into this

@iamareebjamal
Copy link
Member

Since you have removed created_at from factories, you have to fix both tests to restore old behaviour

@me-diru
Copy link
Contributor Author

me-diru commented Feb 2, 2020

Able to solve test_csv_jobs_util error.
I am not able to run test_scheduled_jobs locally.
What I have seen from the code so far is :
image
the function which deletes the ticket holder with no order_id, I don't know why it is using deleted_at(which isn't present )and created_at from TicketHolder in querying.
image

@iamareebjamal
Copy link
Member

You have to fix the test, not the schedule task

@me-diru
Copy link
Contributor Author

me-diru commented Feb 2, 2020

actually, the test is dependent on the schedule task. Before it somehow deleted the ticketHolder and test passed. Now it is not doing so

@iamareebjamal
Copy link
Member

schedule task is dependent on the test. First the created_at was set at 2016, now it is set to current time. How is it going to work?

Before it somehow deleted the ticketHolder and test passed. Now it is not doing so

There is no somehow with computers. There's always a reason. They are deterministic machines

@me-diru
Copy link
Contributor Author

me-diru commented Feb 2, 2020

Sir, I just overthought it before as expiry time is added to it. I thought If it checks with 2016, there's no point in checking it. Now I understand the use of tests a bit more.

Please review my submission, made all changes as mentioned.

@iamareebjamal iamareebjamal changed the title Added test for createdat test: Added test for created_at Feb 2, 2020
@auto-label auto-label bot added the testing label Feb 2, 2020
if model_factory in models_with_utcnow:
current_time = datetime.utcnow().astimezone()
else:
current_time = datetime.now(timezone.utc).astimezone()
Copy link
Member

Choose a reason for hiding this comment

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

There shouldn't be any difference. Please make them consistent

Copy link
Contributor Author

@me-diru me-diru Feb 2, 2020

Choose a reason for hiding this comment

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

There's actually a difference, as you can see in the below image.
Printing in this order:
image

Output:
image

Copy link
Member

Choose a reason for hiding this comment

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

I never said there isn't a difference. I said there shouldn't be a difference

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The values am getting are weird, one has date with an offset added and specifying the offset[1]. Other one has no offset set also coming with offset added[2].
There is no python function to standardize these two.

  1. 2020-02-02 20:24:32.051071+05:30
  2. 2020-02-02 14:54:32.064461+05:30

@iamareebjamal
Copy link
Member

Thanks for contributing and being patient :)

@niranjan94
Copy link
Member

Codacy Here is an overview of what got changed by this pull request:

Complexity increasing per file
==============================
- app/factories/user_token_blacklist.py  1
- tests/all/unit/api/validation/test_createdat.py  5
         

See the complete overview on Codacy

@iamareebjamal iamareebjamal merged commit 2afce3b into fossasia:development Feb 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create tests for all created_at
5 participants