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 33 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
3 changes: 1 addition & 2 deletions app/factories/access_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class AccessCodeFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = AccessCode
sqlalchemy_session = db.session
sqlalchemy_session_persistence = 'commit'
me-diru marked this conversation as resolved.
Show resolved Hide resolved
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


tickets = factory.RelatedFactory(TicketFactory)
user = factory.RelatedFactory(UserFactory)
Expand All @@ -21,7 +22,5 @@ class Meta:
tickets_number = 30
min_quantity = 10
max_quantity = 20
marketer_id = 1
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

3 changes: 1 addition & 2 deletions app/factories/discount_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class DiscountCodeFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = DiscountCode
sqlalchemy_session = db.session
sqlalchemy_session_persistence = 'commit'
me-diru marked this conversation as resolved.
Show resolved Hide resolved

marketer = factory.RelatedFactory(UserFactory)
code = common.string_
Expand All @@ -24,8 +25,6 @@ class Meta:
valid_from = common.date_
valid_till = common.dateEnd_
used_for = "event"
marketer_id = 1
event_id = None


class DiscountCodeTicketFactory(factory.alchemy.SQLAlchemyModelFactory):
Expand Down
1 change: 1 addition & 0 deletions app/factories/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class EventFactoryBasic(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = Event
sqlalchemy_session = db.session
sqlalchemy_session_persistence = 'commit'
me-diru marked this conversation as resolved.
Show resolved Hide resolved

name = common.string_
external_event_url = common.url_
Expand Down
4 changes: 1 addition & 3 deletions app/factories/event_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class EventInvoiceFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = EventInvoice
sqlalchemy_session = db.session
sqlalchemy_session_persistence = 'commit'
me-diru marked this conversation as resolved.
Show resolved Hide resolved

event = factory.RelatedFactory(EventFactoryBasic)
user = factory.RelatedFactory(UserFactory)
Expand All @@ -30,6 +31,3 @@ class Meta:
last4 = "1234"
stripe_token = common.string_
paypal_token = common.string_
event_id = 1
user_id = 2
discount_code_id = 1
2 changes: 1 addition & 1 deletion app/factories/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class OrderFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = Order
sqlalchemy_session = db.session
sqlalchemy_session_persistence = 'commit'
me-diru marked this conversation as resolved.
Show resolved Hide resolved

event = factory.RelatedFactory(EventFactoryBasic)
event_id = 1
payment_mode = 'free'
status = 'initializing'
4 changes: 1 addition & 3 deletions app/factories/role_invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ class RoleInviteFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = RoleInvite
sqlalchemy_session = db.session
sqlalchemy_session_persistence = 'commit'
me-diru marked this conversation as resolved.
Show resolved Hide resolved

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_
event_id = 1
role_id = 1
6 changes: 1 addition & 5 deletions app/factories/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class SessionFactoryBase(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = Session
sqlalchemy_session = db.session
sqlalchemy_session_persistence = 'commit'
me-diru marked this conversation as resolved.
Show resolved Hide resolved

title = common.string_
subtitle = common.string_
Expand All @@ -27,13 +28,8 @@ 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
session_type_id = 1
track_id = 1
microlocation_id = 1


class SessionFactory(SessionFactoryBase):
Expand Down
35 changes: 35 additions & 0 deletions app/factories/ticket_holder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import factory

import app.factories.common as common
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!

class Meta:
model = TicketHolder
sqlalchemy_session = db.session
sqlalchemy_session_persistence = 'commit'
me-diru marked this conversation as resolved.
Show resolved Hide resolved

firstname = 'John'
lastname = 'Doe'
email = common.email_
address = common.string_
city = common.string_
state = common.string_
country = common.country_
job_title = common.string_
phone = common.string_
billing_address = common.string_
home_address = common.string_
shipping_address = common.string_
company = common.string_
work_address = common.string_
work_phone = common.string_
website = common.url_
blog = common.url_
twitter = common.socialUrl_('twitter')
facebook = common.socialUrl_('facebook')
github = common.socialUrl_('github')
age_group = common.string_
birth_date = common.date_
pdf_url = common.url_
1 change: 1 addition & 0 deletions app/factories/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class UserFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = User
sqlalchemy_session = db.session
sqlalchemy_session_persistence = 'commit'
me-diru marked this conversation as resolved.
Show resolved Hide resolved

email = common.email_
password = 'password'
Expand Down
13 changes: 13 additions & 0 deletions app/factories/user_token_blacklist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import factory

from app.models.user_token_blacklist import UserTokenBlackListTime, db
from app.factories.user import UserFactory


class UserTokenBlacklistFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = UserTokenBlackListTime
sqlalchemy_session = db.session
sqlalchemy_session_persistence = 'commit'
me-diru marked this conversation as resolved.
Show resolved Hide resolved

user = factory.SubFactory(UserFactory)
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
55 changes: 55 additions & 0 deletions tests/all/unit/api/validation/test_createdat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import unittest
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.ticket_holder import TicketHolderFactory
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,
TicketHolderFactory,
SessionFactory,
UserTokenBlacklistFactory,
]
models_with_utcnow = [
TicketHolderFactory,
DiscountCodeFactory,
EventInvoiceFactory,
]
for model_factory in model_factories:
with self.subTest(model_factory=model_factory):
test_model = model_factory()
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=500)
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()