@@ -83,6 +83,11 @@ public void TestTableOperations()
8383
8484 // Test Count on Query
8585 TestSelectCountOnQuery ( hashTable ) ;
86+
87+ TestExpressionPutWithDocumentOperationRequest ( hashTable ) ;
88+ TestExpressionUpdateWithDocumentOperationRequest ( hashTable ) ;
89+ TestExpressionsOnDeleteWithDocumentOperationRequest ( hashTable ) ;
90+
8691 }
8792 }
8893
@@ -163,6 +168,11 @@ public void TestTableOperationsViaBuilder()
163168
164169 // Test that attributes stored as Datetimes can be retrieved in UTC.
165170 TestAsDateTimeUtc ( numericHashRangeTable ) ;
171+
172+ TestExpressionPutWithDocumentOperationRequest ( hashTable ) ;
173+ TestExpressionUpdateWithDocumentOperationRequest ( hashTable ) ;
174+ TestExpressionsOnDeleteWithDocumentOperationRequest ( hashTable ) ;
175+
166176 }
167177 }
168178
@@ -1852,6 +1862,148 @@ private void TestSelectCountOnQuery(ITable hashTable)
18521862 Assert . AreEqual ( 0 , docs . Count ) ;
18531863 }
18541864
1865+ private void TestExpressionPutWithDocumentOperationRequest ( ITable table )
1866+ {
1867+ var doc = new Document
1868+ {
1869+ [ "Id" ] = DateTime . UtcNow . Ticks ,
1870+ [ "name" ] = "docop-conditional-form"
1871+ } ;
1872+
1873+ table . PutItem ( doc ) ;
1874+
1875+ var conditionalExpression = new Expression
1876+ {
1877+ ExpressionStatement = "attribute_not_exists(referencecounter) OR referencecounter = :zero" ,
1878+ ExpressionAttributeValues = { [ ":zero" ] = 0 }
1879+ } ;
1880+
1881+ var putRequest = new PutItemDocumentOperationRequest
1882+ {
1883+ Document = doc ,
1884+ ConditionalExpression = conditionalExpression
1885+ } ;
1886+
1887+ doc [ "update-test" ] = 1 ;
1888+ Assert . IsTrue ( table . TryPutItem ( putRequest ) ) ;
1889+
1890+ doc [ "referencecounter" ] = 0 ;
1891+ table . UpdateItem ( doc ) ;
1892+
1893+ doc [ "update-test" ] = null ;
1894+ Assert . IsTrue ( table . TryPutItem ( new PutItemDocumentOperationRequest { Document = doc , ConditionalExpression = conditionalExpression } ) ) ;
1895+
1896+ var reloaded = table . GetItem ( doc ) ;
1897+ Assert . IsFalse ( reloaded . Contains ( "update-test" ) ) ;
1898+
1899+ doc [ "referencecounter" ] = 1 ;
1900+ table . UpdateItem ( doc ) ;
1901+
1902+ doc [ "update-test" ] = 3 ;
1903+ Assert . IsFalse ( table . TryPutItem ( new PutItemDocumentOperationRequest { Document = doc , ConditionalExpression = conditionalExpression } ) ) ;
1904+
1905+ table . DeleteItem ( doc ) ;
1906+ }
1907+
1908+ private void TestExpressionUpdateWithDocumentOperationRequest ( ITable table )
1909+ {
1910+ var doc = new Document
1911+ {
1912+ [ "Id" ] = DateTime . UtcNow . Ticks ,
1913+ [ "name" ] = "docop-update-conditional"
1914+ } ;
1915+ table . PutItem ( doc ) ;
1916+
1917+ var conditionalExpression = new Expression
1918+ {
1919+ ExpressionStatement = "attribute_not_exists(referencecounter) OR referencecounter = :zero" ,
1920+ ExpressionAttributeValues = { [ ":zero" ] = 0 }
1921+ } ;
1922+
1923+ var config = new UpdateItemOperationConfig
1924+ {
1925+ ConditionalExpression = conditionalExpression
1926+ } ;
1927+
1928+ doc [ "update-test" ] = 1 ;
1929+ Assert . IsTrue ( table . TryUpdateItem ( new UpdateItemDocumentOperationRequest
1930+ {
1931+ Document = doc ,
1932+ ConditionalExpression = conditionalExpression
1933+ } ) ) ;
1934+
1935+ doc [ "referencecounter" ] = 0 ;
1936+ table . UpdateItem ( doc ) ;
1937+
1938+ doc [ "update-test" ] = null ;
1939+ Assert . IsTrue ( table . TryUpdateItem ( new UpdateItemDocumentOperationRequest
1940+ {
1941+ Document = doc ,
1942+ ConditionalExpression = conditionalExpression
1943+ } ) ) ;
1944+
1945+ var reloaded = table . GetItem ( doc ) ;
1946+ Assert . IsFalse ( reloaded . Contains ( "update-test" ) ) ;
1947+
1948+ doc [ "referencecounter" ] = 1 ;
1949+ table . UpdateItem ( doc ) ;
1950+
1951+ doc [ "update-test" ] = 3 ;
1952+ Assert . IsFalse ( table . TryUpdateItem ( new UpdateItemDocumentOperationRequest
1953+ {
1954+ Document = doc ,
1955+ ConditionalExpression = conditionalExpression
1956+ } ) ) ;
1957+
1958+ table . DeleteItem ( doc ) ;
1959+ }
1960+
1961+ private void TestExpressionsOnDeleteWithDocumentOperationRequest ( ITable table )
1962+ {
1963+ var doc = new Document
1964+ {
1965+ [ "Id" ] = 9001 ,
1966+ [ "Price" ] = 6
1967+ } ;
1968+ table . PutItem ( doc ) ;
1969+
1970+ var key = new Dictionary < string , DynamoDBEntry >
1971+ {
1972+ { "Id" , doc [ "Id" ] }
1973+ } ;
1974+
1975+ var expression = new Expression
1976+ {
1977+ ExpressionStatement = "Price > :price" ,
1978+ ExpressionAttributeValues = { [ ":price" ] = 7 }
1979+ } ;
1980+
1981+ var failingRequest = new DeleteItemDocumentOperationRequest
1982+ {
1983+ Key = key ,
1984+ ConditionalExpression = expression ,
1985+ ReturnValues = ReturnValues . AllOldAttributes
1986+ } ;
1987+
1988+ Assert . IsFalse ( table . TryDeleteItem ( failingRequest ) ) ;
1989+ Assert . IsNotNull ( table . GetItem ( doc ) ) ;
1990+
1991+ expression . ExpressionAttributeValues [ ":price" ] = 4 ;
1992+
1993+ var succeedingRequest = new DeleteItemDocumentOperationRequest
1994+ {
1995+ Key = key ,
1996+ ConditionalExpression = expression ,
1997+ ReturnValues = ReturnValues . AllOldAttributes
1998+ } ;
1999+
2000+ var oldAttributes = table . DeleteItem ( succeedingRequest ) ;
2001+ Assert . IsNotNull ( oldAttributes ) ;
2002+ Assert . AreEqual ( 6 , oldAttributes [ "Price" ] . AsInt ( ) ) ;
2003+
2004+ Assert . IsNull ( table . GetItem ( doc ) ) ;
2005+ }
2006+
18552007 private bool AreValuesEqual ( Document docA , Document docB , DynamoDBEntryConversion conversion = null )
18562008 {
18572009 if ( conversion != null )
0 commit comments