Coverage for tests\test_schema.py: 100%

53 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-05 17:55 +0100

1from airtable_to_sqlite.constants import PreferedNamingMethod 

2from airtable_to_sqlite.schema import BaseRecord, FieldSchema, TableSchema, ViewSchema 

3 

4 

5def test_fieldschema_dbname(): 

6 f = FieldSchema(id="fld123", name="Name", type="text") 

7 assert f.db_name(PreferedNamingMethod.ID) == "fld123" 

8 assert f.db_name(PreferedNamingMethod.NAME) == "Name" 

9 assert f.db_name() == "Name" 

10 

11 

12def test_fieldschema_column_type(): 

13 assert FieldSchema(id="fld123", name="Name", type="text").column_type == str 

14 assert FieldSchema(id="fld123", name="Name", type="number").column_type == float 

15 assert FieldSchema(id="fld123", name="Name", type="multipleRecordLinks").column_type is None 

16 assert FieldSchema(id="fld123", name="Name", type="checkbox").column_type == bool 

17 assert FieldSchema(id="fld123", name="Name", type="formula").column_type == str 

18 assert ( 

19 FieldSchema(id="fld123", name="Name", type="formula", options={"result": {"type": "number"}}).column_type 

20 == float 

21 ) 

22 assert ( 

23 FieldSchema(id="fld123", name="Name", type="formula", options={"result": {"type": "text"}}).column_type == str 

24 ) 

25 assert ( 

26 FieldSchema(id="fld123", name="Name", type="formula", options={"result": {"type": "checkbox"}}).column_type 

27 == bool 

28 ) 

29 

30 

31def test_fieldschema_choices(): 

32 assert FieldSchema(id="fld123", name="Name", type="text").choices is None 

33 assert FieldSchema(id="fld123", name="Name", type="number").choices is None 

34 assert FieldSchema(id="fld123", name="Name", type="multipleRecordLinks").choices is None 

35 

36 assert FieldSchema(id="fld123", name="Name", type="text", options={"choices": []}).choices == [] 

37 assert FieldSchema( 

38 id="fld123", 

39 name="Name", 

40 type="text", 

41 options={ 

42 "choices": [ 

43 { 

44 "id": "opt123", 

45 "name": "Option 1", 

46 "color": "red", 

47 } 

48 ] 

49 }, 

50 ).choices == [ 

51 { 

52 "id": "opt123", 

53 "name": "Option 1", 

54 "color": "red", 

55 "fieldId": "fld123", 

56 } 

57 ] 

58 assert FieldSchema(id="fld123", name="Name", type="text", options={}).choices == [] 

59 

60 

61def test_fieldschema_for_insertion(): 

62 f = FieldSchema(id="fld123", name="Name", type="text") 

63 t = TableSchema(id="tbl123", name="Table", fields=[f], views=[], primaryFieldId="fld123") 

64 assert f.for_insertion(t) == { 

65 "id": "fld123", 

66 "name": "Name", 

67 "type": "text", 

68 "tableId": "tbl123", 

69 "options": None, 

70 "linkedTableId": None, 

71 "isReversed": None, 

72 "prefersSingleRecordLink": None, 

73 "inverseLinkFieldId": None, 

74 "isValid": None, 

75 "recordLinkFieldId": None, 

76 "icon": None, 

77 "color": None, 

78 "referencedFieldIds": None, 

79 "result": None, 

80 "precision": None, 

81 "symbol": None, 

82 } 

83 

84 

85def test_fieldschema_for_insertion_options(): 

86 f = FieldSchema(id="fld123", name="Name", type="text", options={"precision": 4}) 

87 t = TableSchema(id="tbl123", name="Table", fields=[f], views=[], primaryFieldId="fld123") 

88 assert f.for_insertion(t) == { 

89 "id": "fld123", 

90 "name": "Name", 

91 "type": "text", 

92 "tableId": "tbl123", 

93 "options": {}, 

94 "linkedTableId": None, 

95 "isReversed": None, 

96 "prefersSingleRecordLink": None, 

97 "inverseLinkFieldId": None, 

98 "isValid": None, 

99 "recordLinkFieldId": None, 

100 "icon": None, 

101 "color": None, 

102 "referencedFieldIds": None, 

103 "result": None, 

104 "precision": 4, 

105 "symbol": None, 

106 } 

107 

108 

109def test_fieldschema_for_insertion_options_unknown(): 

110 f = FieldSchema(id="fld123", name="Name", type="text", options={"precision": 4, "flurb": "blurb"}) 

111 t = TableSchema(id="tbl123", name="Table", fields=[f], views=[], primaryFieldId="fld123") 

112 assert f.for_insertion(t) == { 

113 "id": "fld123", 

114 "name": "Name", 

115 "type": "text", 

116 "tableId": "tbl123", 

117 "options": {"flurb": "blurb"}, 

118 "linkedTableId": None, 

119 "isReversed": None, 

120 "prefersSingleRecordLink": None, 

121 "inverseLinkFieldId": None, 

122 "isValid": None, 

123 "recordLinkFieldId": None, 

124 "icon": None, 

125 "color": None, 

126 "referencedFieldIds": None, 

127 "result": None, 

128 "precision": 4, 

129 "symbol": None, 

130 } 

131 

132 

133def test_tableschema_dbname(): 

134 f = TableSchema(id="tbl123", name="Table", fields=[], views=[], primaryFieldId="fld123") 

135 assert f.db_name(PreferedNamingMethod.ID) == "tbl123" 

136 assert f.db_name(PreferedNamingMethod.NAME) == "Table" 

137 assert f.db_name() == "Table" 

138 

139 

140def test_viewschema_dbname(): 

141 f = ViewSchema(id="vew123", name="View", type="View") 

142 assert f.db_name(PreferedNamingMethod.ID) == "vew123" 

143 assert f.db_name(PreferedNamingMethod.NAME) == "View" 

144 assert f.db_name() == "View" 

145 

146 

147def test_tableschema_get_table_data(_mock_api): 

148 t = TableSchema(id="tbl123", name="Table", fields=[], views=[], primaryFieldId="fld123") 

149 

150 table_data = list(t.get_table_data(_mock_api)) 

151 assert len(table_data) == 4 

152 assert table_data[0] == { 

153 "id": "rec123", 

154 "createdTime": "2021-01-01T00:00:00.000Z", 

155 "fields": { 

156 "Name": "Test 3", 

157 "Number": 123, 

158 "Checkbox": True, 

159 }, 

160 } 

161 

162 

163def test_baserecord(): 

164 b = BaseRecord( 

165 id="app123", 

166 name="Base", 

167 permissionLevel="create", 

168 ) 

169 assert b.id == "app123"