Add update timestamp defaults to lexicon and article

This commit is contained in:
Tim Van Baak 2021-08-02 21:59:45 -07:00
parent 10a0c59d5e
commit 9025be28bf
3 changed files with 38 additions and 10 deletions

View File

@ -11,6 +11,7 @@ from sqlalchemy import (
DateTime, DateTime,
Enum, Enum,
ForeignKey, ForeignKey,
func,
Integer, Integer,
String, String,
Text, Text,
@ -81,7 +82,7 @@ class User(ModelBase):
#################### ####################
# The timestamp the user was created # 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 # The timestamp the user last logged in
# This is NULL if the user has never 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 # 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 # The timestamp of the last change in game state
last_updated = Column( 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 # The timestamp the first turn was started
@ -287,7 +288,7 @@ class Membership(ModelBase):
#################### ####################
# Timestamp the user joined the game # 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 # Timestamp of the last time the user viewed the post feed
# This is NULL if the player has never viewed posts # 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 # Timestamp the content of the article was last updated
last_updated = Column( 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 # Timestamp the article was last submitted
@ -646,7 +647,7 @@ class Post(ModelBase):
################ ################
# The timestamp the post was created # 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 # The body of the post
body = Column(Text, nullable=False) body = Column(Text, nullable=False)

View File

@ -1,13 +1,14 @@
import pytest import pytest
import time
from amanuensis.db import DbContext from amanuensis.db import DbContext
from amanuensis.db.models import Character, Lexicon, User from amanuensis.db.models import Character, Lexicon, User
import amanuensis.backend.article as artiq import amanuensis.backend.article as artiq
from amanuensis.errors import ArgumentError 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""" """Test new article creation"""
# Create two users in a shared lexicon # Create two users in a shared lexicon
user1: User = make.user() 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, user1.id, character_id=None)
assert artiq.create(db, lexicon1.id, user2.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) 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

View File

@ -1,10 +1,12 @@
import datetime import datetime
import time
import pytest import pytest
import amanuensis.backend.lexicon as lexiq import amanuensis.backend.lexicon as lexiq
from amanuensis.db import DbContext, Lexicon, User from amanuensis.db import DbContext, Lexicon, User
from amanuensis.errors import ArgumentError from amanuensis.errors import ArgumentError
from tests.conftest import ObjectFactory
def test_create_lexicon(db: DbContext): def test_create_lexicon(db: DbContext):
@ -52,7 +54,7 @@ def test_create_lexicon(db: DbContext):
lexiq.create(**defaults) lexiq.create(**defaults)
def test_lexicon_from(db: DbContext, make): def test_lexicon_from(db: DbContext, make: ObjectFactory):
"""Test lexiq.from_*.""" """Test lexiq.from_*."""
lexicon1: Lexicon = make.lexicon() lexicon1: Lexicon = make.lexicon()
lexicon2: 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 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.""" """Test the various scoped get functions."""
user: User = make.user() user: User = make.user()
@ -97,3 +99,13 @@ def test_get_lexicon(db: DbContext, make):
assert private_joined not in get_public assert private_joined not in get_public
assert public_open in get_public assert public_open in get_public
assert private_open not 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