在 mongodb 中测试数据 - 时间不同

我目前正在测试我的数据库可访问性,但遇到了一个严重的问题。我正在模拟数据库并插入一个具有注册日期的用户对象。该日期首先写入一个变量,然后传递给应产生完全相同值的数据。嗯,它不...


数据文件


class User(Document):

    email = EmailField(required=True)

    first_name = StringField(max_length=50)

    last_name = StringField(required=True, max_length=50)

    registration_date = DateTimeField(default=datetime.utcnow())

    last_login = DateTimeField()

test_storage.py


import datetime

import pytest


from mongoengine.connection import (

    connect,

    get_connection,

)


from mongodb.data import User

from storage.users import get_users



FMT = "%Y-%m-%d %H:%M"



@pytest.fixture(scope='module')

def mock_db():

    connect('mongoenginetest', host='mongomock://localhost')


@pytest.fixture(scope='module')

def mock_user(mock_db):

    user = {

        'email': 'wijgowol@kogun.gi',

        'registration_date': datetime.datetime.utcnow(),

        'last_name': 'Bowen',

        'first_name': 'Charlie',

    }

    db_user = User (**user)

    db_user.save()


    return user, db_user




def test_storage_get_user(mock_user):

    user, db_user = mock_user


    users = get_users()

    assert len(users) == 1

    assert type(users[0]) == User


    u = users[0]

    assert u == db_user

    assert u.email == user['email']

    assert u.registration_date == user['registration_date']

    assert u.last_name == user['last_name']

    assert u.first_name == user['first_name']

当我运行测试时,我得到以下结果:


_____________________________________________________________________________________________________________________ test_storage_get_user _____________________________________________________________________________________________________________________


mock_user = ({'email': 'wijgowol@kogun.gi', 'first_name': 'Charlie', 'last_name': 'Bowen', 'registration_date': datetime.datetime(2018, 10, 4, 10, 49, 56, 814025)}, <User: User object>)


def test_storage_get_user(mock_user):

    user, db_user = mock_user


    users = get_users()

    assert len(users) == 1

    assert type(users[0]) == User


为什么那里的时间不一样?


湖上湖
浏览 151回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python