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

How do the isolation test #375

Open
g0lang opened this issue May 5, 2022 · 0 comments
Open

How do the isolation test #375

g0lang opened this issue May 5, 2022 · 0 comments

Comments

@g0lang
Copy link

g0lang commented May 5, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant