From 9025be28bfba6c6fe49087835b568466355bada7 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Mon, 2 Aug 2021 21:59:45 -0700 Subject: [PATCH] Add update timestamp defaults to lexicon and article --- amanuensis/db/models.py | 13 +++++++------ tests/backend/test_article.py | 19 +++++++++++++++++-- tests/backend/test_lexicon.py | 16 ++++++++++++++-- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/amanuensis/db/models.py b/amanuensis/db/models.py index c304e13..5f7a600 100644 --- a/amanuensis/db/models.py +++ b/amanuensis/db/models.py @@ -11,6 +11,7 @@ from sqlalchemy import ( DateTime, Enum, ForeignKey, + func, Integer, String, Text, @@ -81,7 +82,7 @@ class User(ModelBase): #################### # The timestamp the user was created - created = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP")) + created = Column(DateTime, nullable=False, server_default=func.now()) # The timestamp the user last logged in # This is NULL if the user has never logged in @@ -149,11 +150,11 @@ class Lexicon(ModelBase): #################### # The timestamp the lexicon was created - created = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP")) + created = Column(DateTime, nullable=False, server_default=func.now()) # The timestamp of the last change in game state last_updated = Column( - DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP") + DateTime, nullable=False, server_default=func.now(), onupdate=func.now() ) # The timestamp the first turn was started @@ -287,7 +288,7 @@ class Membership(ModelBase): #################### # Timestamp the user joined the game - joined = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP")) + joined = Column(DateTime, nullable=False, server_default=func.now()) # Timestamp of the last time the user viewed the post feed # This is NULL if the player has never viewed posts @@ -420,7 +421,7 @@ class Article(ModelBase): # Timestamp the content of the article was last updated last_updated = Column( - DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP") + DateTime, nullable=False, server_default=func.now(), onupdate=func.now() ) # Timestamp the article was last submitted @@ -646,7 +647,7 @@ class Post(ModelBase): ################ # The timestamp the post was created - created = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP")) + created = Column(DateTime, nullable=False, server_default=func.now()) # The body of the post body = Column(Text, nullable=False) diff --git a/tests/backend/test_article.py b/tests/backend/test_article.py index b19a088..7dafe02 100644 --- a/tests/backend/test_article.py +++ b/tests/backend/test_article.py @@ -1,13 +1,14 @@ import pytest +import time from amanuensis.db import DbContext from amanuensis.db.models import Character, Lexicon, User import amanuensis.backend.article as artiq - from amanuensis.errors import ArgumentError +from tests.conftest import ObjectFactory -def test_create_article(db: DbContext, make): +def test_create_article(db: DbContext, make: ObjectFactory): """Test new article creation""" # Create two users in a shared lexicon user1: User = make.user() @@ -52,3 +53,17 @@ def test_create_article(db: DbContext, make): assert artiq.create(db, lexicon1.id, user1.id, character_id=None) assert artiq.create(db, lexicon1.id, user2.id, character_id=None) assert artiq.create(db, lexicon2.id, user2.id, character_id=None) + + +def test_article_update_ts(db: DbContext, make: ObjectFactory): + """Test the update timestamp.""" + user: User = make.user() + lexicon: Lexicon = make.lexicon() + make.membership(user_id=user.id, lexicon_id=lexicon.id) + char: Character = make.character(lexicon_id=lexicon.id, user_id=user.id) + article = artiq.create(db, lexicon.id, user.id, char.id) + created = article.last_updated + time.sleep(1) # The update timestamp has only second-level precision + article.title = "New title, who dis" + db.session.commit() + assert created != article.last_updated diff --git a/tests/backend/test_lexicon.py b/tests/backend/test_lexicon.py index 41caef4..b2f07c9 100644 --- a/tests/backend/test_lexicon.py +++ b/tests/backend/test_lexicon.py @@ -1,10 +1,12 @@ import datetime +import time import pytest import amanuensis.backend.lexicon as lexiq from amanuensis.db import DbContext, Lexicon, User from amanuensis.errors import ArgumentError +from tests.conftest import ObjectFactory def test_create_lexicon(db: DbContext): @@ -52,7 +54,7 @@ def test_create_lexicon(db: DbContext): lexiq.create(**defaults) -def test_lexicon_from(db: DbContext, make): +def test_lexicon_from(db: DbContext, make: ObjectFactory): """Test lexiq.from_*.""" lexicon1: Lexicon = make.lexicon() lexicon2: Lexicon = make.lexicon() @@ -60,7 +62,7 @@ def test_lexicon_from(db: DbContext, make): assert lexiq.from_name(db, lexicon2.name) == lexicon2 -def test_get_lexicon(db: DbContext, make): +def test_get_lexicon(db: DbContext, make: ObjectFactory): """Test the various scoped get functions.""" user: User = make.user() @@ -97,3 +99,13 @@ def test_get_lexicon(db: DbContext, make): assert private_joined not in get_public assert public_open in get_public assert private_open not in get_public + + +def test_lexicon_update_ts(db: DbContext, make: ObjectFactory): + """Test the update timestamp.""" + lexicon: Lexicon = make.lexicon() + assert lexicon.created == lexicon.last_updated + time.sleep(1) # The update timestamp has only second-level precision + lexicon.prompt = "New prompt, who dis" + db.session.commit() + assert lexicon.created != lexicon.last_updated