File: test_stats.py

package info (click to toggle)
python-admesh 0.98.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 244 kB
  • sloc: python: 331; sh: 97; makefile: 4
file content (67 lines) | stat: -rw-r--r-- 2,420 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
import sys
import pytest
from admesh import Stl
from utils import asset


pypy2 = '__pypy__' in sys.builtin_module_names and sys.version_info.major == 2


class TestStats(object):
    '''Tests that stats are propagated'''
    def test_number_of_facets(self):
        '''Tests if block has 12 facets'''
        stl = Stl(asset('block.stl'))
        assert stl.stats['number_of_facets'] == 12

    def test_volume(self):
        '''Tests the volume of the block'''
        stl = Stl(asset('block.stl'))
        stl.calculate_volume()
        assert stl.stats['volume'] == 1

    def test_header(self):
        '''Tests the header of the block'''
        stl = Stl(asset('block.stl'))
        assert stl.stats['header'] == 'solid  admesh'.encode('UTF-8')

    def test_add_facets_updates_stats(self):
        '''Tests if adding new facets updates the mesh stats'''
        stl = Stl(asset('block.stl'))
        max_x = stl.stats['max']['x']
        bounding_diameter = stl.stats['bounding_diameter']
        stl.add_facets([(((0, 0, 0), (1, 1, 1), (max_x + 1, 0, 0)), (1, 0, 0))])
        assert max_x + 1 == stl.stats['max']['x']
        assert bounding_diameter < stl.stats['bounding_diameter']

    @pytest.mark.parametrize('type', ('iterable', 'dict'))
    def test_stats_are_same_with_created(self, type):
        ''''Test a manually constructed cube has the same stats as if loaded'''
        XYZ = ('x', 'y', 'z')
        stl1 = Stl(asset('block.stl'))
        if type == 'iterable':
            facets = [[[[v[a] for a in XYZ] for v in f['vertex']],
                       [f['normal'][a] for a in XYZ]] for f in stl1]
        else:
            facets = list(stl1)
        stl2 = Stl()
        stl2.add_facets(facets)
        stats1, stats2 = stl1.stats, stl2.stats
        for stats in (stats1, stats2):
            del stats['type']  # ASCII != INMEMORY
            del stats['original_num_facets']  # 12 != 0
            del stats['header']  # nothing != "solid  admesh"
        assert stats1 == stats2

    def test_write_to_stats(self):
        '''Test if writing to stats raises exception'''
        stl = Stl()
        with pytest.raises(TypeError if pypy2 else AttributeError):
            stl.stats = {}

    def test_delete_stats(self):
        '''Test if deleting stats raises exception'''
        stl = Stl()
        with pytest.raises(AttributeError):
            del stl.stats