|
| 1 | +import os |
| 2 | +import pytest |
| 3 | + |
| 4 | +from pynamodb.attributes import BooleanAttribute, LegacyBooleanAttribute, UnicodeAttribute |
| 5 | +from pynamodb.expressions.operand import Path |
| 6 | +from pynamodb.migration import migrate_boolean_attributes |
| 7 | +from pynamodb.models import Model |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture() |
| 11 | +def ddb_url(): |
| 12 | + """Obtain the URL of a local DynamoDB instance. |
| 13 | +
|
| 14 | + This is meant to be used with something like DynamoDB Local: |
| 15 | +
|
| 16 | + http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html |
| 17 | +
|
| 18 | + It must be set up "out of band"; we merely assume it exists on |
| 19 | + http://localhost:8000 or a URL specified though the |
| 20 | + PYNAMODB_INTEGRATION_TEST_DDB_URL environment variable. |
| 21 | + """ |
| 22 | + ddb_url = os.getenv("PYNAMODB_INTEGRATION_TEST_DDB_URL") |
| 23 | + return "http://localhost:8000" if ddb_url is None else ddb_url |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.ddblocal |
| 27 | +def test_migrate_boolean_attributes_upgrade_path(ddb_url): |
| 28 | + class BAModel(Model): |
| 29 | + class Meta: |
| 30 | + table_name = 'migration_test_lba_to_ba' |
| 31 | + host = ddb_url |
| 32 | + id = UnicodeAttribute(hash_key=True) |
| 33 | + flag = BooleanAttribute(null=True) |
| 34 | + |
| 35 | + class LBAModel(Model): |
| 36 | + class Meta: |
| 37 | + table_name = 'migration_test_lba_to_ba' |
| 38 | + host = ddb_url |
| 39 | + id = UnicodeAttribute(hash_key=True) |
| 40 | + flag = LegacyBooleanAttribute(null=True) |
| 41 | + |
| 42 | + LBAModel.create_table(read_capacity_units=1, write_capacity_units=1) |
| 43 | + |
| 44 | + # Create one "offending" object written as an integer using LBA. |
| 45 | + LBAModel('pkey', flag=True).save() |
| 46 | + assert 1 == len([_ for _ in LBAModel.query('pkey', LBAModel.flag == True)]) |
| 47 | + |
| 48 | + # We should NOT be able to read it using BA. |
| 49 | + assert 0 == len([_ for _ in BAModel.query('pkey', BAModel.flag == True)]) |
| 50 | + |
| 51 | + # ... unless we jump through hoops using Path |
| 52 | + assert 1 == len([_ for _ in BAModel.query('pkey', Path('flag') == 1)]) |
| 53 | + |
| 54 | + # Migrate the object to being stored as Boolean. |
| 55 | + assert (1, 0) == migrate_boolean_attributes(BAModel, ['flag'], allow_rate_limited_scan_without_consumed_capacity=True) |
| 56 | + |
| 57 | + # We should now be able to read it using BA. |
| 58 | + assert 1 == len([_ for _ in BAModel.query('pkey', BAModel.flag == True)]) |
| 59 | + |
| 60 | + # ... or through the hoop jumping. |
| 61 | + assert 1 == len([_ for _ in BAModel.query('pkey', Path('flag') == True)]) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.ddblocal |
| 65 | +def test_migrate_boolean_attributes_none_okay(ddb_url): |
| 66 | + """Ensure migration works for attributes whose value is None.""" |
| 67 | + class LBAModel(Model): |
| 68 | + class Meta: |
| 69 | + table_name = 'migration_test_lba_to_ba' |
| 70 | + host = ddb_url |
| 71 | + id = UnicodeAttribute(hash_key=True) |
| 72 | + flag = LegacyBooleanAttribute(null=True) |
| 73 | + |
| 74 | + LBAModel.create_table(read_capacity_units=1, write_capacity_units=1) |
| 75 | + LBAModel('pkey', flag=None).save() |
| 76 | + assert (0, 0) == migrate_boolean_attributes(LBAModel, ['flag'], allow_rate_limited_scan_without_consumed_capacity=True) |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.ddblocal |
| 80 | +def test_migrate_boolean_attributes_conditional_update_failure(ddb_url): |
| 81 | + """Ensure migration works for attributes whose value is None.""" |
| 82 | + class LBAModel(Model): |
| 83 | + class Meta: |
| 84 | + table_name = 'migration_test_lba_to_ba' |
| 85 | + host = ddb_url |
| 86 | + id = UnicodeAttribute(hash_key=True) |
| 87 | + flag = LegacyBooleanAttribute(null=True) |
| 88 | + |
| 89 | + LBAModel.create_table(read_capacity_units=1, write_capacity_units=1) |
| 90 | + LBAModel('pkey', flag=1).save() |
| 91 | + assert (1, 1) == migrate_boolean_attributes(LBAModel, ['flag'], |
| 92 | + allow_rate_limited_scan_without_consumed_capacity=True, |
| 93 | + mock_conditional_update_failure=True) |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.ddblocal |
| 97 | +def test_migrate_boolean_attributes_missing_attribute(ddb_url): |
| 98 | + class LBAModel(Model): |
| 99 | + class Meta: |
| 100 | + table_name = 'migration_test_lba_to_ba' |
| 101 | + host = ddb_url |
| 102 | + id = UnicodeAttribute(hash_key=True) |
| 103 | + flag = LegacyBooleanAttribute(null=True) |
| 104 | + |
| 105 | + LBAModel.create_table(read_capacity_units=1, write_capacity_units=1) |
| 106 | + LBAModel('pkey', flag=True).save() |
| 107 | + with pytest.raises(ValueError) as e: |
| 108 | + migrate_boolean_attributes(LBAModel, ['flag', 'bogus'], allow_rate_limited_scan_without_consumed_capacity=True) |
| 109 | + assert str(e.value).find('does not exist on model') != -1 |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.ddblocal |
| 113 | +def test_migrate_boolean_attributes_wrong_attribute_type(ddb_url): |
| 114 | + class LBAModel(Model): |
| 115 | + class Meta: |
| 116 | + table_name = 'migration_test_lba_to_ba' |
| 117 | + host = ddb_url |
| 118 | + id = UnicodeAttribute(hash_key=True) |
| 119 | + flag = LegacyBooleanAttribute(null=True) |
| 120 | + other = UnicodeAttribute(null=True) |
| 121 | + |
| 122 | + LBAModel.create_table(read_capacity_units=1, write_capacity_units=1) |
| 123 | + LBAModel('pkey', flag=True, other='test').save() |
| 124 | + with pytest.raises(ValueError) as e: |
| 125 | + migrate_boolean_attributes(LBAModel, ['flag', 'other'], allow_rate_limited_scan_without_consumed_capacity=True) |
| 126 | + assert str(e.value).find('does not appear to be a boolean attribute') != -1 |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.ddblocal |
| 130 | +def test_migrate_boolean_attributes_multiple_attributes(ddb_url): |
| 131 | + class LBAModel(Model): |
| 132 | + class Meta: |
| 133 | + table_name = 'migration_test_lba_to_ba' |
| 134 | + host = ddb_url |
| 135 | + id = UnicodeAttribute(hash_key=True) |
| 136 | + flag = LegacyBooleanAttribute(null=True) |
| 137 | + flag2 = LegacyBooleanAttribute(null=True) |
| 138 | + |
| 139 | + LBAModel.create_table(read_capacity_units=1, write_capacity_units=1) |
| 140 | + # specifically use None and True here rather than two Trues |
| 141 | + LBAModel('pkey', flag=None, flag2=True).save() |
| 142 | + assert (1, 0) == migrate_boolean_attributes(LBAModel, ['flag', 'flag2'], allow_rate_limited_scan_without_consumed_capacity=True) |
| 143 | + |
| 144 | + |
| 145 | +@pytest.mark.ddblocal |
| 146 | +def test_migrate_boolean_attributes_skip_native_booleans(ddb_url): |
| 147 | + class BAModel(Model): |
| 148 | + class Meta: |
| 149 | + table_name = 'migration_test_lba_to_ba' |
| 150 | + host = ddb_url |
| 151 | + id = UnicodeAttribute(hash_key=True) |
| 152 | + flag = BooleanAttribute(null=True) |
| 153 | + |
| 154 | + BAModel.create_table(read_capacity_units=1, write_capacity_units=1) |
| 155 | + BAModel('pkey', flag=True).save() |
| 156 | + assert (0, 0) == migrate_boolean_attributes(BAModel, ['flag'], allow_rate_limited_scan_without_consumed_capacity=True) |
0 commit comments