Skip to content

How do the isolation test #375

@sir2024

Description

@sir2024

How do the isolation test

The project structure


$ tree -L 2
.
├── config.py
├── sandbox
│   ├── config.py
│   ├── __init__.py
│   ├── main.py
│   ├── models.py
│   └── tasks.py
├── settings.toml
├── templates
│   ├── compose_template
...
└── tests
    ├── common.py
    ├── conftest.py
    ├── __init__.py
    ├── test_models.py
    └── test_sandbox.py

11 directories, 48 files

sandbox/models.py

from pymongo import MongoClient
from umongo import Document, EmbeddedDocument, fields, validate
from umongo.frameworks import PyMongoInstance


username = "root"
password = "example"
host = "192.168.204.129"
uri = f"mongodb://{username}:{password}@{host}"
db = MongoClient(uri).ioc
# instance = PyMongoInstance(db)

instance = PyMongoInstance()
instance.set_db(db)

...

@instance.register
class UrlStatusTable(BaseIOCDocument):
    value = fields.StrField(required=True) # url
    hash_infos = fields.ListField(fields.ReferenceField(HashTable))

    class Meta:
        collection_name = "UrlStatusTable"

tests/test_models.py

import pytest
from pymongo import MongoClient
from config import settings

from sandbox.models import (
  UrlStatusTable, PocStatusTable
)

from .common import BaseTest, BaseDBTest, TEST_DB

def make_db():
        
    host = settings.mongodb.host
    username = settings.mongodb.username
    password = settings.mongodb.password
    uri = f"mongodb://{username}:{password}@{host}"
    TEST_DB = settings.mongodb.test_dbname						#   test db name:   ioc_test

    return MongoClient(uri)[TEST_DB]

@pytest.fixture
def db():
    return make_db()


class TestURLCRUD(BaseDBTest):
  
    def test_url_simple_crud(self):
        md5 = HashTable(value="7adcd9e020cf273b7f197abcf9305173", value_type="md5", source="url")
        md5.commit() 
        baidu = UrlStatusTable(value="www.baidu.com")
        baidu.hash_infos = [md5]
        # print(baidu.dump())


        assert baidu.value == "www.baidu.com"
        assert baidu.dump()['hash_infos'][0] == str(md5.id)
        assert baidu.hash_infos[0].fetch().value_type == "md5"
        assert baidu.hash_infos[0].fetch().source == "url"

if __name__ == "__main__":
    pytest.main(['-s', 'test_models.py'])

run pytest

root @ c7 in .../sec/sandbox |06:24:35  |master U:8 ✗|
$ pytest -v
========================================================================== test session starts ==========================================================================
platform linux -- Python 3.8.13, pytest-7.1.2, pluggy-0.13.1 -- /root/.cache/pypoetry/virtualenvs/sandbox-Yi_B9d---py3.8/bin/python
cachedir: .pytest_cache
rootdir: /home/projects/sec/sandbox, configfile: pytest.ini
plugins: anyio-3.5.0
collected 2 items

tests/test_models.py::TestURLCRUD::test_url_simple_crud PASSED                                                                                                    [ 50%]
tests/test_sandbox.py::test_version PASSED                                                                                                                        [100%]

=========================================================================== 2 passed in 0.44s ===========================================================================

The document model is already decorated with decorators when it is imported, and the test data is written directly to the database specified by instance.set_db(db) in the models.py file, but not to the specified test database ioc_test

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions