-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Conversation
#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 |
There was a problem hiding this comment.
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 '# '
from app.models.user_token_blacklist import UserTokenBlackListTime | ||
from app.models.event_invoice import EventInvoice | ||
|
||
class TestCreatedatValidation(TestCase): |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 '# '
@@ -0,0 +1,35 @@ | |||
import unittest | |||
#improting datetime |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
@iamareebjamal |
@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 |
Also, all hound violations should be fixed as well |
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)) |
There was a problem hiding this comment.
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)
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? |
If you're getting None, that means you've caught an issue with the model. Fix the model so it returns date |
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. |
I cannot understand what you mean by that
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. |
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
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") |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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)
Hello @iamareebjamal , |
I think this PR just broke the record of violations/lines changed |
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 |
Why haven't you run |
There's no use of marking issues resolved. I can still see them and you will still need to resolve them |
app/factories/access_code.py
Outdated
@@ -11,6 +11,7 @@ class AccessCodeFactory(factory.alchemy.SQLAlchemyModelFactory): | |||
class Meta: | |||
model = AccessCode | |||
sqlalchemy_session = db.session | |||
sqlalchemy_session_persistence = 'commit' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why add this?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
app/factories/access_code.py
Outdated
valid_from = common.date_ | ||
valid_till = common.dateEnd_ | ||
event_id = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert these changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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
app/factories/ticket_holder.py
Outdated
from app.models.ticket_holder import TicketHolder, db | ||
|
||
|
||
class TicketHolderFactory(factory.alchemy.SQLAlchemyModelFactory): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AttendeeFactory already exists
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will remove thanks!
test_export_sessions_with_details_csv and test_delete_ticket_holders_with_no_order_id are failing in travis build. |
Since you have removed created_at from factories, you have to fix both tests to restore old behaviour |
You have to fix the test, not the schedule task |
actually, the test is dependent on the schedule task. Before it somehow deleted the ticketHolder and test passed. Now it is not doing so |
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?
There is no somehow with computers. There's always a reason. They are deterministic machines |
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. |
updated with upstream
if model_factory in models_with_utcnow: | ||
current_time = datetime.utcnow().astimezone() | ||
else: | ||
current_time = datetime.now(timezone.utc).astimezone() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
- 2020-02-02 20:24:32.051071+05:30
- 2020-02-02 14:54:32.064461+05:30
Thanks for contributing and being patient :) |
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 |
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:
Checklist
development
branch.