Coverage for gws-app/gws/plugin/model_field/time/_test.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-16 22:59 +0200

1import gws 

2import gws.config 

3import gws.base.feature 

4import gws.test.util as u 

5import gws.lib.datetimex as dtx 

6 

7 

8@u.fixture(scope='module') 

9def model(): 

10 u.pg.create('t', {'id': 'int primary key', 'time': 'TIME'}) 

11 cfg = ''' 

12 models+ {  

13 uid "TEST_MODEL" type "postgres" tableName "t" 

14 fields+ { name "id" type "integer" } 

15 fields+ { name "time" type "time" }  

16 } 

17 ''' 

18 root = u.gws_root(cfg) 

19 yield u.cast(gws.Model, root.get('TEST_MODEL')) 

20 

21 

22## 

23 

24def test_create(model: gws.Model): 

25 mc = u.model_context() 

26 time = dtx.new(2000, 2, 3, 4, 5, 6) 

27 

28 f = u.feature(model, id=3, time=time) 

29 model.create_feature(f, mc) 

30 fs = model.get_features([3], mc) 

31 

32 assert dtx.to_iso_time_string(fs[0].get('time')) == '04:05:06' 

33 

34 

35def test_read(model: gws.Model): 

36 mc = u.model_context() 

37 

38 ds = [ 

39 dtx.new(2000, 2, 3, 4, 5, 6), 

40 dtx.new(2001, 12, 13, 14, 15, 16), 

41 ] 

42 u.pg.insert('t', [{'id': 1, 'time': ds[0]}, {'id': 2, 'time': ds[1]}]) 

43 

44 fs = model.get_features([1, 2], mc) 

45 

46 assert dtx.to_iso_time_string(fs[0].get('time')) == '04:05:06' 

47 assert dtx.to_iso_time_string(fs[1].get('time')) == '14:15:16' 

48 

49 

50def test_update(model: gws.Model): 

51 mc = u.model_context() 

52 

53 ds = [ 

54 dtx.new(2000, 2, 3, 4, 5, 6), 

55 dtx.new(2001, 12, 13, 14, 15, 16), 

56 ] 

57 u.pg.insert('t', [{'id': 1, 'time': ds[0]}, {'id': 2, 'time': ds[1]}]) 

58 

59 new = dtx.new(2009, 9, 19, 11, 22, 33) 

60 f = u.feature(model, id=1, time=new) 

61 model.update_feature(f, mc) 

62 

63 fs = model.get_features([1, 2], mc) 

64 assert dtx.to_iso_time_string(fs[0].get('time')) == '11:22:33' 

65 assert dtx.to_iso_time_string(fs[1].get('time')) == '14:15:16'