Erlang Mnesia数据库操作和eUnit测试
学erlang应该有6天了, 迷迷糊糊的今天突然想起还有任务写了个基于mnesia数据库的操作
-module(agent_server).
-compile(export_all).
-include_lib("stdlib/include/qlc.hrl").
-record(agent,{
id,
name,
groupid,
state = ready::'pause'|'talk'|'wrappup',
logintime::{integer(), integer(), integer()}
}).
%%%感觉 start 和 stop 有点像打开和关闭数据库连接
% 初始化
% 创建数据库和表
init() ->
mnesia:create_schema([node()]), % 创建节点 相当数据库
mnesia:start(), % start mneisa
mnesia:create_table(agent, [{attributes, record_info(fields, agent)}]), %根据record创建agent表
mnesia:stop(). % 关闭 mneisa
%stop
% 开始
start() ->
mnesia:start(),
mnesia:wait_for_tables([agent], 10000). % 等待去连接表,超时报错
% 停止(结束)
stop() ->
mnesia:stop().
% 查询全部
% mneisa 的事务
% transaction(Fun [[, Args], Retries]) -> {aborted, Reason} | {atomic, ResultOfFun}
find_all() ->
Q = qlc:q([X || X <- mnesia:table(agent)]),
F = fun() -> qlc:e(Q) end,
{atomic, Result} = mnesia:transaction(F),
Result.
% 查询记录 ByItem
find_ByItem(Item) ->
Q = qlc:q([
X || X <- mnesia:table(agent),
X#agent.id =:= Item
]),
F = fun() -> qlc:e(Q) end,
{atomic, Result} = mnesia:transaction(F),
Result.
% 测试的数据
test_example() ->
[
{agent, 1, 'lin', 1, "talk", {11, 1, 19}},
{agent, 2, 'hao', 1, "pause", {11, 1, 19}},
{agent, 3, 'jie', 1, "wrappup", {11, 1, 19}},
{agent, 4, 'study', 1, "pause", {11, 1, 19}}
].
% 插入测试数据
test_insert() ->
mnesia:clear_table(agent),
F = fun() ->
lists:foreach(fun mnesia:write/1, test_example())
end,
mnesia:transaction(F).
% 删除数据
deleteByItem(Item) ->
Object = {agent, Item}, % 根据 Object 删除数据
F = fun() -> mnesia:delete(Object) end,
mnesia:transaction(F).
% 插入一条数据
insert_info(Id, Name, GroupId, State, LogTime) ->
% 估计插入也要正则
% 先写
F = fun() -> mnesia:write({agent, Id, Name, GroupId, State, LogTime}) end,
mnesia:transaction(F).
% 更新一条数据
%%% 这里更新话 我想给更新的话应该是名字和状态吧
%%% 不通过 id 取数据的话还是有点不明白的
%%% 感觉写的不是很好
update_info(Item, Name, State) ->
{match, _First, Match} = regexp:first_match(State, "(pause)?|(talk)?|(wrappup)?"), % 不是很好
case (Match > 0) of
true ->
F = fun() ->
[OldRes] = mnesia:read({agent, Item}),
NewRes = OldRes#agent{name=Name, state=State},% 需要注意一下
mnesia:write(NewRes)
end,
mnesia:transaction(F);
false -> io:format("To check out you State Input!~n State = { pause | talk | wrappup }~n")
end.
% 排序
%%% 关于排序不明白,是表排序还是输出排序
% eUnit_test
% test的顺序跟外部的调用顺序差不的
agent_server_test_() ->
[
?_test(init()),
?_test(start()),
?_assert(test_insert() =:= {atomic,ok}),
?_test(find_all()),
?_assert(find_ByItem(1) /= []), % 返回列表判断?
?_assert(insert_info(6, 'aaaa', 1, "pause", {12.12,12}) =:= {atomic,ok}),
?_assert(deleteByItem(1) =:= {atomic,ok})
%?_test(stop())
].
下面的是测试:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
-include_lib("eunit/include/eunit.hrl").
% eUnit_test
% test的顺序跟外部的调用顺序差不的
agent_server_test_() ->
[
?_test(init()),
?_test(start()),
?_assert(test_insert() =:= {atomic,ok}),
?_test(find_all()),
?_assert(find_ByItem(1) /= []), % 返回列表判断?
?_assert(insert_info(6, 'aaaa', 1, "pause", {12.12,12}) =:= {atomic,ok}),
?_assert(deleteByItem(1) =:= {atomic,ok})
%?_test(stop())
].
%测试的结果
27> agent_server:test().
===INFO REPORT==== 20-Jan-2011::14:51:36 ===
application: mnesia
exited: stopped
type: temporary
All 7 tests passed.
ok
写的总体感觉不是很好, 也看了些代码感觉自己也是菜鸟,继续努力吧