|
| 1 | +from tests.test_helper import * |
| 2 | + |
| 3 | +class TestCustomerSearch(unittest.TestCase): |
| 4 | + def test_advanced_search_no_results(self): |
| 5 | + collection = Transaction.search([ |
| 6 | + TransactionSearch.billing_first_name == "no_such_person" |
| 7 | + ]) |
| 8 | + self.assertEquals(0, collection.maximum_size) |
| 9 | + |
| 10 | + def test_advanced_search_searches_all_text_fields(self): |
| 11 | + token = "creditcard%s" % randint(1, 100000) |
| 12 | + |
| 13 | + customer = Customer.create({ |
| 14 | + "first_name": "Timmy", |
| 15 | + "last_name": "O'Toole", |
| 16 | + "company": "O'Toole and Son(s)", |
| 17 | + "email": "timmy@example.com", |
| 18 | + "fax": "3145551234", |
| 19 | + "phone": "5551231234", |
| 20 | + "website": "http://example.com", |
| 21 | + "credit_card": { |
| 22 | + "cardholder_name": "Tim Toole", |
| 23 | + "number": "4111111111111111", |
| 24 | + "expiration_date": "05/2010", |
| 25 | + "token": token, |
| 26 | + "billing_address": { |
| 27 | + "first_name": "Thomas", |
| 28 | + "last_name": "Otool", |
| 29 | + "street_address": "1 E Main St", |
| 30 | + "extended_address": "Suite 3", |
| 31 | + "locality": "Chicago", |
| 32 | + "region": "Illinois", |
| 33 | + "postal_code": "60622", |
| 34 | + "country_name": "United States of America" |
| 35 | + } |
| 36 | + } |
| 37 | + }).customer |
| 38 | + |
| 39 | + search_criteria = { |
| 40 | + "first_name": "Timmy", |
| 41 | + "last_name": "O'Toole", |
| 42 | + "company": "O'Toole and Son(s)", |
| 43 | + "email": "timmy@example.com", |
| 44 | + "phone": "5551231234", |
| 45 | + "fax": "3145551234", |
| 46 | + "website": "http://example.com", |
| 47 | + "address_first_name": "Thomas", |
| 48 | + "address_last_name": "Otool", |
| 49 | + "address_street_address": "1 E Main St", |
| 50 | + "address_postal_code": "60622", |
| 51 | + "address_extended_address": "Suite 3", |
| 52 | + "address_locality": "Chicago", |
| 53 | + "address_region": "Illinois", |
| 54 | + "payment_method_token": token, |
| 55 | + "cardholder_name": "Tim Toole", |
| 56 | + "credit_card_number": "4111111111111111", |
| 57 | + "credit_card_expiration_date": "05/2010" |
| 58 | + } |
| 59 | + |
| 60 | + criteria = [getattr(CustomerSearch, search_field) == value for search_field, value in search_criteria.items()] |
| 61 | + criteria.append(CustomerSearch.id == customer.id) |
| 62 | + |
| 63 | + collection = Customer.search(criteria) |
| 64 | + |
| 65 | + self.assertEquals(1, collection.maximum_size) |
| 66 | + self.assertEquals(customer.id, collection.first.id) |
| 67 | + |
| 68 | + for search_field, value in search_criteria.items(): |
| 69 | + collection = Customer.search( |
| 70 | + CustomerSearch.id == customer.id, |
| 71 | + getattr(CustomerSearch, search_field) == value |
| 72 | + ) |
| 73 | + |
| 74 | + self.assertEquals(1, collection.maximum_size) |
| 75 | + self.assertEquals(customer.id, collection.first.id) |
| 76 | + |
| 77 | + def test_advanced_search_range_node_created_at(self): |
| 78 | + customer = Customer.create().customer |
| 79 | + |
| 80 | + past = customer.created_at - timedelta(minutes=10) |
| 81 | + future = customer.created_at + timedelta(minutes=10) |
| 82 | + |
| 83 | + collection = Customer.search( |
| 84 | + CustomerSearch.id == customer.id, |
| 85 | + CustomerSearch.created_at.between(past, future) |
| 86 | + ) |
| 87 | + |
| 88 | + self.assertEquals(1, collection.maximum_size) |
| 89 | + self.assertEquals(customer.id, collection.first.id) |
| 90 | + |
| 91 | + collection = Customer.search( |
| 92 | + CustomerSearch.id == customer.id, |
| 93 | + CustomerSearch.created_at <= future |
| 94 | + ) |
| 95 | + |
| 96 | + self.assertEquals(1, collection.maximum_size) |
| 97 | + self.assertEquals(customer.id, collection.first.id) |
| 98 | + |
| 99 | + collection = Customer.search( |
| 100 | + CustomerSearch.id == customer.id, |
| 101 | + CustomerSearch.created_at >= past |
| 102 | + ) |
| 103 | + |
| 104 | + self.assertEquals(1, collection.maximum_size) |
| 105 | + self.assertEquals(customer.id, collection.first.id) |
0 commit comments