Showing 1 changed files with 269 additions and 0 deletions
+269
t/common.t
... ...
@@ -1876,6 +1876,275 @@ is_deeply($result->stash, {}, 'default');
1876 1876
 $result->stash->{foo} = 1;
1877 1877
 is($result->stash->{foo}, 1, 'get and set');
1878 1878
 
1879
+test 'delete_at';
1880
+$dbi = DBIx::Custom->connect;
1881
+eval { $dbi->execute('drop table table1') };
1882
+$dbi->execute($create_table1_2);
1883
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1884
+$dbi->delete_at(
1885
+    table => 'table1',
1886
+    primary_key => ['key1', 'key2'],
1887
+    where => [1, 2],
1888
+);
1889
+is_deeply($dbi->select(table => 'table1')->all, []);
1890
+
1891
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1892
+$dbi->delete_at(
1893
+    table => 'table1',
1894
+    primary_key => 'key1',
1895
+    where => 1,
1896
+);
1897
+is_deeply($dbi->select(table => 'table1')->all, []);
1898
+
1899
+test 'insert_at';
1900
+$dbi = DBIx::Custom->connect;
1901
+eval { $dbi->execute('drop table table1') };
1902
+$dbi->execute($create_table1_2);
1903
+$dbi->insert_at(
1904
+    primary_key => ['key1', 'key2'], 
1905
+    table => 'table1',
1906
+    where => [1, 2],
1907
+    param => {key3 => 3}
1908
+);
1909
+is($dbi->select(table => 'table1')->one->{key1}, 1);
1910
+is($dbi->select(table => 'table1')->one->{key2}, 2);
1911
+is($dbi->select(table => 'table1')->one->{key3}, 3);
1912
+
1913
+$dbi->delete_all(table => 'table1');
1914
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1915
+$dbi->insert_at(
1916
+    primary_key => 'key1', 
1917
+    table => 'table1',
1918
+    where => 1,
1919
+    param => {key2 => 2, key3 => 3}
1920
+);
1921
+
1922
+is($dbi->select(table => 'table1')->one->{key1}, 1);
1923
+is($dbi->select(table => 'table1')->one->{key2}, 2);
1924
+is($dbi->select(table => 'table1')->one->{key3}, 3);
1925
+
1926
+eval {
1927
+    $dbi->insert_at(
1928
+        table => 'table1',
1929
+        primary_key => ['key1', 'key2'],
1930
+        where => {},
1931
+        param => {key1 => 1, key2 => 2, key3 => 3},
1932
+    );
1933
+};
1934
+like($@, qr/must be/);
1935
+
1936
+$dbi = DBIx::Custom->connect;
1937
+eval { $dbi->execute('drop table table1') };
1938
+$dbi->execute($create_table1_2);
1939
+$dbi->insert_at(
1940
+    {key3 => 3},
1941
+    primary_key => ['key1', 'key2'], 
1942
+    table => 'table1',
1943
+    where => [1, 2],
1944
+);
1945
+is($dbi->select(table => 'table1')->one->{key1}, 1);
1946
+is($dbi->select(table => 'table1')->one->{key2}, 2);
1947
+is($dbi->select(table => 'table1')->one->{key3}, 3);
1948
+
1949
+test 'update_at';
1950
+$dbi = DBIx::Custom->connect;
1951
+eval { $dbi->execute('drop table table1') };
1952
+$dbi->execute($create_table1_2);
1953
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1954
+$dbi->update_at(
1955
+    table => 'table1',
1956
+    primary_key => ['key1', 'key2'],
1957
+    where => [1, 2],
1958
+    param => {key3 => 4}
1959
+);
1960
+is($dbi->select(table => 'table1')->one->{key1}, 1);
1961
+is($dbi->select(table => 'table1')->one->{key2}, 2);
1962
+is($dbi->select(table => 'table1')->one->{key3}, 4);
1963
+
1964
+$dbi->delete_all(table => 'table1');
1965
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1966
+$dbi->update_at(
1967
+    table => 'table1',
1968
+    primary_key => 'key1',
1969
+    where => 1,
1970
+    param => {key3 => 4}
1971
+);
1972
+is($dbi->select(table => 'table1')->one->{key1}, 1);
1973
+is($dbi->select(table => 'table1')->one->{key2}, 2);
1974
+is($dbi->select(table => 'table1')->one->{key3}, 4);
1975
+
1976
+$dbi = DBIx::Custom->connect;
1977
+eval { $dbi->execute('drop table table1') };
1978
+$dbi->execute($create_table1_2);
1979
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1980
+$dbi->update_at(
1981
+    {key3 => 4},
1982
+    table => 'table1',
1983
+    primary_key => ['key1', 'key2'],
1984
+    where => [1, 2]
1985
+);
1986
+is($dbi->select(table => 'table1')->one->{key1}, 1);
1987
+is($dbi->select(table => 'table1')->one->{key2}, 2);
1988
+is($dbi->select(table => 'table1')->one->{key3}, 4);
1989
+
1990
+test 'select_at';
1991
+$dbi = DBIx::Custom->connect;
1992
+eval { $dbi->execute('drop table table1') };
1993
+$dbi->execute($create_table1_2);
1994
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
1995
+$result = $dbi->select_at(
1996
+    table => 'table1',
1997
+    primary_key => ['key1', 'key2'],
1998
+    where => [1, 2]
1999
+);
2000
+$row = $result->one;
2001
+is($row->{key1}, 1);
2002
+is($row->{key2}, 2);
2003
+is($row->{key3}, 3);
2004
+
2005
+$dbi->delete_all(table => 'table1');
2006
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2007
+$result = $dbi->select_at(
2008
+    table => 'table1',
2009
+    primary_key => 'key1',
2010
+    where => 1,
2011
+);
2012
+$row = $result->one;
2013
+is($row->{key1}, 1);
2014
+is($row->{key2}, 2);
2015
+is($row->{key3}, 3);
2016
+
2017
+$dbi->delete_all(table => 'table1');
2018
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2019
+$result = $dbi->select_at(
2020
+    table => 'table1',
2021
+    primary_key => ['key1', 'key2'],
2022
+    where => [1, 2]
2023
+);
2024
+$row = $result->one;
2025
+is($row->{key1}, 1);
2026
+is($row->{key2}, 2);
2027
+is($row->{key3}, 3);
2028
+
2029
+eval {
2030
+    $result = $dbi->select_at(
2031
+        table => 'table1',
2032
+        primary_key => ['key1', 'key2'],
2033
+        where => {},
2034
+    );
2035
+};
2036
+like($@, qr/must be/);
2037
+
2038
+eval {
2039
+    $result = $dbi->select_at(
2040
+        table => 'table1',
2041
+        primary_key => ['key1', 'key2'],
2042
+        where => [1],
2043
+    );
2044
+};
2045
+like($@, qr/same/);
2046
+
2047
+eval {
2048
+    $result = $dbi->update_at(
2049
+        table => 'table1',
2050
+        primary_key => ['key1', 'key2'],
2051
+        where => {},
2052
+        param => {key1 => 1, key2 => 2},
2053
+    );
2054
+};
2055
+like($@, qr/must be/);
2056
+
2057
+eval {
2058
+    $result = $dbi->delete_at(
2059
+        table => 'table1',
2060
+        primary_key => ['key1', 'key2'],
2061
+        where => {},
2062
+    );
2063
+};
2064
+like($@, qr/must be/);
2065
+
2066
+test 'columns';
2067
+use MyDBI1;
2068
+$dbi = MyDBI1->connect;
2069
+$model = $dbi->model('book');
2070
+
2071
+
2072
+test 'model delete_at';
2073
+$dbi = MyDBI6->connect;
2074
+eval { $dbi->execute('drop table table1') };
2075
+eval { $dbi->execute('drop table table2') };
2076
+eval { $dbi->execute('drop table table3') };
2077
+$dbi->execute($create_table1_2);
2078
+$dbi->execute($create_table2_2);
2079
+$dbi->execute($create_table3);
2080
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2081
+$dbi->model('table1')->delete_at(where => [1, 2]);
2082
+is_deeply($dbi->select(table => 'table1')->all, []);
2083
+$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2084
+$dbi->model('table1_1')->delete_at(where => [1, 2]);
2085
+is_deeply($dbi->select(table => 'table1')->all, []);
2086
+$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2087
+$dbi->model('table1_3')->delete_at(where => [1, 2]);
2088
+is_deeply($dbi->select(table => 'table1')->all, []);
2089
+
2090
+test 'model insert_at';
2091
+$dbi = MyDBI6->connect;
2092
+eval { $dbi->execute('drop table table1') };
2093
+$dbi->execute($create_table1_2);
2094
+$dbi->model('table1')->insert_at(
2095
+    where => [1, 2],
2096
+    param => {key3 => 3}
2097
+);
2098
+$result = $dbi->model('table1')->select;
2099
+$row = $result->one;
2100
+is($row->{key1}, 1);
2101
+is($row->{key2}, 2);
2102
+is($row->{key3}, 3);
2103
+
2104
+test 'model update_at';
2105
+$dbi = MyDBI6->connect;
2106
+eval { $dbi->execute('drop table table1') };
2107
+$dbi->execute($create_table1_2);
2108
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2109
+$dbi->model('table1')->update_at(
2110
+    where => [1, 2],
2111
+    param => {key3 => 4}
2112
+);
2113
+$result = $dbi->model('table1')->select;
2114
+$row = $result->one;
2115
+is($row->{key1}, 1);
2116
+is($row->{key2}, 2);
2117
+is($row->{key3}, 4);
2118
+
2119
+test 'model select_at';
2120
+$dbi = MyDBI6->connect;
2121
+eval { $dbi->execute('drop table table1') };
2122
+$dbi->execute($create_table1_2);
2123
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2124
+$result = $dbi->model('table1')->select_at(where => [1, 2]);
2125
+$row = $result->one;
2126
+is($row->{key1}, 1);
2127
+is($row->{key2}, 2);
2128
+is($row->{key3}, 3);
2129
+
2130
+
2131
+test 'mycolumn and column';
2132
+$dbi = MyDBI7->connect;
2133
+eval { $dbi->execute('drop table table1') };
2134
+eval { $dbi->execute('drop table table2') };
2135
+$dbi->execute($create_table1);
2136
+$dbi->execute($create_table2);
2137
+$dbi->separator('__');
2138
+$dbi->setup_model;
2139
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2140
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2141
+$model = $dbi->model('table1');
2142
+$result = $model->select(
2143
+    column => [$model->mycolumn, $model->column('table2')],
2144
+    where => {'table1.key1' => 1}
2145
+);
2146
+is_deeply($result->one,
2147
+          {key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3});
1879 2148
 
1880 2149
 
1881 2150