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
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
1c57c7e
changed ENABLE_ELASTICSEARCH to false
me-diru Dec 1, 2019
159d941
confict removed
me-diru Jan 5, 2020
a4e1f4f
reverted unnecessary commit code
me-diru Jan 5, 2020
f6c7a71
Removed conflicting code in .env.example
me-diru Jan 5, 2020
b6fc7d1
Added unittest code to validate createdat is set to current time
me-diru Jan 7, 2020
58bc9a7
Removed extra spaces in test_createdat.py
me-diru Jan 7, 2020
e0a1ac5
Made changes as recommended
me-diru Jan 7, 2020
3407e89
updated docs with correct celery running command
me-diru Jan 8, 2020
3578bb3
made changes as recommended
me-diru Jan 27, 2020
6f88d5a
removed unresolved conflict changes
me-diru Jan 28, 2020
c2a37d4
added necessary libraries and corrected test code
me-diru Jan 28, 2020
79a5fb6
reverted previously updated file permissions
me-diru Jan 28, 2020
7beda8c
changed ENABLE_ELASTICSEARCH to false
me-diru Dec 1, 2019
9a1cdd5
confict removed
me-diru Jan 5, 2020
eaba78f
reverted unnecessary commit code
me-diru Jan 5, 2020
f1d82b8
Removed conflicting code in .env.example
me-diru Jan 5, 2020
5733a7d
Added unittest code to validate createdat is set to current time
me-diru Jan 7, 2020
9d73404
Removed extra spaces in test_createdat.py
me-diru Jan 7, 2020
952dc5f
Made changes as recommended
me-diru Jan 7, 2020
75631fb
updated docs with correct celery running command
me-diru Jan 8, 2020
4ee726f
made changes as recommended
me-diru Jan 27, 2020
a3f1a10
removed unresolved conflict changes
me-diru Jan 28, 2020
15b4863
added necessary libraries and corrected test code
me-diru Jan 28, 2020
fa610c5
reverted previously updated file permissions
me-diru Jan 28, 2020
10ae24b
Merge pull request #4 from fossasia/development
me-diru Jan 30, 2020
952612b
rebase
me-diru Jan 30, 2020
1a9993f
wrote all tests in single function
me-diru Jan 31, 2020
538ea10
Merge pull request #5 from fossasia/development
me-diru Jan 31, 2020
4971648
modified test_created with black and isort commands
me-diru Jan 31, 2020
d6b02f9
Merge pull request #6 from fossasia/development
me-diru Feb 1, 2020
118153b
used factories for testing and added new ones
me-diru Feb 1, 2020
95832a0
used factories for testing and added new ones
me-diru Feb 1, 2020
9944d37
did pre-commit
me-diru Feb 1, 2020
0735ccf
made changes as requested
me-diru Feb 1, 2020
f6fba0e
changed user_blacklist factory to correct one
me-diru Feb 2, 2020
cb91457
correct test_csv_jobs_util for travis
me-diru Feb 2, 2020
c55c679
updated branch in travis for local testing
me-diru Feb 2, 2020
c6f6fa9
cahnged test_schedule_jobs
me-diru Feb 2, 2020
75aa289
corrected test_scheduled_jobs
me-diru Feb 2, 2020
91e0de8
Merge pull request #7 from fossasia/development
me-diru Feb 2, 2020
dfd2b3e
Merge branch 'development' into development
iamareebjamal Feb 2, 2020
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
1 change: 0 additions & 1 deletion app/factories/attendee.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class Meta:
event_id = 1
ticket_id = None
order_id = None
created_at = common.date_
modified_at = common.date_


Expand Down
1 change: 0 additions & 1 deletion app/factories/role_invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class Meta:
event = factory.RelatedFactory(EventFactoryBasic)
role = factory.RelatedFactory(RoleFactory)
email = common.email_
created_at = common.date_
hash = common.string_
status = common.string_
role_name = common.string_
Expand Down
1 change: 0 additions & 1 deletion app/factories/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Meta:
audio_url = common.url_
signup_url = common.url_
state = "accepted"
created_at = common.date_
submitted_at = common.date_
is_mail_sent = True
event_id = 1
Expand Down
11 changes: 11 additions & 0 deletions app/factories/user_token_blacklist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import factory

from app.models.user_token_blacklist import UserTokenBlackListTime, db


class UserTokenBlacklistFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = UserTokenBlackListTime
sqlalchemy_session = db.session

user_id = 1
4 changes: 2 additions & 2 deletions app/models/access_code.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass
from datetime import datetime

from sqlalchemy.sql import func
from app.models import db
from app.models.base import SoftDeletionModel

Expand Down Expand Up @@ -29,7 +29,7 @@ class AccessCode(SoftDeletionModel):
db.Integer, db.ForeignKey('tickets.id', ondelete='CASCADE')
)
event_id: int = db.Column(db.Integer, db.ForeignKey('events.id', ondelete='CASCADE'))
created_at: datetime = db.Column(db.DateTime(timezone=True))
created_at: datetime = db.Column(db.DateTime(timezone=True), default=func.now())
marketer_id: int = db.Column(
db.Integer, db.ForeignKey('users.id', ondelete='CASCADE')
)
Expand Down
4 changes: 2 additions & 2 deletions app/models/discount_code.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime

from sqlalchemy.sql import func
from app.models import db
from app.models.base import SoftDeletionModel

Expand All @@ -23,7 +23,7 @@ class DiscountCode(SoftDeletionModel):
valid_till = db.Column(db.DateTime(timezone=True), nullable=True)
event_id = db.Column(db.Integer, db.ForeignKey('events.id', ondelete='CASCADE'))
event = db.relationship('Event', backref='discount_codes', foreign_keys=[event_id])
created_at = db.Column(db.DateTime(timezone=True))
created_at = db.Column(db.DateTime(timezone=True), default=func.now())
me-diru marked this conversation as resolved.
Show resolved Hide resolved
marketer_id = db.Column(db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'))
marketer = db.relationship('User', backref='discount_codes_')

Expand Down
4 changes: 2 additions & 2 deletions app/models/role_invite.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import random
from datetime import datetime, timedelta

from sqlalchemy.sql import func
import pytz
from sqlalchemy.schema import UniqueConstraint

Expand Down Expand Up @@ -31,7 +31,7 @@ class RoleInvite(SoftDeletionModel):
role = db.relationship("Role")

hash = db.Column(db.String)
created_at = db.Column(db.DateTime(timezone=True))
created_at = db.Column(db.DateTime(timezone=True), default=func.now())

Choose a reason for hiding this comment

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

unexpected indentation

me-diru marked this conversation as resolved.
Show resolved Hide resolved
status = db.Column(db.String, default="pending")

def __init__(
Expand Down
1 change: 1 addition & 0 deletions tests/all/integration/api/helpers/test_csv_jobs_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def test_export_sessions_with_details_csv(self):
long_abstract='long_abstract',
comments='comment',
level='level',
created_at=common.date_,
)
db.session.commit()
field_data = export_sessions_csv([test_session])
Expand Down
3 changes: 2 additions & 1 deletion tests/all/integration/api/helpers/test_scheduled_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
delete_ticket_holders_no_order_id,
event_invoices_mark_due,
)
import app.factories.common as common
from app.factories.attendee import AttendeeFactory
from app.factories.event_invoice import EventInvoiceFactory
from app.models import db
Expand Down Expand Up @@ -39,7 +40,7 @@ def test_delete_ticket_holders_with_no_order_id(self):
"""Method to test deleting ticket holders with no order id after expiry time"""

with self.app.test_request_context():
attendee = AttendeeFactory()
attendee = AttendeeFactory(created_at=common.date_)
db.session.commit()
attendee_id = attendee.id
delete_ticket_holders_no_order_id()
Expand Down
57 changes: 57 additions & 0 deletions tests/all/unit/api/validation/test_createdat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import unittest
from app.models import db
from datetime import datetime, timedelta, timezone
from app.factories.session import SessionFactory
from app.factories.user import UserFactory
from app.factories.event import EventFactoryBasic
from app.factories.access_code import AccessCodeFactory
from app.factories.event_invoice import EventInvoiceFactory
from app.factories.attendee import AttendeeFactory
from app.factories.discount_code import DiscountCodeFactory
from app.factories.order import OrderFactory
from app.factories.role_invite import RoleInviteFactory
from app.factories.user_token_blacklist import UserTokenBlacklistFactory
from tests.all.integration.utils import OpenEventTestCase


class TestCreatedatValidation(OpenEventTestCase):
def test_createdat(self):
""" Validate time : Tests if created_at is set to current time in all models
"""
with self.app.test_request_context():
model_factories = [
UserFactory,
EventInvoiceFactory,
AccessCodeFactory,
EventFactoryBasic,
OrderFactory,
RoleInviteFactory,
DiscountCodeFactory,
AttendeeFactory,
SessionFactory,
UserTokenBlacklistFactory,
]
models_with_utcnow = [
AttendeeFactory,
DiscountCodeFactory,
EventInvoiceFactory,
]
for model_factory in model_factories:
with self.subTest(model_factory=model_factory):
test_model = model_factory()
db.session.commit()
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

createdat_db = test_model.created_at
time_diff = current_time - createdat_db
allowed_time_lag = timedelta(milliseconds=250)
message = "created_at not set" " to current time in {} \n".format(
model_factory
)
self.assertLessEqual(time_diff, allowed_time_lag, message)


if __name__ == "__main__":
iamareebjamal marked this conversation as resolved.
Show resolved Hide resolved
iamareebjamal marked this conversation as resolved.
Show resolved Hide resolved
unittest.main()