1#!/usr/bin/python -S 2# 3# Copyright 2014 Google Inc. All rights reserved. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17""" 18sum_bits_test.py: Tests for sum_bits.py 19""" 20 21import cStringIO 22import unittest 23 24import rappor 25import sum_bits # module under test 26 27 28CSV_IN = """\ 29user_id,cohort,bloom,prr,rappor 305,1,dummy,dummy,0000111100001111 315,1,dummy,dummy,0000000000111100 32""" 33 34# NOTE: bit order is reversed. 35EXPECTED_CSV_OUT = """\ 360,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\r 372,1,1,2,2,1,1,0,0,1,1,1,1,0,0,0,0\r 38""" 39 40TOO_MANY_COLUMNS = """\ 41user_id,cohort,rappor 425,1,0000111100001111,extra 43""" 44 45 46class SumBitsTest(unittest.TestCase): 47 48 def setUp(self): 49 self.params = rappor.Params() 50 self.params.num_bloombits = 16 51 self.params.num_cohorts = 2 52 53 def testSum(self): 54 stdin = cStringIO.StringIO(CSV_IN) 55 stdout = cStringIO.StringIO() 56 57 sum_bits.SumBits(self.params, stdin, stdout) 58 59 self.assertMultiLineEqual(EXPECTED_CSV_OUT, stdout.getvalue()) 60 61 def testErrors(self): 62 stdin = cStringIO.StringIO(TOO_MANY_COLUMNS) 63 stdout = cStringIO.StringIO() 64 65 self.assertRaises( 66 RuntimeError, sum_bits.SumBits, self.params, stdin, stdout) 67 68 69if __name__ == '__main__': 70 unittest.main() 71