~benji/lazr.restful/tweak-etag

« back to all changes in this revision

Viewing changes to src/lazr/restful/tests/test_etag.py

  • Committer: Benji York
  • Date: 2010-09-28 16:15:55 UTC
  • Revision ID: benji@benji-laptop-20100928161555-2ngpfhy2xzirt9kr
add forgotten file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2008 Canonical Ltd.  All rights reserved.
 
2
"""Tests for ETag generation."""
 
3
 
 
4
import unittest
 
5
 
 
6
from lazr.restful._resource import make_entry_etag_cores
 
7
 
 
8
class TestEntryResourceETags(unittest.TestCase):
 
9
    # The EntryResource uses the field values that can be written or might
 
10
    # othwerise change as the basis for its ETags.  The make_entry_etag_cores
 
11
    # function is passed the data about the fields and returns the read and
 
12
    # write cores.
 
13
 
 
14
    def test_no_field_details(self):
 
15
        # If make_entry_etag_cores is given no field details (because no
 
16
        # fields exist), the resulting cores empty strings.
 
17
        self.assertEquals(make_entry_etag_cores([]), ['', ''])
 
18
 
 
19
    def test_writable_fields(self):
 
20
        # If there are writable fields, their values are incorporated into the
 
21
        # writable portion of the cores.
 
22
        field_details = [
 
23
            ('first_field',
 
24
             {'writable': True,
 
25
              'modifiable': False,
 
26
              'value': 'first'}),
 
27
            ('second_field',
 
28
             {'writable': True,
 
29
              'modifiable': False,
 
30
              'value': 'second'}),
 
31
            ]
 
32
        self.assertEquals(
 
33
            make_entry_etag_cores(field_details), ['', 'first\0second'])
 
34
 
 
35
    def test_modifiable_fields(self):
 
36
        # If there are modifiable fields -- fields that aren't writable, but
 
37
        # might change nonetheless -- their values are incorporated into the
 
38
        # "readable" portion of the cores.
 
39
        field_details = [
 
40
            ('first_field',
 
41
             {'writable': False,
 
42
              'modifiable': True,
 
43
              'value': 'first'}),
 
44
            ('second_field',
 
45
             {'writable': False,
 
46
              'modifiable': True,
 
47
              'value': 'second'}),
 
48
            ]
 
49
        self.assertEquals(
 
50
            make_entry_etag_cores(field_details), ['first\0second', ''])
 
51
 
 
52
    def test_unchanging_fields(self):
 
53
        # If there are fields that are not modifiable their values are not
 
54
        # reflected in the generated cores.
 
55
        field_details = [
 
56
            ('first_field',
 
57
             {'writable': False,
 
58
              'modifiable': False,
 
59
              'value': 'the value'}),
 
60
            ]
 
61
        self.assertEquals(
 
62
            make_entry_etag_cores(field_details),
 
63
            ['', ''])
 
64
 
 
65
    def test_combinations_of_fields(self):
 
66
        # If there are a combination of writable, changable, and unchanable
 
67
        # fields, their values are -- or are not in the case of unchanable
 
68
        # fields -- reflected in the resulting cores.
 
69
        field_details = [
 
70
            ('first_writable',
 
71
             {'writable': True,
 
72
              'modifiable': False,
 
73
              'value': 'first-writable'}),
 
74
            ('second_modifiable',
 
75
             {'writable': True,
 
76
              'modifiable': False,
 
77
              'value': 'second-writable'}),
 
78
            ('first_modifiable',
 
79
             {'writable': False,
 
80
              'modifiable': True,
 
81
              'value': 'first-modifiable'}),
 
82
            ('second_modifiable',
 
83
             {'writable': False,
 
84
              'modifiable': True,
 
85
              'value': 'second-modifiable'}),
 
86
            ('first_unchanging',
 
87
             {'writable': False,
 
88
              'modifiable': False,
 
89
              'value': 'first-unchanging'}),
 
90
            ('second_unchanging',
 
91
             {'writable': False,
 
92
              'modifiable': False,
 
93
              'value': 'second-unchanging'}),
 
94
            ]
 
95
        self.assertEquals(
 
96
            make_entry_etag_cores(field_details),
 
97
            ['first-modifiable\0second-modifiable',
 
98
             'first-writable\0second-writable'])