File: test_compound.py

package info (click to toggle)
python-chemspipy 2.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 288 kB
  • sloc: python: 1,125; makefile: 14
file content (167 lines) | stat: -rw-r--r-- 5,309 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -*- coding: utf-8 -*-
"""
test_compound
~~~~~~~~~~~~~

Test the Compound object.

"""

from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
import logging
import os

import pytest
import requests

from chemspipy import ChemSpider, Compound


logging.basicConfig(level=logging.WARN, format='%(levelname)s:%(name)s:(%(threadName)-10s):%(message)s')
logging.getLogger('chemspipy').setLevel(logging.DEBUG)

# API key is retrieved from environment variables
CHEMSPIDER_API_KEY = os.environ['CHEMSPIDER_API_KEY']
cs = ChemSpider(CHEMSPIDER_API_KEY)


def test_get_compound():
    """Test getting a compound by ChemSpider ID."""
    compound = cs.get_compound(2157)
    assert isinstance(compound, Compound)
    assert compound.record_id == 2157
    with pytest.deprecated_call():
        assert compound.csid == 2157
    compound = cs.get_compound('2157')
    assert isinstance(compound, Compound)
    assert compound.record_id == 2157
    with pytest.deprecated_call():
        assert compound.csid == 2157


def test_get_compounds():
    """Test getting multiple compounds by ChemSpider ID."""
    compounds = cs.get_compounds([2157, 13837760])
    assert [c.csid for c in compounds], [2157 == 13837760]
    for c in compounds:
        assert 'http://' in c.image_url
        assert c.average_mass > 0


def test_compound_init():
    """Test instantiating a Compound directly."""
    compound = Compound(cs, 2157)
    assert compound.csid == 2157


def test_compound_equality():
    """Test equality test by ChemSpider ID."""
    c1 = cs.get_compound(13837760)
    c2 = cs.get_compound(2157)
    c3 = cs.get_compound(2157)
    assert c1 != c2
    assert c2 == c3


def test_compound_repr():
    """Test Compound object repr."""
    assert repr(cs.get_compound(1234)) == 'Compound(1234)'


def test_image_url():
    """Test image_url returns a valid URL."""
    url = cs.get_compound(2157).image_url
    response = requests.get(url)
    assert 'http://www.chemspider.com/ImagesHandler.ashx?id=' in url
    assert response.status_code == 200


def test_molecular_formula():
    """Test Compound property molecular_formula."""
    compound = cs.get_compound(2157)
    assert compound.molecular_formula == 'C_{9}H_{8}O_{4}'
    # Ensure value is the same on subsequent access from cache
    assert compound.molecular_formula == 'C_{9}H_{8}O_{4}'


def test_smiles():
    """Test Compound property smiles."""
    compound = cs.get_compound(2157)
    assert compound.smiles == 'CC(=O)Oc1ccccc1C(=O)O'
    # Ensure value is the same on subsequent access from cache
    assert compound.smiles == 'CC(=O)Oc1ccccc1C(=O)O'


def test_inchi():
    """Test Compound property inchi."""
    compound = cs.get_compound(2157)
    assert compound.inchi == 'InChI=1S/C9H8O4/c1-6(10)13-8-5-3-2-4-7(8)9(11)12/h2-5H,1H3,(H,11,12)'
    # Ensure value is the same on subsequent access from cache
    assert compound.inchi == 'InChI=1S/C9H8O4/c1-6(10)13-8-5-3-2-4-7(8)9(11)12/h2-5H,1H3,(H,11,12)'


def test_stdinchi():
    """Test Compound property stdinchi."""
    compound = cs.get_compound(2157)
    with pytest.deprecated_call():
        assert compound.stdinchi == 'InChI=1S/C9H8O4/c1-6(10)13-8-5-3-2-4-7(8)9(11)12/h2-5H,1H3,(H,11,12)'
        # Ensure value is the same on subsequent access from cache
        assert compound.stdinchi == 'InChI=1S/C9H8O4/c1-6(10)13-8-5-3-2-4-7(8)9(11)12/h2-5H,1H3,(H,11,12)'


def test_inchikey():
    """Test Compound property inchikey."""
    compound = cs.get_compound(2157)
    assert compound.inchikey == 'BSYNRYMUTXBXSQ-UHFFFAOYSA-N'
    # Ensure value is the same on subsequent access from cache
    assert compound.inchikey == 'BSYNRYMUTXBXSQ-UHFFFAOYSA-N'


def test_stdinchikey():
    """Test Compound property stdinchikey."""
    compound = cs.get_compound(2157)
    with pytest.deprecated_call():
        assert compound.stdinchikey == 'BSYNRYMUTXBXSQ-UHFFFAOYSA-N'
        # Ensure value is the same on subsequent access from cache
        assert compound.stdinchikey == 'BSYNRYMUTXBXSQ-UHFFFAOYSA-N'


def test_masses():
    """Test Compound property average_mass, molecular_weight, monoisotopic_mass, nominal_mass."""
    compound = cs.get_compound(2157)
    assert 180 < compound.average_mass < 180.2
    assert 180 < compound.molecular_weight < 180.2
    assert 180 < compound.monoisotopic_mass < 180.2
    assert compound.nominal_mass == 180


def test_name():
    """Test Compound property common_name."""
    compound = cs.get_compound(2157)
    assert compound.common_name == 'Aspirin'


def test_molfiles():
    """Test Compound property mol2d, mol3d, mol_raw."""
    compound = cs.get_compound(2157)
    assert 'V2000' in compound.mol_2d
    assert 'V2000' in compound.mol_3d


def test_image():
    """Test Compound property image."""
    compound = cs.get_compound(2157)
    assert compound.image[:8] == b'\x89PNG\x0d\x0a\x1a\x0a'  # PNG magic number


def test_external_references():
    """Test Compound property external_references."""
    compound = cs.get_compound(97809)
    assert len(compound.external_references) > 50
    for xref in compound.external_references:
        assert 'externalId' in xref
        assert 'externalUrl' in xref
        assert 'source' in xref
        assert 'sourceUrl' in xref